custom_app.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from typing import Optional
  2. from embedchain.config import CustomAppConfig
  3. from embedchain.embedchain import EmbedChain
  4. from embedchain.embedder.base import BaseEmbedder
  5. from embedchain.helper.json_serializable import register_deserializable
  6. from embedchain.llm.base import BaseLlm
  7. from embedchain.vectordb.base import BaseVectorDB
  8. @register_deserializable
  9. class CustomApp(EmbedChain):
  10. """
  11. Embedchain's custom app allows for most flexibility.
  12. You can craft your own mix of various LLMs, vector databases and embedding model/functions.
  13. Methods:
  14. add(source, data_type): adds the data from the given URL to the vector db.
  15. query(query): finds answer to the given query using vector database and LLM.
  16. chat(query): finds answer to the given query using vector database and LLM, with conversation history.
  17. """
  18. def __init__(
  19. self,
  20. config: Optional[CustomAppConfig] = None,
  21. llm: BaseLlm = None,
  22. db: BaseVectorDB = None,
  23. embedder: BaseEmbedder = None,
  24. system_prompt: Optional[str] = None,
  25. ):
  26. """
  27. Initialize a new `CustomApp` instance. You have to choose a LLM, database and embedder.
  28. :param config: Config for the app instance. This is the most basic configuration,
  29. that does not fall into the LLM, database or embedder category, defaults to None
  30. :type config: Optional[CustomAppConfig], optional
  31. :param llm: LLM Class instance. example: `from embedchain.llm.openai import OpenAILlm`, defaults to None
  32. :type llm: BaseLlm
  33. :param db: The database to use for storing and retrieving embeddings,
  34. example: `from embedchain.vectordb.chroma_db import ChromaDb`, defaults to None
  35. :type db: BaseVectorDB
  36. :param embedder: The embedder (embedding model and function) use to calculate embeddings.
  37. example: `from embedchain.embedder.gpt4all_embedder import GPT4AllEmbedder`, defaults to None
  38. :type embedder: BaseEmbedder
  39. :param system_prompt: System prompt that will be provided to the LLM as such, defaults to None
  40. :type system_prompt: Optional[str], optional
  41. :raises ValueError: LLM, database or embedder has not been defined.
  42. :raises TypeError: LLM, database or embedder is not a valid class instance.
  43. """
  44. # Config is not required, it has a default
  45. if config is None:
  46. config = CustomAppConfig()
  47. if llm is None:
  48. raise ValueError("LLM must be provided for custom app. Please import from `embedchain.llm`.")
  49. if db is None:
  50. raise ValueError("Database must be provided for custom app. Please import from `embedchain.vectordb`.")
  51. if embedder is None:
  52. raise ValueError("Embedder must be provided for custom app. Please import from `embedchain.embedder`.")
  53. if not isinstance(config, CustomAppConfig):
  54. raise TypeError(
  55. "Config is not a `CustomAppConfig` instance. "
  56. "Please make sure the type is right and that you are passing an instance."
  57. )
  58. if not isinstance(llm, BaseLlm):
  59. raise TypeError(
  60. "LLM is not a `BaseLlm` instance. "
  61. "Please make sure the type is right and that you are passing an instance."
  62. )
  63. if not isinstance(db, BaseVectorDB):
  64. raise TypeError(
  65. "Database is not a `BaseVectorDB` instance. "
  66. "Please make sure the type is right and that you are passing an instance."
  67. )
  68. if not isinstance(embedder, BaseEmbedder):
  69. raise TypeError(
  70. "Embedder is not a `BaseEmbedder` instance. "
  71. "Please make sure the type is right and that you are passing an instance."
  72. )
  73. super().__init__(config=config, llm=llm, db=db, embedder=embedder, system_prompt=system_prompt)