跳至主要內容

透過 API 呼叫 GPT-5

· 1 分鐘閱讀

GPT-5 已經公開了,所以我用 PowerShell 試著呼叫它的 API。

程式碼

$uri = "https://api.openai.com/v1/chat/completions"
$headers = @{
"Authorization" = "Bearer $env:OPENAI_API_KEY"
"Content-Type" = "application/json"
}

$body = @{
model = "gpt-5"
messages = @(
@{
role = "user"
content = "筆記本和鉛筆合計是100元。鉛筆比筆記本便宜40元。鉛筆多少錢?"
}
)
} | ConvertTo-Json -Depth 2

$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body

foreach($choice in $response.choices){
$choice.message.content
}

輸出結果

30元

理由:
- 設筆記本為 x 元、鉛筆為 y 元,則
- x + y = 100
- y = x - 40
- 代入得 2x - 40 = 100 → x = 70 → y = 30
- 驗算: 70 + 30 = 100,且鉛筆比筆記本便宜 40 元。

コメント

読み込み中...

コメントを投稿する