DeepSeekService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Services;
  3. /**
  4. * DeepSeek服务管理-服务类
  5. * @author laravel开发员
  6. * @since 2020/11/11
  7. * @package App\Services
  8. */
  9. class DeepSeekService extends BaseService
  10. {
  11. // 静态对象
  12. protected static $instance = null;
  13. protected $debug = true;
  14. protected $expireTime = 7200; // 缓存日志时长
  15. protected $apiKey = '';
  16. protected $apiName = '';
  17. protected $apiurl = '';
  18. // 接口地址
  19. protected $apiUrls = [
  20. // 授权登录
  21. 'deepseek-chat' => '/chat/completions',
  22. 'deepseek-reasoner' => '/chat/completions',
  23. 'upload' => '/v1/ocr/invoice',
  24. ];
  25. public function __construct()
  26. {
  27. $this->apiUrl = ConfigService::make()->getConfigByCode('dk_base_url');
  28. $this->apiKey = ConfigService::make()->getConfigByCode('dk_api_key');
  29. $this->apiName = ConfigService::make()->getConfigByCode('dk_api_name');
  30. }
  31. /**
  32. * 静态入口
  33. * @return static|null
  34. */
  35. public static function make()
  36. {
  37. if (!self::$instance) {
  38. self::$instance = new static();
  39. }
  40. return self::$instance;
  41. }
  42. /**
  43. * AI 分析接口
  44. * @param $params
  45. * @param string $model
  46. * @return array|false|mixed
  47. */
  48. public function apiRequest($params, $model='deepseek-chat')
  49. {
  50. if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
  51. $this->error = 'AI接口参数未配置';
  52. return false;
  53. }
  54. $headers = [
  55. 'Content-Type: application/json',
  56. 'Accept: application/json',
  57. 'Authorization: Bearer ' . $this->apiKey
  58. ];
  59. $answer = isset($params['answer'])? $params['answer'] : '';
  60. $topic = isset($params['topic'])? $params['topic'] : '';
  61. $score = isset($params['score'])? $params['score'] : 0;
  62. $type = isset($params['type'])? $params['type'] : 1; // 1-文字答案图片题目,2-文字答案文字题目,3-图片答案图片题目,4-图片答案文字题目
  63. $content = "你是一个答题高手";
  64. if($type==1){
  65. $message = "请判断答案【{$answer}】内容并针对图片[image]{$topic}[/image]中题目共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
  66. }else if ($type == 2){
  67. $message = "题目为【{$topic}】的答案【{$answer}】总分共{$score}分能得多少分?请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
  68. }else if ($type == 3){
  69. $file = file_get_contents(ATTACHMENT_PATH.get_image_path($answer));
  70. $file = "data:image/jpeg;base64".base64_encode($file);
  71. $file1 = file_get_contents(ATTACHMENT_PATH.get_image_path($topic));
  72. $file1 = "data:image/jpeg;base64".base64_encode($file1);
  73. $message = "请判断图片内容{$file}中答案内容并针对图片{$file1}中题目内容总分共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
  74. }else{
  75. $message = "请判断图片[image]{$answer}[/image]中答文字案内容并针对【{$topic}】题目内容总分共{$score}分给出评分,请返回针对该题目的包含score字段评分、topic字段题目内容以及analyze题目解析字段的结果";
  76. }
  77. $file = file_get_contents(ATTACHMENT_PATH.get_image_path($answer));
  78. // var_dump($file);
  79. $file = "data:image/jpeg;base64,".base64_encode($file);
  80. $message = "请上传图片[image]{$answer}[/image],返回分子分析数据";
  81. //$message = "请分析图片[image]{$image}[/image]中答案与".($type==1?'题目'.$topic:"图片[image]{$topic}[/image]中题目内容")."的正确度,共{$score}分,请返回包含score字段评分、topic字段题目内容、analyze题目解析字段的结果";
  82. dump($message);
  83. $data = [
  84. 'model'=> $model,
  85. 'messages'=> [
  86. [
  87. 'role' => 'system',
  88. 'content' => $content
  89. ],
  90. [
  91. 'role' => 'user',
  92. 'content' => $message
  93. ],
  94. ],
  95. //'stream' => false, //false 非流 true//流返回,需要前端追加和保持长连接
  96. // 'response_format'=>[
  97. // 'type'=>'text' //返回格式(text,json_object)
  98. // ],
  99. // "max_tokens"=>2048, //最大返回token数
  100. ];
  101. $url = $this->apiUrl.$this->apiUrls[$model];
  102. $result = aiRequest($url, json_encode($data), 20, $headers);
  103. dump($result);
  104. $choices = isset($result['choices'])? $result['choices'] : [];
  105. $choiceData = isset($choices[0]['message'])? $choices[0]['message'] : [];
  106. $choiceContent = isset($choiceData['content'])? $choiceData['content'] : '';
  107. $content = $choiceContent? str_replace('\n','', $choiceContent) : '';
  108. $content = $content? json_decode($content, true) : [];
  109. return $content;
  110. }
  111. public function upload($image)
  112. {
  113. if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
  114. $this->error = 'AI接口参数未配置';
  115. return false;
  116. }
  117. $headers = [
  118. 'Content-Type: application/json',
  119. 'Accept: application/json',
  120. 'Authorization: Bearer ' . $this->apiKey
  121. ];
  122. $filePath = ATTACHMENT_PATH.get_image_path($image);
  123. var_dump($filePath);
  124. $obj = new \CURLFile($filePath);
  125. $data = [
  126. 'image'=> $obj,
  127. ];
  128. $url = $this->apiUrl.$this->apiUrls['upload'];
  129. var_dump($url);
  130. // $url = 'http://127.0.5.12/api/v1/upload/image';
  131. // var_dump($options);
  132. // $result = file_get_contents($url, false, $context);
  133. $result = aiRequest($url, $data, 10, $headers);
  134. dump($result);
  135. }
  136. }