GPT-5 を API で叩く
· 約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円安い。