base.py 5.5 KB

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