Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/model_verifier/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def target_auth_headers(target: VerificationTarget) -> dict[str, str]:
return auth_headers(target.api_key)


def anthropic_auth_headers(target: VerificationTarget) -> dict[str, str]:
# Native Anthropic Messages API authenticates via `x-api-key`. Keep the
# `Authorization: Bearer` header too so Anthropic-compatible gateways that
# expect a bearer token continue to work.
return {
**target_auth_headers(target),
"x-api-key": target.api_key,
"anthropic-version": "2023-06-01",
}


def build_chat_payload(model_name: str, prompt: str, stream: bool = False) -> dict:
payload = {
"model": model_name,
Expand Down Expand Up @@ -197,7 +208,7 @@ def call_model(target: VerificationTarget, prompt: str, options: VerificationOpt
try:
with httpx.Client(timeout=options.timeout_seconds) as client:
if effective_mode == "anthropic":
headers = {**target_auth_headers(target), "anthropic-version": "2023-06-01"}
headers = anthropic_auth_headers(target)
res = client.post(target.base_url + "/v1/messages", headers=headers, json=build_anthropic_payload(target.model, prompt))
res.raise_for_status()
answer, error, upstream_model = parse_anthropic_message_completion(res)
Expand Down Expand Up @@ -225,7 +236,7 @@ def run_protocol_verification(target: VerificationTarget, options: VerificationO
try:
with httpx.Client(timeout=options.timeout_seconds) as client:
if effective_mode == "anthropic":
headers = {**target_auth_headers(target), "anthropic-version": "2023-06-01"}
headers = anthropic_auth_headers(target)
non_stream = client.post(target.base_url + "/v1/messages", headers=headers, json=build_anthropic_payload(target.model, prompt))
checks.append(parse_anthropic_message_response(non_stream, target.model))
stream = client.post(target.base_url + "/v1/messages", headers=headers, json=build_anthropic_payload(target.model, prompt, stream=True))
Expand Down
4 changes: 3 additions & 1 deletion src/model_verifier/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ def live_report(body: bytes) -> dict:
data = parse_json_body(body)
base_url = validate_public_base_url(require_text(data, "base_url"))
api_key = require_text(data, "api_key")
model = require_text(data, "model")
claimed_model = require_text(data, "claimed_model")
# The verifier UI's temporary flow sends only `claimed_model`; the upstream
# request model defaults to it when an explicit `model` is not provided.
model = str(data.get("model") or "").strip() or claimed_model
provider_mode = str(data.get("provider_mode") or "auto").strip()
if provider_mode not in {"auto", "openai", "anthropic"}:
raise ValueError("provider_mode must be auto, openai, or anthropic")
Expand Down
29 changes: 29 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,32 @@ def test_run_protocol_verification_openai_reports_scores():
assert report["protocol_score"] >= 90
assert report["usage_score"] == 75
assert report["observed_model"] == "gpt-4.1"


def test_call_model_anthropic_sends_x_api_key_header():
anthropic_target = VerificationTarget(
source="temporary",
base_url="https://gateway.example.test",
model="claude-3-5-sonnet",
api_key="sk-ant-secret",
api_key_masked="sk-ant...cret",
site_name="临时接入",
)
with respx.mock:
route = respx.post("https://gateway.example.test/v1/messages").mock(return_value=Response(200, json={
"model": "claude-3-5-sonnet",
"content": [{"type": "text", "text": "ok"}],
}))

result = call_model(
anthropic_target,
"hello",
VerificationOptions(claimed_model="claude-3-5-sonnet", provider_mode="anthropic"),
)

request = route.calls.last.request
assert request.headers["x-api-key"] == "sk-ant-secret"
assert request.headers["authorization"] == "Bearer sk-ant-secret"
assert request.headers["anthropic-version"] == "2023-06-01"
assert result.answer == "ok"
assert result.upstream_model == "claude-3-5-sonnet"
33 changes: 33 additions & 0 deletions tests/test_demo_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json

import respx
from httpx import Response

from model_verifier import demo
from model_verifier.demo import demo_report, dispatch_demo_request

Expand Down Expand Up @@ -104,6 +107,36 @@ def test_model_verification_runs_endpoint_accepts_temporary_payload():
assert data["verdict"] == "verified"


def test_temporary_live_run_defaults_model_to_claimed_model(monkeypatch):
# The temporary flow posts only `claimed_model` (no `model`); a live gateway
# run must default the upstream request model to it instead of failing 400.
monkeypatch.setattr(demo, "validate_public_base_url", lambda url: url)
with respx.mock:
respx.post("https://live.example.test/v1/chat/completions").mock(return_value=Response(200, json={
"model": "gpt-4.1",
"choices": [{"message": {"content": "ok"}}],
"usage": {"prompt_tokens": 5, "completion_tokens": 2, "total_tokens": 7},
}))
response = dispatch_demo_request(
"POST",
"/api/model-verification/runs",
json.dumps(
{
"source": "temporary",
"base_url": "https://live.example.test",
"api_key": "sk-test-secret",
"claimed_model": "gpt-4.1",
"provider_mode": "openai",
}
).encode("utf-8"),
)
data = decode_json(response.body)

assert response.status == 200
assert data["claimed_model"] == "gpt-4.1"
assert data["actual_model"] == "gpt-4.1"


def test_sites_endpoint_is_empty_for_open_source_demo():
response = dispatch_demo_request("GET", "/api/sites", b"")
data = decode_json(response.body)
Expand Down
Loading