QueryConfig.py 4.7 KB

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