ChatConfig.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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__(
  18. self,
  19. template: Template = None,
  20. model=None,
  21. temperature=None,
  22. max_tokens=None,
  23. top_p=None,
  24. stream: bool = False,
  25. ):
  26. """
  27. Initializes the ChatConfig instance.
  28. :param template: Optional. The `Template` instance to use as a template for
  29. prompt.
  30. :param model: Optional. Controls the OpenAI model used.
  31. :param temperature: Optional. Controls the randomness of the model's output.
  32. Higher values (closer to 1) make output more random,lower values make it more
  33. deterministic.
  34. :param max_tokens: Optional. Controls how many tokens are generated.
  35. :param top_p: Optional. Controls the diversity of words.Higher values
  36. (closer to 1) make word selection more diverse, lower values make words less
  37. diverse.
  38. :param stream: Optional. Control if response is streamed back to the user
  39. :raises ValueError: If the template is not valid as template should contain
  40. $context and $query and $history
  41. """
  42. if template is None:
  43. template = DEFAULT_PROMPT_TEMPLATE
  44. # History is set as 0 to ensure that there is always a history, that way,
  45. # there don't have to be two templates. Having two templates would make it
  46. # complicated because the history is not user controlled.
  47. super().__init__(
  48. template,
  49. model=model,
  50. temperature=temperature,
  51. max_tokens=max_tokens,
  52. top_p=top_p,
  53. history=[0],
  54. stream=stream,
  55. )
  56. def set_history(self, history):
  57. """
  58. Chat history is not user provided and not set at initialization time
  59. :param history: (string) history to set
  60. """
  61. self.history = history
  62. return