更新星火认知模型聊天
This commit is contained in:
parent
fe9eb7cb41
commit
bd77a97d2f
@ -18,6 +18,7 @@ use IFlytek\Xfyun\Speech\ChatClient;
|
|||||||
use IFlytek\Xfyun\Speech\IatClient;
|
use IFlytek\Xfyun\Speech\IatClient;
|
||||||
use IFlytek\Xfyun\Speech\TtsClient;
|
use IFlytek\Xfyun\Speech\TtsClient;
|
||||||
use IFlytek\Xfyun\Speech\OcrClient;
|
use IFlytek\Xfyun\Speech\OcrClient;
|
||||||
|
use think\facade\Db;
|
||||||
use WebSocket\Client;
|
use WebSocket\Client;
|
||||||
use GuzzleHttp\Client as GzClient;
|
use GuzzleHttp\Client as GzClient;
|
||||||
use GuzzleHttp\Psr7\Request;
|
use GuzzleHttp\Psr7\Request;
|
||||||
@ -30,14 +31,13 @@ use Guzzle\Http\Exception\RequestException;
|
|||||||
*/
|
*/
|
||||||
class XunFeiController extends BaseApiController
|
class XunFeiController extends BaseApiController
|
||||||
{
|
{
|
||||||
public array $notNeedLogin = ['chat', 'iat', 'tts', 'ocr', 'iatWss'];
|
public array $notNeedLogin = ['chat', 'iat', 'tts', 'ocr', 'iatWss', 'analyse'];
|
||||||
|
|
||||||
private $app_id='2eda6c2e';
|
private $app_id='2eda6c2e';
|
||||||
|
|
||||||
private $api_key='12ec1f9d113932575fc4b114a2f60ffd';
|
private $api_key='12ec1f9d113932575fc4b114a2f60ffd';
|
||||||
|
|
||||||
private $api_secret='MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2';
|
private $api_secret='MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2';
|
||||||
|
|
||||||
|
//星火认知chat
|
||||||
public function chat()
|
public function chat()
|
||||||
{
|
{
|
||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
@ -100,6 +100,90 @@ class XunFeiController extends BaseApiController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//AI分析信息
|
||||||
|
public function analyse()
|
||||||
|
{
|
||||||
|
$informationg_demand_id = $this->request->param('informationg_demand_id');
|
||||||
|
if(empty($informationg_demand_id)){
|
||||||
|
return $this->fail('信息参数错误');
|
||||||
|
}
|
||||||
|
$informationg = Db::name('user_informationg_demand')->where('id', $informationg_demand_id)->where('status', 1)->find();
|
||||||
|
$type_name = Db::name('category_business')->where('id', $informationg['category_child'])->value('name');
|
||||||
|
$data_field = json_decode($informationg['data_field'], true);
|
||||||
|
$demand = '';
|
||||||
|
foreach($data_field as $k=>$v) {
|
||||||
|
$demand .= $k . ':' . $v . ';';
|
||||||
|
}
|
||||||
|
$question = "分析以下{$type_name}信息【{$demand}】请问有那些商机?";
|
||||||
|
$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'));
|
||||||
|
// 连接到 WebSocket 服务器
|
||||||
|
if ($client) {
|
||||||
|
$header = [
|
||||||
|
"app_id" => $this->app_id,
|
||||||
|
"uid" => "1"
|
||||||
|
];
|
||||||
|
$parameter = [
|
||||||
|
"chat" => [
|
||||||
|
"domain" => "generalv2",
|
||||||
|
"temperature" => 0.5,
|
||||||
|
"max_tokens" => 1024
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$payload = [
|
||||||
|
"message" => [
|
||||||
|
"text" => [
|
||||||
|
["role" => "user", "content" => $question]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$data = json_encode([
|
||||||
|
"header" => $header,
|
||||||
|
"parameter" => $parameter,
|
||||||
|
"payload" => $payload
|
||||||
|
]);
|
||||||
|
|
||||||
|
$client->send($data);
|
||||||
|
$answer = '';
|
||||||
|
while(true){
|
||||||
|
$response = $client->receive();
|
||||||
|
$resp = json_decode($response, true);
|
||||||
|
$code = $resp["header"]["code"] ?? 0;
|
||||||
|
if($code == 0){
|
||||||
|
$status = $resp["header"]["status"];
|
||||||
|
$content = $resp['payload']['choices']['text'][0]['content'] ?? '';
|
||||||
|
$content = trim(str_replace(['\n', '\n\n', '\t', '\r', ' '], '', $content));
|
||||||
|
if ($content) {
|
||||||
|
$answer .= $content;
|
||||||
|
}
|
||||||
|
if($status == 2){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
return $this->fail( "服务返回报错 " . $response);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$data = [
|
||||||
|
'informationg_demand_id' => $informationg_demand_id,
|
||||||
|
'information_id' => $informationg['information_id'] ?? 0,
|
||||||
|
'category_id' => $informationg['category_id'] ?? 0,
|
||||||
|
'category_child' => $informationg['category_child'] ?? 0,
|
||||||
|
'send_data' => $question,
|
||||||
|
'receive_data' => $answer,
|
||||||
|
'create_time' => time(),
|
||||||
|
'update_time' => time(),
|
||||||
|
];
|
||||||
|
$res = Db::name('user_informationg_aianalyse')->insert($data);
|
||||||
|
if (!$res) {
|
||||||
|
return $this->fail('AI分析信息失败');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return $this->fail('无法连接到 WebSocket 服务器');
|
||||||
|
}
|
||||||
|
return $this->data(['question'=>$question, 'answer' => $answer]);
|
||||||
|
}
|
||||||
|
|
||||||
//语音听写(流式版)
|
//语音听写(流式版)
|
||||||
public function iat()
|
public function iat()
|
||||||
{
|
{
|
||||||
@ -252,7 +336,7 @@ class XunFeiController extends BaseApiController
|
|||||||
return $this->data(['words' => $words]);
|
return $this->data(['words' => $words]);
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取websocket
|
//获取语音听写(流式版)websocket地址
|
||||||
public function iatWss()
|
public function iatWss()
|
||||||
{
|
{
|
||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user