ArticleTranslateService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Services\AIAssistant;
  3. use App\Services\ArticleService;
  4. use App\Services\PaliContentService;
  5. use App\Services\SentenceService;
  6. use App\Models\CustomBook;
  7. use Illuminate\Support\Facades\Log;
  8. use App\Http\Api\ChannelApi;
  9. use Carbon\Callback;
  10. class ArticleTranslateService
  11. {
  12. protected ArticleService $articleService;
  13. protected PaliContentService $paliContentService;
  14. protected TranslateService $translateService;
  15. protected SentenceService $sentenceService;
  16. protected string $modelId;
  17. protected array $translation = [];
  18. protected string $outputChannelId;
  19. protected string $systemPrompt = <<<PROMPT
  20. 请根据提供的原文,翻译为简体中文。
  21. 原文为逐句数据,翻译时请依照句子的上下文翻译。
  22. id:句子编号
  23. content:内容
  24. # 翻译要求:
  25. 1. 缅文巴利要给出罗马巴利转写
  26. 2. 使用现代汉语
  27. 3. 逐句翻译
  28. # 输出格式要求:
  29. - jsonl 格式
  30. - 每条记录是一个句子
  31. - 每个句子只输出两个字段
  32. 1. id(句子编号)
  33. 2. content(译文)
  34. - 无需输出原文
  35. - 只输出jsonl格式的译文 无需出处额外的解释
  36. PROMPT;
  37. public function __construct(
  38. ArticleService $article,
  39. PaliContentService $paliContent,
  40. TranslateService $translateService,
  41. SentenceService $sentenceService
  42. ) {
  43. $this->articleService = $article;
  44. $this->paliContentService = $paliContent;
  45. $this->translateService = $translateService;
  46. $this->sentenceService = $sentenceService;
  47. }
  48. /**
  49. * 设置模型配置
  50. *
  51. * @param string $model
  52. * @return self
  53. */
  54. public function setModel(string $model): self
  55. {
  56. $this->modelId = $model;
  57. return $this;
  58. }
  59. /**
  60. * 设置模型配置
  61. *
  62. * @param string $model
  63. * @return self
  64. */
  65. public function setChannel(string $id): self
  66. {
  67. $this->outputChannelId = $id;
  68. return $this;
  69. }
  70. public function translateAnthology($anthologyId, ?callable $fn = null): int
  71. {
  72. $articles = $this->articleService->articlesInAnthology($anthologyId);
  73. foreach ($articles as $article) {
  74. $sentences = $this->translateArticle($article)->save();
  75. if ($fn) {
  76. $fn($article, $sentences);
  77. }
  78. }
  79. return count($articles);
  80. }
  81. public function translateArticle(string $articleId)
  82. {
  83. //获取文章中的句子id
  84. $sentenceIds = $this->articleService->sentenceIds($articleId);
  85. if (!$sentenceIds || count($sentenceIds) === 0) {
  86. $this->translation = [];
  87. return $this;
  88. }
  89. $bookId = (int)explode('-', $sentenceIds[0])[0];
  90. //提取原文
  91. $originalChannelId = CustomBook::where('book_id', $bookId)->value('channel_id');
  92. $original = $this->paliContentService->sentences($sentenceIds, [$originalChannelId], 'read');
  93. $orgData = [];
  94. foreach ($original as $key => $paragraph) {
  95. foreach ($paragraph['children'] as $key => $sent) {
  96. $org = $sent['origin'][0];
  97. $orgData[] = [
  98. 'id' => "{$org['book']}-{$org['para']}-{$org['wordStart']}-{$org['wordEnd']}",
  99. 'content' => !empty($org['content']) ? $org['content'] : $org['html'],
  100. ];
  101. }
  102. }
  103. //翻译
  104. $result = $this->translateService->setModel($this->modelId)
  105. ->setSystemPrompt($this->systemPrompt)
  106. ->setTranslatePrompt("# 原文\n\n" .
  107. "```json\n" .
  108. json_encode($orgData, JSON_UNESCAPED_UNICODE) .
  109. "\n```")
  110. ->translate();
  111. Log::debug('ai translation', ['data' => $result->toArray()['data']]);
  112. $this->translation = $result->toArray()['data'];
  113. return $this;
  114. }
  115. //写入结果channel
  116. public function save()
  117. {
  118. if (
  119. !is_array($this->translation) ||
  120. count($this->translation) === 0
  121. ) {
  122. return 0;
  123. }
  124. $channelInfo = ChannelApi::getById($this->outputChannelId);
  125. $sentData = [];
  126. $sentData = array_map(function ($n) use ($channelInfo) {
  127. $sId = explode('-', $n['id']);
  128. return [
  129. 'book_id' => $sId[0],
  130. 'paragraph' => $sId[1],
  131. 'word_start' => $sId[2],
  132. 'word_end' => $sId[3],
  133. 'channel_uid' => $channelInfo['id'],
  134. 'content' => $n['content'],
  135. 'content_type' => $n['content_type'] ?? 'markdown',
  136. 'lang' => $channelInfo['lang'],
  137. 'status' => $channelInfo['status'],
  138. 'editor_uid' => $this->modelId,
  139. ];
  140. }, $this->translation);
  141. foreach ($sentData as $value) {
  142. $this->sentenceService->save($value);
  143. }
  144. return count($sentData);
  145. }
  146. public function get()
  147. {
  148. return $this->translation;
  149. }
  150. }