admin 管理员组文章数量: 887021
TP6自行安装,本文只介绍openai api接口
*国内服务器必须配置代理才能访问成功.
1poser安装open-ai
composer require orhanerday/open-ai
2.命名空间引入
use Orhanerday\OpenAi\OpenAi;
3.请求接口
OPENAI_API_KEY申请地址
$question = $this->request->param('question','');
$open_ai_key = "xxx";//你的key
$open_ai = new OpenAi($open_ai_key);
$open_ai->setProxy("http://127.0.0.1:2023");//本地调试代理
// 返回文本
$complete = $open_ai->chat([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
"role" => "user",
"content" => $question
],
],
'temperature' => 1.0,
'max_tokens' => 4000,
'frequency_penalty' => 0,
'presence_penalty' => 0,
]);
$returnData = json_decode($complete,true);
$this->success('返回成功', $returnData);
使用示例
4.其它类型接口使用详见GitHub源码
// 返回图片
$complete = $open_ai->image([
"prompt" => $question,
"n" => 1,
"size" => "256x256",
"response_format" => "url",
]);
// 将音频转录成输入语言
$c_file = curl_file_create('./assets/img/CKwRIRwE9j-9ABqphgDaXVV7.m4a');
$result = $open_ai->transcribe([
"model" => "whisper-1",
"file" => $c_file,
]);
// 上传文件
$c_file = curl_file_create('./assets/img/openai.docx');
$result = $open_ai->uploadFile([
"purpose" => "answers",
"file" => $c_file,
]);
版权声明:本文标题:Chatgpt,ThinkPHP6使用openai api示例 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1717034220h691539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论