Răsfoiți Sursa

支持rpc save

visuddhinanda 1 săptămână în urmă
părinte
comite
c5bcc55404

+ 47 - 6
api-v13/app/Services/AIAssistant/ArticleTranslateService.php

@@ -5,6 +5,7 @@ namespace App\Services\AIAssistant;
 use App\Services\ArticleService;
 use App\Services\PaliContentService;
 use App\Services\SentenceService;
+use App\Services\AuthService;
 
 use App\Models\CustomBook;
 
@@ -20,9 +21,12 @@ class ArticleTranslateService
     protected TranslateService $translateService;
     protected SentenceService $sentenceService;
 
+
     protected string $modelId;
+    protected string $modelToken;
     protected array $translation = [];
     protected string $outputChannelId;
+    protected string $currArticleId;
 
     protected string $systemPrompt = <<<PROMPT
     请根据提供的原文,翻译为简体中文。
@@ -75,6 +79,7 @@ class ArticleTranslateService
     public function setModel(string $model): self
     {
         $this->modelId = $model;
+        $this->modelToken = app(AuthService::class)->getUserToken($model);
         return $this;
     }
     /**
@@ -88,21 +93,27 @@ class ArticleTranslateService
         $this->outputChannelId = $id;
         return $this;
     }
-    public function translateAnthology($anthologyId, ?callable $onEach = null): int
+
+    public function getCurrArticleId()
+    {
+        return $this->currArticleId;
+    }
+    public function translateAnthology(string $anthologyId, ?callable $onEach = null): int
     {
-        $articles = $this->articleService->articlesInAnthology($anthologyId);
+        $articleIds = $this->articleService->articlesInAnthology($anthologyId);
 
-        foreach ($articles as $article) {
-            $sentences = $this->translateArticle($article)->save();
+        foreach ($articleIds as $article) {
+            $this->translateArticle($article);
             if ($onEach) {
-                $onEach($article, $sentences);
+                $onEach($this);
             }
         }
 
-        return count($articles);
+        return count($articleIds);
     }
     public function translateArticle(string $articleId)
     {
+        $this->currArticleId = $articleId;
         //获取文章中的句子id
         $sentenceIds = $this->articleService->sentenceIds($articleId);
         if (!$sentenceIds || count($sentenceIds) === 0) {
@@ -167,6 +178,36 @@ class ArticleTranslateService
         }
         return count($sentData);
     }
+
+    public function saveRpc(string $endpoint, string $accessToken)
+    {
+        if (
+            !is_array($this->translation) ||
+            count($this->translation) === 0
+        ) {
+            return 0;
+        }
+        $channelInfo = ChannelApi::getById($this->outputChannelId);
+        $sentData = [];
+        $sentData = array_map(function ($n) use ($channelInfo, $accessToken) {
+            $sId = explode('-', $n['id']);
+            return [
+                'book_id' => $sId[0],
+                'paragraph' => $sId[1],
+                'word_start' => $sId[2],
+                'word_end' => $sId[3],
+                'channel_uid' => $channelInfo['id'],
+                'content' => $n['content'],
+                'content_type' => $n['content_type'] ?? 'markdown',
+                'access_token' => $accessToken,
+            ];
+        }, $this->translation);
+        foreach ($sentData as  $value) {
+            $this->sentenceService->saveRpc($endpoint, $value, $this->modelToken);
+        }
+        return count($sentData);
+    }
+
     public function get()
     {
         return $this->translation;

+ 2 - 2
api-v13/app/Services/SentenceService.php

@@ -65,9 +65,9 @@ class SentenceService
                     'access_token' => $sentChannelInfo[1] ?? $params['token'],
                 ],
      */
-    public function saveRpc(array $sentence, string $editorToken)
+    public function saveRpc(string $endpoint, array $sentence, string $editorToken)
     {
-        $url = config('app.url') . '/api/v2/sentence';
+        $url = $endpoint;
 
         Log::info(" sentence update {$url}");
         $response = Http::timeout($this->timeOut)