Skip to content

Instantly share code, notes, and snippets.

Last active February 9, 2023 16:34
What would you like to do?
function Get-Completion {
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.
A prompt name should be provided.
A token name should be provided.
Get-Completion -DeploymentName $DeploymentName -ResourceName $ResourceName -maxtokens 100 -prompt "What is the meaning of life?"
Author: Alexander Holmeset
Twitter: @AlexHolmeset
Created: 09-02-2023
Version history:
1.0.0 - (09-02-2023) Function created
param (
[parameter(Mandatory = $true, HelpMessage = "Your azure openai deployment name")]
[parameter(Mandatory = $true, HelpMessage = "your azure openai resource name")]
[parameter(Mandatory = $true, HelpMessage = "Your Azure OpenAI prompt")]
[parameter(Mandatory = $true, HelpMessage = "Max number of tokens allowed to be used in this request")]
Process {
$ErrorActionPreference = "Stop"
$APIVersion = "2022-12-01"
# Construct URI
$uri = "https://$$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