Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Last active February 9, 2023 16:34
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 AlexanderHolmeset/7fe8ccebf27ade2df6c62329b5a07b6d to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/7fe8ccebf27ade2df6c62329b5a07b6d to your computer and use it in GitHub Desktop.
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