-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Azure/ai-hub-update300
adding AOAI function
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |