diff --git a/deeptutor/services/rag/pipelines/pageindex/client.py b/deeptutor/services/rag/pipelines/pageindex/client.py index 37b105709..ab8019137 100644 --- a/deeptutor/services/rag/pipelines/pageindex/client.py +++ b/deeptutor/services/rag/pipelines/pageindex/client.py @@ -30,19 +30,25 @@ def _prefixed_model(prefix: str, model: str) -> str: def resolve_oss_sdk_config() -> tuple[str, dict[str, Any]]: - """Translate DeepTutor's active LLM into PageIndex's indexing lane.""" - from deeptutor.services.config import resolve_llm_runtime_config + """Translate DeepTutor's indexing model into PageIndex's indexing lane. - cfg = resolve_llm_runtime_config() + Indexing is background work, so it runs on the Task model when one is + configured and otherwise on the active chat LLM. + """ + from deeptutor.services.config import resolve_llm_runtime_config + from deeptutor.services.model_selection.tasks import TASK_SERVICE, task_service_configured + + if task_service_configured(): + cfg = resolve_llm_runtime_config(service_name=TASK_SERVICE) + else: + cfg = resolve_llm_runtime_config() model = str(getattr(cfg, "model", "") or "").strip() binding = str( getattr(cfg, "binding", None) or getattr(cfg, "provider_name", None) or "openai" ).strip() spec = find_by_name(binding) if not model: - raise RuntimeError( - "PageIndex OSS needs an active LLM. Configure one under Settings → Catalog." - ) + raise RuntimeError("PageIndex OSS needs an LLM. Configure one under Settings → Catalog.") if ( spec is None or spec.is_oauth @@ -55,7 +61,7 @@ def resolve_oss_sdk_config() -> tuple[str, dict[str, Any]]: ): raise RuntimeError( "PageIndex OSS indexing needs an API-key or local LLM profile; " - "the active OAuth-only provider cannot be used." + "an OAuth-only provider cannot be used." ) resolved_model = strip_provider_prefix(model, spec) @@ -81,6 +87,9 @@ def resolve_oss_sdk_config() -> tuple[str, dict[str, Any]]: elif api_key: backend["api_key"] = api_key + if spec.backend == "anthropic": + # litellm appends /v1/messages itself; a base ending in /v1 would 404. + base_url = base_url.rstrip("/").removesuffix("/v1") if base_url: backend["api_base"] = base_url if api_version and spec.backend != "openai_compat": diff --git a/tests/services/rag/test_pageindex_oss_config.py b/tests/services/rag/test_pageindex_oss_config.py new file mode 100644 index 000000000..5aa6b1710 --- /dev/null +++ b/tests/services/rag/test_pageindex_oss_config.py @@ -0,0 +1,107 @@ +"""PageIndex OSS indexing follows the background-task model, not the chat model.""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +from deeptutor.services.config import provider_runtime +from deeptutor.services.config.model_catalog import ModelCatalogService +from deeptutor.services.model_selection import tasks +from deeptutor.services.rag.pipelines.pageindex.client import resolve_oss_sdk_config + + +def _codex_llm() -> dict: + return { + "active_profile_id": "codex", + "active_model_id": "codex-model", + "profiles": [ + { + "id": "codex", + "name": "OpenAI Codex", + "binding": "openai_codex", + "models": [{"id": "codex-model", "model": "gpt-6-luna"}], + } + ], + } + + +def _anthropic_profile(model: str) -> dict: + return { + "id": "claude", + "name": "Anthropic", + "binding": "anthropic", + "api_key": "sk-test", + "models": [{"id": "claude-model", "model": model}], + } + + +@pytest.fixture +def catalog(tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + service = ModelCatalogService(path=tmp_path / "model_catalog.json") + monkeypatch.setattr(provider_runtime, "get_model_catalog_service", lambda: service) + monkeypatch.setattr(tasks, "get_model_catalog_service", lambda: service) + + def save(**services: dict) -> None: + data = service.load() + data["services"].update(services) + service.save(data) + + return save + + +def test_task_model_indexes_while_chat_stays_on_oauth(catalog) -> None: + llm = _codex_llm() + llm["profiles"].append(_anthropic_profile("claude-haiku")) + catalog( + llm=llm, + task={ + "mode": "reference", + "selection": {"profile_id": "claude", "model_id": "claude-model"}, + "active_profile_id": None, + "active_model_id": None, + "profiles": [], + }, + ) + + model, backend = resolve_oss_sdk_config() + + assert model == "anthropic/claude-haiku" + assert backend["api_key"] == "sk-test" + + +def test_inherited_task_model_still_rejects_an_oauth_chat_model(catalog) -> None: + catalog(llm=_codex_llm()) + + with pytest.raises(RuntimeError, match="OAuth-only"): + resolve_oss_sdk_config() + + +def test_inherited_task_model_uses_the_api_key_chat_model(catalog) -> None: + llm = { + "active_profile_id": "claude", + "active_model_id": "claude-model", + "profiles": [_anthropic_profile("claude-sonnet")], + } + catalog(llm=llm) + + model, _backend = resolve_oss_sdk_config() + + assert model == "anthropic/claude-sonnet" + + +def test_anthropic_base_url_drops_the_version_segment_litellm_appends(catalog) -> None: + profile = _anthropic_profile("claude-haiku") + profile["base_url"] = "https://api.anthropic.com/v1" + catalog( + llm={ + "active_profile_id": "claude", + "active_model_id": "claude-model", + "profiles": [profile], + } + ) + + _model, backend = resolve_oss_sdk_config() + + assert backend["api_base"] == "https://api.anthropic.com"