Explorar o código

Merge pull request #2397 from visuddhinanda/development

Development
visuddhinanda hai 2 días
pai
achega
a6667e8010

+ 26 - 9
api-v13/app/Console/Commands/AiArticleTranslate.php

@@ -9,7 +9,14 @@ use App\Services\AIAssistant\ArticleTranslateService;
 use App\Services\ArticleService;
 
 
-#[Signature('app:ai-article-translate  {--article=} {--anthology=} {--model=}  {--channel=}  {--token=} {--endpoint=}')]
+#[Signature('app:ai-article-translate
+{--article=}
+{--anthology=}
+{--model=}
+{--channel=}
+{--thinking=} : deepseek deep thinking true or false
+{--token=}
+{--endpoint=}')]
 #[Description('translate article by ai ')]
 class AiArticleTranslate extends Command
 {
@@ -32,10 +39,15 @@ class AiArticleTranslate extends Command
         // ===== 执行 =====
         if ($this->option('article')) {
             $this->info('article translate start');
-            $total = $service->setModel($this->option('model'))
-                ->setChannel($this->option('channel'))
-                ->translateArticle($this->option('article'))
-                ->saveRpc($this->option('endpoint'), $this->option('token'));
+            $llm = $service->setModel($this->option('model'))
+                ->setChannel($this->option('channel'));
+
+            if ($this->option('thinking')) {
+                $llm = $llm->setThinking($this->option('thinking') === 'true');
+            }
+
+            $total =    $llm->translateArticle($this->option('article'))
+                ->save();
             $this->info("{$total} sentences saved");
         }
         if ($this->option('anthology')) {
@@ -44,10 +56,15 @@ class AiArticleTranslate extends Command
 
             foreach ($articleIds as $article) {
                 $this->info('article translate start');
-                $total = $service->setModel($this->option('model'))
-                    ->setChannel($this->option('channel'))
-                    ->translateArticle($article)
-                    ->saveRpc($this->option('endpoint'), $this->option('token'));
+                $llm = $service->setModel($this->option('model'))
+                    ->setChannel($this->option('channel'));
+
+                if ($this->option('thinking')) {
+                    $llm = $llm->setThinking($this->option('thinking') === 'true');
+                }
+
+                $total = $llm->translateArticle($article)
+                    ->save();
                 $this->info("{$total} sentences saved");
             }
 

+ 1 - 1
api-v13/app/Console/Commands/UpdateCorpus.php

@@ -16,7 +16,7 @@ use App\Models\Channel;
 use App\Http\Api\UserApi;
 
 
-#[Signature('app:update-corpus --dir= --es')]
+#[Signature('app:update-corpus {--dir=} {--es}')]
 #[Description('Update corpus from JSONL files in corpus directory')]
 class UpdateCorpus extends Command
 {

+ 13 - 0
api-v13/app/Services/AIAssistant/ArticleTranslateService.php

@@ -28,6 +28,8 @@ class ArticleTranslateService
     protected string $outputChannelId;
     protected string $currArticleId;
 
+    protected bool $thinking;
+
     protected string $systemPrompt = <<<PROMPT
     请根据提供的原文,翻译为简体中文。
 
@@ -82,6 +84,17 @@ class ArticleTranslateService
         $this->modelToken = app(AuthService::class)->getUserToken($model);
         return $this;
     }
+    /**
+     * 设置模型配置
+     *
+     * @param bool $thinking
+     * @return self
+     */
+    public function setThinking(bool $thinking): self
+    {
+        $this->thinking = $thinking;
+        return $this;
+    }
     /**
      * 设置模型配置
      *

+ 21 - 7
api-v13/app/Services/AIAssistant/TranslateService.php

@@ -21,6 +21,7 @@ class TranslateService
     protected AIModelService $aiModelService;
     protected AiModelResource $model;
     protected string $modelToken;
+    protected bool $thinking;
 
     protected bool $stream = false;
     protected array $original; //需要被翻译的原文
@@ -51,6 +52,17 @@ class TranslateService
         $this->modelToken = AuthService::getUserToken($model);
         return $this;
     }
+    /**
+     * 设置模型配置
+     *
+     * @param bool $thinking
+     * @return self
+     */
+    public function setThinking(bool $thinking): self
+    {
+        $this->thinking = $thinking;
+        return $this;
+    }
     /**
      * 设置翻译提示词
      *
@@ -90,28 +102,30 @@ class TranslateService
         try {
 
 
-            Log::info('准备翻译', [
+            Log::debug('准备翻译', [
                 'systemPrompt' => $this->systemPrompt,
                 'translatePrompt' => $this->translatePrompt,
             ]);
 
             // 3. 调用LLM进行翻译
-            $response = $this->openAIService
+            $llm = $this->openAIService
                 ->setApiUrl($this->model['url'])
                 ->setModel($this->model['model'])
                 ->setApiKey($this->model['key'])
                 ->setSystemPrompt($this->systemPrompt)
                 ->setTemperature(0.3)
-                ->setStream($this->stream)
-                ->send($this->translatePrompt);
-
+                ->setStream($this->stream);
+            if (isset($this->thinking)) {
+                $llm = $llm->setThinking($this->thinking);
+            }
+            $response = $llm->send($this->translatePrompt);
             $complete = time() - $startAt;
             $content = $response['choices'][0]['message']['content'] ?? '';
 
             if (empty($content)) {
                 throw new \Exception('LLM返回内容为空');
             }
-            Log::info('翻译完成', [
+            Log::debug('翻译完成', [
                 'content' => $content,
                 'duration' => $complete,
                 'input_tokens' => $response['usage']['prompt_tokens'] ?? 0,
@@ -120,7 +134,7 @@ class TranslateService
             // 4. 解析JSONL格式的翻译结果
             $translatedData = $this->jsonlToArray($content);
 
-            Log::info('解析完成', [
+            Log::debug('解析完成', [
                 'output_items' => count($translatedData),
             ]);
 

+ 17 - 0
api-v13/app/Services/OpenAIService.php

@@ -19,6 +19,7 @@ class OpenAIService
     protected bool $stream = false;
     protected int $timeout = 600;
     protected int $maxTokens = 0;
+    protected bool $thinking;
 
     public static function withRetry(int $retries = 3, int $delayMs = 2000): static
     {
@@ -38,6 +39,18 @@ class OpenAIService
         return $this;
     }
 
+    /**
+     * 设置模型配置
+     *
+     * @param bool $thinking
+     * @return self
+     */
+    public function setThinking(bool $thinking): self
+    {
+        $this->thinking = $thinking;
+        return $this;
+    }
+
     public function setApiUrl(string $url): static
     {
         $this->apiUrl = $url;
@@ -160,6 +173,10 @@ class OpenAIService
             'stream' => false,
         ];
 
+        if (isset($this->thinking)) {
+            $data['enable_thinking'] = $this->thinking;
+        }
+
         if ($this->maxTokens > 0) {
             $data['max_tokens'] = $this->maxTokens;
         }