94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace app\job;
|
|||
|
|
|||
|
use app\common\model\informationg\UserInformationg;
|
|||
|
use think\facade\Db;
|
|||
|
use think\queue\Job;
|
|||
|
use IFlytek\Xfyun\Speech\ChatClient;
|
|||
|
use WebSocket\Client;
|
|||
|
use GuzzleHttp\Client as GzClient;
|
|||
|
use GuzzleHttp\Psr7\Request;
|
|||
|
use GuzzleHttp\Exception\GuzzleException;
|
|||
|
use Guzzle\Http\Exception\RequestException;
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 任务执行AI分析
|
|||
|
*/
|
|||
|
class AiAianalyse
|
|||
|
{
|
|||
|
private $app_id='2eda6c2e';
|
|||
|
private $api_key='12ec1f9d113932575fc4b114a2f60ffd';
|
|||
|
private $api_secret='MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2';
|
|||
|
|
|||
|
public function fire(Job $job, $data)
|
|||
|
{
|
|||
|
if ($job->attempts() > 3) {
|
|||
|
//通过这个方法可以检查这个任务已经重试了几次了
|
|||
|
$job->delete();
|
|||
|
}
|
|||
|
$type_name = Db::name('category_business')->where('id', $data['category_child'])->value('name');
|
|||
|
$data_field = json_decode($data['data_field'], true);
|
|||
|
$demand = '';
|
|||
|
foreach($data_field as $k=>$v) {
|
|||
|
$demand .= $k . ':' . $v . ';';
|
|||
|
}
|
|||
|
$question = "根据以下{$type_name}信息【{$demand}】请问有那些商机?需要购买哪些商品?";
|
|||
|
try {
|
|||
|
$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'));
|
|||
|
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]
|
|||
|
]
|
|||
|
]
|
|||
|
];
|
|||
|
$chat_data = json_encode([
|
|||
|
"header" => $header,
|
|||
|
"parameter" => $parameter,
|
|||
|
"payload" => $payload
|
|||
|
]);
|
|||
|
|
|||
|
$client->send($chat_data);
|
|||
|
$answer = '';
|
|||
|
while(true){
|
|||
|
$response = $client->receive();
|
|||
|
$resp = json_decode($response, true);
|
|||
|
$code = $resp["header"]["code"] ?? 0;
|
|||
|
if($code > 0){
|
|||
|
break;
|
|||
|
}
|
|||
|
$status = $resp["header"]["status"];
|
|||
|
$content = $resp['payload']['choices']['text'][0]['content'] ?? '';
|
|||
|
$answer .= $content;
|
|||
|
if($status == 2){
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
$update_data = [
|
|||
|
'ai_question' => $question,
|
|||
|
'ai_aianalyse' => $answer,
|
|||
|
'update_time' => time(),
|
|||
|
];
|
|||
|
unset($data['data'], $data['data_field']);
|
|||
|
Db::name('user_informationg_demand')->where($data)->update($update_data);
|
|||
|
}
|
|||
|
} catch (\Exception $e) {}
|
|||
|
$job->delete();
|
|||
|
}
|
|||
|
}
|