data_formatter.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from importlib import import_module
  2. from embedchain.chunkers.base_chunker import BaseChunker
  3. from embedchain.config import AddConfig
  4. from embedchain.config.add_config import ChunkerConfig, LoaderConfig
  5. from embedchain.helper.json_serializable import JSONSerializable
  6. from embedchain.loaders.base_loader import BaseLoader
  7. from embedchain.models.data_type import DataType
  8. class DataFormatter(JSONSerializable):
  9. """
  10. DataFormatter is an internal utility class which abstracts the mapping for
  11. loaders and chunkers to the data_type entered by the user in their
  12. .add or .add_local method call
  13. """
  14. def __init__(self, data_type: DataType, config: AddConfig):
  15. """
  16. Initialize a dataformatter, set data type and chunker based on datatype.
  17. :param data_type: The type of the data to load and chunk.
  18. :type data_type: DataType
  19. :param config: AddConfig instance with nested loader and chunker config attributes.
  20. :type config: AddConfig
  21. """
  22. self.loader = self._get_loader(data_type=data_type, config=config.loader)
  23. self.chunker = self._get_chunker(data_type=data_type, config=config.chunker)
  24. def _lazy_load(self, module_path: str):
  25. module_path, class_name = module_path.rsplit(".", 1)
  26. module = import_module(module_path)
  27. return getattr(module, class_name)
  28. def _get_loader(self, data_type: DataType, config: LoaderConfig) -> BaseLoader:
  29. """
  30. Returns the appropriate data loader for the given data type.
  31. :param data_type: The type of the data to load.
  32. :type data_type: DataType
  33. :param config: Config to initialize the loader with.
  34. :type config: LoaderConfig
  35. :raises ValueError: If an unsupported data type is provided.
  36. :return: The loader for the given data type.
  37. :rtype: BaseLoader
  38. """
  39. loaders = {
  40. DataType.YOUTUBE_VIDEO: "embedchain.loaders.youtube_video.YoutubeVideoLoader",
  41. DataType.PDF_FILE: "embedchain.loaders.pdf_file.PdfFileLoader",
  42. DataType.WEB_PAGE: "embedchain.loaders.web_page.WebPageLoader",
  43. DataType.QNA_PAIR: "embedchain.loaders.local_qna_pair.LocalQnaPairLoader",
  44. DataType.TEXT: "embedchain.loaders.local_text.LocalTextLoader",
  45. DataType.DOCX: "embedchain.loaders.docx_file.DocxFileLoader",
  46. DataType.SITEMAP: "embedchain.loaders.sitemap.SitemapLoader",
  47. DataType.XML: "embedchain.loaders.xml.XmlLoader",
  48. DataType.DOCS_SITE: "embedchain.loaders.docs_site_loader.DocsSiteLoader",
  49. DataType.CSV: "embedchain.loaders.csv.CsvLoader",
  50. DataType.MDX: "embedchain.loaders.mdx.MdxLoader",
  51. DataType.IMAGES: "embedchain.loaders.images.ImagesLoader",
  52. DataType.UNSTRUCTURED: "embedchain.loaders.unstructured_file.UnstructuredLoader",
  53. DataType.JSON: "embedchain.loaders.json.JSONLoader",
  54. DataType.OPENAPI: "embedchain.loaders.openapi.OpenAPILoader",
  55. DataType.GMAIL: "embedchain.loaders.gmail.GmailLoader",
  56. DataType.NOTION: "embedchain.loaders.notion.NotionLoader",
  57. }
  58. if data_type in loaders:
  59. loader_class: type = self._lazy_load(loaders[data_type])
  60. return loader_class()
  61. else:
  62. raise ValueError(f"Unsupported data type: {data_type}")
  63. def _get_chunker(self, data_type: DataType, config: ChunkerConfig) -> BaseChunker:
  64. """Returns the appropriate chunker for the given data type (updated for lazy loading)."""
  65. chunker_classes = {
  66. DataType.YOUTUBE_VIDEO: "embedchain.chunkers.youtube_video.YoutubeVideoChunker",
  67. DataType.PDF_FILE: "embedchain.chunkers.pdf_file.PdfFileChunker",
  68. DataType.WEB_PAGE: "embedchain.chunkers.web_page.WebPageChunker",
  69. DataType.QNA_PAIR: "embedchain.chunkers.qna_pair.QnaPairChunker",
  70. DataType.TEXT: "embedchain.chunkers.text.TextChunker",
  71. DataType.DOCX: "embedchain.chunkers.docx_file.DocxFileChunker",
  72. DataType.SITEMAP: "embedchain.chunkers.sitemap.SitemapChunker",
  73. DataType.XML: "embedchain.chunkers.xml.XmlChunker",
  74. DataType.DOCS_SITE: "embedchain.chunkers.docs_site.DocsSiteChunker",
  75. DataType.CSV: "embedchain.chunkers.table.TableChunker",
  76. DataType.MDX: "embedchain.chunkers.mdx.MdxChunker",
  77. DataType.IMAGES: "embedchain.chunkers.images.ImagesChunker",
  78. DataType.UNSTRUCTURED: "embedchain.chunkers.unstructured_file.UnstructuredFileChunker",
  79. DataType.JSON: "embedchain.chunkers.json.JSONChunker",
  80. DataType.OPENAPI: "embedchain.chunkers.openapi.OpenAPIChunker",
  81. DataType.GMAIL: "embedchain.chunkers.gmail.GmailChunker",
  82. DataType.NOTION: "embedchain.chunkers.notion.NotionChunker",
  83. }
  84. if data_type in chunker_classes:
  85. chunker_class = self._lazy_load(chunker_classes[data_type])
  86. chunker = chunker_class(config)
  87. chunker.set_data_type(data_type)
  88. return chunker
  89. else:
  90. raise ValueError(f"Unsupported data type: {data_type}")