Created
January 17, 2024 13:50
-
-
Save krnese/5200a3e0725a6eb34bac798c492f7f65 to your computer and use it in GitHub Desktop.
New-AzOpenAIVisionPrompt.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-AzOpenAIVisionPrompt { | |
<# | |
- Kristian Nese, SWAT team | |
Quick function to validate 1) access to Azure Open AI instance (GTP4), and 2) ability to generate a prompt, with 3) reference to an image URL. | |
Note: for this to work, the caller ID must have appropriate permissions to the Azure Open AI instance via data plane RBAC. | |
This is brought to you by the courtesy of Kristian, who is a true believer of Leo Messi. | |
.Synopsis | |
Prompt Azure Open AI w Vision using PowerShell function | |
.Example | |
New-AzOpenAIVisionPrompt -Endpoint "foobar.openai.azure.com" -DeploymentName "gpt4vision" -Prompt "Have this player ever won the World Cup" -ImageUri "https://<url>" | |
#> | |
[cmdletbinding()] | |
param ( | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[ValidateNotNullOrEmpty()] | |
[string] $Endpoint, | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[string] $DeploymentName, | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[string] $Prompt, | |
[Parameter(Mandatory)] | |
[ValidateNotNullOrEmpty()] | |
[string] $ImageUrl | |
) | |
# Get Token | |
$TokenRequest = Get-AzAccessToken -ResourceUrl "https://cognitiveservices.azure.com" | |
$MyToken = $TokenRequest.token | |
$Body = @" | |
{ | |
"enhancements": { | |
"ocr": { | |
"enabled": true | |
}, | |
"grounding": { | |
"enabled": true | |
} | |
}, | |
"messages": [ | |
{ | |
"role": "system", | |
"content": "You are a helpful assistant." | |
}, | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "$($Prompt):" | |
}, | |
{ | |
"type": "image_url", | |
"image_url": { | |
"url":"$($ImageUrl)" | |
} | |
} | |
] | |
} | |
], | |
"max_tokens": 100, | |
"stream": false | |
} | |
"@ | |
# AI Request | |
$AzureOAIRequest = @{ | |
Uri = "https://$($Endpoint)/openai/deployments/$($DeploymentName)/extensions/chat/completions?api-version=2023-12-01-preview" | |
Headers = @{ | |
Authorization = "Bearer $($MyToken)" | |
'Content-Type' = 'application/json' | |
} | |
Method = 'POST' | |
Body = $Body | |
UseBasicParsing = $true | |
} | |
$Response = Invoke-WebRequest @AzureOAIRequest | |
[Newtonsoft.Json.Linq.JObject]::Parse($Response.Content).ToString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment