Hi Guys.
In PowerShell 7.x and code:
$apiEndpoint = “https://api.openai.com/v1/completions”
$apiKey = “sk-APIKEY”
$headers = @{
Authorization = “Bearer $apiKey”
}
$body = @{
model = “text-davinci-003”
prompt = “This is only a test”
}
$response = Invoke-WebRequest -Uri $apiEndpoint -Method POST -Headers $headers -Body $body
I get error message:
{
“error”: {
“message”: “you must provide a model parameter”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}
I doesn’t seem to matter if $body is hashtable or json.
Any hint/clue would be highly appreciated.
BR,
Marcin
1 Like
You could try sending an extra header to tell it the mime type is application/json
And $body has to be json - a hashtable wont work
Can yo ulet me know if it works, so I can add the finished Powershell example to a file of examples I’m compiling for different programming languages
1 Like
It works
$body content piped to JSON
and -ContentType added to Invoke-WebRequest did the job.
$apiEndpoint = “https://api.openai.com/v1/completions”
$apiKey = “sk-APIKEY”
$headers = @{
Authorization = “Bearer $apiKey”
}
$contenttype = “application/json”
$body = @{
model = “text-davinci-003”
prompt = “This is only a test”
} | ConvertTo-Json
$response = Invoke-WebRequest -Uri $apiEndpoint -Method POST -Headers $headers -Body $body -ContentType $contenttype
$generatedText = $response.Content | ConvertFrom-Json
Write-Output $generatedText.choices[0].text
And the output is:
This sentence is only a test. It has no meaning or purpose.
Thanks a lot.
2 Likes
Hi There,
The script works well with GPT 3.5 but not with GPT 4 model.
How could you make the working script support GPT-4 model with messages and send a payload that looks like,
"messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ], "