Skip to content

Instantly share code, notes, and snippets.

@mkht
Created March 4, 2023 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkht/1be9ee5f53ec93a5793f330bf6c35834 to your computer and use it in GitHub Desktop.
Save mkht/1be9ee5f53ec93a5793f330bf6c35834 to your computer and use it in GitHub Desktop.
ChatGPTへ質問するPowerShellコード(Windows PowerShell 5.1対応版)
# まず ChatGPT を使うための各種パラメータを指定します。
$Token = 'ここにAPIキーを入れてください'
$Uri = 'https://api.openai.com/v1/chat/completions'
$PostBody = @{
model = 'gpt-3.5-turbo'
}
# 次に質問する文章を決めます。
$PostBody.messages = @(
@{
role = 'user'
content = 'ここにChatGPTへの質問を書いてください'
}
)
# 最後に ChatGPT に質問を投げます
$headers = @{Authorization = "Bearer $Token"}
$Response = Invoke-WebRequest `
-Method Post `
-Uri $Uri `
-ContentType 'application/json' `
-Headers $headers `
-UseBasicParsing `
-Body ([System.Text.Encoding]::UTF8.GetBytes(($PostBody | ConvertTo-Json -Compress)))
# 返ってきた回答を表示します
$Content = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::GetEncoding('ISO-8859-1').GetBytes($Response.Content))
$Answer = ($Content | ConvertFrom-Json).choices[0].message.content
Write-Output $Answer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment