QueryConfig.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. DOCS_SITE_DEFAULT_PROMPT = """
  21. Use the following pieces of context to answer the query at the end.
  22. If you don't know the answer, just say that you don't know, don't try to make up an answer. Wherever possible, give complete code snippet. Dont make up any code snippet on your own.
  23. $context
  24. Query: $query
  25. Helpful Answer:
  26. """ # noqa:E501
  27. DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
  28. DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE = Template(DEFAULT_PROMPT_WITH_HISTORY)
  29. DOCS_SITE_PROMPT_TEMPLATE = Template(DOCS_SITE_DEFAULT_PROMPT)
  30. query_re = re.compile(r"\$\{*query\}*")
  31. context_re = re.compile(r"\$\{*context\}*")
  32. history_re = re.compile(r"\$\{*history\}*")
  33. class QueryConfig(BaseConfig):
  34. """
  35. Config for the `query` method.
  36. """
  37. def __init__(
  38. self,
  39. number_documents=None,
  40. template: Template = None,
  41. model=None,
  42. temperature=None,
  43. max_tokens=None,
  44. top_p=None,
  45. history=None,
  46. stream: bool = False,
  47. deployment_name=None,
  48. ):
  49. """
  50. Initializes the QueryConfig instance.
  51. :param number_documents: Number of documents to pull from the database as
  52. context.
  53. :param template: Optional. The `Template` instance to use as a template for
  54. prompt.
  55. :param model: Optional. Controls the OpenAI model used.
  56. :param temperature: Optional. Controls the randomness of the model's output.
  57. Higher values (closer to 1) make output more random, lower values make it more
  58. deterministic.
  59. :param max_tokens: Optional. Controls how many tokens are generated.
  60. :param top_p: Optional. Controls the diversity of words. Higher values
  61. (closer to 1) make word selection more diverse, lower values make words less
  62. diverse.
  63. :param history: Optional. A list of strings to consider as history.
  64. :param stream: Optional. Control if response is streamed back to user
  65. :raises ValueError: If the template is not valid as template should
  66. contain $context and $query (and optionally $history).
  67. """
  68. if number_documents is None:
  69. self.number_documents = 1
  70. else:
  71. self.number_documents = number_documents
  72. if not history:
  73. self.history = None
  74. else:
  75. if len(history) == 0:
  76. self.history = None
  77. else:
  78. self.history = history
  79. if template is None:
  80. if self.history is None:
  81. template = DEFAULT_PROMPT_TEMPLATE
  82. else:
  83. template = DEFAULT_PROMPT_WITH_HISTORY_TEMPLATE
  84. self.temperature = temperature if temperature else 0
  85. self.max_tokens = max_tokens if max_tokens else 1000
  86. self.model = model
  87. self.top_p = top_p if top_p else 1
  88. self.deployment_name = deployment_name
  89. if self.validate_template(template):
  90. self.template = template
  91. else:
  92. if self.history is None:
  93. raise ValueError("`template` should have `query` and `context` keys")
  94. else:
  95. raise ValueError("`template` should have `query`, `context` and `history` keys")
  96. if not isinstance(stream, bool):
  97. raise ValueError("`stream` should be bool")
  98. self.stream = stream
  99. def validate_template(self, template: Template):
  100. """
  101. validate the template
  102. :param template: the template to validate
  103. :return: Boolean, valid (true) or invalid (false)
  104. """
  105. if self.history is None:
  106. return re.search(query_re, template.template) and re.search(context_re, template.template)
  107. else:
  108. return (
  109. re.search(query_re, template.template)
  110. and re.search(context_re, template.template)
  111. and re.search(history_re, template.template)
  112. )