QueryConfig.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import re
  2. from string import Template
  3. from embedchain.config.BaseConfig import BaseConfig
  4. DEFAULT_PROMPT = """
  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. """ # noqa:E501
  11. DEFAULT_PROMPT_WITH_HISTORY = """
  12. Use the following pieces of context to answer the query at the end.
  13. If you don't know the answer, just say that you don't know, don't try to make up an answer.
  14. I will provide you with our conversation history.
  15. $context
  16. History: $history
  17. Query: $query
  18. Helpful Answer:
  19. """ # noqa:E501
  20. DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
  21. DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE = Template(DEFAULT_PROMPT_WITH_HISTORY)
  22. query_re = re.compile(r"\$\{*query\}*")
  23. context_re = re.compile(r"\$\{*context\}*")
  24. history_re = re.compile(r"\$\{*history\}*")
  25. class QueryConfig(BaseConfig):
  26. """
  27. Config for the `query` method.
  28. """
  29. def __init__(self, template: Template = None, history=None, stream: bool = False):
  30. """
  31. Initializes the QueryConfig instance.
  32. :param template: Optional. The `Template` instance to use as a
  33. template for prompt.
  34. :param history: Optional. A list of strings to consider as history.
  35. :param stream: Optional. Control if response is streamed back to user
  36. :raises ValueError: If the template is not valid as template should
  37. contain $context and $query (and optionally $history).
  38. """
  39. if not history:
  40. self.history = None
  41. else:
  42. if len(history) == 0:
  43. self.history = None
  44. else:
  45. self.history = history
  46. if template is None:
  47. if self.history is None:
  48. template = DEFAULT_PROMPT_TEMPLATE
  49. else:
  50. template = DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE
  51. if self.validate_template(template):
  52. self.template = template
  53. else:
  54. if self.history is None:
  55. raise ValueError("`template` should have `query` and `context` keys")
  56. else:
  57. raise ValueError(
  58. "`template` should have `query`, `context` and `history` keys"
  59. )
  60. if not isinstance(stream, bool):
  61. raise ValueError("`stream` should be bool")
  62. self.stream = stream
  63. def validate_template(self, template: Template):
  64. """
  65. validate the template
  66. :param template: the template to validate
  67. :return: Boolean, valid (true) or invalid (false)
  68. """
  69. if self.history is None:
  70. return re.search(query_re, template.template) and re.search(
  71. context_re, template.template
  72. )
  73. else:
  74. return (
  75. re.search(query_re, template.template)
  76. and re.search(context_re, template.template)
  77. and re.search(history_re, template.template)
  78. )