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(); } }