data_formatter.py 5.6 KB

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