| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Services;
- /**
- * DeepSeek服务管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services
- */
- class DeepSeekService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- protected $debug = true;
- protected $expireTime = 7200; // 缓存日志时长
- protected $apiKey = '';
- protected $apiName = '';
- protected $apiurl = '';
- // 接口地址
- protected $apiUrls = [
- // 授权登录
- 'deepseek-chat' => '/chat/completions',
- 'deepseek-reasoner' => '/chat/completions',
- 'upload' => '/v1/ocr/invoice',
- ];
- public function __construct()
- {
- $this->apiUrl = ConfigService::make()->getConfigByCode('dk_base_url');
- $this->apiKey = ConfigService::make()->getConfigByCode('dk_api_key');
- $this->apiName = ConfigService::make()->getConfigByCode('dk_api_name');
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * AI 分析接口
- * @param $params
- * @param string $model
- * @return array|false|mixed
- */
- public function apiRequest($params, $model='deepseek-chat')
- {
- if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
- $this->error = 'AI接口参数未配置';
- return false;
- }
- $headers = [
- 'Content-Type: application/json',
- 'Accept: application/json',
- 'Authorization: Bearer ' . $this->apiKey
- ];
- $answer = isset($params['answer'])? $params['answer'] : '';
- $topic = isset($params['topic'])? $params['topic'] : '';
- $score = isset($params['score'])? $params['score'] : 0;
- $type = isset($params['type'])? $params['type'] : 1; // 1-文字答案图片题目,2-文字答案文字题目,3-图片答案图片题目,4-图片答案文字题目
- $content = "你是一个答题高手";
- if($type==1){
- $message = "请判断答案【{$answer}】内容并针对图片[image]{$topic}[/image]中题目共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
- }else if ($type == 2){
- $message = "题目为【{$topic}】的答案【{$answer}】总分共{$score}分能得多少分?请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
- }else if ($type == 3){
- $file = file_get_contents(ATTACHMENT_PATH.get_image_path($answer));
- $file = "data:image/jpeg;base64".base64_encode($file);
- $file1 = file_get_contents(ATTACHMENT_PATH.get_image_path($topic));
- $file1 = "data:image/jpeg;base64".base64_encode($file1);
- $message = "请判断图片内容{$file}中答案内容并针对图片{$file1}中题目内容总分共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
- }else{
- $message = "请判断图片[image]{$answer}[/image]中答文字案内容并针对【{$topic}】题目内容总分共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
- }
- $file = file_get_contents(ATTACHMENT_PATH.get_image_path($answer));
- // var_dump($file);
- $file = "data:image/jpeg;base64,".base64_encode($file);
- $message = "请上传图片[image]{$answer}[/image],返回分子分析数据";
- //$message = "请分析图片[image]{$image}[/image]中答案与".($type==1?'题目'.$topic:"图片[image]{$topic}[/image]中题目内容")."的正确度,共{$score}分,请返回包含score字段评分、topic字段题目内容、analyze题目解析字段的结果";
- dump($message);
- $data = [
- 'model'=> $model,
- 'messages'=> [
- [
- 'role' => 'system',
- 'content' => $content
- ],
- [
- 'role' => 'user',
- 'content' => $message
- ],
- ],
- //'stream' => false, //false 非流 true//流返回,需要前端追加和保持长连接
- // 'response_format'=>[
- // 'type'=>'text' //返回格式(text,json_object)
- // ],
- // "max_tokens"=>2048, //最大返回token数
- ];
- $url = $this->apiUrl.$this->apiUrls[$model];
- $result = aiRequest($url, json_encode($data), 20, $headers);
- dump($result);
- $choices = isset($result['choices'])? $result['choices'] : [];
- $choiceData = isset($choices[0]['message'])? $choices[0]['message'] : [];
- $choiceContent = isset($choiceData['content'])? $choiceData['content'] : '';
- $content = $choiceContent? str_replace('\n','', $choiceContent) : '';
- $content = $content? json_decode($content, true) : [];
- return $content;
- }
- public function upload($image)
- {
- if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
- $this->error = 'AI接口参数未配置';
- return false;
- }
- $headers = [
- 'Content-Type: application/json',
- 'Accept: application/json',
- 'Authorization: Bearer ' . $this->apiKey
- ];
- $filePath = ATTACHMENT_PATH.get_image_path($image);
- var_dump($filePath);
- $obj = new \CURLFile($filePath);
- $data = [
- 'image'=> $obj,
- ];
- $url = $this->apiUrl.$this->apiUrls['upload'];
- var_dump($url);
- // $url = 'http://127.0.5.12/api/v1/upload/image';
- // var_dump($options);
- // $result = file_get_contents($url, false, $context);
- $result = aiRequest($url, $data, 10, $headers);
- dump($result);
- }
- }
|