-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstart-chatGPT.ps1
34 lines (32 loc) · 1.02 KB
/
start-chatGPT.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
$script:OpenAI_Key = "My key"
function Start-ChatGPT {
$key = $script:openai_key
$url = "https://api.openai.com/v1/chat/completions"
$body = [pscustomobject]@{
"model" = "gpt-3.5-turbo"
"messages" = @(
@{"role" = "system"; "content" = "You are an assistant" }
)
}
$header = @{
"Authorization" = "Bearer $key"
"Content-Type" = "application/json"
}
while ($true) {
$message = Read-Host "Prompt"
$body.messages+=@{"role"="user";"content"=$message}
$bodyJSON = ($body | ConvertTo-Json -Compress)
try {
$res = Invoke-WebRequest -Headers $header -Body $bodyJSON -Uri $url -method post
$output = ($res.content | convertfrom-json).choices.message
Write-Host ""
write-host $output.content -ForegroundColor Green
Write-Host "--------------"
$body.messages+=$output
}
catch {
Write-Error $error[-1]
return $res
}
}
}