Function Callingとresponse_formatの二つのやり方があるが、ここではresponse_formatを使ったやり方
<?php
function callOpenAIWithCurl(string $userInput, string $apiKey, string $url): array|string
{
$postData = [
"model" => "gpt-4o-mini",
"messages" => [
[
"role" => "system",
"content" => "あなたは賢いアシスタントです。"
],
[
"role" => "user",
"content" => $userInput
]
],
"response_format" => [
"type" => "json_schema",
"json_schema" => [
"name" => "reasoning_schema", //スキーマに名前。APIの動作には影響しない
"strict" => true, //レスポンスがこのスキーマに厳密に一致することを要求
"schema" => [ //データの構造を定義
"type" => "object",
"properties" => [
"reasoning_steps" => [
"type" => "array",
"items" => [
"type" => "string"
],
"description" => "最終結論に至る推論のステップ"
],
"answer" => [
"type" => "string",
"description" => "推論のステップを考慮した最終的な答え"
]
],
"required" => ["reasoning_steps", "answer"],
"additionalProperties" => false
]
]
]
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer " . $apiKey
],
CURLOPT_POSTFIELDS => json_encode($postData)
]);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
return "cURLエラー: " . $error;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
return "HTTPエラー: $httpCode - " . $response; // レスポンス全体を返す
}
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return "JSONデコードエラー: " . json_last_error_msg() . " - Response: " . $response;
}
if (!isset($responseData['choices'][0]['message']['content'])) {
return "レスポンスの形式が不正です: " . $response;
}
$content = json_decode($responseData['choices'][0]['message']['content'], true);
if (json_last_error() !== JSON_ERROR_NONE) {
return "contentのJSONデコードエラー: " . json_last_error_msg() . " - Content: " . $responseData['choices'][0]['message']['content'];
}
return $content;
}
// 実行例
$apiKey = your_apikey;
$url = 'https://api.openai.com/v1/chat/completions';
$userInput = "9.11 と 9.9では、どちらが大きいか?";
$result = callOpenAIWithCurl($userInput, $apiKey, $url);
print_r($result);
echo "<br><br>";
if (is_array($result)) {
echo "ステップ:" . print_r($result['reasoning_steps']) . PHP_EOL;
echo "答え:" . $result['answer'] . PHP_EOL;
} else {
echo $result . PHP_EOL;
}
?>
JSONスキーマ部分を別の変数にする場合。
// スキーマ部分を変数に格納
$schema = [
"schema" => [
"type" => "object",
"properties" => [
"reasoning_steps" => [
"type" => "array",
"items" => [
"type" => "string"
],
"description" => "最終結論に至る推論のステップ"
],
"answer" => [
"type" => "string",
"description" => "推論のステップを考慮した最終的な答え"
]
],
"required" => ["reasoning_steps", "answer"],
"additionalProperties" => false
]
];
$postData = [
"model" => "gpt-4o-mini",
"messages" => [
[
"role" => "system",
"content" => "あなたは賢いアシスタントです。"
],
[
"role" => "user",
"content" => $userInput
]
],
"response_format" => [
"type" => "json_schema",
"json_schema" => array_merge([ // array_mergeを使用
"name" => "reasoning_schema", //スキーマに名前。APIの動作には影響しない
"strict" => true, //レスポンスがこのスキーマに厳密に一致することを要求
], $schema)
]
];