data_formatter.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.IMAGE: "embedchain.loaders.image.ImageLoader",
  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.GOOGLE_DRIVE: "embedchain.loaders.google_drive.GoogleDriveLoader",
  70. DataType.DIRECTORY: "embedchain.loaders.directory_loader.DirectoryLoader",
  71. DataType.SLACK: "embedchain.loaders.slack.SlackLoader",
  72. DataType.DROPBOX: "embedchain.loaders.dropbox.DropboxLoader",
  73. DataType.TEXT_FILE: "embedchain.loaders.text_file.TextFileLoader",
  74. }
  75. if data_type == DataType.CUSTOM or loader is not None:
  76. loader_class: type = loader
  77. if loader_class:
  78. return loader_class
  79. elif data_type in loaders:
  80. loader_class: type = self._lazy_load(loaders[data_type])
  81. return loader_class()
  82. raise ValueError(
  83. f"Cant find the loader for {data_type}.\
  84. We recommend to pass the loader to use data_type: {data_type},\
  85. check `https://docs.embedchain.ai/data-sources/overview`."
  86. )
  87. def _get_chunker(self, data_type: DataType, config: ChunkerConfig, chunker: Optional[BaseChunker]) -> BaseChunker:
  88. """Returns the appropriate chunker for the given data type (updated for lazy loading)."""
  89. chunker_classes = {
  90. DataType.YOUTUBE_VIDEO: "embedchain.chunkers.youtube_video.YoutubeVideoChunker",
  91. DataType.PDF_FILE: "embedchain.chunkers.pdf_file.PdfFileChunker",
  92. DataType.WEB_PAGE: "embedchain.chunkers.web_page.WebPageChunker",
  93. DataType.QNA_PAIR: "embedchain.chunkers.qna_pair.QnaPairChunker",
  94. DataType.TEXT: "embedchain.chunkers.text.TextChunker",
  95. DataType.DOCX: "embedchain.chunkers.docx_file.DocxFileChunker",
  96. DataType.SITEMAP: "embedchain.chunkers.sitemap.SitemapChunker",
  97. DataType.XML: "embedchain.chunkers.xml.XmlChunker",
  98. DataType.DOCS_SITE: "embedchain.chunkers.docs_site.DocsSiteChunker",
  99. DataType.CSV: "embedchain.chunkers.table.TableChunker",
  100. DataType.MDX: "embedchain.chunkers.mdx.MdxChunker",
  101. DataType.IMAGE: "embedchain.chunkers.image.ImageChunker",
  102. DataType.UNSTRUCTURED: "embedchain.chunkers.unstructured_file.UnstructuredFileChunker",
  103. DataType.JSON: "embedchain.chunkers.json.JSONChunker",
  104. DataType.OPENAPI: "embedchain.chunkers.openapi.OpenAPIChunker",
  105. DataType.GMAIL: "embedchain.chunkers.gmail.GmailChunker",
  106. DataType.NOTION: "embedchain.chunkers.notion.NotionChunker",
  107. DataType.SUBSTACK: "embedchain.chunkers.substack.SubstackChunker",
  108. DataType.YOUTUBE_CHANNEL: "embedchain.chunkers.common_chunker.CommonChunker",
  109. DataType.DISCORD: "embedchain.chunkers.common_chunker.CommonChunker",
  110. DataType.CUSTOM: "embedchain.chunkers.common_chunker.CommonChunker",
  111. DataType.RSSFEED: "embedchain.chunkers.rss_feed.RSSFeedChunker",
  112. DataType.BEEHIIV: "embedchain.chunkers.beehiiv.BeehiivChunker",
  113. DataType.GOOGLE_DRIVE: "embedchain.chunkers.google_drive.GoogleDriveChunker",
  114. DataType.DIRECTORY: "embedchain.chunkers.common_chunker.CommonChunker",
  115. DataType.SLACK: "embedchain.chunkers.common_chunker.CommonChunker",
  116. DataType.DROPBOX: "embedchain.chunkers.common_chunker.CommonChunker",
  117. DataType.TEXT_FILE: "embedchain.chunkers.common_chunker.CommonChunker",
  118. }
  119. if chunker is not None:
  120. return chunker
  121. elif data_type in chunker_classes:
  122. chunker_class = self._lazy_load(chunker_classes[data_type])
  123. chunker = chunker_class(config)
  124. chunker.set_data_type(data_type)
  125. return chunker
  126. raise ValueError(
  127. f"Cant find the chunker for {data_type}.\
  128. We recommend to pass the chunker to use data_type: {data_type},\
  129. check `https://docs.embedchain.ai/data-sources/overview`."
  130. )