ChatConfig.py 2.3 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, model = None, temperature = None, max_tokens = None, top_p = None, stream: bool = False):
  18. """
  19. Initializes the ChatConfig instance.
  20. :param template: Optional. The `Template` instance to use as a template for prompt.
  21. :param model: Optional. Controls the OpenAI model used.
  22. :param temperature: Optional. Controls the randomness of the model's output.
  23. Higher values (closer to 1) make output more random, lower values make it more deterministic.
  24. :param max_tokens: Optional. Controls how many tokens are generated.
  25. :param top_p: Optional. Controls the diversity of words. Higher values (closer to 1) make word selection more diverse, lower values make words less diverse.
  26. :param stream: Optional. Control if response is streamed back to the user
  27. :raises ValueError: If the template is not valid as template should contain $context and $query and $history
  28. """
  29. if template is None:
  30. template = DEFAULT_PROMPT_TEMPLATE
  31. # History is set as 0 to ensure that there is always a history, that way, there don't have to be two templates.
  32. # Having two templates would make it complicated because the history is not user controlled.
  33. super().__init__(template, model=model, temperature=temperature, max_tokens=max_tokens, top_p=top_p, 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