更新星火认知模型聊天
This commit is contained in:
parent
b1e33a2656
commit
fe9eb7cb41
@ -41,90 +41,65 @@ class XunFeiController extends BaseApiController
|
|||||||
public function chat()
|
public function chat()
|
||||||
{
|
{
|
||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
$parmas=$this->request->param('content');
|
$content = $this->request->param('content');
|
||||||
if(empty($parmas)){
|
if(empty($content)){
|
||||||
return $this->success('success');
|
return $this->data(['answer' => '']);
|
||||||
}
|
}
|
||||||
$chat=new ChatClient($this->app_id,$this->api_key,$this->api_secret);
|
$chat=new ChatClient($this->app_id,$this->api_key,$this->api_secret);
|
||||||
$client = new Client($chat->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat'));
|
$client = new Client($chat->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat'));
|
||||||
// 连接到 WebSocket 服务器
|
// 连接到 WebSocket 服务器
|
||||||
if ($client) {
|
if ($client) {
|
||||||
// 发送数据到 WebSocket 服务器
|
$header = [
|
||||||
$data = $this->getBody($this->app_id,$parmas);
|
"app_id" => $this->app_id,
|
||||||
|
"uid" => "1"
|
||||||
|
];
|
||||||
|
$parameter = [
|
||||||
|
"chat" => [
|
||||||
|
"domain" => "generalv2",
|
||||||
|
"temperature" => 0.5,
|
||||||
|
"max_tokens" => 1024
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$payload = [
|
||||||
|
"message" => [
|
||||||
|
"text" => [
|
||||||
|
["role" => "user", "content" => $content]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$data = json_encode([
|
||||||
|
"header" => $header,
|
||||||
|
"parameter" => $parameter,
|
||||||
|
"payload" => $payload
|
||||||
|
]);
|
||||||
|
|
||||||
$client->send($data);
|
$client->send($data);
|
||||||
// 从 WebSocket 服务器接收数据
|
|
||||||
$answer = "";
|
$answer = "";
|
||||||
while(true){
|
while(true){
|
||||||
$response = $client->receive();
|
$response = $client->receive();
|
||||||
$resp = json_decode($response,true);
|
$resp = json_decode($response, true);
|
||||||
$code = $resp["header"]["code"];
|
$code = $resp["header"]["code"] ?? 0;
|
||||||
// echo "从服务器接收到的数据: " . $response;
|
|
||||||
if(0 == $code){
|
if(0 == $code){
|
||||||
$status = $resp["header"]["status"];
|
$status = $resp["header"]["status"];
|
||||||
if($status != 2){
|
if($status != 2){
|
||||||
$content = $resp['payload']['choices']['text'][0]['content'];
|
$content = $resp['payload']['choices']['text'][0]['content'];
|
||||||
$answer .= $content;
|
$answer .= $content;
|
||||||
print($answer);
|
|
||||||
ob_flush(); // 刷新输出缓冲区
|
|
||||||
flush(); // 刷新系统输出缓冲区
|
|
||||||
}else{
|
}else{
|
||||||
$content = $resp['payload']['choices']['text'][0]['content'];
|
$content = $resp['payload']['choices']['text'][0]['content'];
|
||||||
$answer .= $content;
|
$answer .= $content;
|
||||||
$total_tokens = $resp['payload']['usage']['text']['total_tokens'];
|
|
||||||
print("\n本次消耗token用量:\n");
|
|
||||||
print($total_tokens);
|
|
||||||
ob_flush(); // 刷新输出缓冲区
|
|
||||||
flush(); // 刷新系统输出缓冲区
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
return $this->fail( "服务返回报错".$response);
|
return $this->fail( "服务返回报错 " . $response);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ob_flush(); // 刷新输出缓冲区
|
return $this->data(['answer' => $answer]);
|
||||||
flush(); // 刷新系统输出缓冲区
|
|
||||||
return $this->success('success');
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return $this->fail('无法连接到 WebSocket 服务器');
|
return $this->fail('无法连接到 WebSocket 服务器');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//构造参数体
|
|
||||||
function getBody($appid,$question){
|
|
||||||
$header = array(
|
|
||||||
"app_id" => $appid,
|
|
||||||
"uid" => "1"
|
|
||||||
);
|
|
||||||
|
|
||||||
$parameter = array(
|
|
||||||
"chat" => array(
|
|
||||||
"domain" => "generalv2",
|
|
||||||
"temperature" => 0.5,
|
|
||||||
"max_tokens" => 1024
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$payload = array(
|
|
||||||
"message" => array(
|
|
||||||
"text" => array(
|
|
||||||
array("role" => "user", "content" => $question)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$json_string = json_encode(array(
|
|
||||||
"header" => $header,
|
|
||||||
"parameter" => $parameter,
|
|
||||||
"payload" => $payload
|
|
||||||
));
|
|
||||||
|
|
||||||
return $json_string;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//语音听写(流式版)
|
//语音听写(流式版)
|
||||||
public function iat()
|
public function iat()
|
||||||
{
|
{
|
||||||
@ -147,7 +122,7 @@ class XunFeiController extends BaseApiController
|
|||||||
$last_file = substr($savename, -36);
|
$last_file = substr($savename, -36);
|
||||||
$copyFile = app()->getRootPath() . '/public/uploads/iat/' . $last_file;
|
$copyFile = app()->getRootPath() . '/public/uploads/iat/' . $last_file;
|
||||||
|
|
||||||
// 临时方案
|
// ********** 临时方案 ********** //
|
||||||
copy($file, $copyFile);
|
copy($file, $copyFile);
|
||||||
// $last_file = 'a1fcdd96c7967b48add17b52ab456368.mp3';
|
// $last_file = 'a1fcdd96c7967b48add17b52ab456368.mp3';
|
||||||
$curl_url = "https://dev.app.tword.cn/ffmpeg.php?file={$last_file}";
|
$curl_url = "https://dev.app.tword.cn/ffmpeg.php?file={$last_file}";
|
||||||
|
@ -93,6 +93,7 @@ class UserInformationg extends BaseModel
|
|||||||
$category_child = $param['card_id'];
|
$category_child = $param['card_id'];
|
||||||
$field_array = json_decode($category_info['data_field'], true);
|
$field_array = json_decode($category_info['data_field'], true);
|
||||||
if (!empty($field_array) && is_array($field_array)) {
|
if (!empty($field_array) && is_array($field_array)) {
|
||||||
|
// 拼装词语
|
||||||
foreach($param['datas'] as $k => $v) {
|
foreach($param['datas'] as $k => $v) {
|
||||||
if (!empty($field_array[$k]['text'])) {
|
if (!empty($field_array[$k]['text'])) {
|
||||||
$key = $field_array[$k]['text'];
|
$key = $field_array[$k]['text'];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user