generate_translate_prompt.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Dict
  15. from .base import BaseGeneratePrompt
  16. class GenerateTranslatePrompt(BaseGeneratePrompt):
  17. """Generate Ensemble Prompt"""
  18. entities = ["translate_prompt"]
  19. def __init__(self, config: Dict) -> None:
  20. """Initializes the GenerateTranslatePrompt instance with the given configuration.
  21. Args:
  22. config (Dict): A dictionary containing configuration settings.
  23. - task_type (str): The type of task to generate a prompt for, in the support entities list.
  24. - task_description (str, optional): A description of the task. Defaults to an empty string.
  25. - output_format (str, optional): The desired output format. Defaults to an empty string.
  26. - rules_str (str, optional): A string representing rules for the task. Defaults to an empty string.
  27. - few_shot_demo_text_content (str, optional): Text content for few-shot demos. Defaults to an empty string.
  28. - few_shot_demo_key_value_list (str, optional): A key-value list for few-shot demos. Defaults to an empty string.
  29. Raises:
  30. ValueError: If the task type is not in the allowed entities for GenerateKIEPrompt.
  31. """
  32. super().__init__()
  33. task_type = config.get("task_type", "")
  34. task_description = config.get("task_description", "")
  35. output_format = config.get("output_format", "")
  36. rules_str = config.get("rules_str", "")
  37. few_shot_demo_text_content = config.get("few_shot_demo_text_content", "")
  38. few_shot_demo_key_value_list = config.get("few_shot_demo_key_value_list", "")
  39. if task_description is None:
  40. task_description = ""
  41. if output_format is None:
  42. output_format = ""
  43. if rules_str is None:
  44. rules_str = ""
  45. if few_shot_demo_text_content is None:
  46. few_shot_demo_text_content = ""
  47. if few_shot_demo_key_value_list is None:
  48. few_shot_demo_key_value_list = ""
  49. if task_type not in self.entities:
  50. raise ValueError(
  51. f"task type must be in {self.entities} of GenerateEnsemblePrompt."
  52. )
  53. self.task_type = task_type
  54. self.task_description = task_description
  55. self.output_format = output_format
  56. self.rules_str = rules_str
  57. self.few_shot_demo_text_content = few_shot_demo_text_content
  58. self.few_shot_demo_key_value_list = few_shot_demo_key_value_list
  59. def generate_prompt(
  60. self,
  61. original_text: str,
  62. language: str,
  63. task_description: str = None,
  64. output_format: str = None,
  65. rules_str: str = None,
  66. few_shot_demo_text_content: str = None,
  67. few_shot_demo_key_value_list: str = None,
  68. ) -> str:
  69. """Generates a prompt based on the given parameters.
  70. Args:
  71. key (str): the input question.
  72. result_methodA (str): the result of method A.
  73. result_methodB (str): the result of method B.
  74. task_description (str, optional): A description of the task. Defaults to None.
  75. output_format (str, optional): The desired output format. Defaults to None.
  76. rules_str (str, optional): A string containing rules or instructions. Defaults to None.
  77. few_shot_demo_text_content (str, optional): Text content for few-shot demos. Defaults to None.
  78. few_shot_demo_key_value_list (str, optional): Key-value list for few-shot demos. Defaults to None.
  79. Returns:
  80. str: The generated prompt.
  81. Raises:
  82. ValueError: If the task_type is not supported.
  83. """
  84. language_map = {
  85. "chinese": "简体中文",
  86. "zh": "简体中文",
  87. "english": "英语",
  88. "en": "英语",
  89. "french": "法语",
  90. "fr": "法语",
  91. "spanish": "西班牙语",
  92. "es": "西班牙语",
  93. "german": "德语",
  94. "de": "德语",
  95. "japanese": "日语",
  96. "ja": "日语",
  97. "korean": "韩语",
  98. "ko": "韩语",
  99. "russian": "俄语",
  100. "ru": "俄语",
  101. "italian": "意大利语",
  102. "it": "意大利语",
  103. "portuguese": "葡萄牙语",
  104. "pt": "葡萄牙语",
  105. "arabic": "阿拉伯语",
  106. "ar": "阿拉伯语",
  107. "hindi": "印地语",
  108. "hi": "印地语",
  109. "dutch": "荷兰语",
  110. "nl": "荷兰语",
  111. "swedish": "瑞典语",
  112. "sv": "瑞典语",
  113. "turkish": "土耳其语",
  114. "tr": "土耳其语",
  115. "thai": "泰语",
  116. "th": "泰语",
  117. "vietnamese": "越南语",
  118. "vi": "越南语",
  119. "hebrew": "希伯来语",
  120. "he": "希伯来语",
  121. "greek": "希腊语",
  122. "el": "希腊语",
  123. "polish": "波兰语",
  124. "pl": "波兰语",
  125. }
  126. if task_description is None:
  127. task_description = self.task_description
  128. if output_format is None:
  129. output_format = self.output_format
  130. if rules_str is None:
  131. rules_str = self.rules_str
  132. if few_shot_demo_text_content is None:
  133. few_shot_demo_text_content = self.few_shot_demo_text_content
  134. if few_shot_demo_text_content:
  135. few_shot_demo_text_content = (
  136. f"这里是一些示例:\n{few_shot_demo_text_content}\n"
  137. )
  138. if few_shot_demo_key_value_list is None:
  139. few_shot_demo_key_value_list = self.few_shot_demo_key_value_list
  140. if few_shot_demo_key_value_list:
  141. few_shot_demo_key_value_list = f"这里是一些专业术语对照表,对照表中单词要参考对照表翻译:\n{few_shot_demo_key_value_list}\n"
  142. prompt = f"""{task_description}{rules_str}{output_format}{few_shot_demo_text_content}{few_shot_demo_key_value_list}"""
  143. language_name = language_map.get(language, language)
  144. task_type = self.task_type
  145. if task_type == "translate_prompt":
  146. prompt += f"""下面正式开始:
  147. \n将以下内容翻译成:{language_name}
  148. \n原文:{original_text}
  149. """
  150. else:
  151. raise ValueError(f"{self.task_type} is currently not supported.")
  152. return prompt