ChatConfig.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from string import Template
  2. from embedchain.config.QueryConfig import QueryConfig
  3. DEFAULT_PROMPT = """
  4. You are a chatbot having a conversation with a human. You are given chat
  5. history and context.
  6. You need to answer the query considering context, chat history and your knowledge base. If you don't know the answer or the answer is neither contained in the context nor in history, then simply say "I don't know".
  7. $context
  8. History: $history
  9. Query: $query
  10. Helpful Answer:
  11. """ # noqa:E501
  12. DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
  13. class ChatConfig(QueryConfig):
  14. """
  15. Config for the `chat` method, inherits from `QueryConfig`.
  16. """
  17. def __init__(self, template: Template = None, stream: bool = False):
  18. """
  19. Initializes the ChatConfig instance.
  20. :param template: Optional. The `Template` instance to use as a
  21. template for prompt.
  22. :param stream: Optional. Control if response is streamed back to the
  23. user
  24. :raises ValueError: If the template is not valid as template should
  25. contain $context and $query and $history
  26. """
  27. if template is None:
  28. template = DEFAULT_PROMPT_TEMPLATE
  29. # History is set as 0 to ensure that there is always a history, that
  30. # way, there don't have to be two templates.
  31. # Having two templates would make it complicated because the history
  32. # is not user controlled.
  33. super().__init__(template, history=[0], stream=stream)
  34. def set_history(self, history):
  35. """
  36. Chat history is not user provided and not set at initialization time
  37. :param history: (string) history to set
  38. """
  39. self.history = history
  40. return