feat(llm): horoskop powstaje zawsze — kontynuacja, okna kontekstu, budzet max
PRZYCZYNA PUSTYCH ODPOWIEDZI NA ANTHROPICU (potwierdzona w dokumentacji API): domyslnym modelem byl `claude-sonnet-5`, ktory przy POMINIETYM parametrze `thinking` wlacza myslenie adaptacyjne, a `thinking.display` domyslnie jest "omitted". Tokeny myslenia licza sie do max_tokens, wiec przy LLM_MAX_TOKENS=2000 cala tura wychodzila jako bloki `thinking` z pustym tekstem — parser filtrowal type=="text" i zwracal pusty string. Opus 4.8 bez `thinking` nie mysli, wiec tam objaw by nie wystapil. Gwarancja niepustej odpowiedzi (wszyscy trzej dostawcy): - generate() to teraz PETLA, nie pojedynczy strzal: tura -> jesli urwana na limicie, dopisz ture „kontynuuj" w tej samej rozmowie i sklej tekst, - tura zlozona z samego myslenia traktowana jak urwana (nie jak pustka), - pusta i NIE urwana -> jedna proba z podpowiedzia, dopiero potem blad, - kontynuacja konczy sie tura UZYTKOWNIKA — Claude odrzuca prefill asystenta (400), - `thinking` konfigurowany JAWNIE (adaptive + effort=high; ANTHROPIC_THINKING=off). Okna kontekstu i rezerwa na odpowiedz (app/llm/limits.py): - tabela okien/limitow wyjscia per model + nadpisanie z ENV, - plan() liczy okno odpowiedzi jako okno - prompt - margines i NIGDY nie oddaje calego kontekstu promptowi, - Anthropic liczy tokeny DOKLADNIE (/v1/messages/count_tokens), reszta szacuje, - >90 tys. tokenow promptu -> ostrzezenie, ale wyslanie NADAL mozliwe i z pelnym oknem odpowiedzi. UI: suwak budzetu rozszerzony o „bardzo obszerny" i „maksymalny kontekst modelu" (liczony z okna wybranego modelu po odjeciu rezerwy); przy wyniku widac plan tokenow, liczbe tur i ostrzezenia. Domyslny model Anthropic: claude-opus-4-8. Testy: 170 passed / 1 skipped (logika) + 15 (prezentacja). Nowe testy pokrywaja sklejanie kontynuacji, brak prefillu asystenta, ture z samego myslenia, rezerwe na odpowiedz i prog ostrzezenia. Zweryfikowane e2e na atrapie Anthropica odtwarzajacej zgloszony objaw: 3 tury, obie czesci tekstu obecne. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
"""Okna kontekstu i planowanie budżetu tokenów (LOG-30/31).
|
||||
|
||||
Naczelna zasada, której pilnują te testy: **zawsze zostaje miejsce na odpowiedź**.
|
||||
Prompt nigdy nie może wypełnić całego okna kontekstu, bo wtedy model kończy na
|
||||
`max_tokens` z pustą albo uciętą treścią — to był zgłoszony błąd.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from app.llm import limits
|
||||
|
||||
|
||||
def test_known_models_have_documented_limits():
|
||||
ctx, out = limits.limits_for("anthropic", "claude-opus-4-8")
|
||||
assert ctx == 1_000_000 and out == 128_000
|
||||
ctx, out = limits.limits_for("anthropic", "claude-haiku-4-5")
|
||||
assert ctx == 200_000 and out == 64_000
|
||||
|
||||
|
||||
def test_local_model_tag_is_matched_by_prefix():
|
||||
"""Modele lokalne niosą tag (`llama3.1:8b`) — dopasowanie musi to znieść."""
|
||||
assert limits.limits_for("local", "llama3.1:8b") == limits.limits_for("local", "llama3.1")
|
||||
|
||||
|
||||
def test_unknown_model_falls_back_conservatively():
|
||||
ctx, out = limits.limits_for("local", "jakis-egzotyczny-model")
|
||||
assert ctx == 8_192 and out == 4_096
|
||||
|
||||
|
||||
def test_env_overrides_win(monkeypatch):
|
||||
"""Modele wychodzą szybciej, niż aktualizuje się tabela."""
|
||||
monkeypatch.setenv("LOCAL_CONTEXT_WINDOW", "131072")
|
||||
monkeypatch.setenv("LOCAL_MAX_OUTPUT", "8192")
|
||||
assert limits.limits_for("local", "llama3.1:8b") == (131_072, 8_192)
|
||||
|
||||
|
||||
def test_env_override_ignores_garbage(monkeypatch):
|
||||
monkeypatch.setenv("LOCAL_CONTEXT_WINDOW", "nie-liczba")
|
||||
assert limits.limits_for("local", "llama3.1:8b")[0] == 8_192
|
||||
|
||||
|
||||
# ------------------------------------------------- rezerwa miejsca na odpowiedź
|
||||
|
||||
def test_prompt_budget_always_reserves_room_for_answer():
|
||||
ctx, out = limits.limits_for("anthropic", "claude-opus-4-8")
|
||||
budget = limits.prompt_token_budget("anthropic", "claude-opus-4-8")
|
||||
assert budget + out + limits.SAFETY_MARGIN <= ctx
|
||||
assert budget > 0
|
||||
|
||||
|
||||
def test_small_context_model_still_leaves_room():
|
||||
budget = limits.prompt_token_budget("local", "llama3.1:8b")
|
||||
ctx, out = limits.limits_for("local", "llama3.1:8b")
|
||||
assert budget + out + limits.SAFETY_MARGIN <= ctx
|
||||
|
||||
|
||||
def test_plan_shrinks_output_when_prompt_is_huge():
|
||||
"""Duży prompt nie może dostać pełnego okna wyjścia — musi się zmieścić."""
|
||||
ctx, model_out = limits.limits_for("local", "llama3.1:8b")
|
||||
p = limits.plan("local", "llama3.1:8b", prompt_tokens=6_000)
|
||||
assert p["max_output"] <= ctx - 6_000 - limits.SAFETY_MARGIN
|
||||
assert p["max_output"] < model_out
|
||||
|
||||
|
||||
def test_plan_reports_not_fitting_instead_of_failing_silently():
|
||||
p = limits.plan("local", "llama3.1:8b", prompt_tokens=8_000)
|
||||
assert p["fits"] is False
|
||||
assert any("odpowied" in w for w in p["warnings"])
|
||||
|
||||
|
||||
def test_plan_warns_above_90k_but_still_fits():
|
||||
"""Powyżej 90 tys. tokenów ostrzegamy — ale wysłanie MA być nadal możliwe."""
|
||||
p = limits.plan("anthropic", "claude-opus-4-8", prompt_tokens=120_000)
|
||||
assert p["fits"] is True, "duży prompt nadal musi dać się wysłać"
|
||||
assert p["max_output"] >= limits.MIN_OUTPUT
|
||||
assert any("dużo" in w for w in p["warnings"])
|
||||
|
||||
|
||||
def test_no_warning_below_threshold():
|
||||
p = limits.plan("anthropic", "claude-opus-4-8", prompt_tokens=10_000)
|
||||
assert p["warnings"] == [] and p["fits"] is True
|
||||
|
||||
|
||||
def test_requested_output_is_capped_by_model_maximum():
|
||||
p = limits.plan("anthropic", "claude-haiku-4-5", prompt_tokens=1_000, want_output=999_999)
|
||||
assert p["max_output"] == 64_000
|
||||
|
||||
|
||||
@pytest.mark.parametrize("tokens", [0, 1_000, 50_000, 200_000, 900_000])
|
||||
def test_plan_never_returns_negative_output(tokens):
|
||||
p = limits.plan("anthropic", "claude-opus-4-8", prompt_tokens=tokens)
|
||||
assert p["max_output"] >= 0
|
||||
@@ -3,6 +3,8 @@
|
||||
Transport podstawiamy przez httpx.MockTransport, więc testy są szybkie,
|
||||
deterministyczne i nic nie wychodzi na zewnątrz.
|
||||
"""
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
@@ -115,7 +117,7 @@ def test_anthropic_generates(monkeypatch):
|
||||
|
||||
|
||||
def test_anthropic_requires_key():
|
||||
with pytest.raises(LLMError, match="LLM_API_KEY"):
|
||||
with pytest.raises(LLMError, match="ANTHROPIC_API_KEY"):
|
||||
AnthropicProvider("https://api.anthropic.com", "m", "").generate("p", 10)
|
||||
|
||||
|
||||
@@ -224,7 +226,7 @@ def test_empty_completion_raises_instead_of_silent_blank(monkeypatch):
|
||||
"choices": [{"message": {"content": ""}, "finish_reason": "length"}],
|
||||
"usage": {"prompt_tokens": 8000, "completion_tokens": 0},
|
||||
})))
|
||||
with pytest.raises(LLMError, match="pustą odpowiedź"):
|
||||
with pytest.raises(LLMError, match="nie zwrócił żadnej treści"):
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 2000)
|
||||
|
||||
|
||||
@@ -238,14 +240,14 @@ def test_empty_completion_explains_context_window(monkeypatch):
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 2000)
|
||||
msg = str(ei.value)
|
||||
assert "kontekstu" in msg and "budżet" in msg
|
||||
assert "tokeny promptu=8000" in msg # diagnostyka w tresci bledu
|
||||
assert "powód zakończenia: length" in msg # diagnostyka w tresci bledu
|
||||
|
||||
|
||||
def test_whitespace_only_is_treated_as_empty(monkeypatch):
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(lambda r: httpx.Response(200, json={
|
||||
"choices": [{"message": {"content": "\n\n \t "}}],
|
||||
})))
|
||||
with pytest.raises(LLMError, match="pustą odpowiedź"):
|
||||
with pytest.raises(LLMError, match="nie zwrócił żadnej treści"):
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 100)
|
||||
|
||||
|
||||
@@ -254,7 +256,7 @@ def test_anthropic_empty_completion_raises(monkeypatch):
|
||||
"model": "claude-x", "content": [], "stop_reason": "max_tokens",
|
||||
"usage": {"input_tokens": 9000, "output_tokens": 0},
|
||||
})))
|
||||
with pytest.raises(LLMError, match="pustą odpowiedź"):
|
||||
with pytest.raises(LLMError, match="nie zwrócił żadnej treści"):
|
||||
AnthropicProvider("https://api.anthropic.com", "claude-x", "klucz").generate("p", 100)
|
||||
|
||||
|
||||
@@ -265,3 +267,101 @@ def test_normal_response_still_passes(monkeypatch):
|
||||
"usage": {"prompt_tokens": 100, "completion_tokens": 20},
|
||||
})))
|
||||
assert ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 100).text == "Horoskop."
|
||||
|
||||
|
||||
# ------------------------------------- kontynuacja: horoskop MA powstac zawsze
|
||||
# Sedno wymagania: niezaleznie od objetosci promptu i limitu wyjscia, pelna tresc
|
||||
# ma wrocic do uzytkownika. Model urwany na max_tokens jest proszony o dokonczenie
|
||||
# w ramach tej samej rozmowy, a kawalki sa sklejane.
|
||||
|
||||
def _scripted(responses):
|
||||
"""Transport oddajacy kolejne odpowiedzi z listy (po jednej na ture)."""
|
||||
seq = list(responses)
|
||||
seen = []
|
||||
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
return httpx.Response(200, json=seq.pop(0) if seq else seq_last)
|
||||
|
||||
seq_last = responses[-1]
|
||||
return handler, seen
|
||||
|
||||
|
||||
def _chat(text, finish):
|
||||
return {"choices": [{"message": {"content": text}, "finish_reason": finish}],
|
||||
"usage": {"completion_tokens": 10}}
|
||||
|
||||
|
||||
def test_truncated_answer_is_continued_and_joined(monkeypatch):
|
||||
handler, seen = _scripted([
|
||||
_chat("Czesc pierwsza.", "length"),
|
||||
_chat("Czesc druga. KONIEC", "stop"),
|
||||
])
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(handler))
|
||||
out = ChatCompletionsProvider("local", "http://x/v1", "m").generate("prompt", 40000)
|
||||
assert "Czesc pierwsza." in out.text and "Czesc druga." in out.text
|
||||
assert "KONIEC" not in out.text # znacznik nie trafia do horoskopu
|
||||
assert out.usage["turns"] == 2
|
||||
|
||||
|
||||
def test_continuation_asks_in_same_conversation(monkeypatch):
|
||||
"""Kontynuacja musi isc jako kolejna tura rozmowy, a ostatnia wiadomosc MUSI
|
||||
byc od uzytkownika — Claude odrzuca prefill w turze asystenta (400)."""
|
||||
handler, seen = _scripted([_chat("Poczatek", "length"), _chat("Reszta", "stop")])
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(handler))
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("prompt", 40000)
|
||||
msgs = json.loads(seen[1].content)["messages"]
|
||||
assert msgs[-1]["role"] == "user", "ostatnia wiadomosc nie moze byc prefillem asystenta"
|
||||
assert msgs[1]["role"] == "assistant" and "Poczatek" in msgs[1]["content"]
|
||||
|
||||
|
||||
def test_anthropic_thinking_only_turn_is_continued(monkeypatch):
|
||||
"""DOKLADNIE zgloszony objaw: cala tura poszla na myslenie, tekst pusty.
|
||||
Wczesniej konczylo sie to pusta strona; teraz pytamy o tresc dalej."""
|
||||
seq = [
|
||||
{"content": [{"type": "thinking", "thinking": ""}], "stop_reason": "max_tokens",
|
||||
"usage": {"output_tokens": 2000}},
|
||||
{"content": [{"type": "text", "text": "Horoskop urodzeniowy..."}],
|
||||
"stop_reason": "end_turn", "usage": {"output_tokens": 500}},
|
||||
]
|
||||
|
||||
def handler(request):
|
||||
return httpx.Response(200, json=seq.pop(0) if seq else seq[-1])
|
||||
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(handler))
|
||||
out = AnthropicProvider("https://api.anthropic.com", "claude-opus-4-8", "k").generate("p", 40000)
|
||||
assert out.text == "Horoskop urodzeniowy..."
|
||||
assert out.usage["turns"] == 2
|
||||
|
||||
|
||||
def test_anthropic_sends_thinking_config(monkeypatch):
|
||||
"""Bez jawnego `thinking` Sonnet 5 wlacza myslenie sam — konfigurujemy to wprost."""
|
||||
seen = []
|
||||
|
||||
def handler(request):
|
||||
seen.append(json.loads(request.content))
|
||||
return httpx.Response(200, json={"content": [{"type": "text", "text": "ok"}],
|
||||
"stop_reason": "end_turn"})
|
||||
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(handler))
|
||||
monkeypatch.delenv("ANTHROPIC_THINKING", raising=False)
|
||||
AnthropicProvider("https://api.anthropic.com", "claude-opus-4-8", "k").generate("p", 5000)
|
||||
assert seen[0]["thinking"] == {"type": "adaptive"}
|
||||
|
||||
seen.clear()
|
||||
monkeypatch.setenv("ANTHROPIC_THINKING", "off")
|
||||
AnthropicProvider("https://api.anthropic.com", "claude-opus-4-8", "k").generate("p", 5000)
|
||||
assert seen[0]["thinking"] == {"type": "disabled"}
|
||||
|
||||
|
||||
def test_complete_answer_does_not_loop(monkeypatch):
|
||||
"""Straznik nie moze mnozyc zapytan, gdy model skonczyl normalnie."""
|
||||
calls = {"n": 0}
|
||||
|
||||
def handler(request):
|
||||
calls["n"] += 1
|
||||
return httpx.Response(200, json=_chat("Gotowe.", "stop"))
|
||||
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(handler))
|
||||
out = ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 40000)
|
||||
assert out.text == "Gotowe." and calls["n"] == 1
|
||||
|
||||
Reference in New Issue
Block a user