AiSentenceTranslate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sentence;
  5. use App\Models\PaliSentence;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Http;
  8. class AiSentenceTranslate extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = <<<command
  16. ai:sentence.translate
  17. {--api= : ai engin url}
  18. {--model= : ai model }
  19. {--sid= : 句子编号 }
  20. {--nissaya= : nissaya channel }
  21. {--result= : result channel }
  22. command;
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '使用LLM 和nissaya数据翻译句子';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return int
  42. */
  43. public function handle()
  44. {
  45. //获取巴利句子
  46. $sid = explode('-', $this->option('sid'));
  47. $pali = PaliSentence::where('book', $sid[0])
  48. ->where('paragraph', $sid[1])
  49. ->where('word_begin', $sid[2])
  50. ->where('word_end', $sid[3])
  51. ->value('text');
  52. //获取nissaya
  53. $nissaya = Sentence::where('channel_uid', $this->option('nissaya'))
  54. ->where('book_id', $sid[0])
  55. ->where('paragraph', $sid[1])
  56. ->where('word_start', $sid[2])
  57. ->where('word_end', $sid[3])
  58. ->value('content');
  59. //获取ai结果
  60. $api = $this->getEngin($this->option('api'));
  61. if (!$api) {
  62. $this->error('ai translate no api');
  63. return 1;
  64. }
  65. $json = $this->fetch($api, $pali, $nissaya);
  66. $this->info('ai translate', ['json' => $json]);
  67. //写入
  68. return 0;
  69. }
  70. private function getEngin($engin)
  71. {
  72. $api = config('mint.ai.accounts');
  73. $selected = array_filter($api, function ($value) use ($engin) {
  74. return $value['name'] === $engin;
  75. });
  76. if (!is_array($selected) || count($selected) === 0) {
  77. return null;
  78. }
  79. return $selected[0];
  80. }
  81. private function fetch($api, $origin, $nissaya = null)
  82. {
  83. $prompt = '翻译上面的巴利文为中文';
  84. if ($nissaya) {
  85. $prompt = '根据下面的解释,' . $prompt;
  86. }
  87. $message = "{$origin}\n\n{$prompt}\n\n{$nissaya}";
  88. $url = $api['api_url'];
  89. $param = [
  90. "model" => $api['model'],
  91. "messages" => [
  92. ["role" => "system", "content" => "你是翻译人工智能助手,bhikkhu 为专有名词,不可翻译成其他语言。"],
  93. ["role" => "user", "content" => $message],
  94. ],
  95. "temperature" => 0.3,
  96. ];
  97. $response = Http::withToken($api['token'])
  98. ->post($url, $param);
  99. if ($response->failed()) {
  100. $this->error('http request error' . $response->json('message'));
  101. Log::error('http request error', ['data' => $response->json()]);
  102. return null;
  103. } else {
  104. return $response->json();
  105. }
  106. }
  107. }