AiTranslate.php 5.5 KB

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