base.py 1.6 KB

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