generate_ensemble_prompt.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 GenerateEnsemblePrompt(BaseGeneratePrompt):
  17. """Generate Ensemble Prompt"""
  18. entities = ["ensemble_prompt"]
  19. def __init__(self, config: Dict) -> None:
  20. """Initializes the GenerateEnsemblePrompt 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. key: str,
  62. result_methodA: str,
  63. result_methodB: str,
  64. task_description: str = None,
  65. output_format: str = None,
  66. rules_str: str = None,
  67. few_shot_demo_text_content: str = None,
  68. few_shot_demo_key_value_list: str = None,
  69. ) -> str:
  70. """Generates a prompt based on the given parameters.
  71. Args:
  72. key (str): the input question.
  73. result_methodA (str): the result of method A.
  74. result_methodB (str): the result of method B.
  75. task_description (str, optional): A description of the task. Defaults to None.
  76. output_format (str, optional): The desired output format. Defaults to None.
  77. rules_str (str, optional): A string containing rules or instructions. Defaults to None.
  78. few_shot_demo_text_content (str, optional): Text content for few-shot demos. Defaults to None.
  79. few_shot_demo_key_value_list (str, optional): Key-value list for few-shot demos. Defaults to None.
  80. Returns:
  81. str: The generated prompt.
  82. Raises:
  83. ValueError: If the task_type is not supported.
  84. """
  85. if task_description is None:
  86. task_description = self.task_description
  87. if output_format is None:
  88. output_format = self.output_format
  89. if rules_str is None:
  90. rules_str = self.rules_str
  91. if few_shot_demo_text_content is None:
  92. few_shot_demo_text_content = self.few_shot_demo_text_content
  93. if few_shot_demo_key_value_list is None:
  94. few_shot_demo_key_value_list = self.few_shot_demo_key_value_list
  95. prompt = f"""{task_description}{rules_str}{output_format}{few_shot_demo_text_content}{few_shot_demo_key_value_list}"""
  96. task_type = self.task_type
  97. if task_type == "ensemble_prompt":
  98. prompt += f"""下面正式开始:
  99. \n问题:```{key}```
  100. \n方法A的结果:{result_methodA}
  101. \n方法B的结果:{result_methodB}
  102. """
  103. else:
  104. raise ValueError(f"{self.task_type} is currently not supported.")
  105. return prompt