base.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from typing import Any
  2. from embedchain import App
  3. from embedchain.config import AddConfig, AppConfig, LlmConfig
  4. from embedchain.embedder.openai import OpenAIEmbedder
  5. from embedchain.helper.json_serializable import JSONSerializable, register_deserializable
  6. from embedchain.llm.openai import OpenAILlm
  7. from embedchain.vectordb.chroma import ChromaDB
  8. @register_deserializable
  9. class BaseBot(JSONSerializable):
  10. def __init__(self):
  11. self.app = App(config=AppConfig(), llm=OpenAILlm(), db=ChromaDB(), embedder=OpenAIEmbedder())
  12. def add(self, data: Any, config: AddConfig = None):
  13. """
  14. Add data to the bot (to the vector database).
  15. Auto-dectects type only, so some data types might not be usable.
  16. :param data: data to embed
  17. :type data: Any
  18. :param config: configuration class instance, defaults to None
  19. :type config: AddConfig, optional
  20. """
  21. config = config if config else AddConfig()
  22. self.app.add(data, config=config)
  23. def query(self, query: str, config: LlmConfig = None) -> str:
  24. """
  25. Query the bot
  26. :param query: the user query
  27. :type query: str
  28. :param config: configuration class instance, defaults to None
  29. :type config: LlmConfig, optional
  30. :return: Answer
  31. :rtype: str
  32. """
  33. config = config
  34. return self.app.query(query, config=config)
  35. def start(self):
  36. """Start the bot's functionality."""
  37. raise NotImplementedError("Subclasses must implement the start method.")