ArticleTranslateService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Services\AIAssistant;
  3. use App\Services\ArticleService;
  4. use App\Services\PaliContentService;
  5. use App\Models\CustomBook;
  6. use Illuminate\Support\Facades\Log;
  7. class ArticleTranslateService
  8. {
  9. protected ArticleService $articleService;
  10. protected PaliContentService $paliContentService;
  11. protected TranslateService $translateService;
  12. protected string $modelId;
  13. protected array $translation;
  14. protected string $systemPrompt = <<<PROMPT
  15. 请根据提供的原文,翻译为简体中文。
  16. 原文为逐句数据,翻译时请依照句子的上下文翻译。
  17. id:句子编号
  18. content:内容
  19. # 翻译要求:
  20. 1. 缅文巴利要给出罗马巴利转写
  21. 2. 使用现代汉语
  22. 3. 逐句翻译
  23. # 输出格式要求:
  24. - jsonl 格式
  25. - 每条记录是一个句子
  26. - 每个句子只输出两个字段
  27. 1. id(句子编号)
  28. 2. content(译文)
  29. - 无需输出原文
  30. - 只输出jsonl格式的译文 无需出处额外的解释
  31. PROMPT;
  32. public function __construct(
  33. ArticleService $article,
  34. PaliContentService $paliContent,
  35. TranslateService $translateService,
  36. ) {
  37. $this->articleService = $article;
  38. $this->paliContentService = $paliContent;
  39. $this->translateService = $translateService;
  40. }
  41. /**
  42. * 设置模型配置
  43. *
  44. * @param string $model
  45. * @return self
  46. */
  47. public function setModel(string $model): self
  48. {
  49. $this->modelId = $model;
  50. return $this;
  51. }
  52. public function translate(string $articleId)
  53. {
  54. //获取文章中的句子id
  55. $sentenceIds = $this->articleService->sentenceIds($articleId);
  56. if (!$sentenceIds || count($sentenceIds) === 0) {
  57. return null;
  58. }
  59. $bookId = (int)explode('-', $sentenceIds[0])[0];
  60. //提取原文
  61. $originalChannelId = CustomBook::where('book_id', $bookId)->value('channel_id');
  62. $original = $this->paliContentService->sentences($sentenceIds, [$originalChannelId], 'read');
  63. $orgData = [];
  64. foreach ($original as $key => $paragraph) {
  65. foreach ($paragraph['children'] as $key => $sent) {
  66. $org = $sent['origin'][0];
  67. $orgData[] = [
  68. 'id' => "{$org['book']}-{$org['para']}-{$org['wordStart']}-{$org['wordEnd']}",
  69. 'content' => !empty($org['content']) ? $org['content'] : $org['html'],
  70. ];
  71. }
  72. }
  73. //翻译
  74. $result = $this->translateService->setModel($this->modelId)
  75. ->setSystemPrompt($this->systemPrompt)
  76. ->setTranslatePrompt("# 原文\n\n" .
  77. "```json\n" .
  78. json_encode($orgData, JSON_UNESCAPED_UNICODE) .
  79. "\n```")
  80. ->translate();
  81. Log::debug('ai translation', ['data' => $result->toArray()['data']]);
  82. $this->translation = $result->toArray()['data'];
  83. return $this;
  84. }
  85. //写入结果channel
  86. public function save(string $channelId) {}
  87. public function get()
  88. {
  89. return $this->translation;
  90. }
  91. }