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
function Get-Completion { | |
<# .SYNOPSIS | |
Get a text completion from Azure OpenAI Completion endpoint. | |
.PARAMETER DeploymentName | |
A deployment name should be provided. | |
.PARAMETER ResourceName | |
A Resource name should be provided. | |
.PARAMETER Prompt | |
A prompt name should be provided. | |
.PARAMETER Token | |
A token name should be provided. | |
.EXAMPLE | |
Get-Completion -DeploymentName $DeploymentName -ResourceName $ResourceName -maxtokens 100 -prompt "What is the meaning of life?" | |
.NOTES | |
Author: Alexander Holmeset | |
Twitter: @AlexHolmeset | |
Website: https://www.alexholmeset.blog | |
Created: 09-02-2023 | |
Updated: | |
Version history: | |
1.0.0 - (09-02-2023) Function created | |
#>[CmdletBinding()] | |
param ( | |
[parameter(Mandatory = $true, HelpMessage = "Your azure openai deployment name")] | |
[ValidateNotNullOrEmpty()] | |
[string]$DeploymentName, | |
[parameter(Mandatory = $true, HelpMessage = "your azure openai resource name")] | |
[ValidateNotNullOrEmpty()] | |
[string]$ResourceName, | |
[parameter(Mandatory = $true, HelpMessage = "Your Azure OpenAI prompt")] | |
[ValidateNotNullOrEmpty()] | |
[string]$Prompt, | |
[parameter(Mandatory = $true, HelpMessage = "Max number of tokens allowed to be used in this request")] | |
[ValidateNotNullOrEmpty()] | |
[int]$Maxtokens | |
) | |
Process { | |
$ErrorActionPreference = "Stop" | |
$APIVersion = "2022-12-01" | |
# Construct URI | |
$uri = "https://$ResourceName.openai.azure.com/openai/deployments/$DeploymentName/completions?api-version=$ApiVersion" | |
# Construct Body | |
$Body = @" | |
{ | |
"prompt": "$Prompt", | |
"max_tokens": $maxtokens | |
} | |
"@ | |
try { | |
$Global:Request = invoke-restmethod -Method POST -Uri $uri -ContentType "application/json" -Body $body -Headers $Global:MyHeader | |
} | |
catch [System.Exception] { | |
Write-Warning "Failed to to POST request: $($_.Exception.Message)"; break | |
} | |
return $Request | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment