QueryConfig.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from embedchain.config.BaseConfig import BaseConfig
  2. from string import Template
  3. import re
  4. DEFAULT_PROMPT_TEMPLATE = Template("""
  5. Use the following pieces of context to answer the query at the end.
  6. If you don't know the answer, just say that you don't know, don't try to make up an answer.
  7. $context
  8. Query: $query
  9. Helpful Answer:
  10. """)
  11. query_re = re.compile(r"\$\{*query\}*")
  12. context_re = re.compile(r"\$\{*context\}*")
  13. class QueryConfig(BaseConfig):
  14. """
  15. Config for the `query` method.
  16. """
  17. def __init__(self, template: Template = None):
  18. """
  19. Initializes the QueryConfig instance.
  20. :param template: Optional. The `Template` instance to use as a template for prompt.
  21. :raises ValueError: If the template is not valid as template should contain $context and $query
  22. """
  23. if template is None:
  24. template = DEFAULT_PROMPT_TEMPLATE
  25. if not (re.search(query_re, template.template) \
  26. and re.search(context_re, template.template)):
  27. raise ValueError("`template` should have `query` and `context` keys")
  28. self.template = template