|
@@ -30,7 +30,7 @@ class DeepSeekService extends BaseService
|
|
|
'upload' => '/v1/upload',
|
|
'upload' => '/v1/upload',
|
|
|
'generations' => '/v1/images/generations',
|
|
'generations' => '/v1/images/generations',
|
|
|
'generate' => '/v1/text/generate',
|
|
'generate' => '/v1/text/generate',
|
|
|
- 'upload_file' => '/v0/file/upload_file',
|
|
|
|
|
|
|
+ 'uploadFile' => '/files',
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
@@ -78,24 +78,10 @@ class DeepSeekService extends BaseService
|
|
|
$answer = isset($params['answer'])? $params['answer'] : '';
|
|
$answer = isset($params['answer'])? $params['answer'] : '';
|
|
|
$topic = isset($params['topic'])? $params['topic'] : '';
|
|
$topic = isset($params['topic'])? $params['topic'] : '';
|
|
|
$score = isset($params['score'])? $params['score'] : 0;
|
|
$score = isset($params['score'])? $params['score'] : 0;
|
|
|
- $type = isset($params['type'])? $params['type'] : 1; // 1-文本,2-图片
|
|
|
|
|
|
|
+ //$type = isset($params['type'])? $params['type'] : 1; // 1-文本,2-图片
|
|
|
$content = '你是一个答题高手,请给描述中问题的答案评分,并尝试按 JSON:{"score": 8,"topic":"this is topic content","analysis":"this is topic analysis"}返回';
|
|
$content = '你是一个答题高手,请给描述中问题的答案评分,并尝试按 JSON:{"score": 8,"topic":"this is topic content","analysis":"this is topic analysis"}返回';
|
|
|
$message = "请给题目【{$topic}】总分{$score}的答案【{$answer}】评分?";
|
|
$message = "请给题目【{$topic}】总分{$score}的答案【{$answer}】评分?";
|
|
|
$format = 'json_object';
|
|
$format = 'json_object';
|
|
|
- if($type == 2){
|
|
|
|
|
- $content = '你是一个识图专家,请读取图中中英文数字或符号内容,并尝试按 JSON:{"content":"this is photo content"}';
|
|
|
|
|
-// $format = 'text';
|
|
|
|
|
-// $content = '你是一个答题高手,请给描述中问题的答案评分,并尝试按 JSON:{"score": 8,"topic":"this is topic content","answer":"this is answer content","analysis":"this is topic analysis"}返回';
|
|
|
|
|
- preg_match("/\.(jpg|jpeg|png)/", $answer, $extData);
|
|
|
|
|
- $ext = isset($extData[0])? $extData[0] : 'jpg';
|
|
|
|
|
- $ext = $ext=='png'? 'png' : 'jpeg';
|
|
|
|
|
- $filePath = ATTACHMENT_PATH.get_image_path($answer);
|
|
|
|
|
- $image = file_get_contents($filePath);
|
|
|
|
|
- $base64 = base64_encode($image);
|
|
|
|
|
- $base64 = "data:image/{$ext};base64,{$base64}";
|
|
|
|
|
- $message = "请读取图片【{$base64}】内容?";
|
|
|
|
|
-// $message = "请给总分为{$score}的题目【{$topic}】针对图片内【{$base64}】答案评分?";
|
|
|
|
|
- }
|
|
|
|
|
$data = [
|
|
$data = [
|
|
|
'model'=> $model,
|
|
'model'=> $model,
|
|
|
'messages'=> [
|
|
'messages'=> [
|
|
@@ -119,7 +105,6 @@ class DeepSeekService extends BaseService
|
|
|
$url = $this->apiUrl.$this->apiUrls[$model];
|
|
$url = $this->apiUrl.$this->apiUrls[$model];
|
|
|
$this->saveLog("caches:dkApi:{$model}:request_1_".date('YmdHis'), ['url'=>$url,'data'=>$data]);
|
|
$this->saveLog("caches:dkApi:{$model}:request_1_".date('YmdHis'), ['url'=>$url,'data'=>$data]);
|
|
|
$result = aiRequest($url, json_encode($data), 60, $headers);
|
|
$result = aiRequest($url, json_encode($data), 60, $headers);
|
|
|
- dump($result);
|
|
|
|
|
$result = $result? json_decode(trim($result),true) : [];
|
|
$result = $result? json_decode(trim($result),true) : [];
|
|
|
if(empty($result)){
|
|
if(empty($result)){
|
|
|
$this->error = '答案验证失败';
|
|
$this->error = '答案验证失败';
|
|
@@ -137,13 +122,80 @@ class DeepSeekService extends BaseService
|
|
|
return $content;
|
|
return $content;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 文件上传和处理(支持图像和文档)
|
|
|
|
|
+ */
|
|
|
|
|
+ public function uploadAndProcessFile($filePath, $prompt = '请处理这个文件') {
|
|
|
|
|
+ // 首先上传文件获取文件ID
|
|
|
|
|
+ $fileId = $this->uploadFile($filePath);
|
|
|
|
|
+
|
|
|
|
|
+ if (!$fileId) {
|
|
|
|
|
+ return ['error' => '文件上传失败'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 使用文件ID进行对话
|
|
|
|
|
+ $messages = [
|
|
|
|
|
+ [
|
|
|
|
|
+ 'role' => 'user',
|
|
|
|
|
+ 'content' => $prompt,
|
|
|
|
|
+ 'file_ids' => [$fileId]
|
|
|
|
|
+ ]
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ return $this->chat($messages);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传文件到DeepSeek
|
|
|
|
|
+ */
|
|
|
|
|
+ public function uploadFile($imageUrl) {
|
|
|
|
|
+ $uploadUrl = 'http://127.0.5.12/api/upload/image';
|
|
|
|
|
+// $uploadUrl = $this->apiUrl.$this->apiUrls['uploadFile'];
|
|
|
|
|
+ $filePath = ATTACHMENT_PATH.get_image_path($imageUrl);
|
|
|
|
|
+ if (!file_exists($filePath)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $mimeType = mime_content_type($filePath);
|
|
|
|
|
+ $fileName = basename($filePath);
|
|
|
|
|
+
|
|
|
|
|
+ // 准备文件数据
|
|
|
|
|
+ $fileData = [
|
|
|
|
|
+ 'file' => new \CURLFile($filePath, $mimeType, $fileName),
|
|
|
|
|
+ 'purpose' => 'vision' // 或者 'assistants',根据用途
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ $ch = curl_init();
|
|
|
|
|
+ curl_setopt_array($ch, [
|
|
|
|
|
+ CURLOPT_URL => $uploadUrl,
|
|
|
|
|
+ CURLOPT_RETURNTRANSFER => true,
|
|
|
|
|
+ CURLOPT_POST => true,
|
|
|
|
|
+ CURLOPT_POSTFIELDS => $fileData,
|
|
|
|
|
+ CURLOPT_HTTPHEADER => [
|
|
|
|
|
+ 'Authorization: Bearer ' . $this->apiKey
|
|
|
|
|
+ ],
|
|
|
|
|
+ CURLOPT_TIMEOUT => 30
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ $response = curl_exec($ch);
|
|
|
|
|
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
|
+ dump($response);
|
|
|
|
|
+ curl_close($ch);
|
|
|
|
|
+
|
|
|
|
|
+ if ($httpCode === 200) {
|
|
|
|
|
+ $result = json_decode($response, true);
|
|
|
|
|
+ return $result['id'] ?? false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 上传图片
|
|
* 上传图片
|
|
|
* @param $image
|
|
* @param $image
|
|
|
* @return false
|
|
* @return false
|
|
|
*/
|
|
*/
|
|
|
- public function upload($image)
|
|
|
|
|
|
|
+ public function analyzeImageByFile($fileIds, $prompt='请分析读取这张图片内容')
|
|
|
{
|
|
{
|
|
|
if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
|
|
if(empty($this->apiUrl) || empty($this->apiKey) || empty($this->apiName)){
|
|
|
$this->error = 'AI接口参数未配置';
|
|
$this->error = 'AI接口参数未配置';
|
|
@@ -152,36 +204,135 @@ class DeepSeekService extends BaseService
|
|
|
|
|
|
|
|
$headers = [
|
|
$headers = [
|
|
|
'Content-Type: application/json',
|
|
'Content-Type: application/json',
|
|
|
-// 'Accept: application/json',
|
|
|
|
|
|
|
+ 'Accept: application/json',
|
|
|
'Authorization: Bearer ' . $this->apiKey
|
|
'Authorization: Bearer ' . $this->apiKey
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+// $filePath = ATTACHMENT_PATH.get_image_path($imageUrl);
|
|
|
|
|
+// $imageData = base64_encode(file_get_contents($filePath));
|
|
|
|
|
+// $mineType = mime_content_type($filePath);
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- $filePath = ATTACHMENT_PATH.get_image_path($image);
|
|
|
|
|
- dump($filePath);
|
|
|
|
|
-
|
|
|
|
|
-// $url = $this->apiUrl.$this->apiUrls['generate'];
|
|
|
|
|
- $url = $this->chaturl.$this->apiUrls['upload_file'];
|
|
|
|
|
-
|
|
|
|
|
- var_dump(basename($image));
|
|
|
|
|
-// $file = fopen($url, 'rb');
|
|
|
|
|
- $filePath = ATTACHMENT_PATH.get_image_path($filePath);
|
|
|
|
|
- $image = file_get_contents($filePath);
|
|
|
|
|
- $base64 = base64_encode($image);
|
|
|
|
|
- $base64 = "data:image/jpeg;base64,{$base64}";
|
|
|
|
|
-// $file = new \CURLFile(realpath($filePath), 'application/octet-stream', basename($image));
|
|
|
|
|
- $data = ['image' => $base64];
|
|
|
|
|
-// $data = [
|
|
|
|
|
-// "prompt"=> "解释量子计算的基本原理",
|
|
|
|
|
-// "max_tokens"=> 200,
|
|
|
|
|
-// "temperature"=>0.7
|
|
|
|
|
-// ];
|
|
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'files'=> $data
|
|
|
|
|
|
|
+ 'model'=> 'deepseek-reasoner',
|
|
|
|
|
+ 'messages'=> [
|
|
|
|
|
+ [
|
|
|
|
|
+ 'role' => 'user',
|
|
|
|
|
+ 'content' => $prompt,
|
|
|
|
|
+ 'file_ids'=> ['file-bc202f5a-0894-478c-b22c-c79c9f8b8298']
|
|
|
|
|
+ ]
|
|
|
|
|
+ ],
|
|
|
|
|
+// 'stream' => false, //false 非流 true//流返回,需要前端追加和保持长连接
|
|
|
|
|
+// 'response_format'=>[
|
|
|
|
|
+// 'type'=> 'text' //返回格式(text,json_object)
|
|
|
|
|
+// ],
|
|
|
|
|
+ "max_tokens"=>2048, //最大返回token数
|
|
|
];
|
|
];
|
|
|
- var_dump($url);
|
|
|
|
|
- var_dump($data);
|
|
|
|
|
- $result = aiRequest($url, '', 10, $headers);
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $model = 'deepseek-reasoner';
|
|
|
|
|
+ $url = $this->apiUrl.$this->apiUrls[$model];
|
|
|
|
|
+ $this->saveLog("caches:dkApi:{$model}:request_1_".date('YmdHis'), ['url'=>$url,'data'=>$data]);
|
|
|
|
|
+ $result = aiRequest($url, json_encode($data), 60, $headers);
|
|
|
|
|
+ dump($result);
|
|
|
|
|
+ var_dump($result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传Base64图片
|
|
|
|
|
+ * @param $image
|
|
|
|
|
+ * @return false
|
|
|
|
|
+ */
|
|
|
|
|
+ public function analyzeImageByBase64($imageUrl, $prompt='请分析读取这张图片内容')
|
|
|
|
|
+ {
|
|
|
|
|
+ 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($imageUrl);
|
|
|
|
|
+ $imageData = base64_encode(file_get_contents($filePath));
|
|
|
|
|
+ $mineType = mime_content_type($filePath);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $data = [
|
|
|
|
|
+ 'purpose'=>'vision',
|
|
|
|
|
+ 'file'=> $imageData,
|
|
|
|
|
+ 'filename'=> basename($filePath),
|
|
|
|
|
+ 'mine_type'=> $mineType
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $model = 'uploadFile';
|
|
|
|
|
+ $url = $this->apiUrl.$this->apiUrls['uploadFile'];
|
|
|
|
|
+ $this->saveLog("caches:dkApi:{$model}:request_1_".date('YmdHis'), ['url'=>$url,'data'=>$data]);
|
|
|
|
|
+ $result = aiRequest($url, json_encode($data), 60, $headers);
|
|
|
|
|
+ dump($result);
|
|
|
|
|
+ var_dump($result);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 上传图片
|
|
|
|
|
+ * @param $image
|
|
|
|
|
+ * @return false
|
|
|
|
|
+ */
|
|
|
|
|
+ public function analyzeImage($imageUrl, $prompt='请分析读取这张图片内容')
|
|
|
|
|
+ {
|
|
|
|
|
+ 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($imageUrl);
|
|
|
|
|
+ $imageData = base64_encode(file_get_contents($filePath));
|
|
|
|
|
+ $mineType = mime_content_type($filePath);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $data = [
|
|
|
|
|
+ 'model'=> 'deepseek-reasoner',
|
|
|
|
|
+ 'messages'=> [
|
|
|
|
|
+ [
|
|
|
|
|
+ 'role' => 'user',
|
|
|
|
|
+ 'content' => [
|
|
|
|
|
+ [
|
|
|
|
|
+ 'type'=>'text',
|
|
|
|
|
+ 'text'=> $prompt
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ 'type'=>'image_url',
|
|
|
|
|
+ 'image_url'=> [
|
|
|
|
|
+ 'url'=>"data:{$mineType};base64,{$imageData}"
|
|
|
|
|
+ ]
|
|
|
|
|
+ ]
|
|
|
|
|
+ ]
|
|
|
|
|
+ ]
|
|
|
|
|
+ ],
|
|
|
|
|
+// 'stream' => false, //false 非流 true//流返回,需要前端追加和保持长连接
|
|
|
|
|
+// 'response_format'=>[
|
|
|
|
|
+// 'type'=> 'text' //返回格式(text,json_object)
|
|
|
|
|
+// ],
|
|
|
|
|
+ "max_tokens"=>2048, //最大返回token数
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $model = 'deepseek-reasoner';
|
|
|
|
|
+ $url = $this->apiUrl.$this->apiUrls[$model];
|
|
|
|
|
+ $this->saveLog("caches:dkApi:{$model}:request_1_".date('YmdHis'), ['url'=>$url,'data'=>$data]);
|
|
|
|
|
+ $result = aiRequest($url, json_encode($data), 60, $headers);
|
|
|
|
|
+ dump($result);
|
|
|
var_dump($result);
|
|
var_dump($result);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -225,14 +376,12 @@ var_dump($result);
|
|
|
// 图片获取数据
|
|
// 图片获取数据
|
|
|
if(preg_match("/(images|temp)/", $imageUrl)){
|
|
if(preg_match("/(images|temp)/", $imageUrl)){
|
|
|
$path = get_image_path($imageUrl);
|
|
$path = get_image_path($imageUrl);
|
|
|
-// foreach((new TesseractOCR())->availableLanguages() as $lang) echo $lang;
|
|
|
|
|
$ocr = new TesseractOCR(ATTACHMENT_PATH.$path);
|
|
$ocr = new TesseractOCR(ATTACHMENT_PATH.$path);
|
|
|
-
|
|
|
|
|
- $data = $ocr->lang('chi_sim','eng','chi_tra')
|
|
|
|
|
- ->allowlist(range('a', 'z'), range('A', 'Z'), range(0, 9), '-_@')
|
|
|
|
|
- ->run(30);
|
|
|
|
|
- var_dump($data);
|
|
|
|
|
- return $data;
|
|
|
|
|
|
|
+ $data = $ocr->psm(3)
|
|
|
|
|
+ ->horc()
|
|
|
|
|
+ ->lang('chi_sim','eng','chi_tra')
|
|
|
|
|
+ ->run(60);
|
|
|
|
|
+ return $data?str_replace("\n",'\n', $data):'';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $imageUrl;
|
|
return $imageUrl;
|