-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeCompletion.ps1
55 lines (40 loc) · 1.23 KB
/
CodeCompletion.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Get-AIAnswer
{
[CmdLetBinding()]
param (
[String]$apiKey = (Get-Secret -Name ChatGptAPI|convertfrom-Securestring -AsPlainText),
[ValidateSet("text-davinci-003", "code-cushman-001", "code-davinci-001")]
[String]$model = "text-davinci-003",
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true
)]
[String]$question,
[int]$maxtokens = 4000,
[ValidateRange(0,1)]
[int]$temperature = 0.5
)
begin {
$apiEndpoint = "https://api.openai.com/v1/completions"
}
process {
# Set the request headers
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $apiKey"
}
# Set the request body
$requestBody = @{
"prompt" = $question
"model" = $model
"max_tokens" = $maxtokens
"temperature" = $temperature
}
# Send the request
$response = Invoke-RestMethod -Method POST -Uri $apiEndpoint -Headers $headers -Body (ConvertTo-Json $requestBody)
# Print the response
$response.choices.text
}
end {
}
}