mirror of
https://github.com/hwchase17/langchain.git
synced 2026-01-29 21:30:18 +00:00
Add MimeType based parser (#4376)
# Add MimeType Based Parser This PR adds a MimeType Based Parser. The parser inspects the mime-type of the blob it is parsing and based on the mime-type can delegate to the sub parser. ## Before submitting Waiting on adding notebooks until more implementations are landed. ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 @vowelparrot
This commit is contained in:
95
tests/unit_tests/document_loader/parsers/test_generic.py
Normal file
95
tests/unit_tests/document_loader/parsers/test_generic.py
Normal file
@@ -0,0 +1,95 @@
|
||||
"""Module to test generic parsers."""
|
||||
|
||||
from typing import Iterator
|
||||
|
||||
import pytest
|
||||
|
||||
from langchain.document_loaders.base import BaseBlobParser
|
||||
from langchain.document_loaders.blob_loaders import Blob
|
||||
from langchain.document_loaders.parsers.generic import MimeTypeBasedParser
|
||||
from langchain.schema import Document
|
||||
|
||||
|
||||
class TestMimeBasedParser:
|
||||
"""Test mime based parser."""
|
||||
|
||||
def test_without_fallback_parser(self) -> None:
|
||||
class FirstCharParser(BaseBlobParser):
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Extract the first character of a blob."""
|
||||
yield Document(page_content=blob.as_string()[0])
|
||||
|
||||
class SecondCharParser(BaseBlobParser):
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Extract the second character of a blob."""
|
||||
yield Document(page_content=blob.as_string()[1])
|
||||
|
||||
parser = MimeTypeBasedParser(
|
||||
handlers={
|
||||
"text/plain": FirstCharParser(),
|
||||
"text/html": SecondCharParser(),
|
||||
},
|
||||
)
|
||||
|
||||
blob = Blob(data=b"Hello World", mimetype="text/plain")
|
||||
docs = parser.parse(blob)
|
||||
assert len(docs) == 1
|
||||
doc = docs[0]
|
||||
assert doc.page_content == "H"
|
||||
|
||||
# Check text/html handler.
|
||||
blob = Blob(data=b"Hello World", mimetype="text/html")
|
||||
docs = parser.parse(blob)
|
||||
assert len(docs) == 1
|
||||
doc = docs[0]
|
||||
assert doc.page_content == "e"
|
||||
|
||||
blob = Blob(data=b"Hello World", mimetype="text/csv")
|
||||
|
||||
with pytest.raises(ValueError, match="Unsupported mime type"):
|
||||
# Check that the fallback parser is used when the mimetype is not found.
|
||||
parser.parse(blob)
|
||||
|
||||
def test_with_fallback_parser(self) -> None:
|
||||
class FirstCharParser(BaseBlobParser):
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Extract the first character of a blob."""
|
||||
yield Document(page_content=blob.as_string()[0])
|
||||
|
||||
class SecondCharParser(BaseBlobParser):
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Extract the second character of a blob."""
|
||||
yield Document(page_content=blob.as_string()[1])
|
||||
|
||||
class ThirdCharParser(BaseBlobParser):
|
||||
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
|
||||
"""Extract the third character of a blob."""
|
||||
yield Document(page_content=blob.as_string()[2])
|
||||
|
||||
parser = MimeTypeBasedParser(
|
||||
handlers={
|
||||
"text/plain": FirstCharParser(),
|
||||
"text/html": SecondCharParser(),
|
||||
},
|
||||
fallback_parser=ThirdCharParser(),
|
||||
)
|
||||
|
||||
blob = Blob(data=b"Hello World", mimetype="text/plain")
|
||||
docs = parser.parse(blob)
|
||||
assert len(docs) == 1
|
||||
doc = docs[0]
|
||||
assert doc.page_content == "H"
|
||||
|
||||
# Check text/html handler.
|
||||
blob = Blob(data=b"Hello World", mimetype="text/html")
|
||||
docs = parser.parse(blob)
|
||||
assert len(docs) == 1
|
||||
doc = docs[0]
|
||||
assert doc.page_content == "e"
|
||||
|
||||
# Check that the fallback parser is used when the mimetype is not found.
|
||||
blob = Blob(data=b"Hello World", mimetype="text/csv")
|
||||
docs = parser.parse(blob)
|
||||
assert len(docs) == 1
|
||||
doc = docs[0]
|
||||
assert doc.page_content == "l"
|
||||
Reference in New Issue
Block a user