Skip to content

Instantly share code, notes, and snippets.

@krnese
Created October 12, 2023 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krnese/a84e7d52c1848f3a9d5df657b692b8a2 to your computer and use it in GitHub Desktop.
Save krnese/a84e7d52c1848f3a9d5df657b692b8a2 to your computer and use it in GitHub Desktop.
Azure Open AI API prompt
function New-AzOpenAIPrompt {
<#
10/10/2023 - Kristian Nese, SWAT team
Quick function to validate 1) access to Azure Open AI instance (GTP4), and 2) ability to generate a prompt.
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 using PowerShell function
.Example
New-AzOpenAIPrompt -Endpoint "foobar.openai.azure.com" -DeploymentName "gpt4" -Prompt "Why does it rain so much in Bergen, Norway?"
#>
[cmdletbinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string] $Endpoint,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $DeploymentName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string] $Prompt
)
# Get Token
$TokenRequest = Get-AzAccessToken -ResourceUrl "https://cognitiveservices.azure.com"
$MyToken = $TokenRequest.token
$Body = @"
{
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "$($Prompt)"
}
]
}
"@
# AI Request
$AzureOAIRequest = @{
Uri = "https://$($Endpoint)/openai/deployments/$($DeploymentName)/chat/completions?api-version=2023-05-15"
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