CustomApp.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from typing import Optional
  2. from embedchain.config import CustomAppConfig
  3. from embedchain.embedchain import EmbedChain
  4. from embedchain.embedder.base_embedder import BaseEmbedder
  5. from embedchain.helper_classes.json_serializable import register_deserializable
  6. from embedchain.llm.base_llm import BaseLlm
  7. from embedchain.vectordb.base_vector_db import BaseVectorDB
  8. @register_deserializable
  9. class CustomApp(EmbedChain):
  10. """
  11. The custom EmbedChain app.
  12. Has two functions: add and query.
  13. adds(data_type, url): adds the data from the given URL to the vector db.
  14. query(query): finds answer to the given query using vector database and LLM.
  15. dry_run(query): test your prompt without consuming tokens.
  16. """
  17. def __init__(
  18. self,
  19. config: CustomAppConfig = None,
  20. llm: BaseLlm = None,
  21. db: BaseVectorDB = None,
  22. embedder: BaseEmbedder = None,
  23. system_prompt: Optional[str] = None,
  24. ):
  25. """
  26. :param config: Optional. `CustomAppConfig` instance to load as configuration.
  27. :raises ValueError: Config must be provided for custom app
  28. :param system_prompt: Optional. System prompt string.
  29. """
  30. # Config is not required, it has a default
  31. if config is None:
  32. config = CustomAppConfig()
  33. if llm is None:
  34. raise ValueError("LLM must be provided for custom app. Please import from `embedchain.llm`.")
  35. if db is None:
  36. raise ValueError("Database must be provided for custom app. Please import from `embedchain.vectordb`.")
  37. if embedder is None:
  38. raise ValueError("Embedder must be provided for custom app. Please import from `embedchain.embedder`.")
  39. if not isinstance(config, CustomAppConfig):
  40. raise TypeError(
  41. "Config is not a `CustomAppConfig` instance. "
  42. "Please make sure the type is right and that you are passing an instance."
  43. )
  44. if not isinstance(llm, BaseLlm):
  45. raise TypeError(
  46. "LLM is not a `BaseLlm` instance. "
  47. "Please make sure the type is right and that you are passing an instance."
  48. )
  49. if not isinstance(db, BaseVectorDB):
  50. raise TypeError(
  51. "Database is not a `BaseVectorDB` instance. "
  52. "Please make sure the type is right and that you are passing an instance."
  53. )
  54. if not isinstance(embedder, BaseEmbedder):
  55. raise TypeError(
  56. "Embedder is not a `BaseEmbedder` instance. "
  57. "Please make sure the type is right and that you are passing an instance."
  58. )
  59. super().__init__(config=config, llm=llm, db=db, embedder=embedder, system_prompt=system_prompt)