mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-08 08:28:02 +08:00
fix: honor dataset language across VisionFigureParser paths (#17227)
This commit is contained in:
298
test/unit_test/deepdoc/parser/test_figure_parser.py
Normal file
298
test/unit_test/deepdoc/parser/test_figure_parser.py
Normal file
@@ -0,0 +1,298 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _package(monkeypatch, name):
|
||||
package = ModuleType(name)
|
||||
package.__path__ = []
|
||||
monkeypatch.setitem(sys.modules, name, package)
|
||||
return package
|
||||
|
||||
|
||||
def _module(monkeypatch, name, **attributes):
|
||||
module = ModuleType(name)
|
||||
for key, value in attributes.items():
|
||||
setattr(module, key, value)
|
||||
monkeypatch.setitem(sys.modules, name, module)
|
||||
return module
|
||||
|
||||
|
||||
def _load_figure_parser(monkeypatch):
|
||||
repo_root = Path(__file__).resolve().parents[4]
|
||||
|
||||
for package_name in (
|
||||
"api",
|
||||
"api.db",
|
||||
"api.db.services",
|
||||
"api.db.joint_services",
|
||||
"common",
|
||||
"rag",
|
||||
"rag.app",
|
||||
"rag.prompts",
|
||||
"rag.utils",
|
||||
):
|
||||
_package(monkeypatch, package_name)
|
||||
|
||||
class FakeImage:
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
image_module = _module(monkeypatch, "PIL.Image", Image=FakeImage)
|
||||
pil_module = _package(monkeypatch, "PIL")
|
||||
pil_module.Image = image_module
|
||||
|
||||
_module(
|
||||
monkeypatch,
|
||||
"common.constants",
|
||||
LLMType=SimpleNamespace(VISION="vision"),
|
||||
)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"api.db.services.llm_service",
|
||||
LLMBundle=Mock(),
|
||||
)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"api.db.joint_services.tenant_model_service",
|
||||
get_tenant_default_model_by_type=Mock(),
|
||||
)
|
||||
|
||||
def timeout(*_args, **_kwargs):
|
||||
return lambda function: function
|
||||
|
||||
_module(monkeypatch, "common.connection_utils", timeout=timeout)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"rag.app.picture",
|
||||
vision_llm_chunk=Mock(return_value="description"),
|
||||
)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"rag.prompts.generator",
|
||||
vision_llm_figure_describe_prompt=Mock(return_value="prompt"),
|
||||
vision_llm_figure_describe_prompt_with_context=Mock(return_value="prompt"),
|
||||
)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"rag.nlp",
|
||||
append_context2table_image4pdf=Mock(return_value=[]),
|
||||
)
|
||||
_module(
|
||||
monkeypatch,
|
||||
"rag.utils.lazy_image",
|
||||
ensure_pil_image=lambda image: image,
|
||||
open_image_for_processing=lambda image, **_kwargs: (image, False),
|
||||
is_image_like=lambda _image: True,
|
||||
)
|
||||
|
||||
module_path = repo_root / "deepdoc" / "parser" / "figure_parser.py"
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"test_figure_parser_module",
|
||||
module_path,
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
monkeypatch.setitem(sys.modules, spec.name, module)
|
||||
spec.loader.exec_module(module)
|
||||
return module, FakeImage
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
("context_above", "context_below", "prompt_name", "expected_arguments"),
|
||||
[
|
||||
(
|
||||
"",
|
||||
"",
|
||||
"vision_llm_figure_describe_prompt",
|
||||
{},
|
||||
),
|
||||
(
|
||||
"Above ",
|
||||
"Below",
|
||||
"vision_llm_figure_describe_prompt_with_context",
|
||||
{
|
||||
"context_above": "Above Caption",
|
||||
"context_below": "Below",
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("language", "expected_language"),
|
||||
[
|
||||
("Chinese", "Chinese"),
|
||||
("", "English"),
|
||||
],
|
||||
)
|
||||
def test_docx_wrapper_passes_dataset_language_to_vision_model_and_prompt(
|
||||
monkeypatch,
|
||||
context_above,
|
||||
context_below,
|
||||
prompt_name,
|
||||
expected_arguments,
|
||||
language,
|
||||
expected_language,
|
||||
):
|
||||
module, FakeImage = _load_figure_parser(monkeypatch)
|
||||
model_config = {"llm_name": "vision-model"}
|
||||
vision_model = object()
|
||||
|
||||
module.get_tenant_default_model_by_type = Mock(return_value=model_config)
|
||||
module.LLMBundle = Mock(return_value=vision_model)
|
||||
module.picture_vision_llm_chunk = Mock(return_value="description")
|
||||
|
||||
default_prompt = Mock(return_value="prompt")
|
||||
contextual_prompt = Mock(return_value="prompt")
|
||||
module.vision_llm_figure_describe_prompt = default_prompt
|
||||
module.vision_llm_figure_describe_prompt_with_context = contextual_prompt
|
||||
|
||||
chunks = [
|
||||
{
|
||||
"image": FakeImage(),
|
||||
"text": "Caption",
|
||||
"context_above": context_above,
|
||||
"context_below": context_below,
|
||||
}
|
||||
]
|
||||
|
||||
module.vision_figure_parser_docx_wrapper_naive(
|
||||
chunks=chunks,
|
||||
idx_lst=[0],
|
||||
callback=lambda *_args, **_kwargs: None,
|
||||
tenant_id="tenant-id",
|
||||
lang=language,
|
||||
)
|
||||
|
||||
module.LLMBundle.assert_called_once_with(
|
||||
"tenant-id",
|
||||
model_config,
|
||||
lang=expected_language,
|
||||
)
|
||||
|
||||
selected_prompt = getattr(module, prompt_name)
|
||||
selected_prompt.assert_called_once_with(
|
||||
**expected_arguments,
|
||||
language=expected_language,
|
||||
)
|
||||
assert chunks[0]["text"].endswith("description")
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
("language", "expected_language"),
|
||||
[
|
||||
("Chinese", "Chinese"),
|
||||
("", "English"),
|
||||
],
|
||||
)
|
||||
def test_vision_figure_parser_passes_dataset_language_to_prompt(
|
||||
monkeypatch,
|
||||
language,
|
||||
expected_language,
|
||||
):
|
||||
module, FakeImage = _load_figure_parser(monkeypatch)
|
||||
prompt = Mock(return_value="prompt")
|
||||
module.vision_llm_figure_describe_prompt = prompt
|
||||
module.picture_vision_llm_chunk = Mock(return_value="description")
|
||||
|
||||
parser = module.VisionFigureParser(
|
||||
vision_model=object(),
|
||||
figures_data=[(FakeImage(), ["caption"])],
|
||||
lang=language,
|
||||
)
|
||||
|
||||
parser(callback=lambda *_args, **_kwargs: None)
|
||||
|
||||
prompt.assert_called_once_with(language=expected_language)
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
"wrapper_name",
|
||||
[
|
||||
"vision_figure_parser_docx_wrapper",
|
||||
"vision_figure_parser_figure_xlsx_wrapper",
|
||||
"vision_figure_parser_pdf_wrapper",
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("language", "expected_language"),
|
||||
[
|
||||
("Chinese", "Chinese"),
|
||||
("", "English"),
|
||||
],
|
||||
)
|
||||
def test_figure_wrappers_pass_dataset_language_to_model_and_parser(
|
||||
monkeypatch,
|
||||
wrapper_name,
|
||||
language,
|
||||
expected_language,
|
||||
):
|
||||
module, FakeImage = _load_figure_parser(monkeypatch)
|
||||
model_config = {"llm_name": "vision-model"}
|
||||
vision_model = object()
|
||||
parser_instance = Mock(return_value=[])
|
||||
|
||||
module.get_tenant_default_model_by_type = Mock(return_value=model_config)
|
||||
module.LLMBundle = Mock(return_value=vision_model)
|
||||
module.VisionFigureParser = Mock(return_value=parser_instance)
|
||||
|
||||
if wrapper_name == "vision_figure_parser_docx_wrapper":
|
||||
arguments = {
|
||||
"sections": [("caption", FakeImage())],
|
||||
"tbls": [],
|
||||
}
|
||||
elif wrapper_name == "vision_figure_parser_figure_xlsx_wrapper":
|
||||
arguments = {
|
||||
"images": [
|
||||
{
|
||||
"image": FakeImage(),
|
||||
"image_description": "caption",
|
||||
}
|
||||
],
|
||||
}
|
||||
else:
|
||||
arguments = {
|
||||
"tbls": [
|
||||
(
|
||||
(FakeImage(), ["caption"]),
|
||||
[(0, 0, 0, 0, 0)],
|
||||
)
|
||||
],
|
||||
"sections": [],
|
||||
}
|
||||
|
||||
getattr(module, wrapper_name)(
|
||||
**arguments,
|
||||
callback=lambda *_args, **_kwargs: None,
|
||||
tenant_id="tenant-id",
|
||||
lang=language,
|
||||
)
|
||||
|
||||
module.LLMBundle.assert_called_once_with(
|
||||
"tenant-id",
|
||||
model_config,
|
||||
lang=expected_language,
|
||||
)
|
||||
assert module.VisionFigureParser.call_args.kwargs["lang"] == expected_language
|
||||
parser_instance.assert_called_once()
|
||||
50
test/unit_test/rag/app/test_one.py
Normal file
50
test/unit_test/rag/app/test_one.py
Normal file
@@ -0,0 +1,50 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import logging
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from rag.app import one
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_docx_chunk_forwards_language_to_vision_wrapper(monkeypatch, caplog):
|
||||
docx_parser = Mock(return_value=[("caption", object(), None)])
|
||||
monkeypatch.setattr(one.naive, "Docx", Mock(return_value=docx_parser))
|
||||
|
||||
vision_wrapper = Mock()
|
||||
monkeypatch.setattr(one, "vision_figure_parser_docx_wrapper_naive", vision_wrapper)
|
||||
monkeypatch.setattr(one.rag_tokenizer, "tokenize", lambda text: text)
|
||||
monkeypatch.setattr(one.rag_tokenizer, "fine_grained_tokenize", lambda text: text)
|
||||
monkeypatch.setattr(one, "tokenize", Mock())
|
||||
|
||||
with caplog.at_level(logging.INFO, logger=one.__name__):
|
||||
one.chunk(
|
||||
"document.docx",
|
||||
binary=b"docx",
|
||||
lang="Japanese",
|
||||
callback=lambda *_args, **_kwargs: None,
|
||||
tenant_id="tenant-id",
|
||||
)
|
||||
|
||||
vision_wrapper.assert_called_once()
|
||||
args = vision_wrapper.call_args.args
|
||||
kwargs = vision_wrapper.call_args.kwargs
|
||||
assert args[1] == [0]
|
||||
assert kwargs["lang"] == "Japanese"
|
||||
assert kwargs["tenant_id"] == "tenant-id"
|
||||
assert "DOCX figure vision enhancement: language=Japanese image_count=1" in caplog.messages
|
||||
100
test/unit_test/rag/app/test_vision_language_callers.py
Normal file
100
test/unit_test/rag/app/test_vision_language_callers.py
Normal file
@@ -0,0 +1,100 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import ast
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from rag.app import naive
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[4]
|
||||
|
||||
|
||||
def _call_name(call):
|
||||
if isinstance(call.func, ast.Name):
|
||||
return call.func.id
|
||||
if isinstance(call.func, ast.Attribute):
|
||||
return call.func.attr
|
||||
return None
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
("relative_path", "expected_call_count"),
|
||||
[
|
||||
("rag/app/book.py", 1),
|
||||
("rag/app/manual.py", 2),
|
||||
("rag/app/naive.py", 2),
|
||||
("rag/app/one.py", 1),
|
||||
("rag/app/paper.py", 1),
|
||||
("rag/app/table.py", 1),
|
||||
],
|
||||
)
|
||||
def test_all_figure_wrapper_callers_forward_language(relative_path, expected_call_count):
|
||||
tree = ast.parse((REPO_ROOT / relative_path).read_text())
|
||||
calls = [node for node in ast.walk(tree) if isinstance(node, ast.Call) and (_call_name(node) or "").startswith("vision_figure_parser_")]
|
||||
|
||||
assert len(calls) == expected_call_count
|
||||
for call in calls:
|
||||
language = next((keyword.value for keyword in call.keywords if keyword.arg == "lang"), None)
|
||||
assert isinstance(language, ast.Name), f"{relative_path}:{call.lineno} does not forward lang"
|
||||
assert language.id == "lang"
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_markdown_chunk_forwards_language_to_model_and_figure_parser(monkeypatch):
|
||||
markdown_parser = Mock(return_value=([("section", "")], [], [object()]))
|
||||
monkeypatch.setattr(naive, "Markdown", Mock(return_value=markdown_parser))
|
||||
monkeypatch.setattr(naive, "get_tenant_default_model_by_type", Mock(return_value={"llm_name": "vision-model"}))
|
||||
|
||||
vision_model = object()
|
||||
llm_bundle = Mock(return_value=vision_model)
|
||||
monkeypatch.setattr(naive, "LLMBundle", llm_bundle)
|
||||
|
||||
parser_instance = Mock(return_value=[((None, "description"), None)])
|
||||
parser_factory = Mock(return_value=parser_instance)
|
||||
monkeypatch.setattr(naive, "VisionFigureParser", parser_factory)
|
||||
|
||||
monkeypatch.setattr(naive.rag_tokenizer, "tokenize", lambda text: text)
|
||||
monkeypatch.setattr(naive.rag_tokenizer, "fine_grained_tokenize", lambda text: text)
|
||||
monkeypatch.setattr(naive, "num_tokens_from_string", lambda _text: 1)
|
||||
monkeypatch.setattr(naive, "tokenize_table", Mock(return_value=[]))
|
||||
monkeypatch.setattr(naive, "tokenize_chunks", Mock(return_value=[]))
|
||||
monkeypatch.setattr(naive, "tokenize_chunks_with_images", Mock(return_value=[]))
|
||||
|
||||
naive.chunk(
|
||||
"document.md",
|
||||
binary=b"markdown",
|
||||
lang="Japanese",
|
||||
callback=lambda *_args, **_kwargs: None,
|
||||
tenant_id="tenant-id",
|
||||
is_root=False,
|
||||
parser_config={
|
||||
"chunk_token_num": 128,
|
||||
"delimiter": "\n",
|
||||
"analyze_hyperlink": False,
|
||||
},
|
||||
)
|
||||
|
||||
llm_bundle.assert_called_once_with(
|
||||
"tenant-id",
|
||||
{"llm_name": "vision-model"},
|
||||
lang="Japanese",
|
||||
)
|
||||
assert parser_factory.call_args.kwargs["vision_model"] is vision_model
|
||||
assert parser_factory.call_args.kwargs["lang"] == "Japanese"
|
||||
parser_instance.assert_called_once()
|
||||
122
test/unit_test/rag/flow/parser/test_vision_language.py
Normal file
122
test/unit_test/rag/flow/parser/test_vision_language.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import ast
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[5]
|
||||
|
||||
|
||||
def _package(monkeypatch, name):
|
||||
package = ModuleType(name)
|
||||
package.__path__ = []
|
||||
monkeypatch.setitem(sys.modules, name, package)
|
||||
return package
|
||||
|
||||
|
||||
def _module(monkeypatch, name, **attributes):
|
||||
module = ModuleType(name)
|
||||
for key, value in attributes.items():
|
||||
setattr(module, key, value)
|
||||
monkeypatch.setitem(sys.modules, name, module)
|
||||
return module
|
||||
|
||||
|
||||
def _load_flow_utils(monkeypatch):
|
||||
for package_name in (
|
||||
"api",
|
||||
"api.db",
|
||||
"api.db.services",
|
||||
"api.db.joint_services",
|
||||
"common",
|
||||
"deepdoc",
|
||||
"deepdoc.parser",
|
||||
"rag",
|
||||
):
|
||||
_package(monkeypatch, package_name)
|
||||
|
||||
_module(monkeypatch, "api.db.services.llm_service", LLMBundle=Mock())
|
||||
_module(
|
||||
monkeypatch,
|
||||
"api.db.joint_services.tenant_model_service",
|
||||
get_tenant_default_model_by_type=Mock(),
|
||||
resolve_model_config=Mock(),
|
||||
)
|
||||
_module(monkeypatch, "common.constants", LLMType=SimpleNamespace(VISION="vision"))
|
||||
_module(monkeypatch, "deepdoc.parser.figure_parser", VisionFigureParser=Mock())
|
||||
_module(
|
||||
monkeypatch,
|
||||
"rag.nlp",
|
||||
is_english=Mock(return_value=False),
|
||||
random_choices=Mock(return_value=[]),
|
||||
remove_contents_table=Mock(),
|
||||
)
|
||||
|
||||
module_path = REPO_ROOT / "rag/flow/parser/utils.py"
|
||||
spec = importlib.util.spec_from_file_location("test_flow_parser_utils_module", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
monkeypatch.setitem(sys.modules, spec.name, module)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
("language", "expected_language"),
|
||||
[
|
||||
("Japanese", "Japanese"),
|
||||
("", "English"),
|
||||
],
|
||||
)
|
||||
def test_media_enhancement_forwards_language_to_model_and_parser(monkeypatch, language, expected_language):
|
||||
utils = _load_flow_utils(monkeypatch)
|
||||
model_config = {"llm_name": "vision-model"}
|
||||
vision_model = object()
|
||||
llm_bundle = Mock(return_value=vision_model)
|
||||
parser_instance = Mock(return_value=[((None, "description"), None)])
|
||||
parser_factory = Mock(return_value=parser_instance)
|
||||
|
||||
monkeypatch.setattr(utils, "resolve_model_config", Mock(return_value=model_config))
|
||||
monkeypatch.setattr(utils, "LLMBundle", llm_bundle)
|
||||
monkeypatch.setattr(utils, "VisionFigureParser", parser_factory)
|
||||
|
||||
sections = [{"text": "caption", "image": object(), "doc_type_kwd": "image"}]
|
||||
result = utils.enhance_media_sections_with_vision(
|
||||
sections,
|
||||
"tenant-id",
|
||||
{"llm_id": "vision-model"},
|
||||
lang=language,
|
||||
)
|
||||
|
||||
llm_bundle.assert_called_once_with("tenant-id", model_config, lang=expected_language)
|
||||
assert parser_factory.call_args.kwargs["vision_model"] is vision_model
|
||||
assert parser_factory.call_args.kwargs["lang"] == expected_language
|
||||
assert result[0]["text"] == "caption\ndescription"
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_all_flow_media_enhancement_callers_forward_language():
|
||||
tree = ast.parse((REPO_ROOT / "rag/flow/parser/parser.py").read_text())
|
||||
calls = [node for node in ast.walk(tree) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "enhance_media_sections_with_vision"]
|
||||
|
||||
assert len(calls) == 3
|
||||
for call in calls:
|
||||
assert any(keyword.arg == "lang" for keyword in call.keywords), f"parser.py:{call.lineno} does not forward lang"
|
||||
117
test/unit_test/rag/prompts/test_vision_figure_prompt.py
Normal file
117
test/unit_test/rag/prompts/test_vision_figure_prompt.py
Normal file
@@ -0,0 +1,117 @@
|
||||
#
|
||||
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _load_generator(monkeypatch):
|
||||
repo_root = Path(__file__).resolve().parents[4]
|
||||
|
||||
json_repair = ModuleType("json_repair")
|
||||
json_repair.repair_json = lambda text, **_kwargs: text
|
||||
monkeypatch.setitem(sys.modules, "json_repair", json_repair)
|
||||
|
||||
common = ModuleType("common")
|
||||
common.__path__ = [str(repo_root / "common")]
|
||||
monkeypatch.setitem(sys.modules, "common", common)
|
||||
|
||||
misc_utils = ModuleType("common.misc_utils")
|
||||
misc_utils.hash_str2int = lambda value, _mod=500: 0
|
||||
monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils)
|
||||
|
||||
constants = ModuleType("common.constants")
|
||||
constants.TAG_FLD = "tag"
|
||||
monkeypatch.setitem(sys.modules, "common.constants", constants)
|
||||
|
||||
token_utils = ModuleType("common.token_utils")
|
||||
token_utils.encoder = SimpleNamespace()
|
||||
token_utils.num_tokens_from_string = len
|
||||
monkeypatch.setitem(sys.modules, "common.token_utils", token_utils)
|
||||
|
||||
rag = ModuleType("rag")
|
||||
rag.__path__ = [str(repo_root / "rag")]
|
||||
monkeypatch.setitem(sys.modules, "rag", rag)
|
||||
|
||||
rag_nlp = ModuleType("rag.nlp")
|
||||
rag_nlp.rag_tokenizer = SimpleNamespace()
|
||||
monkeypatch.setitem(sys.modules, "rag.nlp", rag_nlp)
|
||||
|
||||
prompts = ModuleType("rag.prompts")
|
||||
prompts.__path__ = [str(repo_root / "rag" / "prompts")]
|
||||
monkeypatch.setitem(sys.modules, "rag.prompts", prompts)
|
||||
|
||||
template = ModuleType("rag.prompts.template")
|
||||
template.load_prompt = lambda name: (repo_root / "rag" / "prompts" / f"{name}.md").read_text(encoding="utf-8").strip()
|
||||
monkeypatch.setitem(sys.modules, "rag.prompts.template", template)
|
||||
|
||||
module_path = repo_root / "rag" / "prompts" / "generator.py"
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"test_vision_figure_prompt_generator",
|
||||
module_path,
|
||||
)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
monkeypatch.setitem(sys.modules, spec.name, module)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
("function_name", "arguments", "expected_language"),
|
||||
[
|
||||
(
|
||||
"vision_llm_figure_describe_prompt",
|
||||
{},
|
||||
"English",
|
||||
),
|
||||
(
|
||||
"vision_llm_figure_describe_prompt",
|
||||
{"language": "Chinese"},
|
||||
"Chinese",
|
||||
),
|
||||
(
|
||||
"vision_llm_figure_describe_prompt_with_context",
|
||||
{"context_above": "Above", "context_below": "Below"},
|
||||
"English",
|
||||
),
|
||||
(
|
||||
"vision_llm_figure_describe_prompt_with_context",
|
||||
{
|
||||
"context_above": "Above",
|
||||
"context_below": "Below",
|
||||
"language": "Chinese",
|
||||
},
|
||||
"Chinese",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_figure_prompt_renders_output_language(
|
||||
monkeypatch,
|
||||
function_name,
|
||||
arguments,
|
||||
expected_language,
|
||||
):
|
||||
generator = _load_generator(monkeypatch)
|
||||
|
||||
prompt = getattr(generator, function_name)(**arguments)
|
||||
|
||||
assert f"Write all descriptions and field values in {expected_language}." in prompt
|
||||
assert "Preserve all visible text verbatim in its original language" in prompt
|
||||
assert "{{ language }}" not in prompt
|
||||
Reference in New Issue
Block a user