data_formatter.py 5.2 KB

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