From 95a8b36ecbcebd5e416935c7d18d6065a768920b Mon Sep 17 00:00:00 2001 From: anor Date: Thu, 26 Nov 2026 22:14:19 +1000 Subject: [PATCH] Use fast non-thinking DeepSeek for stove matching --- gateway.py | 2 ++ stove_match.py | 7 +++++-- tests/test_stove_match.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/gateway.py b/gateway.py index db0f21e..f2d1af6 100644 --- a/gateway.py +++ b/gateway.py @@ -216,6 +216,8 @@ def _to_provider(body: dict, model: str, cap: int) -> dict: payload = {"model": model, "max_tokens": min(int(body.get("max_tokens") or cap), cap), "stream": True, "messages": messages} + if body.get("thinking") == {"type": "disabled"}: + payload["thinking"] = {"type": "disabled"} if system_parts: payload["system"] = "\n\n".join(system_parts) tools = [] diff --git a/stove_match.py b/stove_match.py index 73978fd..b37a65c 100644 --- a/stove_match.py +++ b/stove_match.py @@ -91,7 +91,7 @@ def _profile_chain(): quota = zaika_quota.snapshot() if quota.get("known") and (quota.get("percent_used") or 0) >= 80: chain = [p for p in chain if p.name != "glm"] - return chain + return sorted(chain, key=lambda profile: profile.name != "deepseek") async def _call(profile, body: dict, timeout: float) -> tuple[str, dict]: @@ -128,7 +128,10 @@ async def _ask(batch: list[dict], key_id: int, budget: float, deadline: float, r started = time.time() usage = {} try: - text, usage = await _call(profile, body, max(0.1, min(20, deadline-time.monotonic()))) + provider_body = ({**body, "thinking": {"type": "disabled"}} + if profile.name == "deepseek" else body) + text, usage = await _call(profile, provider_body, + max(0.1, min(20, deadline-time.monotonic()))) payload = json.loads(text.strip().removeprefix("```json").removesuffix("```").strip()) if not isinstance(payload, list): raise ValueError("ответ модели не массив") diff --git a/tests/test_stove_match.py b/tests/test_stove_match.py index 05cf442..5c06fdb 100644 --- a/tests/test_stove_match.py +++ b/tests/test_stove_match.py @@ -2,6 +2,7 @@ import asyncio import os import sys +import time from types import SimpleNamespace ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -13,6 +14,15 @@ import stove_pricing import stove_contract as c +def test_explicit_non_thinking_mode_reaches_anthropic_provider(): + body = {"messages": [{"role": "user", "content": "Верни JSON"}], + "max_tokens": 2000, "thinking": {"type": "disabled"}} + payload = stove_match.gateway._to_provider(body, "deepseek-flash", 4096) + assert payload["thinking"] == {"type": "disabled"} + assert "thinking" not in stove_match.gateway._to_provider( + {"messages": body["messages"]}, "glm-5.3", 4096) + + def _prices(): return {"region": "Москва", "zone": None, "valid_at": "III кв. 2026", "lines": [{"name": "Глина шамотная", "qty": 0.5, "unit": "т", "code": None}]} @@ -105,3 +115,30 @@ def test_match_reuses_price_search_for_site_material(monkeypatch): priced, key_id=1, budget=300, deadline=__import__('time').monotonic()+10, request_id="rid")) assert calls.count("Дверка топочная 250×205") == 1 assert result["lines"][2]["code"] == candidate["code"] + + +def test_match_prefers_deepseek_without_thinking_and_keeps_glm_fallback(monkeypatch): + glm = SimpleNamespace(name="glm", model="glm-5.3", uses_api_token=True) + deepseek = SimpleNamespace(name="deepseek", model="deepseek-flash", uses_api_token=True) + monkeypatch.setattr(stove_match.agent_provider, "provider_chain", lambda user: [glm, deepseek]) + monkeypatch.setattr(stove_match.provider_health, "should_include", lambda name: True) + monkeypatch.setattr(stove_match.zaika_quota, "snapshot", lambda: {"known": False}) + assert [p.name for p in stove_match._profile_chain()] == ["deepseek", "glm"] + + calls = [] + monkeypatch.setattr(stove_match, "_reserve", lambda *args: 42) + monkeypatch.setattr(stove_match.db, "settle_llm_usage_stub", lambda *args, **kw: None) + monkeypatch.setattr(stove_match.stove_history, "add_ai_cost", lambda *args, **kw: None) + + async def call(profile, body, timeout): + calls.append((profile.name, body.get("thinking"))) + if profile.name == "deepseek": + raise stove_match.gateway.ProviderError(503, "temporary") + return '[{"index":0,"code":null}]', {} + + monkeypatch.setattr(stove_match, "_call", call) + batch = [{"index": 0, "line": {"name": "Бетон B20", "unit": "м3"}, + "candidates": [{"code": "x", "name": "Балки из бетона B20", "unit": "м3"}]}] + picked = asyncio.run(stove_match._ask(batch, 1, 300, time.monotonic()+10, "rid")) + assert picked == [None] + assert calls == [("deepseek", {"type": "disabled"}), ("glm", None)]