美烦资源网

专注技术文章分享,涵盖编程教程、IT 资源与前沿资讯

【开源推荐】给大家推荐个基于ChatGPT的PHP开发库 openai-php-api

有了这个库大家就可以愉快的使用PHP对接chatGPT的官方接口了,至于对接了官方接口想要做什么就看你自己的啦

环境要求

PHP7.4或以上

composer 1.6.5以上

支持框架 Laravel、Symfony、Yii2、Cake PHP 或任何 PHP 框架,完全开源,支持OpenAI GPT-3 API接口。

支持功能

聊天 - 聊天GPT API

模型 - 列出模型,检索模型

创建 - 创建完成

编辑 - 创建编辑

图片 - 创建图像,编辑图像

嵌入 - 创建嵌入

声音 - 创建转录,创建翻译

文件 - 列出文件,上传文件,删除文件,检索文件,检索内容

安装使用

composer require orhanerday/open-ai
chat([
'model' => 'gpt-3.5-turbo', //定义模型
'messages' => [
[
"role" => "system", //角色系统
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who won the world series in 2020?"
],
[
"role" => "assistant", //角色助理
"content" => "The Los Angeles Dodgers won the World Series in 2020."
],
[
"role" => "user",
"content" => "Where was it played?"
],
],
'temperature' => 1.0,
'max_tokens' => 4000, //最大token
'frequency_penalty' => 0,
'presence_penalty' => 0,
]);


var_dump($chat);
echo "
"; echo "
"; echo "
"; // 解码响应 $d = json_decode($chat); // 获取内容 echo($d->choices[0]->message->content);

基本网址

使用 setBaseURL() 方法指定Origin URL;

$open_ai_key = getenv('OPENAI_API_KEY');
$open_ai = new OpenAi($open_ai_key,$originURL);
$open_ai->setBaseURL("https://ai.example.com/");

使用代理服务器

$open_ai->setProxy("http://127.0.0.1:1086");

设置标题

$open_ai->setHeader(["Connection"=>"keep-alive"]);

基于ChatGPT API 的聊天

$complete = $open_ai->chat([
'model' => 'gpt-3.5-turbo',
'messages' => [
[
"role" => "system",
"content" => "You are a helpful assistant."
],
[
"role" => "user",
"content" => "Who won the world series in 2020?"
],
[
"role" => "assistant",
"content" => "The Los Angeles Dodgers won the World Series in 2020."
],
[
"role" => "user",
"content" => "Where was it played?"
],
],
'temperature' => 1.0,
'max_tokens' => 4000,
'frequency_penalty' => 0,
'presence_penalty' => 0,
]);

访问元素

choices[0]->message->content);

还有其余的用法,大家可以根据文档好好研究,我这里就不一一列举了。

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言