网站首页 > 开源技术 正文
有了这个库大家就可以愉快的使用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
<?php
use Orhanerday\OpenAi\OpenAi;
$open_ai_key = ""; #openAI的key
$open_ai = new OpenAi($open_ai_key); //类的实例化
$chat = $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 "<br>";
echo "<br>";
echo "<br>";
// 解码响应
$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,
]);
访问元素
<?php
// Dummy Response For Chat API
$j = '
{
"id":"chatcmpl-*****",
"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
}
]
}
';
// decode response
$d = json_decode($j);
// Get Content
echo($d->choices[0]->message->content);
还有其余的用法,大家可以根据文档好好研究,我这里就不一一列举了。
猜你喜欢
- 2025-10-14 PHP写站点地图sitemap代码分享及robots的SEO
- 2025-10-14 php源码开发的商城系统有什么优点
- 2025-10-14 mac下快速搭建本地php开发环境_macbook开发php
- 2025-10-14 开发人员分析和解析PHP代码的7款最佳PHP工具
- 2025-10-14 web开发之-PHP数组常用操作_php中数组的概念是什么
- 2025-10-14 两个php框架在一个网站上开发好吗
- 2025-10-14 web开发之-PHP函数_php在web开发中的处理过程是什么
- 2025-10-14 php扩展开发之路(一)_php扩展直接执行php代码
- 2025-10-14 告别传统开发方式,谈谈PHP8异步编程
- 2025-10-14 评测一款PHP开发的专科医院类型的企业网站模板及安装过程
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)