?一:API 調(diào)用流程簡(jiǎn)介
- 創(chuàng)建一個(gè)智能云應(yīng)用。根據(jù)實(shí)際需求創(chuàng)建智能云應(yīng)用。創(chuàng)建成功后,獲取AppID、API Key、Secret Key 等信息。
- API 授權(quán)。對(duì)應(yīng)用的 AppID 進(jìn)行授權(quán)。
- 獲取接口訪問憑證 access_token 。根據(jù)第1步獲取的 API Key 和 Secret Key ,
獲取 access_token ,通過 access_token 鑒權(quán)調(diào)用者身份。 - 調(diào)用API接口。調(diào)用創(chuàng)建chat接口,詳見本文說明。
二:具體功能實(shí)現(xiàn)
?Chat.php
<?php
namespace bdchat;
class Chat {
private $client_id;// API Key
private $client_secret;// Secret Key
private $message;// 聊天上下文信息
public function __construct($client_id, $client_secret) {
$this->client_id = $client_id;
$this->client_secret = $client_secret;
}
public function runErnieBot($message) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token={$this->getAccessToken()}",
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$message,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
public function runErnieBotTurbo($message) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token={$this->getAccessToken()}",
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$message,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* 使用 AK,SK 生成鑒權(quán)簽名(Access Token)
* @return string 鑒權(quán)簽名信息(Access Token)
*/
private function getAccessToken(){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://aip.baidubce.com/oauth/2.0/token?client_id=".$this->client_id."&client_secret=".$this->client_secret."&grant_type=client_credentials",
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
$rtn = json_decode($response);
return $rtn->access_token;
}
}
?這里我使用的是多輪
public function run() {
$user_id = 1;//用戶ID
$msg = "如何成為更好的人";//用戶聊天內(nèi)容
$is_stream = 0;//是否以流式接口的形式返回?cái)?shù)據(jù),默認(rèn)false。
$cacheKey = $user_id.'@chatlog';// 緩存文件名
$old_content = cache($cacheKey);
include_once CMF_ROOT . 'vendor/baidubce/Chat.php';
$chat = new Chat('ClientId','ClientSecret');//自行更改一下配置
$messages = [];
$my_msg = [];
$my_msg['role'] = 'user';
$my_msg['content'] = $msg;
if (!$old_content) {
// 之前該用戶沒有存在聊天記錄
$messages['messages'][] = $my_msg;
} else {
// 之前有聊天記錄
$messages = json_decode($old_content,true);
$messages['messages'][] = $my_msg;
}
$messages['stream'] = $is_stream == 1 ? true : false;
$data = json_encode($messages,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = $chat->runErnieBotTurbo($data);
$res = [];
if ($is_stream == 1) {
$str_arr = explode("data: ",$response);
array_shift($str_arr);
$res_msg = [];
for ($i=0; $i < count($str_arr); $i++) {
$arr = [];
$arr = json_decode($str_arr[$i],true);
$res_msg[] = $arr['result'];
}
$res['result'] = implode("\n\n",$res_msg);
} else {
$res = json_decode($response,true);
}
$assistant_msg = [];
$assistant_msg['role'] = 'assistant';
$assistant_msg['content'] = $res['result'];
$messages['messages'][] = $assistant_msg;
cache($cacheKey,json_encode($messages));
$this->success('請(qǐng)求成功!',$messages);
}
三:相關(guān)問題
?01 單輪與多輪的區(qū)別
多輪的需要在請(qǐng)求參數(shù)中將之前發(fā)送與返回的數(shù)據(jù)也加上,上面的代碼示例是用的多輪,不過推薦大家使用單輪響應(yīng)的方式。
?
?02 注意流式接口返回?cái)?shù)據(jù)與其他不同,需要對(duì)數(shù)據(jù)進(jìn)行處理
?03 可能會(huì)遇到的問題
最開始對(duì)接的時(shí)候,提示以下錯(cuò)誤信息:(無權(quán)限訪問該用戶數(shù)據(jù)。)
{"error_code":6,"error_msg":"No permission to access data"}
?首先這種問題需要考慮創(chuàng)建應(yīng)用是否勾選相關(guān)接口權(quán)限。
文章來源:http://www.zghlxwxcb.cn/news/detail-502465.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-502465.html
到了這里,關(guān)于PHP 文心千帆API接口對(duì)接的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!