Created
March 4, 2023 19:19
-
-
Save mkht/1be9ee5f53ec93a5793f330bf6c35834 to your computer and use it in GitHub Desktop.
ChatGPTへ質問するPowerShellコード(Windows PowerShell 5.1対応版)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# まず 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