mirror of
https://github.com/langgenius/dify.git
synced 2026-09-04 16:07:08 +08:00
feat: support ODT document extraction (#39973)
Co-authored-by: Crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
This commit is contained in:
parent
1bb9183af9
commit
ccfb47d2c5
@ -36,6 +36,7 @@ _UNSTRUCTURED_DOCUMENT_EXTENSION_BASE: frozenset[str] = frozenset(
|
||||
"pptx",
|
||||
"xml",
|
||||
"epub",
|
||||
"odt",
|
||||
)
|
||||
)
|
||||
_DEFAULT_DOCUMENT_EXTENSION_BASE: frozenset[str] = frozenset(
|
||||
@ -53,6 +54,7 @@ _DEFAULT_DOCUMENT_EXTENSION_BASE: frozenset[str] = frozenset(
|
||||
"csv",
|
||||
"vtt",
|
||||
"properties",
|
||||
"odt",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@ -441,6 +441,48 @@ def test_extract_text_from_excel_numeric_type_column(mock_excel_file):
|
||||
assert expected_manual == result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("extension", "mime_type", "route_label"),
|
||||
[
|
||||
(".odt", "text/plain", "extension"),
|
||||
(None, "application/vnd.oasis.opendocument.text", "mime_type"),
|
||||
],
|
||||
)
|
||||
def test_extract_text_from_file_routes_odt_inputs_to_graphon_odt_extractor(
|
||||
document_extractor_node,
|
||||
extension,
|
||||
mime_type,
|
||||
route_label,
|
||||
):
|
||||
file = Mock(spec=File)
|
||||
file.extension = extension
|
||||
file.mime_type = mime_type
|
||||
|
||||
def fake_partition(file_content, *, suffix, unstructured_api_config, load_local_partition, render_element):
|
||||
assert file_content == b"odt content"
|
||||
assert suffix == ".odt"
|
||||
assert unstructured_api_config == document_extractor_node._unstructured_api_config
|
||||
assert load_local_partition.__name__ == "_load_partition_odt"
|
||||
assert render_element is not None
|
||||
return f"extracted through {route_label}"
|
||||
|
||||
with (
|
||||
patch(
|
||||
"graphon.nodes.document_extractor.node._download_file_content",
|
||||
return_value=b"odt content",
|
||||
) as mock_download,
|
||||
patch("graphon.nodes.document_extractor.node._partition_unstructured_file", side_effect=fake_partition),
|
||||
):
|
||||
text = _extract_text_from_file(
|
||||
document_extractor_node.http_client,
|
||||
file,
|
||||
unstructured_api_config=document_extractor_node._unstructured_api_config,
|
||||
)
|
||||
|
||||
assert text == f"extracted through {route_label}"
|
||||
mock_download.assert_called_once_with(document_extractor_node.http_client, file)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("extension", "mime_type"),
|
||||
[
|
||||
|
||||
25
api/tests/unit_tests/test_constants.py
Normal file
25
api/tests/unit_tests/test_constants.py
Normal file
@ -0,0 +1,25 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
|
||||
import constants
|
||||
from configs import dify_config
|
||||
|
||||
|
||||
@pytest.mark.parametrize("etl_type", ["SelfHosted", "Unstructured"])
|
||||
def test_document_extensions_include_odt_for_document_etl_modes(monkeypatch: pytest.MonkeyPatch, etl_type: str) -> None:
|
||||
original_etl_type = dify_config.ETL_TYPE
|
||||
original_unstructured_api_url = dify_config.UNSTRUCTURED_API_URL
|
||||
|
||||
try:
|
||||
monkeypatch.setattr(dify_config, "ETL_TYPE", etl_type)
|
||||
monkeypatch.setattr(dify_config, "UNSTRUCTURED_API_URL", None)
|
||||
|
||||
reloaded_constants = importlib.reload(constants)
|
||||
|
||||
assert "odt" in reloaded_constants.DOCUMENT_EXTENSIONS
|
||||
assert "ODT" in reloaded_constants.DOCUMENT_EXTENSIONS
|
||||
finally:
|
||||
monkeypatch.setattr(dify_config, "ETL_TYPE", original_etl_type)
|
||||
monkeypatch.setattr(dify_config, "UNSTRUCTURED_API_URL", original_unstructured_api_url)
|
||||
importlib.reload(constants)
|
||||
Loading…
Reference in New Issue
Block a user