ArticleTranslateService.php 4.9 KB

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