ChatConfig.py 2.7 KB

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