articleService = $article; $this->paliContentService = $paliContent; $this->translateService = $translateService; } /** * 设置模型配置 * * @param string $model * @return self */ public function setModel(string $model): self { $this->modelId = $model; return $this; } public function translate(string $articleId) { //获取文章中的句子id $sentenceIds = $this->articleService->sentenceIds($articleId); if (!$sentenceIds || count($sentenceIds) === 0) { return null; } $bookId = (int)explode('-', $sentenceIds[0])[0]; //提取原文 $originalChannelId = CustomBook::where('book_id', $bookId)->value('channel_id'); $original = $this->paliContentService->sentences($sentenceIds, [$originalChannelId], 'read'); $orgData = []; foreach ($original as $key => $paragraph) { foreach ($paragraph['children'] as $key => $sent) { $org = $sent['origin'][0]; $orgData[] = [ 'id' => "{$org['book']}-{$org['para']}-{$org['wordStart']}-{$org['wordEnd']}", 'content' => !empty($org['content']) ? $org['content'] : $org['html'], ]; } } //翻译 $result = $this->translateService->setModel($this->modelId) ->setSystemPrompt($this->systemPrompt) ->setTranslatePrompt("# 原文\n\n" . "```json\n" . json_encode($orgData, JSON_UNESCAPED_UNICODE) . "\n```") ->translate(); Log::debug('ai translation', ['data' => $result->toArray()['data']]); $this->translation = $result->toArray()['data']; return $this; } //写入结果channel public function save(string $channelId) {} public function get() { return $this->translation; } }