Skip to content

Commit

Permalink
Merge pull request #69 from Azure/ai-hub-update300
Browse files Browse the repository at this point in the history
adding AOAI function
  • Loading branch information
krnese committed Mar 6, 2024
2 parents 0b28cb5 + 31fc6d5 commit a4e26bd
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions utilities/New-AzOpenAIPrompt.ps1
@@ -0,0 +1,58 @@
function New-AzOpenAIPrompt {
<#
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.
.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-08-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()
}

0 comments on commit a4e26bd

Please sign in to comment.