サンプルコード
use OpenAI\Laravel\Facades\OpenAI;
・
・
・
$response = OpenAI::chat()->create([
'model' => 'gpt-4o-mini',
'messages' => [
["role" => "system", "content" => "日本一高い山を教えて"]
],
'response_format' => [
"type" => "json_schema",
"json_schema" => [
"name" => "mountain_data",
"schema" => [
"type" => "object",
"properties" => [
"name" => ["type" => "string"],
"elevation" => ["type" => "number"],
],
"required" => ["name", "elevation"],
"additionalProperties" => false,
],
"strict" => true,
],
],
]);
dd($response);
結果
値を抽出するには
jsonデコードすればOK。
$ary_response = json_decode($response['choices'][0]['message']['content'], true);
$name = $ary_response['name'];
$elevation = $ary_response['elevation'];
dd($name, $elevation);