diff --git a/glmocr/config.py b/glmocr/config.py index 38fa92c..a121a40 100644 --- a/glmocr/config.py +++ b/glmocr/config.py @@ -430,7 +430,6 @@ def from_env( _KW_MAP = { "api_key": "pipeline.maas.api_key", "api_url": "pipeline.maas.api_url", - "model": "pipeline.maas.model", "mode": "pipeline.maas.enabled", "timeout": "pipeline.maas.request_timeout", "enable_layout": "pipeline.enable_layout", @@ -442,6 +441,15 @@ def from_env( "cuda_visible_devices": "pipeline.layout.cuda_visible_devices", "layout_device": "pipeline.layout.device", } + + # `model` is shared by both MaaS and self-hosted modes. + # Keep MaaS behavior while also forwarding it to OCR API so that + # `GlmOcr(mode="selfhosted", model="...")` works as expected. + if "model" in overrides and overrides["model"] is not None: + model_value = str(overrides["model"]) + _set_nested(data, "pipeline.maas.model", model_value) + _set_nested(data, "pipeline.ocr_api.model", model_value) + for kw, dotted in _KW_MAP.items(): if kw in overrides and overrides[kw] is not None: raw = overrides[kw] diff --git a/glmocr/tests/test_unit.py b/glmocr/tests/test_unit.py index 1771dc6..3f0d5af 100644 --- a/glmocr/tests/test_unit.py +++ b/glmocr/tests/test_unit.py @@ -1353,6 +1353,24 @@ def test_explicit_selfhosted_mode(self, monkeypatch): assert parser._use_maas is False parser.close() + def test_selfhosted_model_kwarg_is_forwarded_to_ocr_api(self, monkeypatch): + """model=... should configure self-hosted OCR request model.""" + from glmocr.config import _ENV_MAP, ENV_PREFIX + + for suffix in _ENV_MAP: + monkeypatch.delenv(f"{ENV_PREFIX}{suffix}", raising=False) + monkeypatch.setattr("glmocr.config._find_dotenv", lambda: None) + + with patch("glmocr.pipeline.Pipeline") as mock_pipeline: + mock_pipeline.return_value.start = MagicMock() + mock_pipeline.return_value.enable_layout = False + from glmocr.api import GlmOcr + + parser = GlmOcr(mode="selfhosted", model="glm-ocr") + assert parser._use_maas is False + assert parser.config_model.pipeline.ocr_api.model == "glm-ocr" + parser.close() + class TestOCRClientOllamaConfig: """Tests for OCRClient initialization with Ollama api_mode."""