UpgradeAITranslation.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Http;
  6. use App\Services\OpenAIService;
  7. use App\Services\AIModelService;
  8. use App\Services\SentenceService;
  9. use App\Services\SearchPaliDataService;
  10. use App\Http\Controllers\AuthController;
  11. use App\Models\PaliText;
  12. use App\Models\PaliSentence;
  13. use App\Models\Sentence;
  14. use App\Helpers\LlmResponseParser;
  15. use App\Http\Api\ChannelApi;
  16. use App\Tools\Tools;
  17. class UpgradeAITranslation extends Command
  18. {
  19. /**
  20. * The name and signature of the console command.
  21. * php artisan upgrade:ai.translation translation --book=141 --para=535
  22. * @var string
  23. */
  24. protected $signature = 'upgrade:ai.translation {type} {--book=} {--para=} {--resume} {--model=} ';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Command description';
  31. protected $sentenceService;
  32. protected $modelService;
  33. protected $openAIService;
  34. protected $model;
  35. protected $modelToken;
  36. protected $workChannel;
  37. protected $accessToken;
  38. /**
  39. * Create a new command instance.
  40. *
  41. * @return void
  42. */
  43. public function __construct(
  44. AIModelService $model,
  45. SentenceService $sent,
  46. OpenAIService $openAI
  47. ) {
  48. $this->modelService = $model;
  49. $this->sentenceService = $sent;
  50. $this->openAIService = $openAI;
  51. parent::__construct();
  52. }
  53. /**
  54. * Execute the console command.
  55. *
  56. * @return int
  57. */
  58. public function handle()
  59. {
  60. if ($this->option('model')) {
  61. $this->model = $this->modelService->getModelById($this->option('model'));
  62. $this->info("model:{$this->model['model']}");
  63. $this->modelToken = AuthController::getUserToken($this->model['uid']);
  64. }
  65. $this->workChannel = ChannelApi::getById($this->ask('请输入结果channel'));
  66. $books = [];
  67. if ($this->option('book')) {
  68. $books = [$this->option('book')];
  69. } else {
  70. $books = range(1, 217);
  71. }
  72. foreach ($books as $key => $book) {
  73. $maxParagraph = PaliText::where('book', $book)->max('paragraph');
  74. $paragraphs = range(1, $maxParagraph);
  75. if ($this->option('para')) {
  76. $paragraphs = [$this->option('para')];
  77. }
  78. foreach ($paragraphs as $key => $paragraph) {
  79. $this->info($this->argument('type') . " {$book}-{$paragraph}");
  80. $data = [];
  81. switch ($this->argument('type')) {
  82. case 'translation':
  83. $data = $this->aiPaliTranslate($book, $paragraph);
  84. break;
  85. case 'nissaya':
  86. $data = $this->aiNissayaTranslate($book, $paragraph);
  87. default:
  88. # code...
  89. break;
  90. }
  91. $this->save($data);
  92. }
  93. }
  94. return 0;
  95. }
  96. private function getPaliContent($book, $para)
  97. {
  98. $sentenceService = app(SearchPaliDataService::class);
  99. $sentences = PaliSentence::where('book', $book)
  100. ->where('paragraph', $para)
  101. ->orderBy('word_begin')
  102. ->get();
  103. if (!$sentences) {
  104. return null;
  105. }
  106. $json = [];
  107. foreach ($sentences as $key => $sentence) {
  108. $content = $sentenceService->getSentenceText($book, $para, $sentence->word_begin, $sentence->word_end);
  109. $id = "{$book}-{$para}-{$sentence->word_begin}-{$sentence->word_end}";
  110. $json[] = ['id' => $id, 'content' => $content['markdown']];
  111. }
  112. return $json;
  113. }
  114. private function aiPaliTranslate($book, $para)
  115. {
  116. $prompt = <<<md
  117. 你是一个巴利语翻译助手。
  118. pali 是巴利原文的一个段落,json格式, 每条记录是一个句子。包括id 和 content 两个字段
  119. 请翻译这个段落为简体中文。
  120. 翻译要求
  121. 1. 语言风格为现代汉语书面语,不要使用古汉语或者半文半白。
  122. 2. 译文严谨,完全贴合巴利原文,不要加入自己的理解
  123. 3. 巴利原文中的黑体字在译文中也使用黑体。其他标点符号跟随巴利原文,但应该替换为相应的汉字全角符号
  124. 输出格式jsonl
  125. 输出id 和 content 两个字段,
  126. id 使用巴利原文句子的id ,
  127. content 为中文译文
  128. 直接输出jsonl数据,无需解释
  129. **输出范例**
  130. {"id":"1-2-3-4","content":"译文"}
  131. {"id":"2-3-4-5","content":"译文"}
  132. md;
  133. $pali = $this->getPaliContent($book, $para);
  134. $originalText = "```json\n" . json_encode($pali, JSON_UNESCAPED_UNICODE) . "\n```";
  135. Log::debug($originalText);
  136. if (!$this->model) {
  137. Log::error('model is invalid');
  138. return [];
  139. }
  140. $startAt = time();
  141. $response = $this->openAIService->setApiUrl($this->model['url'])
  142. ->setModel($this->model['model'])
  143. ->setApiKey($this->model['key'])
  144. ->setSystemPrompt($prompt)
  145. ->setTemperature(0.0)
  146. ->setStream(false)
  147. ->send("# pali\n\n{$originalText}\n\n");
  148. $complete = time() - $startAt;
  149. $translationText = $response['choices'][0]['message']['content'] ?? '[]';
  150. Log::debug("complete in {$complete}s", $translationText);
  151. $json = [];
  152. if (is_string($translationText)) {
  153. $json = LlmResponseParser::jsonl($translationText);
  154. }
  155. return $json;
  156. }
  157. private function aiWBW($book, $para)
  158. {
  159. $sysPrompt = <<<md
  160. 你是一个佛教翻译专家,精通巴利文和缅文,精通巴利文逐词解析
  161. ## 翻译要求:
  162. - 请将用户提供的巴利句子单词表中的每个巴利文单词翻译为中文
  163. - 这些单词是一个完整的句子,请根据单词的上下文翻译
  164. - original 里面的数据是巴利文单词
  165. - 输入格式为 json 数组
  166. - 输出jsonl格式
  167. 在原来的数据中添加下列输出字段
  168. 1. meaning:单词的中文意思,如果有两个可能的意思,两个意思之间用/符号分隔
  169. 2. factors:单词的拆分 字符串数组
  170. 3. factorMeaning:每个拆分组分的中文意思 字符串数组
  171. 4. parent:单词的词典原型(词干)
  172. 5. confidence:你认为你给出的这个单词的信息的信心指数(准确程度) 数值1-100 如果觉得非常有把握100, 如果觉得把握不大,适当降低信心指数
  173. 6. note:如果你认为信心指数很低,这个是疑难单词,请在note字段写明原因,如果不是疑难单词,请不要填写note
  174. **范例**:
  175. {"id":1,"original":"bhikkhusanghassa","meaning":"比库僧团[的]","factors":["bhikkhu","sangha","ssa"],"factorMeaning":["比库","僧团","属格"],"parent":"bhikkhusangha","confidence":100}
  176. 直接输出jsonl, 无需其他内容
  177. md;
  178. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  179. $sentences = Sentence::where('channel_uid', $channelId)
  180. ->where('book_id', $book)
  181. ->where('paragraph', $para)
  182. ->get();
  183. $result = [];
  184. foreach ($sentences as $key => $sentence) {
  185. $wbw = json_decode($sentence->content);
  186. $tpl = [];
  187. foreach ($wbw as $key => $word) {
  188. if (!empty($word['real'])) {
  189. $tpl[] = [
  190. 'id' => $word['sn'],
  191. 'original' => $word['real'],
  192. ];
  193. }
  194. }
  195. $tplText = json_encode($tpl, JSON_UNESCAPED_UNICODE);
  196. Log::debug($tplText);
  197. $startAt = time();
  198. $response = $this->openAIService->setApiUrl($this->model['url'])
  199. ->setModel($this->model['model'])
  200. ->setApiKey($this->model['key'])
  201. ->setSystemPrompt($sysPrompt)
  202. ->setTemperature(0.7)
  203. ->setStream(false)
  204. ->send("```json\n{$tplText}\n```");
  205. $complete = time() - $startAt;
  206. $content = $response['choices'][0]['message']['content'] ?? '';
  207. Log::debug("ai response in {$complete}s content=" . $content);
  208. $id = "{$sentence->book_id}-{$sentence->paragraph}-{$sentence->word_start}-{$sentence->word_end}";
  209. $result[] = [
  210. 'id' => $id,
  211. 'content' => $content,
  212. ];
  213. }
  214. return $result;
  215. }
  216. private function aiNissayaTranslate($book, $para)
  217. {
  218. $sysPrompt = <<<md
  219. 你是一个佛教翻译专家,精通巴利文和缅文
  220. ## 翻译要求:
  221. - 请将nissaya单词表中的巴利文和缅文分别翻译为中文
  222. - 输入格式为 巴利文:缅文
  223. - 一行是一条记录,翻译的时候,请不要拆分一行中的巴利文单词或缅文单词,一行中出现多个单词的,一起翻译
  224. - 输出csv格式内容,分隔符为"$",
  225. - 字段如下:巴利文\$巴利文的中文译文\$缅文\$缅文的中文译文 #两个译文的语义相似度(%)
  226. **范例**:
  227. pana\$然而\$ဝါဒန္တရကား\$教义之说 #60%
  228. 直接输出csv, 无需其他内容
  229. 用```包裹的行为注释内容,也需要翻译和解释。放在最后面。如果没有```,无需处理
  230. md;
  231. $sentences = Sentence::nissaya()
  232. ->language('my') // 过滤缅文
  233. ->where('book_id', $book)
  234. ->where('paragraph', $para)
  235. ->orderBy('strlen')
  236. ->get();
  237. $result = [];
  238. foreach ($sentences as $key => $sentence) {
  239. $nissaya = [];
  240. $rows = explode("\n", $sentence->content);
  241. foreach ($rows as $key => $row) {
  242. if (strpos('=', $row) >= 0) {
  243. $factors = explode("=", $row);
  244. $nissaya[] = Tools::MyToRm($factors[0]) . ':' . end($factors);
  245. } else {
  246. $nissaya[] = $row;
  247. }
  248. }
  249. $nissayaText = json_encode(implode("\n", $nissaya), JSON_UNESCAPED_UNICODE);
  250. Log::debug($nissayaText);
  251. $startAt = time();
  252. $response = $this->openAIService->setApiUrl($this->model['url'])
  253. ->setModel($this->model['model'])
  254. ->setApiKey($this->model['key'])
  255. ->setSystemPrompt($sysPrompt)
  256. ->setTemperature(0.7)
  257. ->setStream(false)
  258. ->send("# nissaya\n\n{$nissayaText}\n\n");
  259. $complete = time() - $startAt;
  260. $content = $response['choices'][0]['message']['content'] ?? '';
  261. Log::debug("ai response in {$complete}s content=" . $content);
  262. $id = "{$sentence->book_id}-{$sentence->paragraph}-{$sentence->word_start}-{$sentence->word_end}";
  263. $result[] = [
  264. 'id' => $id,
  265. 'content' => $content,
  266. ];
  267. }
  268. return $result;
  269. }
  270. private function save($data)
  271. {
  272. //写入句子库
  273. $sentData = [];
  274. $sentData = array_map(function ($n) {
  275. $sId = explode('-', $n['id']);
  276. return [
  277. 'book_id' => $sId[0],
  278. 'paragraph' => $sId[1],
  279. 'word_start' => $sId[2],
  280. 'word_end' => $sId[3],
  281. 'channel_uid' => $this->workChannel['id'],
  282. 'content' => $n['content'],
  283. 'content_type' => $n['content_type'] ?? 'markdown',
  284. 'lang' => $this->workChannel['lang'],
  285. 'status' => $this->workChannel['status'],
  286. 'editor_uid' => $this->model['uid'],
  287. ];
  288. }, $data);
  289. foreach ($sentData as $key => $value) {
  290. $this->sentenceService->save($value);
  291. }
  292. }
  293. }