NissayaTranslateService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace App\Services\AIAssistant;
  3. use App\Services\NissayaParser;
  4. use App\Services\OpenAIService;
  5. use App\Services\RomanizeService;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Http\Resources\AiModelResource;
  8. class NissayaTranslateService
  9. {
  10. protected OpenAIService $openAIService;
  11. protected NissayaParser $nissayaParser;
  12. protected RomanizeService $romanizeService;
  13. protected AiModelResource $model;
  14. protected bool $romanize;
  15. /**
  16. * 翻译提示词模板
  17. */
  18. protected string $translatePrompt = <<<PROMPT
  19. 你是一个专业的缅甸语翻译专家。你的任务是将缅文逐词解析(Nissaya)翻译成中文。
  20. 输入格式:
  21. - 每行包含三个部分: original(巴利文), translation(缅文译文), note(缅文注释,可能没有)
  22. - 输入为JSON Lines格式
  23. 输出要求:
  24. 1. 保持巴利文(original)原样输出,不做任何修改
  25. 2. 将巴利文(original)直接翻译成中文
  26. 2. 将缅文译文(translation)翻译成中文
  27. 3. 将缅文注释(note)翻译成中文
  28. 4. 输出必须是严格的JSON Lines格式,每行一个有效的JSON对象
  29. 5. 不要添加任何解释、说明或markdown代码块标记
  30. 6. 保持原有的数据结构和字段名称
  31. 7. 输出三个字段
  32. 1. original:原巴利文
  33. 2. translation:巴利文的中文译文>缅文的中文译文
  34. 3. note:缅文注释的中文译文
  35. 3. confidence:两个译文的语义相似度(0-100)
  36. 示例输入:
  37. {"original":"buddha","translation":"ဗုဒ္ဓ","note":"အဘိဓာန်"}
  38. 示例输出:
  39. {"original":"buddha","translation":"佛>佛陀","note":"词汇表","confidence":100}
  40. 请翻译以下内容:
  41. PROMPT;
  42. public function __construct(
  43. OpenAIService $openAIService,
  44. NissayaParser $nissayaParser,
  45. RomanizeService $romanizeService
  46. ) {
  47. $this->openAIService = $openAIService;
  48. $this->nissayaParser = $nissayaParser;
  49. $this->romanizeService = $romanizeService;
  50. $this->romanize = true;
  51. }
  52. /**
  53. * 设置模型配置
  54. *
  55. * @param \App\Http\Resources\AiModelResource $model
  56. * @return self
  57. */
  58. public function setModel(AiModelResource $model): self
  59. {
  60. $this->model = $model;
  61. return $this;
  62. }
  63. /**
  64. * 设置翻译提示词
  65. *
  66. * @param string $prompt
  67. * @return self
  68. */
  69. public function setTranslatePrompt(string $prompt): self
  70. {
  71. $this->translatePrompt = $prompt;
  72. return $this;
  73. }
  74. /**
  75. * 设置翻译提示词
  76. *
  77. * @param string $prompt
  78. * @return self
  79. */
  80. public function setRomanize(bool $romanize): self
  81. {
  82. $this->romanize = $romanize;
  83. return $this;
  84. }
  85. /**
  86. * 翻译缅文版逐词解析
  87. *
  88. * @param string $text 格式: 巴利文=缅文
  89. * @param bool $stream 是否流式输出
  90. * @return array
  91. * @throws \Exception
  92. */
  93. public function translate(string $text, bool $stream = false): array
  94. {
  95. $startAt = time();
  96. try {
  97. // 1. 解析nissaya文本为数组
  98. $parsedData = $this->nissayaParser->parse($text);
  99. if (empty($parsedData)) {
  100. throw new \Exception('解析nissaya文本失败,返回空数组');
  101. }
  102. $parsedData = $this->romanize($parsedData);
  103. foreach ($parsedData as $key => $value) {
  104. if (isset($value['notes']) && is_array($value['notes'])) {
  105. $parsedData[$key]['note'] = implode("\n\n----\n\n", $value['notes']);
  106. $parsedData[$key]['note'] = str_replace("\n**", "\n\n-----\n\n", $parsedData[$key]['note']);
  107. unset($parsedData[$key]['notes']);
  108. }
  109. }
  110. // 2. 将解析后的数组转换为JSONL格式
  111. $jsonlInput = $this->arrayToJsonl($parsedData);
  112. Log::info('NissayaTranslate: 准备翻译', [
  113. 'items_count' => count($parsedData),
  114. 'input_length' => strlen($jsonlInput),
  115. ]);
  116. // 3. 调用LLM进行翻译
  117. $response = $this->openAIService
  118. ->setApiUrl($this->model['url'])
  119. ->setModel($this->model['model'])
  120. ->setApiKey($this->model['key'])
  121. ->setSystemPrompt($this->translatePrompt)
  122. ->setTemperature(0.3)
  123. ->setStream($stream)
  124. ->send($jsonlInput);
  125. $complete = time() - $startAt;
  126. $content = $response['choices'][0]['message']['content'] ?? '';
  127. if (empty($content)) {
  128. throw new \Exception('LLM返回内容为空');
  129. }
  130. // 4. 解析JSONL格式的翻译结果
  131. $translatedData = $this->jsonlToArray($content);
  132. Log::info('NissayaTranslate: 翻译完成', [
  133. 'duration' => $complete,
  134. 'output_items' => count($translatedData),
  135. 'input_tokens' => $response['usage']['prompt_tokens'] ?? 0,
  136. 'output_tokens' => $response['usage']['completion_tokens'] ?? 0,
  137. ]);
  138. return [
  139. 'success' => true,
  140. 'data' => $translatedData,
  141. 'meta' => [
  142. 'duration' => $complete,
  143. 'items_count' => count($translatedData),
  144. 'usage' => $response['usage'] ?? [],
  145. ],
  146. ];
  147. } catch (\Exception $e) {
  148. Log::error('NissayaTranslate: 翻译失败', [
  149. 'error' => $e->getMessage(),
  150. 'trace' => $e->getTraceAsString(),
  151. ]);
  152. return [
  153. 'success' => false,
  154. 'error' => $e->getMessage(),
  155. 'data' => [],
  156. ];
  157. }
  158. }
  159. protected function romanize(array $data): array
  160. {
  161. if ($this->romanize) {
  162. foreach ($data as $key => $value) {
  163. $data[$key]['original'] = $this->romanizeService->myanmarToRoman($value['original']);
  164. }
  165. }
  166. return $data;
  167. }
  168. /**
  169. * 将数组转换为JSONL格式
  170. *
  171. * @param array $data
  172. * @return string
  173. */
  174. protected function arrayToJsonl(array $data): string
  175. {
  176. $lines = [];
  177. foreach ($data as $item) {
  178. $lines[] = json_encode($item, JSON_UNESCAPED_UNICODE);
  179. }
  180. return implode("\n", $lines);
  181. }
  182. /**
  183. * 将JSONL格式转换为数组
  184. *
  185. * @param string $jsonl
  186. * @return array
  187. */
  188. protected function jsonlToArray(string $jsonl): array
  189. {
  190. // 清理可能的markdown代码块标记
  191. $jsonl = preg_replace('/```json\s*|\s*```/', '', $jsonl);
  192. $jsonl = trim($jsonl);
  193. $lines = explode("\n", $jsonl);
  194. $result = [];
  195. foreach ($lines as $line) {
  196. $line = trim($line);
  197. if (empty($line)) {
  198. continue;
  199. }
  200. $decoded = json_decode($line, true);
  201. if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
  202. $result[] = $decoded;
  203. } else {
  204. Log::warning('NissayaTranslate: 无法解析JSON行', [
  205. 'line' => $line,
  206. 'error' => json_last_error_msg(),
  207. ]);
  208. }
  209. }
  210. return $result;
  211. }
  212. /**
  213. * 批量翻译(将大文本分批处理)
  214. *
  215. * @param string $text
  216. * @param int $batchSize 每批处理的条目数
  217. * @return array
  218. */
  219. public function translateInBatches(string $text, int $batchSize = 50): array
  220. {
  221. try {
  222. $parsedData = $this->nissayaParser->parse($text);
  223. $batches = array_chunk($parsedData, $batchSize);
  224. $allResults = [];
  225. $totalDuration = 0;
  226. $totalUsage = [
  227. 'prompt_tokens' => 0,
  228. 'completion_tokens' => 0,
  229. 'total_tokens' => 0,
  230. ];
  231. foreach ($batches as $index => $batch) {
  232. Log::info("NissayaTranslate: 处理批次 " . ($index + 1) . "/" . count($batches));
  233. $jsonlInput = $this->arrayToJsonl($batch);
  234. $response = $this->openAIService
  235. ->setApiUrl($this->model['url'])
  236. ->setModel($this->model['model'])
  237. ->setApiKey($this->model['key'])
  238. ->setSystemPrompt($this->translatePrompt)
  239. ->setTemperature(0.7)
  240. ->setStream(false)
  241. ->send($jsonlInput);
  242. $content = $response['choices'][0]['message']['content'] ?? '';
  243. $translatedBatch = $this->jsonlToArray($content);
  244. $allResults = array_merge($allResults, $translatedBatch);
  245. // 累计使用统计
  246. if (isset($response['usage'])) {
  247. $totalUsage['prompt_tokens'] += $response['usage']['prompt_tokens'] ?? 0;
  248. $totalUsage['completion_tokens'] += $response['usage']['completion_tokens'] ?? 0;
  249. $totalUsage['total_tokens'] += $response['usage']['total_tokens'] ?? 0;
  250. }
  251. }
  252. return [
  253. 'success' => true,
  254. 'data' => $allResults,
  255. 'meta' => [
  256. 'batches' => count($batches),
  257. 'items_count' => count($allResults),
  258. 'usage' => $totalUsage,
  259. ],
  260. ];
  261. } catch (\Exception $e) {
  262. Log::error('NissayaTranslate: 批量翻译失败', [
  263. 'error' => $e->getMessage(),
  264. ]);
  265. return [
  266. 'success' => false,
  267. 'error' => $e->getMessage(),
  268. 'data' => [],
  269. ];
  270. }
  271. }
  272. }