data_formatter.py 4.9 KB

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