i18n.service.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { Injectable } from '@nestjs/common';
  2. import { errorMessages, logMessages, statusMessages } from './messages';
  3. import { i18nStore } from './i18n.store';
  4. @Injectable()
  5. export class I18nService {
  6. private readonly defaultLanguage = 'ja'; // プロジェクト要件に従い、日本語をデフォルトとして使用
  7. private getLanguage(lang?: string): string {
  8. if (lang) return lang;
  9. const store = i18nStore.getStore();
  10. return store?.language || this.defaultLanguage;
  11. }
  12. getErrorMessage(key: string, language?: string): string {
  13. const lang = this.getLanguage(language);
  14. return errorMessages[lang]?.[key] || errorMessages[this.defaultLanguage][key] || key;
  15. }
  16. getLogMessage(key: string, language?: string): string {
  17. const lang = this.getLanguage(language);
  18. return logMessages[lang]?.[key] || logMessages[this.defaultLanguage][key] || key;
  19. }
  20. getStatusMessage(key: string, language?: string): string {
  21. const lang = this.getLanguage(language);
  22. return statusMessages[lang]?.[key] || statusMessages[this.defaultLanguage][key] || key;
  23. }
  24. // 汎用メッセージ取得メソッド、順次検索
  25. getMessage(key: string, language?: string): string {
  26. const lang = this.getLanguage(language);
  27. // ステータスメッセージ、エラーメッセージ、ログメッセージの順に検索
  28. return statusMessages[lang]?.[key] ||
  29. statusMessages[this.defaultLanguage][key] ||
  30. errorMessages[lang]?.[key] ||
  31. errorMessages[this.defaultLanguage][key] ||
  32. logMessages[lang]?.[key] ||
  33. logMessages[this.defaultLanguage][key] ||
  34. key;
  35. }
  36. // メッセージの取得とフォーマット
  37. formatMessage(key: string, args: Record<string, any>, language?: string): string {
  38. let message = this.getMessage(key, language);
  39. for (const [argKey, argValue] of Object.entries(args)) {
  40. message = message.replace(new RegExp(`\\{${argKey}\\}`, 'g'), String(argValue));
  41. }
  42. return message;
  43. }
  44. // サポートされている言語リストを取得
  45. getSupportedLanguages(): string[] {
  46. return Object.keys(errorMessages);
  47. }
  48. // 言語がサポートされているか確認
  49. isLanguageSupported(language: string): boolean {
  50. return this.getSupportedLanguages().includes(language);
  51. }
  52. // システムプロンプトを取得
  53. getPrompt(lang: string = this.defaultLanguage, type: 'withContext' | 'withoutContext' = 'withContext', hasKnowledgeGroup: boolean = false): string {
  54. const language = this.getLanguage(lang);
  55. const noMatchMsg = statusMessages[language]?.noMatchInKnowledgeGroup || statusMessages['ja'].noMatchInKnowledgeGroup;
  56. if (language === 'zh') {
  57. return type === 'withContext' ? `
  58. 基于以下知识库内容回答用户问题。
  59. ${hasKnowledgeGroup ? `
  60. **重要提示**: 用户已选择特定知识组,请严格基于以下知识库内容回答。如果知识库中没有相关信息,请明确告知用户:"${noMatchMsg}",然后再提供答案。
  61. ` : ''}
  62. 知识库内容:
  63. {context}
  64. 历史对话:
  65. {history}
  66. 用户问题:{question}
  67. 请用中文回答,并严格遵循以下 Markdown 格式要求:
  68. 1. **段落与结构**:
  69. - 使用清晰的段落分隔,每个要点之间空一行
  70. - 使用标题(## 或 ###)组织长回答
  71. 2. **文本格式**:
  72. - 使用 **粗体** 强调重要概念和关键词
  73. - 使用列表(- 或 1.)组织多个要点
  74. - 使用 \`代码\` 标记技术术语、命令、文件名
  75. 3. **代码展示**:
  76. - 使用代码块展示代码,并指定语言:
  77. \`\`\`python
  78. def example():
  79. return "示例"
  80. \`\`\`
  81. - 支持语言:python, javascript, typescript, java, bash, sql 等
  82. 4. **图表与可视化**:
  83. - 使用 Mermaid 语法绘制流程图、序列图等:
  84. \`\`\`mermaid
  85. graph LR
  86. A[开始] --> B[处理]
  87. B --> C[结束]
  88. \`\`\`
  89. - 适用场景:流程、架构、状态机、时序图
  90. 5. **其他要求**:
  91. - 回答精炼准确
  92. - 多步骤操作使用有序列表
  93. - 对比类信息建议用表格展示(如果适用)
  94. ` : `
  95. 作为智能助手,请回答用户的问题。
  96. 历史对话:
  97. {history}
  98. 用户问题:{question}
  99. 请用中文回答。
  100. `;
  101. } else if (language === 'en') {
  102. return type === 'withContext' ? `
  103. Answer the user's question based on the following knowledge base content.
  104. ${hasKnowledgeGroup ? `
  105. **IMPORTANT**: The user has selected a specific knowledge group. Please answer strictly based on the knowledge base content below. If the relevant information is not found in the knowledge base, explicitly tell the user: "${noMatchMsg}", before providing an answer.
  106. ` : ''}
  107. Knowledge Base CONTENT:
  108. {context}
  109. Conversation history:
  110. {history}
  111. User question: {question}
  112. Please answer in English and strictly follow these Markdown formatting guidelines:
  113. 1. **Paragraphs & Structure**:
  114. - Use clear paragraph breaks with blank lines between key points
  115. - Use headings (## or ###) to organize longer answers
  116. 2. **Text Formatting**:
  117. - Use **bold** to emphasize important concepts and keywords
  118. - Use lists (- or 1.) to organize multiple points
  119. - Use \`code\` to mark technical terms, commands, file names
  120. 3. **Code Display**:
  121. - Use code blocks with language specification:
  122. \`\`\`python
  123. def example():
  124. return "example"
  125. \`\`\`
  126. - Supported languages: python, javascript, typescript, java, bash, sql, etc.
  127. 4. **Diagrams & Charts**:
  128. - Use Mermaid syntax for flowcharts, sequence diagrams, etc.:
  129. \`\`\`mermaid
  130. graph LR
  131. A[Start] --> B[Process]
  132. B --> C[End]
  133. \`\`\`
  134. - Use cases: process flows, architecture diagrams, state diagrams, sequence diagrams
  135. 5. **Other Requirements**:
  136. - Keep answers concise and clear
  137. - Use numbered lists for multi-step processes
  138. - Use tables for comparison information (if applicable)
  139. ` : `
  140. As an intelligent assistant, please answer the user's question.
  141. Conversation history:
  142. {history}
  143. User question: {question}
  144. Please answer in English.
  145. `;
  146. } else { // 默认为日语,符合项目要求
  147. return type === 'withContext' ? `
  148. 以下のナレッジベースの内容に基づいてユーザーの質問に答えてくplease。
  149. ${hasKnowledgeGroup ? `
  150. **重要**: ユーザーが特定の知識グループを選択しました。以下のナレッジベースの内容に厳密に基づいて回答please。ナレッジベースに関連情報がない場合は、「${noMatchMsg}」とユーザーに明示的に伝えてfrom、回答を提供please。
  151. ` : ''}
  152. ナレッジベースの内容:
  153. {context}
  154. 会話履歴:
  155. {history}
  156. ユーザーの質問:{question}
  157. 日本語で回答please。以下の Markdown 書式要件に厳密に従ってくplease:
  158. 1. **段落と構造**:
  159. - 明確な段落分けを使用し、要点間に空行を入れる
  160. - 長い回答には見出し(## または ###)を使用
  161. 2. **テキスト書式**:
  162. - 重要な概念やキーワードを強調するために **太字** を使用
  163. - 複数のポイントを整理するためにリスト(- または 1.)を使用
  164. - 技術用語、コマンド、ファイル名をマークするために \`コード\` を使用
  165. 3. **コード表示**:
  166. - 言語を指定してコードブロックを使用:
  167. \`\`\`python
  168. def example():
  169. return "例"
  170. \`\`\`
  171. - 対応言語:python, javascript, typescript, java, bash, sql etc.
  172. 4. **図表とチャート**:
  173. - フローチャート、シーケンス図etc.に Mermaid 構文を使用:
  174. \`\`\`mermaid
  175. graph LR
  176. A[開始] --> B[処理]
  177. B --> C[終了]
  178. \`\`\`
  179. - 使用例:プロセスフロー、アーキテクチャ図、状態図、シーケンス図
  180. 5. **その他の要件**:
  181. - 簡潔で明確な回答を心がける
  182. - 複数のステップがある場合は番号付きリストを使用
  183. - 比較情報には表を使用(該当する場合)
  184. ` : `
  185. インテリジェントアシスタントとして、ユーザーの質問に答えてくplease。
  186. 会話履歴:
  187. {history}
  188. ユーザーの質問:{question}
  189. 日本語で回答please。
  190. `;
  191. }
  192. }
  193. // タイトル生成用のプロンプトを取得
  194. getDocumentTitlePrompt(lang: string = this.defaultLanguage, contentSample: string): string {
  195. const language = this.getLanguage(lang);
  196. if (language === 'zh') {
  197. return `你是一个文档分析师。请阅读以下文本(文档开头部分),并生成一个简炼、专业的标题(不超过50个字符)。
  198. 只返回标题文本。不要包含任何解释性文字或前导词(如“标题是:”)。
  199. 语言:中文
  200. 文本内容:
  201. ${contentSample}`;
  202. } else if (language === 'en') {
  203. return `You are a document analyzer. Read the following text (start of a document) and generate a concise, professional title (max 50 chars).
  204. Return ONLY the title text. No preamble like "The title is...".
  205. Language: English
  206. Text:
  207. ${contentSample}`;
  208. } else {
  209. return `あなたはドキュメントアナライザー.以下のテキスト(ドキュメントの冒頭部分)を読み、簡潔でプロフェッショナルなタイトル(最大50文字)を生成please。
  210. タイトルテキストのみを返please。説明文や前置き(例:「タイトルは:」)は含めないでくplease。
  211. 言語:日本語
  212. テキスト:
  213. ${contentSample}`;
  214. }
  215. }
  216. getChatTitlePrompt(lang: string = this.defaultLanguage, userMessage: string, aiResponse: string): string {
  217. const language = this.getLanguage(lang);
  218. if (language === 'zh') {
  219. return `根据以下对话片段,生成一个简短、描述性的标题(不超过50个字符),总结讨论的主题。
  220. 只返回标题文本。不要包含任何前导词。
  221. 语言:中文
  222. 片段:
  223. 用户: ${userMessage}
  224. 助手: ${aiResponse}`;
  225. } else if (language === 'en') {
  226. return `Based on the following conversation snippet, generate a short, descriptive title (max 50 chars) that summarizes the topic.
  227. Return ONLY the title. No preamble.
  228. Language: English
  229. Snippet:
  230. User: ${userMessage}
  231. Assistant: ${aiResponse}`;
  232. } else {
  233. return `以下の会話スニペットに基づいて、トピックを要約する短く説明的なタイトル(最大50文字)を生成please。
  234. タイトルのみを返please。前置きは不要.
  235. 言語:日本語
  236. スニペット:
  237. ユーザー: ${userMessage}
  238. アシスタント: ${aiResponse}`;
  239. }
  240. }
  241. }