如何在Laravel中只从JSON响应中插入'message'到数据库?

oug3syen  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(158)

我有这个代码

$data = Http::withHeaders([
                    'Content-Type' => 'application/json',
                    'Authorization' => 'Bearer '.env('OPENAI_API_KEY'),
                  ])
                  ->post("https://api.openai.com/v1/chat/completions", [
                    "model" => "gpt-3.5-turbo",
                    'messages' => [
                        [
                           "role" => "user",
                           "content" => $search
                       ]
                    ],
                    'temperature' => 0.5,
                    "max_tokens" => 200,
                    "top_p" => 1.0,
                    "frequency_penalty" => 0.52,
                    "presence_penalty" => 0.5,
                    "stop" => ["11."],
                  ])
                  ->json();

                  return response()->json($data['choices'][0]['message'], 200, array(), JSON_PRETTY_PRINT);

如何在Laravel数据库中只插入$data ['message']
先谢谢你了
echo $data ['message'];

mnemlml8

mnemlml81#

通过openai的API docs with a response你需要保存:

$data['choices'][0]['message']['content'];

相关问题