prompt_template_parser.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import re
  2. REGEX = re.compile(r"\{\{([a-zA-Z_][a-zA-Z0-9_]{0,29}|#histories#|#query#|#context#)\}\}")
  3. WITH_VARIABLE_TMPL_REGEX = re.compile(
  4. r"\{\{([a-zA-Z_][a-zA-Z0-9_]{0,29}|#[a-zA-Z0-9_]{1,50}\.[a-zA-Z0-9_\.]{1,100}#|#histories#|#query#|#context#)\}\}"
  5. )
  6. class PromptTemplateParser:
  7. """
  8. Rules:
  9. 1. Template variables must be enclosed in `{{}}`.
  10. 2. The template variable Key can only be: letters + numbers + underscore, with a maximum length of 16 characters,
  11. and can only start with letters and underscores.
  12. 3. The template variable Key cannot contain new lines or spaces, and must comply with rule 2.
  13. 4. In addition to the above, 3 types of special template variable Keys are accepted:
  14. `{{#histories#}}` `{{#query#}}` `{{#context#}}`. No other `{{##}}` template variables are allowed.
  15. """
  16. def __init__(self, template: str, with_variable_tmpl: bool = False):
  17. self.template = template
  18. self.with_variable_tmpl = with_variable_tmpl
  19. self.regex = WITH_VARIABLE_TMPL_REGEX if with_variable_tmpl else REGEX
  20. self.variable_keys = self.extract()
  21. def extract(self) -> list:
  22. # Regular expression to match the template rules
  23. return re.findall(self.regex, self.template)
  24. def format(self, inputs: dict, remove_template_variables: bool = True) -> str:
  25. def replacer(match):
  26. key = match.group(1)
  27. value = inputs.get(key, match.group(0)) # return original matched string if key not found
  28. if remove_template_variables and isinstance(value, str):
  29. return PromptTemplateParser.remove_template_variables(value, self.with_variable_tmpl)
  30. return value
  31. prompt = re.sub(self.regex, replacer, self.template)
  32. return re.sub(r"<\|.*?\|>", "", prompt)
  33. @classmethod
  34. def remove_template_variables(cls, text: str, with_variable_tmpl: bool = False):
  35. return re.sub(WITH_VARIABLE_TMPL_REGEX if with_variable_tmpl else REGEX, r"{\1}", text)