(object) null, 'query' => $content, 'response_mode' => "streaming", "conversation_id"=>$conversation_id, "user"=>$userId, "files"=>null ]; $headers = array( "Content-Type: application/json", "Authorization: Bearer ".$AIConfig["secret"] ); $url = $AIConfig["url"].self::SEND_CHAT_URL; try { $addData[] = array( "user_id"=>$userId, "conversation_id"=>null, "target_id"=>AiChatContent::ROBOT_USER_ID, "content"=>$content, "operate_type"=>AiChatContent::OPERATE_TYPE_ASK ); $result = $this->postData($data,$headers,$url); $addData[]=array( "user_id"=>AiChatContent::ROBOT_USER_ID, "conversation_id"=>$result["conversation_id"], "target_id"=>$userId, "content"=>$result["answer"], "path"=>$result["path"], "operate_type"=>AiChatContent::OPERATE_TYPE_ANSWER ); foreach ($addData as &$val){ if (empty($val["conversation_id"])){ $val["conversation_id"] = $result["conversation_id"]; $val["path"]=$result["path"]; } AiChatContent::query()->create($val); } if (isset($result["path"])){ unset($result["path"]); } return $this->response($result); }catch (\Exception $exception){ return $this->error()->fail($exception->getMessage()); } } /** * 发送数据 * @param array $data * @param array $headers * @param $url * @return array * @throws \Exception */ private function postData(array $data,array $headers,$url) { $this->AIStreamHandler = new AIStreamHandler($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->AIStreamHandler,"callback"]); // 设置超时时间 // curl_setopt($ch, CURLOPT_TIMEOUT, 120); // 整个请求的超时时间为 120 秒 // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // 连接建立的超时时间为 30 秒 $response = curl_exec($ch); if (curl_errno($ch)) { $this->logError(curl_error($ch).PHP_EOL.PHP_EOL); } curl_close($ch); return $this->AIStreamHandler->getData(null); } private function logError($content) { $sqlFile = 'logs/curl/curl.error.log'; (new Logger('curl'))->pushHandler(new RotatingFileHandler(storage_path($sqlFile)))->info($content); } public function getChatContentList(array $conditions, array $fields, string $sort, int $page, int $limit) { $result = AiChatContent::query() ->select(["id","user_id","conversation_id", "target_id","content","operate_type", "created_at"] )->with([ "user"=>function ($with) { $with->select([ "id","third_up_nickname","third_up_headimg", "wx_nickname","wx_headimgurl" ] ); }, "target"=>function ($with) { $with->select([ "id","third_up_nickname","third_up_headimg", "wx_nickname","wx_headimgurl" ] ); }, ]) ->where( function(Builder $query) use($conditions){ if (isset($conditions['Robot'])){ if (!isset($conditions["user_id"])){ return $this->error()->fail("请联系管理员,必填参数user_id不能为空"); }; $ROBOT_USER_ID = AiChatContent::ROBOT_USER_ID; $query->whereRaw( "((user_id={$conditions["user_id"]} and target_id = {$ROBOT_USER_ID}) OR (user_id={$ROBOT_USER_ID} and target_id = {$conditions["user_id"]}))" ); $startTime = Carbon::parse('-3 days')->toDateTimeString(); $endTime = Carbon::now(); $query->WhereBetween("created_at",array($startTime,$endTime)); if (!empty($conditions["conversation_id"])){ $query->where("conversation_id",$conditions["conversation_id"]); } } } )->orderByRaw($sort)->paginate($limit, $fields); return $this->response($result); } }