data_formatter.py 5.0 KB

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