当前位置: 首页> 财经> 金融 > Get content output from OpenAI library

Get content output from OpenAI library

时间:2025/7/10 10:04:36来源:https://blog.csdn.net/suiusoar/article/details/141239868 浏览次数:0次

题意:从 OpenAI 库中获取内容输出

问题背景:

I prepare integration openAI API with Codeigniter 4, for this I use library:

我准备将 OpenAI API 集成到 Codeigniter 4 中,为此我使用了以下库:

https://github.com/orhanerday/open-ai

I write controller CI4:        我编写了 CI4 控制器:

public function openAIPost(){$response = array();if ($this->request->getMethod() === 'post'){question = $this->request->getPost('question');$open_ai_key = getenv('OPENAI_API_KEY');$open_ai = new OpenAi($open_ai_key);$complete = $open_ai->chat(['model' => 'gpt-3.5-turbo','messages' => [["role" => "user","content" => $question]],'temperature' => 1.0,'max_tokens' => 10,'frequency_penalty' => 0,'presence_penalty' => 0,]);$response['complete'] = $complete;}echo json_encode($response);
}

But I get in output:        但我在输出中得到:

 {"id": "chatcmpl-6xxhQ2FLm08VhE8KKHsTpilkvpMTS","object": "chat.completion","created": 1679748856,"model": "gpt-3.5-turbo-0301","usage": {"prompt_tokens": 9,"completion_tokens": 10,"total_tokens": 19},"choices": [{"message": {"role": "assistant","content": "This is a test of the AI language model."},"finish_reason": "length","index": 0}]}

How to get only content from response?

如何仅从响应中获取内容?

问题解决:

You should all you need in the JSON response returned by the OpenAI API you have.

你所需要的一切都在你收到的 OpenAI API 返回的 JSON 响应中。

You should navigate the JSON object and extract the message content using the following line.

你应该遍历 JSON 对象,并使用以下代码行提取消息内容。

$content = $complete.choices[0].message.content

it should give you "This is a test of the AI language model."

它应该返回给你 “This is a test of the AI language model.”

关键字:Get content output from OpenAI library

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: