ChatConfig.py 2.7 KB

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