Initial curated RAGcore source from c16667f0c9397e2bfc8dd0255b114fba23c8d05e
This commit is contained in:
commit
7ca726caf2
659 files changed
+98171
No files matched your search
@@ -0,0 +1,170 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from ragcore.infrastructure.models import (
|
||||
ModelForgeRerankingAdapter,
|
||||
RerankDocument,
|
||||
)
|
||||
from ragcore.retrieval.rerank.models import RerankProviderError
|
||||
|
||||
|
||||
def credential(tmp_path: Path) -> Path:
|
||||
path = tmp_path / "reranking-credential"
|
||||
path.write_text("mf_" + "x" * 48, encoding="utf-8")
|
||||
path.chmod(0o600)
|
||||
return path
|
||||
|
||||
|
||||
def response(ids: list[str]) -> dict[str, object]:
|
||||
return {
|
||||
"capability": "rag.reranking@1",
|
||||
"results": [
|
||||
{"id": identifier, "score": 1.0 / rank, "rank": rank}
|
||||
for rank, identifier in enumerate(ids, 1)
|
||||
],
|
||||
"request_id": str(uuid4()),
|
||||
"execution": {
|
||||
"cold": False,
|
||||
"node": "Tower",
|
||||
"residency": "warm",
|
||||
"load_count": 1,
|
||||
"timings": {
|
||||
"gateway_ms": 1,
|
||||
"queue_ms": 0,
|
||||
"load_ms": 0,
|
||||
"inference_ms": 2,
|
||||
"total_ms": 3,
|
||||
},
|
||||
},
|
||||
"usage": {"input_count": len(ids), "input_tokens": 10},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_scoped_capability_request_and_ranked_id_validation(tmp_path: Path) -> None:
|
||||
seen: list[httpx.Request] = []
|
||||
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
seen.append(request)
|
||||
body = json.loads(request.content)
|
||||
return httpx.Response(200, json=response([body["documents"][1]["id"]]))
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
adapter = ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="http://modelforge:8000",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
ranked, evidence = await adapter.rerank(
|
||||
"which passage?",
|
||||
(RerankDocument("one", "irrelevant"), RerankDocument("two", "relevant")),
|
||||
top_n=1,
|
||||
)
|
||||
assert [(item.id, item.rank) for item in ranked] == [("two", 1)]
|
||||
assert seen[0].url.path == "/api/v1/capabilities/rag.reranking@1/invoke"
|
||||
assert seen[0].headers["authorization"].startswith("Bearer mf_")
|
||||
assert seen[0].headers["x-correlation-id"].startswith("ragcore-rerank-")
|
||||
assert evidence.gateway_round_trip_ms >= 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("status", [401, 403])
|
||||
async def test_scope_or_auth_rejection_fails_closed(tmp_path: Path, status: int) -> None:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.MockTransport(lambda _request: httpx.Response(status))
|
||||
) as client:
|
||||
adapter = ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="http://127.0.0.1:8000",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
with pytest.raises(RerankProviderError, match="scope_rejected"):
|
||||
await adapter.rerank("query", (RerankDocument("one", "text"),), top_n=1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("failure", ["duplicate", "unknown", "rank", "nan"])
|
||||
async def test_invalid_ranking_response_fails_closed(tmp_path: Path, failure: str) -> None:
|
||||
body = response(["one", "two"])
|
||||
results = body["results"]
|
||||
assert isinstance(results, list)
|
||||
if failure == "duplicate":
|
||||
results[1]["id"] = "one"
|
||||
elif failure == "unknown":
|
||||
results[1]["id"] = "three"
|
||||
elif failure == "rank":
|
||||
results[1]["rank"] = 1
|
||||
else:
|
||||
results[1]["score"] = float("nan")
|
||||
|
||||
def handler(_request: httpx.Request) -> httpx.Response:
|
||||
return httpx.Response(
|
||||
200,
|
||||
content=json.dumps(body, allow_nan=True).encode(),
|
||||
headers={"content-type": "application/json"},
|
||||
)
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
adapter = ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="http://127.0.0.1:8000",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
with pytest.raises(RerankProviderError, match="result_invalid"):
|
||||
await adapter.rerank(
|
||||
"query",
|
||||
(RerankDocument("one", "a"), RerankDocument("two", "b")),
|
||||
top_n=2,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_input_bounds_apply_before_network(tmp_path: Path) -> None:
|
||||
calls = 0
|
||||
|
||||
def handler(_request: httpx.Request) -> httpx.Response:
|
||||
nonlocal calls
|
||||
calls += 1
|
||||
return httpx.Response(500)
|
||||
|
||||
async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
|
||||
adapter = ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="http://127.0.0.1:8000",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
with pytest.raises(ValueError, match="document count"):
|
||||
await adapter.rerank(
|
||||
"query", tuple(RerankDocument(str(index), "text") for index in range(41))
|
||||
)
|
||||
with pytest.raises(ValueError, match="unique"):
|
||||
await adapter.rerank(
|
||||
"query",
|
||||
(RerankDocument("same", "a"), RerankDocument("same", "b")),
|
||||
top_n=2,
|
||||
)
|
||||
assert calls == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_remote_host_and_redirecting_client_are_rejected(tmp_path: Path) -> None:
|
||||
async with httpx.AsyncClient() as client:
|
||||
with pytest.raises(ValueError, match="local/private"):
|
||||
ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="https://api.example.com",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
async with httpx.AsyncClient(follow_redirects=True) as client:
|
||||
with pytest.raises(ValueError, match="redirects"):
|
||||
ModelForgeRerankingAdapter(
|
||||
client,
|
||||
base_url="http://127.0.0.1:8000",
|
||||
credential_file=credential(tmp_path),
|
||||
)
|
||||
Reference in new issue
Block a user