feat(llm): make tier-C/tier-B model + OpenRouter URL configurable from .env
LLM_MODEL_TIER_C, LLM_MODEL_TIER_B e OPENROUTER_BASE_URL ora override-abili via env. Default invariati (back-compat). LLMClient accetta i tre valori come kwargs opzionali; run_phase1 li propaga da Settings. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -40,3 +40,31 @@ def test_settings_requires_tokens(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
# Disable .env loading to keep the test deterministic regardless of
|
||||
# whether a developer's local .env exists and is populated.
|
||||
Settings(_env_file=None) # type: ignore[call-arg]
|
||||
|
||||
|
||||
def test_settings_loads_llm_model_overrides(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("CERBERO_TESTNET_TOKEN", "tok-test")
|
||||
monkeypatch.setenv("OPENROUTER_API_KEY", "or-key")
|
||||
monkeypatch.setenv("LLM_MODEL_TIER_C", "deepseek/deepseek-chat")
|
||||
monkeypatch.setenv("LLM_MODEL_TIER_B", "claude-opus-4-7")
|
||||
monkeypatch.setenv("OPENROUTER_BASE_URL", "https://example.com/api/v1")
|
||||
|
||||
s = Settings(_env_file=None) # type: ignore[call-arg]
|
||||
|
||||
assert s.llm_model_tier_c == "deepseek/deepseek-chat"
|
||||
assert s.llm_model_tier_b == "claude-opus-4-7"
|
||||
assert s.openrouter_base_url == "https://example.com/api/v1"
|
||||
|
||||
|
||||
def test_settings_llm_model_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv("CERBERO_TESTNET_TOKEN", "tok-test")
|
||||
monkeypatch.setenv("OPENROUTER_API_KEY", "or-key")
|
||||
monkeypatch.delenv("LLM_MODEL_TIER_C", raising=False)
|
||||
monkeypatch.delenv("LLM_MODEL_TIER_B", raising=False)
|
||||
monkeypatch.delenv("OPENROUTER_BASE_URL", raising=False)
|
||||
|
||||
s = Settings(_env_file=None) # type: ignore[call-arg]
|
||||
|
||||
assert s.llm_model_tier_c == "qwen/qwen-2.5-72b-instruct"
|
||||
assert s.llm_model_tier_b == "claude-sonnet-4-6"
|
||||
assert s.openrouter_base_url == "https://openrouter.ai/api/v1"
|
||||
|
||||
@@ -75,6 +75,52 @@ def test_completion_retries_on_connection_error(mocker):
|
||||
assert fake_openai.chat.completions.create.call_count == 3
|
||||
|
||||
|
||||
def test_completion_uses_custom_model_tier_c(mocker):
|
||||
fake_openai = mocker.MagicMock()
|
||||
fake_response = mocker.MagicMock()
|
||||
fake_response.choices = [
|
||||
mocker.MagicMock(message=mocker.MagicMock(content="(strategy ...)"))
|
||||
]
|
||||
fake_response.usage = mocker.MagicMock(prompt_tokens=10, completion_tokens=20)
|
||||
fake_openai.chat.completions.create.return_value = fake_response
|
||||
mocker.patch("multi_swarm.llm.client.OpenAI", return_value=fake_openai)
|
||||
|
||||
client = LLMClient(
|
||||
openrouter_api_key="or-x",
|
||||
anthropic_api_key=None,
|
||||
model_tier_c="deepseek/deepseek-chat",
|
||||
)
|
||||
g = make_genome(ModelTier.C)
|
||||
out = client.complete(g, system="sys", user="usr")
|
||||
|
||||
fake_openai.chat.completions.create.assert_called_once()
|
||||
call_kwargs = fake_openai.chat.completions.create.call_args.kwargs
|
||||
assert call_kwargs["model"] == "deepseek/deepseek-chat"
|
||||
assert out.model == "deepseek/deepseek-chat"
|
||||
|
||||
|
||||
def test_completion_uses_custom_model_tier_b(mocker):
|
||||
fake_anthropic = mocker.MagicMock()
|
||||
fake_msg = mocker.MagicMock()
|
||||
fake_msg.content = [mocker.MagicMock(text="(strategy ...)")]
|
||||
fake_msg.usage = mocker.MagicMock(input_tokens=10, output_tokens=20)
|
||||
fake_anthropic.messages.create.return_value = fake_msg
|
||||
mocker.patch("multi_swarm.llm.client.Anthropic", return_value=fake_anthropic)
|
||||
|
||||
client = LLMClient(
|
||||
openrouter_api_key="or-x",
|
||||
anthropic_api_key="an-x",
|
||||
model_tier_b="claude-opus-4-7",
|
||||
)
|
||||
g = make_genome(ModelTier.B)
|
||||
out = client.complete(g, system="sys", user="usr")
|
||||
|
||||
fake_anthropic.messages.create.assert_called_once()
|
||||
call_kwargs = fake_anthropic.messages.create.call_args.kwargs
|
||||
assert call_kwargs["model"] == "claude-opus-4-7"
|
||||
assert out.model == "claude-opus-4-7"
|
||||
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_completion_succeeds_after_one_retry(mocker):
|
||||
"""Dopo 1 fallimento transient, il retry riesce al 2 tentativo."""
|
||||
|
||||
Reference in New Issue
Block a user