data_formatter.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from importlib import import_module
  2. from typing import Optional
  3. from embedchain.chunkers.base_chunker import BaseChunker
  4. from embedchain.config import AddConfig
  5. from embedchain.config.add_config import ChunkerConfig, LoaderConfig
  6. from embedchain.helpers.json_serializable import JSONSerializable
  7. from embedchain.loaders.base_loader import BaseLoader
  8. from embedchain.models.data_type import DataType
  9. class DataFormatter(JSONSerializable):
  10. """
  11. DataFormatter is an internal utility class which abstracts the mapping for
  12. loaders and chunkers to the data_type entered by the user in their
  13. .add or .add_local method call
  14. """
  15. def __init__(
  16. self,
  17. data_type: DataType,
  18. config: AddConfig,
  19. loader: Optional[BaseLoader] = None,
  20. chunker: Optional[BaseChunker] = None,
  21. ):
  22. """
  23. Initialize a dataformatter, set data type and chunker based on datatype.
  24. :param data_type: The type of the data to load and chunk.
  25. :type data_type: DataType
  26. :param config: AddConfig instance with nested loader and chunker config attributes.
  27. :type config: AddConfig
  28. """
  29. self.loader = self._get_loader(data_type=data_type, config=config.loader, loader=loader)
  30. self.chunker = self._get_chunker(data_type=data_type, config=config.chunker, chunker=chunker)
  31. def _lazy_load(self, module_path: str):
  32. module_path, class_name = module_path.rsplit(".", 1)
  33. module = import_module(module_path)
  34. return getattr(module, class_name)
  35. def _get_loader(self, data_type: DataType, config: LoaderConfig, loader: Optional[BaseLoader]) -> BaseLoader:
  36. """
  37. Returns the appropriate data loader for the given data type.
  38. :param data_type: The type of the data to load.
  39. :type data_type: DataType
  40. :param config: Config to initialize the loader with.
  41. :type config: LoaderConfig
  42. :raises ValueError: If an unsupported data type is provided.
  43. :return: The loader for the given data type.
  44. :rtype: BaseLoader
  45. """
  46. loaders = {
  47. DataType.YOUTUBE_VIDEO: "embedchain.loaders.youtube_video.YoutubeVideoLoader",
  48. DataType.PDF_FILE: "embedchain.loaders.pdf_file.PdfFileLoader",
  49. DataType.WEB_PAGE: "embedchain.loaders.web_page.WebPageLoader",
  50. DataType.QNA_PAIR: "embedchain.loaders.local_qna_pair.LocalQnaPairLoader",
  51. DataType.TEXT: "embedchain.loaders.local_text.LocalTextLoader",
  52. DataType.DOCX: "embedchain.loaders.docx_file.DocxFileLoader",
  53. DataType.SITEMAP: "embedchain.loaders.sitemap.SitemapLoader",
  54. DataType.XML: "embedchain.loaders.xml.XmlLoader",
  55. DataType.DOCS_SITE: "embedchain.loaders.docs_site_loader.DocsSiteLoader",
  56. DataType.CSV: "embedchain.loaders.csv.CsvLoader",
  57. DataType.MDX: "embedchain.loaders.mdx.MdxLoader",
  58. DataType.IMAGES: "embedchain.loaders.images.ImagesLoader",
  59. DataType.UNSTRUCTURED: "embedchain.loaders.unstructured_file.UnstructuredLoader",
  60. DataType.JSON: "embedchain.loaders.json.JSONLoader",
  61. DataType.OPENAPI: "embedchain.loaders.openapi.OpenAPILoader",
  62. DataType.GMAIL: "embedchain.loaders.gmail.GmailLoader",
  63. DataType.NOTION: "embedchain.loaders.notion.NotionLoader",
  64. DataType.SUBSTACK: "embedchain.loaders.substack.SubstackLoader",
  65. DataType.YOUTUBE_CHANNEL: "embedchain.loaders.youtube_channel.YoutubeChannelLoader",
  66. DataType.DISCORD: "embedchain.loaders.discord.DiscordLoader",
  67. DataType.RSSFEED: "embedchain.loaders.rss_feed.RSSFeedLoader",
  68. DataType.BEEHIIV: "embedchain.loaders.beehiiv.BeehiivLoader",
  69. DataType.DIRECTORY: "embedchain.loaders.directory_loader.DirectoryLoader",
  70. DataType.SLACK: "embedchain.loaders.slack.SlackLoader",
  71. DataType.DROPBOX: "embedchain.loaders.dropbox.DropboxLoader",
  72. DataType.TEXT_FILE: "embedchain.loaders.text_file.TextFileLoader",
  73. }
  74. if data_type == DataType.CUSTOM or loader is not None:
  75. loader_class: type = loader
  76. if loader_class:
  77. return loader_class
  78. elif data_type in loaders:
  79. loader_class: type = self._lazy_load(loaders[data_type])
  80. return loader_class()
  81. raise ValueError(
  82. f"Cant find the loader for {data_type}.\
  83. We recommend to pass the loader to use data_type: {data_type},\
  84. check `https://docs.embedchain.ai/data-sources/overview`."
  85. )
  86. def _get_chunker(self, data_type: DataType, config: ChunkerConfig, chunker: Optional[BaseChunker]) -> BaseChunker:
  87. """Returns the appropriate chunker for the given data type (updated for lazy loading)."""
  88. chunker_classes = {
  89. DataType.YOUTUBE_VIDEO: "embedchain.chunkers.youtube_video.YoutubeVideoChunker",
  90. DataType.PDF_FILE: "embedchain.chunkers.pdf_file.PdfFileChunker",
  91. DataType.WEB_PAGE: "embedchain.chunkers.web_page.WebPageChunker",
  92. DataType.QNA_PAIR: "embedchain.chunkers.qna_pair.QnaPairChunker",
  93. DataType.TEXT: "embedchain.chunkers.text.TextChunker",
  94. DataType.DOCX: "embedchain.chunkers.docx_file.DocxFileChunker",
  95. DataType.SITEMAP: "embedchain.chunkers.sitemap.SitemapChunker",
  96. DataType.XML: "embedchain.chunkers.xml.XmlChunker",
  97. DataType.DOCS_SITE: "embedchain.chunkers.docs_site.DocsSiteChunker",
  98. DataType.CSV: "embedchain.chunkers.table.TableChunker",
  99. DataType.MDX: "embedchain.chunkers.mdx.MdxChunker",
  100. DataType.IMAGES: "embedchain.chunkers.images.ImagesChunker",
  101. DataType.UNSTRUCTURED: "embedchain.chunkers.unstructured_file.UnstructuredFileChunker",
  102. DataType.JSON: "embedchain.chunkers.json.JSONChunker",
  103. DataType.OPENAPI: "embedchain.chunkers.openapi.OpenAPIChunker",
  104. DataType.GMAIL: "embedchain.chunkers.gmail.GmailChunker",
  105. DataType.NOTION: "embedchain.chunkers.notion.NotionChunker",
  106. DataType.SUBSTACK: "embedchain.chunkers.substack.SubstackChunker",
  107. DataType.YOUTUBE_CHANNEL: "embedchain.chunkers.common_chunker.CommonChunker",
  108. DataType.DISCORD: "embedchain.chunkers.common_chunker.CommonChunker",
  109. DataType.CUSTOM: "embedchain.chunkers.common_chunker.CommonChunker",
  110. DataType.RSSFEED: "embedchain.chunkers.rss_feed.RSSFeedChunker",
  111. DataType.BEEHIIV: "embedchain.chunkers.beehiiv.BeehiivChunker",
  112. DataType.DIRECTORY: "embedchain.chunkers.common_chunker.CommonChunker",
  113. DataType.SLACK: "embedchain.chunkers.common_chunker.CommonChunker",
  114. DataType.DROPBOX: "embedchain.chunkers.common_chunker.CommonChunker",
  115. DataType.TEXT_FILE: "embedchain.chunkers.common_chunker.CommonChunker",
  116. }
  117. if chunker is not None:
  118. return chunker
  119. elif data_type in chunker_classes:
  120. chunker_class = self._lazy_load(chunker_classes[data_type])
  121. chunker = chunker_class(config)
  122. chunker.set_data_type(data_type)
  123. return chunker
  124. raise ValueError(
  125. f"Cant find the chunker for {data_type}.\
  126. We recommend to pass the chunker to use data_type: {data_type},\
  127. check `https://docs.embedchain.ai/data-sources/overview`."
  128. )