1、 PHP調(diào)用OpenAI API的方法
1.如何注冊(cè) openAI 以及使用
https://platform.openai.com/account/api-keys 在這個(gè)地址進(jìn)行注冊(cè),但是需要翻墻,可自己查找國(guó)內(nèi)的試用地址。就不多贅述…文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-801050.html
2.php 調(diào)用接口 (symfony框架)
php端代碼:
思路:前端通過(guò)一個(gè)圖標(biāo)入口,點(diǎn)擊后 發(fā)送請(qǐng)求,創(chuàng)建一個(gè)會(huì)話(也就是聊天室),并且把創(chuàng)建的這個(gè)會(huì)話 入庫(kù)。并且后端返回一個(gè)入庫(kù)生成的uuid,和默認(rèn)的消息會(huì)話返回給前端,前端暫時(shí)存放在 input 隱藏域中,以備后續(xù)使用。此時(shí)完成一個(gè)會(huì)話的連接和創(chuàng)建。
ps:前端發(fā)送請(qǐng)求前,需要做一些校驗(yàn),$this->verifySend(); 驗(yàn)證是否登錄,和發(fā)送請(qǐng)求的頻率
直接上代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-801050.html
<?php
namespace LdWxappPlugin\Api\Resource\Chatapi;
use ApiBundle\Api\ApiRequest;
use ApiBundle\Api\Resource\AbstractResource;
use ApiBundle\Api\Annotation\ApiConf;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class Chatapi extends AbstractResource
{
public function add(ApiRequest $request): array
{
$uuid = $request->request->get('conversationId');
// 沒(méi)有 uuid 就要新建會(huì)話,入庫(kù),有uuid 就直接打開(kāi)
// 這里在頁(yè)面上做了一個(gè) 顯示隱藏效果 不刷新頁(yè)面的情況下,關(guān)閉側(cè)邊窗口,元素移除屏幕
// 再次打開(kāi),有uuid ,直接讓元素 移回原來(lái)的尺寸 'right','-1000px' 'right','0px'
if(empty($uuid)){
// 這里用它來(lái)區(qū)分是 右側(cè)側(cè)邊欄 還是 獨(dú)立網(wǎng)頁(yè)版
$display = $request->request->get('display',0);
// 入庫(kù)的會(huì)話名字 從側(cè)邊欄進(jìn)來(lái)的是 默認(rèn)對(duì)話,從網(wǎng)頁(yè)進(jìn)來(lái)的是 新建對(duì)話
$conversationName = $request->request->get('conversationName','默認(rèn)對(duì)話');
// 課程 ID 和 課程類型 這里可根據(jù)自己的需求傳遞不同參數(shù),這個(gè)課程ID 是為了跟蹤 根據(jù)哪一個(gè)課程,點(diǎn)擊聊天窗口的
$sourceId = $request->request->get('sourceId');
$sourceType = $request->request->get('sourceType');
if (empty($sourceId) || empty($sourceType)){
throw new BadRequestHttpException('sourceId或sourceType不能為空');
}
$currentUser = $this->getCurrentUser();
// 生成 uuid ,uuid 是后端生成好,返回給前端,前端卸載 隱藏域里面,每次請(qǐng)求,用隱藏域里面的去做判斷。
$random = 'lingdai'.microtime(true).rand(1000,9999);
$conversationId = Uuid::uuid5(Uuid::NAMESPACE_OID,$random)->toString();
$goods = $this->findGoodsByTargetIdAndType($sourceId,$sourceType);
$conversationData = [
'conversationId' => $conversationId,
'conversationName' => $conversationName,
'userId' => $currentUser->getId(),
'display' => $display,
'goodsId' => $goods['id'],
'createdTime' => time()
];
$insertResult = $this->getChatApiService()->createConversation($conversationData);
if ($insertResult){
// role : 返回給前端的默認(rèn)對(duì)話 可根據(jù)情況自行設(shè)置
return [
'status'=> 'success',
'message'=> '創(chuàng)建成功',
'code'=> 1,
'data'=> [
'conversationId'=> $conversationId,
'conversationName'=> $conversationName,
'aiDocId'=>$goods['aiDocId'],
'id'=>$insertResult['id'],
'display'=>$insertResult['display'],
'goodsId'=>$insertResult['goodsId'],
'role' => [
[
'role' => 'AI',
'content' => "Hi:你好,哈哈哈哈哈".'</br>'.'123123123'
]
],
]
];
} else {
return ['status'=> 'fail','message'=> '創(chuàng)建失敗','code'=> 0,'data'=> []];
}
}else{
return [];
}
}
// 根據(jù) 傳過(guò)來(lái)的 課程ID 和 課程type 查詢數(shù)據(jù)庫(kù),查到對(duì)應(yīng)的商品ID 數(shù)據(jù)
public function findGoodsByTargetIdAndType($sourceId,$sourceType)
{
if ($sourceType == "goods"){
$goods = $this->getGoodsService()->getGoods($sourceId);
return $goods;
}
$product = $this->getProductService()->getProductByTargetIdAndType($sourceId, $sourceType);
$goods = $this->getGoodsService()->getGoodsByProductId($product['id']);
return $goods;
}
// 新建對(duì)話的 入庫(kù)完成!
//http://www.lingdaipc.win/lddev.php/chat/pcAI/index?chatId=684&uuid=3b9aad06-356e-5534-a8bf-4a98a95a7497&goodsId=3021
// 獨(dú)立的 PC 網(wǎng)頁(yè)版 查詢方法 根據(jù)上面 url中 傳遞的參數(shù) ,查詢對(duì)應(yīng)的 chatId的 的對(duì)話
public function search(ApiRequest $request)
{
// 判斷 ID 是否存在,不存在 是獨(dú)立出來(lái)的窗口 存在是跳轉(zhuǎn)過(guò)來(lái)的
$params = $request->query->all();
$conditions = $this->filterParams($params);
$conditions['display'] = 1;
$currentUser = $this->getCurrentUser();
$conditions['userIds'] = $currentUser['id'];
list($offset, $limit) = $this->getOffsetAndLimit($request);
$conversationTotal = $this->getChatApiService()->count($conditions);
$searchResult = $this->getChatApiService()
到了這里,關(guān)于如何在PHP中對(duì)接openAI接口,PHP創(chuàng)建AI會(huì)話思路以及代碼講解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!