Laravel10 OpenAI API structured outputsコール雛形

サンプルコード

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);

結果

OpenAI API structured outputsレスポンス

値を抽出するには

jsonデコードすればOK。

        $ary_response = json_decode($response['choices'][0]['message']['content'], true);
        $name = $ary_response['name'];
        $elevation = $ary_response['elevation'];
dd($name, $elevation);

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です