123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Models\AiChatContent;
- use App\Repositories\Contracts\AiChatContentInterface;
- use App\Repositories\StreamHandler\AIStreamHandler;
- use Carbon\Carbon;
- use Illuminate\Database\Eloquent\Builder;
- use Monolog\Handler\RotatingFileHandler;
- use Monolog\Logger;
- class AiChatContentFacadeRepository extends BaseRepository implements AiChatContentInterface
- {
- private $AIStreamHandler = null;
- public function sendData(int $userId, string $conversation_id, string $content)
- {
- $AIConfig = config("console.AI");
- $data = [
- 'inputs' => (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);
- }
- }
|