ChatConfig.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from string import Template
  2. from typing import Optional
  3. from embedchain.config.QueryConfig import QueryConfig
  4. from embedchain.helper_classes.json_serializable import register_deserializable
  5. DEFAULT_PROMPT = """
  6. You are a chatbot having a conversation with a human. You are given chat
  7. history and context.
  8. 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".
  9. $context
  10. History: $history
  11. Query: $query
  12. Helpful Answer:
  13. """ # noqa:E501
  14. DEFAULT_PROMPT_TEMPLATE = Template(DEFAULT_PROMPT)
  15. @register_deserializable
  16. class ChatConfig(QueryConfig):
  17. """
  18. Config for the `chat` method, inherits from `QueryConfig`.
  19. """
  20. def __init__(
  21. self,
  22. number_documents=None,
  23. template: Template = None,
  24. model=None,
  25. temperature=None,
  26. max_tokens=None,
  27. top_p=None,
  28. stream: bool = False,
  29. deployment_name=None,
  30. system_prompt: Optional[str] = None,
  31. ):
  32. """
  33. Initializes the ChatConfig instance.
  34. :param number_documents: Number of documents to pull from the database as
  35. context.
  36. :param template: Optional. The `Template` instance to use as a template for
  37. prompt.
  38. :param model: Optional. Controls the OpenAI model used.
  39. :param temperature: Optional. Controls the randomness of the model's output.
  40. Higher values (closer to 1) make output more random,lower values make it more
  41. deterministic.
  42. :param max_tokens: Optional. Controls how many tokens are generated.
  43. :param top_p: Optional. Controls the diversity of words.Higher values
  44. (closer to 1) make word selection more diverse, lower values make words less
  45. diverse.
  46. :param stream: Optional. Control if response is streamed back to the user
  47. :param deployment_name: t.b.a.
  48. :param system_prompt: Optional. System prompt string.
  49. :raises ValueError: If the template is not valid as template should contain
  50. $context and $query and $history
  51. """
  52. if template is None:
  53. template = DEFAULT_PROMPT_TEMPLATE
  54. # History is set as 0 to ensure that there is always a history, that way,
  55. # there don't have to be two templates. Having two templates would make it
  56. # complicated because the history is not user controlled.
  57. super().__init__(
  58. number_documents=number_documents,
  59. template=template,
  60. model=model,
  61. temperature=temperature,
  62. max_tokens=max_tokens,
  63. top_p=top_p,
  64. history=[0],
  65. stream=stream,
  66. deployment_name=deployment_name,
  67. system_prompt=system_prompt,
  68. )
  69. def set_history(self, history):
  70. """
  71. Chat history is not user provided and not set at initialization time
  72. :param history: (string) history to set
  73. """
  74. self.history = history
  75. return