DeepSeekService.php 4.5 KB

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