AiTranslate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\PaliSentence;
  4. use App\Models\PaliText;
  5. use App\Models\Sentence;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Http;
  8. use Illuminate\Support\Facades\Log;
  9. class AiTranslate extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan ai:sentence.translate --type=chapter --api=deepseek --model=deepseek-chat --sid=107-2357
  14. * php artisan ai:sentence.translate --type=sentence --api=kimi --model=moonshot-v1-8k --sid=107-2357-9-47
  15. *
  16. * @var string
  17. */
  18. protected $signature = <<<'command'
  19. ai:sentence.translate
  20. {--type=sentence : sentence|paragraph|chapter}
  21. {--api= : ai engin url}
  22. {--model= : ai model }
  23. {--sid= : 句子编号 }
  24. {--nissaya= : nissaya channel }
  25. {--result= : result channel }
  26. command;
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '使用LLM 和nissaya数据翻译句子';
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. }
  42. /**
  43. * Execute the console command.
  44. *
  45. * @return int
  46. */
  47. public function handle()
  48. {
  49. // 句子号列表
  50. $sentences = [];
  51. $totalLen = 0;
  52. switch ($this->option('type')) {
  53. case 'sentence':
  54. $sentences[] = explode('-', $this->option('sid'));
  55. break;
  56. case 'paragraph':
  57. $para = explode('-', $this->option('sid'));
  58. $sent = PaliSentence::where('book', $para[0])
  59. ->where('paragraph', $para[1])->orderBy('word_begin')->get();
  60. foreach ($sent as $key => $value) {
  61. $sentences[] = [$para[0], $para[1], $value->word_begin, $value->word_end];
  62. }
  63. break;
  64. case 'chapter':
  65. $para = explode('-', $this->option('sid'));
  66. $chapterLen = PaliText::where('book', $para[0])
  67. ->where('paragraph', $para[1])->value('chapter_len');
  68. $sent = PaliSentence::where('book', $para[0])
  69. ->whereBetween('paragraph', [$para[1], $para[1] + $chapterLen - 1])
  70. ->orderBy('paragraph')
  71. ->orderBy('word_begin')->get();
  72. foreach ($sent as $key => $value) {
  73. $sentences[] = [$para[0], $para[1], $value->word_begin, $value->word_end];
  74. }
  75. break;
  76. default:
  77. return 1;
  78. break;
  79. }
  80. // 获取句子总长度
  81. foreach ($sentences as $key => $sentence) {
  82. $totalLen += $this->sentLen($sentence);
  83. }
  84. //
  85. foreach ($sentences as $key => $sentence) {
  86. // 获取巴利句子
  87. $pali = PaliSentence::where('book', $sentence[0])
  88. ->where('paragraph', $sentence[1])
  89. ->where('word_begin', $sentence[2])
  90. ->where('word_end', $sentence[3])
  91. ->value('text');
  92. // 获取nissaya
  93. $nissaya = Sentence::where('channel_uid', $this->option('nissaya'))
  94. ->where('book_id', $sentence[0])
  95. ->where('paragraph', $sentence[1])
  96. ->where('word_start', $sentence[2])
  97. ->where('word_end', $sentence[3])
  98. ->value('content');
  99. // 获取ai结果
  100. $api = $this->getEngin($this->option('api'));
  101. if (! $api) {
  102. $this->error('ai translate no api');
  103. return 1;
  104. }
  105. $json = $this->fetch($api, $this->option('model'), $pali, $nissaya);
  106. Log::info('ai translate', ['json' => $json]);
  107. $this->info($json['choices'][0]['message']['content']);
  108. // 写入
  109. }
  110. return 0;
  111. }
  112. private function sentLen($id)
  113. {
  114. return PaliSentence::where('book', $id[0])
  115. ->where('paragraph', $id[1])
  116. ->where('word_begin', $id[2])
  117. ->where('word_end', $id[3])
  118. ->value('length');
  119. }
  120. private function getEngin($engin)
  121. {
  122. $api = config('mint.ai.accounts');
  123. $selected = array_filter($api, function ($value) use ($engin) {
  124. return $value['name'] === $engin;
  125. });
  126. if (! is_array($selected) || count($selected) === 0) {
  127. return null;
  128. }
  129. return $selected[0];
  130. }
  131. private function fetch($api, $model, $origin, $nissaya = null)
  132. {
  133. $prompt = '翻译上面的巴利文为中文';
  134. if ($nissaya) {
  135. $prompt = '根据下面的解释,'.$prompt;
  136. }
  137. $message = "{$origin}\n\n{$prompt}\n\n{$nissaya}";
  138. $url = $api['api_url'];
  139. $param = [
  140. 'model' => $model,
  141. 'messages' => [
  142. ['role' => 'system', 'content' => '你是翻译人工智能助手.bhikkhu 为专有名词,不可翻译成其他语言。'],
  143. ['role' => 'user', 'content' => $message],
  144. ],
  145. 'temperature' => 0.3,
  146. 'stream' => false,
  147. ];
  148. $response = Http::withToken($api['token'])
  149. ->post($url, $param);
  150. if ($response->failed()) {
  151. $this->error('http request error'.$response->json('message'));
  152. Log::error('http request error', ['data' => $response->json()]);
  153. return null;
  154. } else {
  155. return $response->json();
  156. }
  157. }
  158. }