data_formatter.py 5.4 KB

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