Compare commits
3 Commits
master
...
46a21427db
| Author | SHA1 | Date | |
|---|---|---|---|
| 46a21427db | |||
| e6dc649013 | |||
| 5772fa0a60 |
@@ -0,0 +1,75 @@
|
||||
"""Katalog modeli do wyboru w UI (LOG-31).
|
||||
|
||||
To są **podpowiedzi**, nie zamknięta lista. Pole modelu w UI jest tekstowe z
|
||||
datalistą, więc można wpisać dowolny identyfikator — konto może mieć dostęp do
|
||||
modeli, których tu nie ma, a nowe wychodzą szybciej, niż aktualizuje się kod.
|
||||
Puste pole = model domyślny dostawcy.
|
||||
|
||||
Uwaga o pewności danych:
|
||||
* modele **Anthropic** pochodzą z oficjalnej dokumentacji API (okna kontekstu
|
||||
i limity wyjścia zgadzają się z `app/llm/limits.py`);
|
||||
* modele **OpenAI** to podpowiedzi — nie weryfikowałem ich katalogu, więc
|
||||
traktuj je jako wygodę, a nie źródło prawdy;
|
||||
* modele **lokalne** zależą wyłącznie od tego, co masz pobrane w Ollamie/vLLM.
|
||||
|
||||
Katalog można nadpisać/rozszerzyć zmienną `<DOSTAWCA>_MODELS` (lista po przecinku),
|
||||
np. `OPENAI_MODELS="gpt-5,gpt-4o"`.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from app.llm.limits import limits_for
|
||||
|
||||
# dostawca -> [(id modelu, krótki opis dla człowieka)]
|
||||
_CATALOG: dict[str, list[tuple[str, str]]] = {
|
||||
"anthropic": [
|
||||
("claude-opus-4-8", "Opus 4.8 — domyślny, bardzo zdolny, 1M kontekstu"),
|
||||
("claude-fable-5", "Fable 5 — najbardziej zdolny, do najtrudniejszych zadań"),
|
||||
("claude-sonnet-5", "Sonnet 5 — szybszy i tańszy, jakość blisko Opusa"),
|
||||
("claude-opus-4-7", "Opus 4.7 — poprzednia generacja Opusa"),
|
||||
("claude-haiku-4-5", "Haiku 4.5 — najszybszy i najtańszy, mniejsze okno"),
|
||||
],
|
||||
"openai": [
|
||||
("gpt-4o-mini", "GPT-4o mini — tani i szybki"),
|
||||
("gpt-4o", "GPT-4o"),
|
||||
("gpt-5", "GPT-5 — jeśli Twoje konto ma dostęp"),
|
||||
("gpt-4.1", "GPT-4.1"),
|
||||
("gpt-4.1-mini", "GPT-4.1 mini"),
|
||||
],
|
||||
"local": [
|
||||
("llama3.1:8b", "Llama 3.1 8B"),
|
||||
("llama3.2", "Llama 3.2"),
|
||||
("qwen2.5", "Qwen 2.5 — większe okno kontekstu"),
|
||||
("mistral", "Mistral"),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def models_for(provider: str) -> list[dict]:
|
||||
"""Podpowiedzi modeli dla dostawcy, wraz z oknem kontekstu.
|
||||
|
||||
Okno kontekstu podajemy, bo wprost przekłada się na opcję „maksymalny
|
||||
kontekst modelu" — użytkownik widzi, na ile budżetu promptu może liczyć.
|
||||
"""
|
||||
override = os.getenv(f"{provider.upper()}_MODELS", "").strip()
|
||||
if override:
|
||||
entries = [(m.strip(), "") for m in override.split(",") if m.strip()]
|
||||
else:
|
||||
entries = _CATALOG.get(provider, [])
|
||||
|
||||
out = []
|
||||
for model_id, label in entries:
|
||||
context_window, max_output = limits_for(provider, model_id)
|
||||
out.append({
|
||||
"id": model_id,
|
||||
"label": label or model_id,
|
||||
"context_window": context_window,
|
||||
"max_output": max_output,
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def catalog() -> dict[str, list[dict]]:
|
||||
"""Pełny katalog dla UI — jedno żądanie zamiast trzech."""
|
||||
return {provider: models_for(provider) for provider in ("local", "anthropic", "openai")}
|
||||
@@ -40,7 +40,11 @@ PROVIDERS = (LOCAL, OPENAI, ANTHROPIC)
|
||||
_DEFAULT_MODEL = {
|
||||
LOCAL: "llama3.1:8b",
|
||||
OPENAI: "gpt-4o-mini",
|
||||
ANTHROPIC: "claude-sonnet-5",
|
||||
# Opus 4.8 świadomie zamiast Sonnet 5: Sonnet uruchamia myślenie adaptacyjne,
|
||||
# gdy pominąć parametr `thinking`, a jego tokeny liczą się do max_tokens —
|
||||
# przy ciasnym limicie cała tura wychodziła jako samo myślenie z pustym
|
||||
# tekstem. To była przyczyna pustych odpowiedzi na Anthropicu.
|
||||
ANTHROPIC: "claude-opus-4-8",
|
||||
}
|
||||
_DEFAULT_URL = {
|
||||
# Ollama i vLLM wystawiają zgodne API pod /v1
|
||||
@@ -78,12 +82,27 @@ def setting(provider: str, suffix: str, fallback: str = "") -> str:
|
||||
return fallback
|
||||
|
||||
|
||||
def build_provider(name: str | None = None) -> LLMProvider:
|
||||
def resolve_model(name: str | None = None, model: str | None = None) -> tuple[str, str]:
|
||||
"""(dostawca, model) BEZ budowania dostawcy — czyli bez wymogu klucza API.
|
||||
|
||||
Rozmiar budżetu promptu zależy tylko od okna kontekstu modelu, więc nie może
|
||||
zależeć od tego, czy klucz jest już skonfigurowany.
|
||||
"""
|
||||
provider = (name or default_provider_name()).lower()
|
||||
if provider not in PROVIDERS:
|
||||
provider = default_provider_name()
|
||||
chosen = (model or "").strip() or setting(provider, "MODEL", _DEFAULT_MODEL[provider])
|
||||
return provider, chosen
|
||||
|
||||
|
||||
def build_provider(name: str | None = None, model: str | None = None) -> LLMProvider:
|
||||
"""Dostawca modelu. `model` z żądania wygrywa nad konfiguracją — użytkownik
|
||||
wybiera model w UI, a konfiguracja podaje tylko wartość domyślną."""
|
||||
name = (name or default_provider_name()).lower()
|
||||
if name not in PROVIDERS:
|
||||
raise LLMError(f"Nieznany dostawca LLM: {name!r} (dostępne: {', '.join(PROVIDERS)})")
|
||||
|
||||
model = setting(name, "MODEL", _DEFAULT_MODEL[name])
|
||||
model = (model or "").strip() or setting(name, "MODEL", _DEFAULT_MODEL[name])
|
||||
base_url = setting(name, "BASE_URL", _DEFAULT_URL[name])
|
||||
api_key = setting(name, "API_KEY")
|
||||
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Okna kontekstu modeli i planowanie budżetu tokenów (LOG-30/31).
|
||||
|
||||
Po co to istnieje: horoskop MA powstać niezależnie od objętości promptu. Żeby to
|
||||
zagwarantować, trzeba wiedzieć dwie rzeczy o każdym modelu — ile zmieści na
|
||||
wejściu (okno kontekstu) i ile maksymalnie wypisze na wyjściu. Bez tego łatwo
|
||||
wysłać prompt, który wypełnia całe okno i **nie zostawia miejsca na odpowiedź** —
|
||||
model kończy wtedy na `max_tokens` z pustą albo uciętą treścią.
|
||||
|
||||
Zasada naczelna: **zawsze rezerwuj miejsce na odpowiedź.** Budżet promptu liczy
|
||||
się jako `okno_kontekstu − zarezerwowane_wyjście − margines`, nigdy odwrotnie.
|
||||
|
||||
Wartości są zaszyte jako rozsądne domyślne i nadpisywalne środowiskiem
|
||||
(`<DOSTAWCA>_CONTEXT_WINDOW`, `<DOSTAWCA>_MAX_OUTPUT`) — modele wychodzą szybciej,
|
||||
niż aktualizuje się ten plik.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
# model -> (okno kontekstu, maksymalne wyjście) w tokenach
|
||||
_MODEL_LIMITS: dict[str, tuple[int, int]] = {
|
||||
# Anthropic
|
||||
"claude-opus-4-8": (1_000_000, 128_000),
|
||||
"claude-opus-4-7": (1_000_000, 128_000),
|
||||
"claude-opus-4-6": (1_000_000, 128_000),
|
||||
"claude-sonnet-5": (1_000_000, 128_000),
|
||||
"claude-sonnet-4-6": (1_000_000, 128_000),
|
||||
"claude-fable-5": (1_000_000, 128_000),
|
||||
"claude-haiku-4-5": (200_000, 64_000),
|
||||
# OpenAI
|
||||
"gpt-4o": (128_000, 16_384),
|
||||
"gpt-4o-mini": (128_000, 16_384),
|
||||
"gpt-4.1": (1_000_000, 32_768),
|
||||
"gpt-4.1-mini": (1_000_000, 32_768),
|
||||
# lokalne (Ollama/vLLM) — zwykle małe okno, dlatego ostrożna domyślna
|
||||
"llama3.1": (8_192, 4_096),
|
||||
"llama3.2": (8_192, 4_096),
|
||||
"qwen2.5": (32_768, 8_192),
|
||||
"mistral": (32_768, 8_192),
|
||||
}
|
||||
|
||||
# gdy modelu nie ma w tabeli — zachowawczo, żeby nie obiecywać nieistniejącego okna
|
||||
_FALLBACK: dict[str, tuple[int, int]] = {
|
||||
"anthropic": (200_000, 32_000),
|
||||
"openai": (128_000, 16_384),
|
||||
"local": (8_192, 4_096),
|
||||
}
|
||||
|
||||
# ile tokenów zostawiamy jako bufor na narzut protokołu i niedokładność liczenia
|
||||
SAFETY_MARGIN = 2_000
|
||||
# poniżej tylu tokenów wyjścia nie ma sensu wołać modelu — nie zmieści horoskopu
|
||||
MIN_OUTPUT = 1_500
|
||||
# powyżej tylu tokenów promptu ostrzegamy użytkownika (nadal pozwalając wysłać)
|
||||
WARN_PROMPT_TOKENS = 90_000
|
||||
|
||||
|
||||
def _env_int(provider: str, suffix: str) -> int | None:
|
||||
raw = os.getenv(f"{provider.upper()}_{suffix}")
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError:
|
||||
return None
|
||||
return value if value > 0 else None
|
||||
|
||||
|
||||
def limits_for(provider: str, model: str) -> tuple[int, int]:
|
||||
"""(okno kontekstu, maksymalne wyjście) dla modelu — z nadpisaniem z ENV.
|
||||
|
||||
Dopasowanie po prefiksie, bo nazwy modeli lokalnych niosą tag (`llama3.1:8b`).
|
||||
"""
|
||||
env_ctx = _env_int(provider, "CONTEXT_WINDOW")
|
||||
env_out = _env_int(provider, "MAX_OUTPUT")
|
||||
|
||||
key = (model or "").strip().lower()
|
||||
known: tuple[int, int] | None = _MODEL_LIMITS.get(key)
|
||||
if known is None:
|
||||
for name, pair in _MODEL_LIMITS.items():
|
||||
if key.startswith(name):
|
||||
known = pair
|
||||
break
|
||||
if known is None:
|
||||
known = _FALLBACK.get(provider, _FALLBACK["local"])
|
||||
|
||||
return (env_ctx or known[0], env_out or known[1])
|
||||
|
||||
|
||||
def plan(provider: str, model: str, prompt_tokens: int,
|
||||
want_output: int | None = None) -> dict:
|
||||
"""Ile tokenów wyjścia zamówić dla promptu tej wielkości.
|
||||
|
||||
Zwraca plan z jawną diagnostyką — UI ma z czego zbudować ostrzeżenie, a błąd
|
||||
ma czym wytłumaczyć, dlaczego się nie udało.
|
||||
"""
|
||||
context_window, model_max_output = limits_for(provider, model)
|
||||
room = context_window - prompt_tokens - SAFETY_MARGIN
|
||||
target = want_output or model_max_output
|
||||
max_output = max(0, min(model_max_output, target, room))
|
||||
|
||||
warnings: list[str] = []
|
||||
if prompt_tokens > WARN_PROMPT_TOKENS:
|
||||
warnings.append(
|
||||
f"Prompt ma ~{prompt_tokens} tokenów — to dużo. Zapytanie zostanie wysłane, "
|
||||
f"ale potrwa dłużej i będzie odpowiednio kosztowne."
|
||||
)
|
||||
if max_output < MIN_OUTPUT:
|
||||
warnings.append(
|
||||
f"Po zmieszczeniu promptu zostaje tylko {max_output} tokenów na odpowiedź "
|
||||
f"(minimum {MIN_OUTPUT}). Zmniejsz budżet promptu albo wybierz model "
|
||||
f"z większym oknem kontekstu."
|
||||
)
|
||||
|
||||
return {
|
||||
"provider": provider,
|
||||
"model": model,
|
||||
"context_window": context_window,
|
||||
"model_max_output": model_max_output,
|
||||
"prompt_tokens": prompt_tokens,
|
||||
"max_output": max_output,
|
||||
"fits": max_output >= MIN_OUTPUT,
|
||||
"warnings": warnings,
|
||||
}
|
||||
|
||||
|
||||
def prompt_token_budget(provider: str, model: str, reserve_output: int | None = None) -> int:
|
||||
"""Ile tokenów promptu wolno wysłać, ZAWSZE zostawiając miejsce na odpowiedź.
|
||||
|
||||
To jest podstawa opcji „maksymalny kontekst modelu" w UI.
|
||||
"""
|
||||
context_window, model_max_output = limits_for(provider, model)
|
||||
reserve = reserve_output or model_max_output
|
||||
return max(0, context_window - reserve - SAFETY_MARGIN)
|
||||
@@ -5,9 +5,24 @@ vLLM, llama.cpp) i OpenAI mówią **tym samym** protokołem `/chat/completions`,
|
||||
więc jedna implementacja obsługuje oba — różni je tylko adres i klucz. Anthropic
|
||||
ma własny kształt `/v1/messages`, stąd druga klasa. Mniej zależności, mniej
|
||||
powierzchni ataku, pełna kontrola nad tym, co wychodzi z sieci.
|
||||
|
||||
**Gwarancja niepustej odpowiedzi.** Horoskop ma powstać niezależnie od objętości
|
||||
promptu, więc `generate()` nie jest pojedynczym strzałem, tylko pętlą:
|
||||
1. wyślij turę z policzonym limitem wyjścia,
|
||||
2. jeśli model urwał na limicie — dopisz turę „kontynuuj" i sklej tekst,
|
||||
3. jeśli tura nie dała ani znaku tekstu — ponów z podpowiedzią,
|
||||
4. dopiero brak tekstu po wszystkich próbach jest błędem (z diagnostyką).
|
||||
Kontynuacja jest pewniejsza niż jedno wielkie żądanie: każda tura mieści się
|
||||
w timeoucie HTTP, a długość odpowiedzi przestaje być ograniczona jedną turą.
|
||||
|
||||
**Anthropic i myślenie.** Modele Claude potrafią mieć włączone myślenie, którego
|
||||
tokeny liczą się do `max_tokens`. Przy ciasnym limicie cała tura potrafi wyjść
|
||||
jako same bloki `thinking` z pustym tekstem — dokładnie ten objaw, który
|
||||
zgłoszono. Traktujemy taką turę jak ucięcie i kontynuujemy, zamiast zwracać pustkę.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import httpx
|
||||
@@ -17,6 +32,23 @@ from app.llm.base import Completion, LLMError, LLMProvider
|
||||
RETRY_STATUSES = {429, 500, 502, 503, 504}
|
||||
MAX_ATTEMPTS = 3
|
||||
|
||||
# ile razy wolno poprosić model o dokończenie urwanej odpowiedzi
|
||||
MAX_CONTINUATIONS = 12
|
||||
# ile tokenów zamawiać na jedną turę — mieści się w timeoucie, a pętla i tak
|
||||
# dociągnie resztę; zbyt duża wartość ryzykuje zerwanie połączenia w trakcie
|
||||
TURN_TOKENS_CAP = 16_000
|
||||
|
||||
_CONTINUE = (
|
||||
"Kontynuuj dokładnie od miejsca, w którym przerwałeś — nie powtarzaj tego, "
|
||||
"co już napisałeś, i nie zaczynaj od nowa. Jeśli skończyłeś całą odpowiedź, "
|
||||
"napisz wyłącznie: KONIEC"
|
||||
)
|
||||
_NUDGE = (
|
||||
"Nie otrzymałem żadnej treści. Napisz odpowiedź zgodnie z powyższym poleceniem, "
|
||||
"zaczynając od razu od treści horoskopu."
|
||||
)
|
||||
_DONE_MARKER = "KONIEC"
|
||||
|
||||
|
||||
def _post_with_retry(url: str, headers: dict, payload: dict, timeout: float) -> dict:
|
||||
"""POST z ponawianiem i backoffem — chroni przed chwilowym 429/5xx."""
|
||||
@@ -37,8 +69,8 @@ def _post_with_retry(url: str, headers: dict, payload: dict, timeout: float) ->
|
||||
time.sleep(2 ** attempt)
|
||||
continue
|
||||
raise LLMError(
|
||||
f"Model nie odpowiedział w czasie {timeout:.0f}s. Dłuższe horoskopy "
|
||||
f"wymagają większego LLM_TIMEOUT albo mniejszego budżetu promptu."
|
||||
f"Model nie odpowiedział w czasie {timeout:.0f}s. Zwiększ LLM_TIMEOUT "
|
||||
f"albo zmniejsz budżet promptu."
|
||||
) from e
|
||||
except httpx.HTTPError as e:
|
||||
last = e
|
||||
@@ -49,7 +81,108 @@ def _post_with_retry(url: str, headers: dict, payload: dict, timeout: float) ->
|
||||
raise LLMError(f"Nie udało się wywołać modelu: {last}")
|
||||
|
||||
|
||||
class ChatCompletionsProvider(LLMProvider):
|
||||
def _merge_usage(total: dict, turn: dict) -> dict:
|
||||
"""Sumuje zużycie tokenów przez wszystkie tury jednej odpowiedzi."""
|
||||
for key, value in (turn or {}).items():
|
||||
if isinstance(value, int):
|
||||
total[key] = total.get(key, 0) + value
|
||||
return total
|
||||
|
||||
|
||||
def _join(parts: list[str]) -> str:
|
||||
return "".join(parts).strip()
|
||||
|
||||
|
||||
def _explain_empty(turns: int, usage: dict, stop: str | None) -> str:
|
||||
detail = []
|
||||
if stop:
|
||||
detail.append(f"powód zakończenia: {stop}")
|
||||
for key in ("completion_tokens", "output_tokens"):
|
||||
if usage.get(key) is not None:
|
||||
detail.append(f"tokeny odpowiedzi: {usage[key]}")
|
||||
break
|
||||
suffix = f" ({', '.join(detail)})" if detail else ""
|
||||
return (
|
||||
f"Model nie zwrócił żadnej treści po {turns} próbach{suffix}. "
|
||||
f"Najczęstsza przyczyna: prompt wypełnił okno kontekstu i nie zostało miejsca "
|
||||
f"na odpowiedź. Zmniejsz budżet promptu albo wybierz model z większym oknem."
|
||||
)
|
||||
|
||||
|
||||
class _Driver:
|
||||
"""Wspólna pętla: tura → ewentualna kontynuacja → sklejony tekst.
|
||||
|
||||
Podklasy dostarczają tylko `_turn()` — reszta (kontynuacje, ponawianie pustej
|
||||
tury, sumowanie zużycia) jest identyczna dla obu protokołów.
|
||||
"""
|
||||
|
||||
name: str
|
||||
model: str
|
||||
leaves_lan: bool
|
||||
|
||||
def _turn(self, messages: list[dict], max_tokens: int):
|
||||
"""(tekst, czy_ucięta, zużycie, nazwa_modelu, powód_zakończenia)."""
|
||||
raise NotImplementedError
|
||||
|
||||
def generate(self, prompt: str, max_tokens: int) -> Completion:
|
||||
messages: list[dict] = [{"role": "user", "content": prompt}]
|
||||
parts: list[str] = []
|
||||
usage: dict = {}
|
||||
model_name = self.model
|
||||
remaining = max(max_tokens, 256)
|
||||
stop: str | None = None
|
||||
turns = 0
|
||||
nudged = False
|
||||
|
||||
while turns <= MAX_CONTINUATIONS:
|
||||
turns += 1
|
||||
budget = max(256, min(remaining, TURN_TOKENS_CAP))
|
||||
text, truncated, turn_usage, model_name, stop = self._turn(messages, budget)
|
||||
_merge_usage(usage, turn_usage)
|
||||
remaining -= budget
|
||||
|
||||
chunk = text.strip()
|
||||
if chunk:
|
||||
if chunk.endswith(_DONE_MARKER): # model zgłasza koniec
|
||||
parts.append(("\n" if parts else "") + chunk[: -len(_DONE_MARKER)].rstrip())
|
||||
break
|
||||
parts.append(("\n" if parts else "") + chunk)
|
||||
if not truncated:
|
||||
break
|
||||
elif not truncated:
|
||||
# pusta i NIE ucięta: jedna próba z podpowiedzią, potem koniec
|
||||
if nudged or parts:
|
||||
break
|
||||
nudged = True
|
||||
messages = messages + [
|
||||
{"role": "assistant", "content": "…"},
|
||||
{"role": "user", "content": _NUDGE},
|
||||
]
|
||||
continue
|
||||
# ucięta (także tura złożona z samego myślenia) — poproś o dokończenie
|
||||
|
||||
if remaining < 256:
|
||||
break
|
||||
messages = [
|
||||
{"role": "user", "content": prompt},
|
||||
{"role": "assistant", "content": _join(parts) or "…"},
|
||||
{"role": "user", "content": _CONTINUE},
|
||||
]
|
||||
|
||||
final = _join(parts)
|
||||
if not final:
|
||||
raise LLMError(_explain_empty(turns, usage, stop))
|
||||
|
||||
usage["turns"] = turns
|
||||
return Completion(text=final, model=model_name, provider=self.name,
|
||||
leaves_lan=self.leaves_lan, usage=usage)
|
||||
|
||||
def count_tokens(self, prompt: str) -> int:
|
||||
"""Szacunek tokenów promptu. Dostawcy z własnym licznikiem nadpisują."""
|
||||
return int(len(prompt) / 3.6)
|
||||
|
||||
|
||||
class ChatCompletionsProvider(_Driver, LLMProvider):
|
||||
"""Protokół OpenAI `/chat/completions` — lokalny serwer modelu ORAZ OpenAI."""
|
||||
|
||||
def __init__(self, name: str, base_url: str, model: str, api_key: str = "",
|
||||
@@ -67,24 +200,20 @@ class ChatCompletionsProvider(LLMProvider):
|
||||
h["Authorization"] = f"Bearer {self.api_key}"
|
||||
return h
|
||||
|
||||
def generate(self, prompt: str, max_tokens: int) -> Completion:
|
||||
def _turn(self, messages: list[dict], max_tokens: int):
|
||||
data = _post_with_retry(
|
||||
f"{self.base_url}/chat/completions", self._headers(),
|
||||
{
|
||||
"model": self.model,
|
||||
"max_tokens": max_tokens,
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
},
|
||||
{"model": self.model, "max_tokens": max_tokens, "messages": messages},
|
||||
self.timeout,
|
||||
)
|
||||
try:
|
||||
text = data["choices"][0]["message"]["content"]
|
||||
choice = data["choices"][0]
|
||||
text = choice["message"].get("content") or ""
|
||||
except (KeyError, IndexError, TypeError) as e:
|
||||
raise LLMError(f"Nieoczekiwany kształt odpowiedzi modelu: {str(data)[:300]}") from e
|
||||
return Completion(
|
||||
text=text, model=data.get("model", self.model), provider=self.name,
|
||||
leaves_lan=self.leaves_lan, usage=data.get("usage") or {},
|
||||
)
|
||||
stop = choice.get("finish_reason")
|
||||
return (text, stop == "length", data.get("usage") or {},
|
||||
data.get("model", self.model), stop)
|
||||
|
||||
def health(self) -> dict:
|
||||
info = {"provider": self.name, "model": self.model, "leaves_lan": self.leaves_lan}
|
||||
@@ -97,7 +226,7 @@ class ChatCompletionsProvider(LLMProvider):
|
||||
return info
|
||||
|
||||
|
||||
class AnthropicProvider(LLMProvider):
|
||||
class AnthropicProvider(_Driver, LLMProvider):
|
||||
"""Protokół Anthropic `/v1/messages`."""
|
||||
|
||||
leaves_lan = True
|
||||
@@ -110,34 +239,68 @@ class AnthropicProvider(LLMProvider):
|
||||
self.api_key = api_key
|
||||
self.timeout = timeout
|
||||
|
||||
def generate(self, prompt: str, max_tokens: int) -> Completion:
|
||||
def _headers(self) -> dict:
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": self.api_key,
|
||||
"anthropic-version": "2023-06-01",
|
||||
}
|
||||
|
||||
def _thinking(self) -> dict:
|
||||
"""Konfiguracja myślenia. Domyślnie adaptacyjne — podnosi jakość tekstu.
|
||||
|
||||
UWAGA: tokeny myślenia liczą się do `max_tokens`, więc przy ciasnym limicie
|
||||
cała tura potrafi wyjść jako samo myślenie z pustym tekstem. Pętla
|
||||
kontynuacji to obsługuje, ale ANTHROPIC_THINKING=off wyłącza myślenie,
|
||||
gdy zależy nam na przewidywalnym zużyciu tokenów.
|
||||
"""
|
||||
mode = os.getenv("ANTHROPIC_THINKING", "adaptive").lower()
|
||||
if mode in ("off", "disabled", "0", "false"):
|
||||
return {"thinking": {"type": "disabled"}}
|
||||
return {
|
||||
"thinking": {"type": "adaptive"},
|
||||
"output_config": {"effort": os.getenv("ANTHROPIC_EFFORT", "high")},
|
||||
}
|
||||
|
||||
def _turn(self, messages: list[dict], max_tokens: int):
|
||||
if not self.api_key:
|
||||
raise LLMError("Brak LLM_API_KEY — dostawca anthropic wymaga klucza.")
|
||||
data = _post_with_retry(
|
||||
f"{self.base_url}/v1/messages",
|
||||
{
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": self.api_key,
|
||||
"anthropic-version": "2023-06-01",
|
||||
},
|
||||
{
|
||||
"model": self.model,
|
||||
"max_tokens": max_tokens,
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
},
|
||||
self.timeout,
|
||||
)
|
||||
raise LLMError("Brak ANTHROPIC_API_KEY — dostawca anthropic wymaga klucza.")
|
||||
payload = {"model": self.model, "max_tokens": max_tokens, "messages": messages}
|
||||
payload.update(self._thinking())
|
||||
data = _post_with_retry(f"{self.base_url}/v1/messages", self._headers(),
|
||||
payload, self.timeout)
|
||||
try:
|
||||
text = "".join(b.get("text", "") for b in data["content"] if b.get("type") == "text")
|
||||
blocks = data["content"]
|
||||
text = "".join(b.get("text", "") for b in blocks if b.get("type") == "text")
|
||||
except (KeyError, TypeError) as e:
|
||||
raise LLMError(f"Nieoczekiwany kształt odpowiedzi modelu: {str(data)[:300]}") from e
|
||||
return Completion(
|
||||
text=text, model=data.get("model", self.model), provider=self.name,
|
||||
leaves_lan=True, usage=data.get("usage") or {},
|
||||
|
||||
stop = data.get("stop_reason")
|
||||
# tura złożona z samego myślenia = budżet poszedł na rozumowanie; traktujemy
|
||||
# jak ucięcie, żeby pętla poprosiła o treść zamiast zwrócić pustkę
|
||||
thinking_only = not text.strip() and any(
|
||||
b.get("type") in ("thinking", "redacted_thinking") for b in blocks
|
||||
)
|
||||
return (text, stop == "max_tokens" or thinking_only, data.get("usage") or {},
|
||||
data.get("model", self.model), stop)
|
||||
|
||||
def count_tokens(self, prompt: str) -> int:
|
||||
"""Dokładny licznik Anthropic — nie szacunek. Od tego zależy, czy po
|
||||
zmieszczeniu promptu zostanie miejsce na odpowiedź."""
|
||||
if not self.api_key:
|
||||
return super().count_tokens(prompt)
|
||||
try:
|
||||
data = _post_with_retry(
|
||||
f"{self.base_url}/v1/messages/count_tokens", self._headers(),
|
||||
{"model": self.model, "messages": [{"role": "user", "content": prompt}]},
|
||||
min(self.timeout, 30.0),
|
||||
)
|
||||
return int(data.get("input_tokens") or super().count_tokens(prompt))
|
||||
except LLMError:
|
||||
return super().count_tokens(prompt)
|
||||
|
||||
def health(self) -> dict:
|
||||
return {
|
||||
"provider": self.name, "model": self.model, "leaves_lan": True,
|
||||
"status": "ok (klucz ustawiony)" if self.api_key else "brak LLM_API_KEY",
|
||||
"status": "ok (klucz ustawiony)" if self.api_key else "brak ANTHROPIC_API_KEY",
|
||||
}
|
||||
|
||||
@@ -129,8 +129,10 @@ class PromptRequest(BaseModel):
|
||||
when_utc: datetime
|
||||
lat: float = 0.0
|
||||
lon: float = 0.0
|
||||
budget: str = "medium" # concise | medium | extensive
|
||||
budget: str = "medium" # concise | medium | extensive | huge | max
|
||||
limit: int = 5000
|
||||
provider: str | None = None # do wyliczenia budżetu „max" wg okna modelu
|
||||
model: str | None = None
|
||||
# tylko dla profilu period:
|
||||
from_date: str | None = None
|
||||
to_date: str | None = None
|
||||
@@ -149,7 +151,7 @@ def chart_prompt(req: PromptRequest) -> dict:
|
||||
"""
|
||||
from app.engine.chart import build_chart
|
||||
from app.engine.models import ChartMoment
|
||||
from app.prompt import build_natal_prompt, build_period_prompt
|
||||
from app.prompt import CHARS_PER_TOKEN, MAX_BUDGET, build_natal_prompt, build_period_prompt
|
||||
|
||||
engine = get_engine()
|
||||
moment = ChartMoment(when_utc=req.when_utc, lat=req.lat, lon=req.lon)
|
||||
@@ -157,6 +159,17 @@ def chart_prompt(req: PromptRequest) -> dict:
|
||||
label = req.when_utc.strftime("%Y-%m-%d %H:%M UTC")
|
||||
data_error = None
|
||||
|
||||
# Budżet „maksymalny kontekst modelu": limit znaków liczymy z okna kontekstu
|
||||
# WYBRANEGO modelu, zawsze po odjęciu miejsca zarezerwowanego na odpowiedź.
|
||||
budget_chars = None
|
||||
if req.budget == MAX_BUDGET:
|
||||
from app.llm.factory import resolve_model
|
||||
from app.llm.limits import prompt_token_budget
|
||||
# celowo bez build_provider(): budżet zależy TYLKO od okna kontekstu modelu,
|
||||
# więc nie może wymagać skonfigurowanego klucza API
|
||||
provider_name, model_name = resolve_model(req.provider, req.model)
|
||||
budget_chars = int(prompt_token_budget(provider_name, model_name) * CHARS_PER_TOKEN)
|
||||
|
||||
# Warstwa danych dokłada wyłącznie WSKAZANIA. Wyliczenia (horoskop, oś czasu) są
|
||||
# od niej niezależne — gdy padnie, prompt musi zachować wszystko, co policzyliśmy.
|
||||
try:
|
||||
@@ -171,7 +184,7 @@ def chart_prompt(req: PromptRequest) -> dict:
|
||||
)
|
||||
except httpx.HTTPError as e:
|
||||
data_error = f"Warstwa danych niedostępna: {e}"
|
||||
out = build_natal_prompt(chart, report, req.budget, label)
|
||||
out = build_natal_prompt(chart, report, req.budget, label, budget_chars)
|
||||
|
||||
elif req.profile == "period":
|
||||
if not (req.from_date and req.to_date):
|
||||
@@ -191,7 +204,7 @@ def chart_prompt(req: PromptRequest) -> dict:
|
||||
except httpx.HTTPError as e:
|
||||
data_error = f"Warstwa danych niedostępna: {e}" # oś czasu zostaje
|
||||
out = build_period_prompt(chart, events, req.from_date, req.to_date,
|
||||
req.budget, label)
|
||||
req.budget, label, budget_chars)
|
||||
else:
|
||||
raise HTTPException(422, f"Nieznany profil: {req.profile!r} (natal | period)")
|
||||
except ValueError as e: # nieznany budżet
|
||||
@@ -204,8 +217,7 @@ def chart_prompt(req: PromptRequest) -> dict:
|
||||
|
||||
|
||||
class HoroscopeRequest(PromptRequest):
|
||||
"""Jak PromptRequest + wybór dostawcy modelu (LOG-31)."""
|
||||
provider: str | None = None # local (dom.) | openai | anthropic
|
||||
"""Jak PromptRequest (niesie już provider i model) + limit wyjścia (LOG-31)."""
|
||||
max_tokens: int | None = None
|
||||
|
||||
|
||||
@@ -218,12 +230,26 @@ def chart_horoscope(req: HoroscopeRequest) -> dict:
|
||||
(LOG-32); prezentacja ma na tej podstawie ostrzegać.
|
||||
"""
|
||||
from app.llm.base import LLMError
|
||||
from app.llm.factory import build_provider, max_tokens
|
||||
from app.llm.factory import build_provider
|
||||
from app.llm.limits import plan
|
||||
|
||||
out = chart_prompt(req) # ten sam prompt co w podglądzie
|
||||
try:
|
||||
provider = build_provider(req.provider)
|
||||
result = provider.generate(out["prompt"], req.max_tokens or max_tokens())
|
||||
provider = build_provider(req.provider, req.model)
|
||||
|
||||
# Ile tokenów ma naprawdę ten prompt i ile zostaje na odpowiedź. Anthropic
|
||||
# liczy dokładnie (własny endpoint), reszta szacuje — od tego zależy, czy
|
||||
# w oknie kontekstu w ogóle zmieści się miejsce na horoskop.
|
||||
prompt_tokens = provider.count_tokens(out["prompt"])
|
||||
budget = plan(provider.name, provider.model, prompt_tokens, req.max_tokens)
|
||||
out["token_plan"] = budget
|
||||
if budget["warnings"]:
|
||||
out["warnings"] = budget["warnings"]
|
||||
if not budget["fits"]:
|
||||
out["llm_error"] = " ".join(budget["warnings"])
|
||||
return out
|
||||
|
||||
result = provider.generate(out["prompt"], budget["max_output"])
|
||||
except LLMError as e:
|
||||
# prompt zostaje — użytkownik moze go skopiowac i uzyc recznie
|
||||
out["llm_error"] = str(e)
|
||||
@@ -239,6 +265,19 @@ def chart_horoscope(req: HoroscopeRequest) -> dict:
|
||||
return out
|
||||
|
||||
|
||||
@app.get("/llm/models")
|
||||
def llm_models() -> dict:
|
||||
"""Podpowiedzi modeli per dostawca — UI buduje z tego listę wyboru.
|
||||
|
||||
To nie jest lista zamknięta: pole modelu jest tekstowe, więc można wpisać
|
||||
dowolny identyfikator, do którego konto ma dostęp.
|
||||
"""
|
||||
from app.llm.catalog import catalog
|
||||
from app.llm.factory import _DEFAULT_MODEL
|
||||
|
||||
return {"providers": catalog(), "defaults": dict(_DEFAULT_MODEL)}
|
||||
|
||||
|
||||
@app.get("/llm/health")
|
||||
def llm_health(provider: str | None = None) -> dict:
|
||||
"""Czy model jest osiągalny i skonfigurowany (bez generowania czegokolwiek)."""
|
||||
|
||||
@@ -28,8 +28,14 @@ BUDGETS: dict[str, int] = {
|
||||
"concise": 4_000,
|
||||
"medium": 12_000,
|
||||
"extensive": 30_000,
|
||||
"huge": 120_000,
|
||||
# „maksymalny kontekst modelu" — wyliczany dynamicznie z okna kontekstu
|
||||
# wybranego modelu, ZAWSZE po odjęciu miejsca zarezerwowanego na odpowiedź.
|
||||
# Wartość poniżej jest tylko zapasem, gdy limity modelu są nieznane.
|
||||
"max": 400_000,
|
||||
}
|
||||
DEFAULT_BUDGET = "medium"
|
||||
MAX_BUDGET = "max"
|
||||
|
||||
MAX_EFFECT_CHARS = 320 # dłuższe opisy skracamy (krok 5 redukcji)
|
||||
CHARS_PER_TOKEN = 4.0 # zgrubny szacunek tokenów do podglądu w UI
|
||||
@@ -284,11 +290,16 @@ def _assemble(task: str, chart_sec: str, ind_sec: str, output: str) -> str:
|
||||
return "\n\n".join([task, chart_sec, ind_sec, output, f"# ZASTRZEŻENIE\n{DISCLAIMER}"])
|
||||
|
||||
|
||||
def _budget_chars(budget: str) -> int:
|
||||
def _budget_chars(budget: str, budget_chars: int | None = None) -> int:
|
||||
"""Limit znaków promptu. `budget_chars` nadpisuje tabelę — używane dla opcji
|
||||
„maksymalny kontekst modelu", gdzie limit zależy od wybranego modelu i musi
|
||||
być policzony po odjęciu miejsca zarezerwowanego na odpowiedź."""
|
||||
if budget not in BUDGETS:
|
||||
raise ValueError(
|
||||
f"Nieznany budżet: {budget!r} (dostępne: {', '.join(BUDGETS)})"
|
||||
)
|
||||
if budget_chars and budget_chars > 0:
|
||||
return budget_chars
|
||||
return BUDGETS[budget]
|
||||
|
||||
|
||||
@@ -305,9 +316,10 @@ def _finish(prompt: str, budget: str, limit: int, stats: dict, profile: str) ->
|
||||
|
||||
|
||||
def build_natal_prompt(chart: dict, report: dict, budget: str = DEFAULT_BUDGET,
|
||||
moment_label: str | None = None) -> dict:
|
||||
moment_label: str | None = None,
|
||||
budget_chars: int | None = None) -> dict:
|
||||
"""Prompt na horoskop urodzeniowy (ekran „Interpretacje")."""
|
||||
limit = _budget_chars(budget)
|
||||
limit = _budget_chars(budget, budget_chars)
|
||||
chart_sec = _chart_section(chart, moment_label)
|
||||
fixed = len(_NATAL_TASK) + len(chart_sec) + len(_NATAL_OUTPUT) + len(DISCLAIMER) + 200
|
||||
chosen, stats = reduce_indications(natal_indications(report), max(limit - fixed, 500))
|
||||
@@ -316,9 +328,10 @@ def build_natal_prompt(chart: dict, report: dict, budget: str = DEFAULT_BUDGET,
|
||||
|
||||
|
||||
def build_period_prompt(chart: dict, events: list[dict], from_date: str, to_date: str,
|
||||
budget: str = DEFAULT_BUDGET, moment_label: str | None = None) -> dict:
|
||||
budget: str = DEFAULT_BUDGET, moment_label: str | None = None,
|
||||
budget_chars: int | None = None) -> dict:
|
||||
"""Prompt na horoskop okresowy (ekran „Kalendarz")."""
|
||||
limit = _budget_chars(budget)
|
||||
limit = _budget_chars(budget, budget_chars)
|
||||
chart_sec = _chart_section(chart, moment_label)
|
||||
task = f"{_PERIOD_TASK}\nZakres prognozy: **{from_date} — {to_date}**."
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -210,3 +212,196 @@ def test_cloud_without_key_is_rejected_clearly(monkeypatch):
|
||||
def test_local_needs_no_key(monkeypatch):
|
||||
_clear(monkeypatch)
|
||||
assert factory.build_provider("local").api_key == ""
|
||||
|
||||
|
||||
# ------------------------------------------- pusta odpowiedz modelu (cicha awaria)
|
||||
# Regresja: model potrafi oddac pusta tresc (prompt zjadl caly kontekst ->
|
||||
# finish_reason=length, completion_tokens=0). Wczesniej generate() zwracalo pusty
|
||||
# tekst BEZ bledu, widok nic nie renderowal i uzytkownik dostawal pusta strone
|
||||
# bez zadnego wyjasnienia. Pusta odpowiedz MUSI byc bledem.
|
||||
|
||||
def test_empty_completion_raises_instead_of_silent_blank(monkeypatch):
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(lambda r: httpx.Response(200, json={
|
||||
"model": "llama3.1:8b",
|
||||
"choices": [{"message": {"content": ""}, "finish_reason": "length"}],
|
||||
"usage": {"prompt_tokens": 8000, "completion_tokens": 0},
|
||||
})))
|
||||
with pytest.raises(LLMError, match="nie zwrócił żadnej treści"):
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 2000)
|
||||
|
||||
|
||||
def test_empty_completion_explains_context_window(monkeypatch):
|
||||
"""Komunikat ma prowadzic do przyczyny, a nie tylko stwierdzac fakt."""
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(lambda r: httpx.Response(200, json={
|
||||
"choices": [{"message": {"content": " "}, "finish_reason": "length"}],
|
||||
"usage": {"prompt_tokens": 8000, "completion_tokens": 0},
|
||||
})))
|
||||
with pytest.raises(LLMError) as ei:
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 2000)
|
||||
msg = str(ei.value)
|
||||
assert "kontekstu" in msg and "budżet" in msg
|
||||
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="nie zwrócił żadnej treści"):
|
||||
ChatCompletionsProvider("local", "http://x/v1", "m").generate("p", 100)
|
||||
|
||||
|
||||
def test_anthropic_empty_completion_raises(monkeypatch):
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(lambda r: httpx.Response(200, json={
|
||||
"model": "claude-x", "content": [], "stop_reason": "max_tokens",
|
||||
"usage": {"input_tokens": 9000, "output_tokens": 0},
|
||||
})))
|
||||
with pytest.raises(LLMError, match="nie zwrócił żadnej treści"):
|
||||
AnthropicProvider("https://api.anthropic.com", "claude-x", "klucz").generate("p", 100)
|
||||
|
||||
|
||||
def test_normal_response_still_passes(monkeypatch):
|
||||
"""Straznik nie moze psuc poprawnej odpowiedzi."""
|
||||
monkeypatch.setattr(httpx, "Client", _mock_client(lambda r: httpx.Response(200, json={
|
||||
"choices": [{"message": {"content": "Horoskop."}, "finish_reason": "stop"}],
|
||||
"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
|
||||
|
||||
|
||||
# ------------------------------------------- wybor modelu przez uzytkownika (UI)
|
||||
|
||||
def test_model_from_request_wins_over_config(monkeypatch):
|
||||
_clear(monkeypatch)
|
||||
monkeypatch.setenv("ANTHROPIC_MODEL", "claude-opus-4-8")
|
||||
monkeypatch.setenv("ANTHROPIC_API_KEY", "k")
|
||||
p = factory.build_provider("anthropic", "claude-fable-5")
|
||||
assert p.model == "claude-fable-5", "wybor z UI musi wygrac nad konfiguracja"
|
||||
|
||||
|
||||
def test_blank_model_falls_back_to_configured_default(monkeypatch):
|
||||
_clear(monkeypatch)
|
||||
monkeypatch.setenv("ANTHROPIC_MODEL", "claude-sonnet-5")
|
||||
monkeypatch.setenv("ANTHROPIC_API_KEY", "k")
|
||||
assert factory.build_provider("anthropic", " ").model == "claude-sonnet-5"
|
||||
|
||||
|
||||
def test_resolve_model_needs_no_api_key(monkeypatch):
|
||||
"""Budzet promptu zalezy od okna kontekstu modelu — nie moze wymagac klucza.
|
||||
|
||||
Wczesniej liczenie budzetu szlo przez build_provider(), ktory bez klucza
|
||||
rzuca bledem, wiec „maksymalny kontekst" cicho spadal do wartosci zapasowej.
|
||||
"""
|
||||
_clear(monkeypatch)
|
||||
provider, model = factory.resolve_model("anthropic", "claude-haiku-4-5")
|
||||
assert (provider, model) == ("anthropic", "claude-haiku-4-5")
|
||||
with pytest.raises(LLMError): # samo zbudowanie nadal wymaga klucza
|
||||
factory.build_provider("anthropic", "claude-haiku-4-5")
|
||||
|
||||
|
||||
def test_max_budget_differs_between_models(monkeypatch):
|
||||
"""Sedno funkcji: wieksze okno = wiekszy budzet promptu."""
|
||||
from app.llm.limits import prompt_token_budget
|
||||
_clear(monkeypatch)
|
||||
opus = prompt_token_budget(*factory.resolve_model("anthropic", "claude-opus-4-8"))
|
||||
haiku = prompt_token_budget(*factory.resolve_model("anthropic", "claude-haiku-4-5"))
|
||||
local = prompt_token_budget(*factory.resolve_model("local", "llama3.1:8b"))
|
||||
assert opus > haiku > local > 0
|
||||
|
||||
@@ -69,11 +69,16 @@ class LogicClient:
|
||||
def prompt(
|
||||
self, profile: str, when_utc_iso: str, lat: float, lon: float,
|
||||
budget: str = "medium", from_date: str | None = None, to_date: str | None = None,
|
||||
provider: str | None = None, model: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Gotowy prompt do LLM z wyliczeń (LOG-29/30) — woła logic /chart/prompt."""
|
||||
"""Gotowy prompt do LLM z wyliczeń (LOG-29/30) — woła logic /chart/prompt.
|
||||
|
||||
`provider` jest potrzebny dla budżetu „maksymalny kontekst modelu": limit
|
||||
znaków zależy wtedy od okna kontekstu konkretnego modelu."""
|
||||
payload: dict[str, Any] = {
|
||||
"profile": profile, "when_utc": when_utc_iso,
|
||||
"lat": lat, "lon": lon, "budget": budget,
|
||||
"lat": lat, "lon": lon, "budget": budget, "provider": provider,
|
||||
"model": model,
|
||||
}
|
||||
if from_date and to_date:
|
||||
payload["from_date"], payload["to_date"] = from_date, to_date
|
||||
@@ -84,7 +89,7 @@ class LogicClient:
|
||||
|
||||
def horoscope(
|
||||
self, profile: str, when_utc_iso: str, lat: float, lon: float,
|
||||
budget: str = "medium", provider: str | None = None,
|
||||
budget: str = "medium", provider: str | None = None, model: str | None = None,
|
||||
from_date: str | None = None, to_date: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Napisany horoskop (LOG-31) — woła logic /chart/horoscope.
|
||||
@@ -97,6 +102,8 @@ class LogicClient:
|
||||
}
|
||||
if provider:
|
||||
payload["provider"] = provider
|
||||
if model:
|
||||
payload["model"] = model
|
||||
if from_date and to_date:
|
||||
payload["from_date"], payload["to_date"] = from_date, to_date
|
||||
with httpx.Client(timeout=max(settings.http_timeout, 300.0)) as client:
|
||||
@@ -104,6 +111,13 @@ class LogicClient:
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
def llm_models(self) -> dict[str, Any]:
|
||||
"""Katalog modeli per dostawca (podpowiedzi do pola wyboru w UI)."""
|
||||
with httpx.Client(timeout=settings.http_timeout) as client:
|
||||
r = client.get(f"{self.base_url}/llm/models", headers=_auth_headers())
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
def timeline(
|
||||
self, when_utc_iso: str, lat: float, lon: float,
|
||||
from_date: str, to_date: str, interpret: bool = True,
|
||||
|
||||
@@ -39,6 +39,15 @@ def _build_utc(date: str, time: str, tz_offset: float) -> tuple[str, str]:
|
||||
return utc.isoformat(), label
|
||||
|
||||
|
||||
def _llm_catalog() -> dict:
|
||||
"""Podpowiedzi modeli dla pola wyboru. Awaria logiki nie może wywrócić strony —
|
||||
pole modelu jest tekstowe, więc bez katalogu nadal da się wpisać model ręcznie."""
|
||||
try:
|
||||
return logic.llm_models()
|
||||
except httpx.HTTPError:
|
||||
return {"providers": {}, "defaults": {}}
|
||||
|
||||
|
||||
def _logic_error(e: Exception) -> str:
|
||||
if isinstance(e, httpx.HTTPStatusError) and e.response.status_code == 404:
|
||||
return (
|
||||
@@ -53,7 +62,8 @@ def _logic_error(e: Exception) -> str:
|
||||
def chart_form(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request, "chart.html",
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL},
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL,
|
||||
"llm_catalog": _llm_catalog()},
|
||||
)
|
||||
|
||||
|
||||
@@ -115,7 +125,8 @@ def significators_search(
|
||||
def interpret_form(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request, "interpret.html",
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL},
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL,
|
||||
"llm_catalog": _llm_catalog()},
|
||||
)
|
||||
|
||||
|
||||
@@ -131,22 +142,25 @@ def interpret_run(
|
||||
action: str = Form("report"),
|
||||
prompt_budget: str = Form("medium"),
|
||||
llm_provider: str = Form("local"),
|
||||
llm_model: str = Form(""),
|
||||
):
|
||||
form = {"date": date, "time": time, "tz_offset": tz_offset,
|
||||
"lat": lat, "lon": lon, "group": group, "prompt_budget": prompt_budget,
|
||||
"llm_provider": llm_provider}
|
||||
ctx: dict = {"form": form, "result": None, "error": None, "moment": None}
|
||||
"llm_provider": llm_provider, "llm_model": llm_model}
|
||||
ctx: dict = {"form": form, "result": None, "error": None, "moment": None,
|
||||
"llm_catalog": _llm_catalog()}
|
||||
try:
|
||||
iso_utc, label = _build_utc(date, time, tz_offset)
|
||||
ctx["moment"] = label
|
||||
if action == "prompt":
|
||||
ctx["prompt_result"] = logic.prompt(
|
||||
profile="natal", when_utc_iso=iso_utc, lat=lat, lon=lon, budget=prompt_budget,
|
||||
provider=llm_provider, model=llm_model,
|
||||
)
|
||||
elif action == "horoscope":
|
||||
ctx["prompt_result"] = logic.horoscope(
|
||||
profile="natal", when_utc_iso=iso_utc, lat=lat, lon=lon,
|
||||
budget=prompt_budget, provider=llm_provider,
|
||||
budget=prompt_budget, provider=llm_provider, model=llm_model,
|
||||
)
|
||||
else:
|
||||
ctx["result"] = logic.report(when_utc_iso=iso_utc, lat=lat, lon=lon, group=group)
|
||||
@@ -162,7 +176,8 @@ def interpret_run(
|
||||
def timeline_form(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request, "timeline.html",
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL},
|
||||
{"result": None, "form": default_form(), "location_label": DEFAULT_LOCATION_LABEL,
|
||||
"llm_catalog": _llm_catalog()},
|
||||
)
|
||||
|
||||
|
||||
@@ -179,11 +194,13 @@ def timeline_run(
|
||||
action: str = Form("timeline"),
|
||||
prompt_budget: str = Form("medium"),
|
||||
llm_provider: str = Form("local"),
|
||||
llm_model: str = Form(""),
|
||||
):
|
||||
form = {"date": date, "time": time, "tz_offset": tz_offset, "lat": lat, "lon": lon,
|
||||
"from_date": from_date, "to_date": to_date, "prompt_budget": prompt_budget,
|
||||
"llm_provider": llm_provider}
|
||||
ctx: dict = {"form": form, "result": None, "error": None, "moment": None}
|
||||
"llm_provider": llm_provider, "llm_model": llm_model}
|
||||
ctx: dict = {"form": form, "result": None, "error": None, "moment": None,
|
||||
"llm_catalog": _llm_catalog()}
|
||||
try:
|
||||
iso_utc, label = _build_utc(date, time, tz_offset)
|
||||
ctx["moment"] = label
|
||||
@@ -191,11 +208,12 @@ def timeline_run(
|
||||
ctx["prompt_result"] = logic.prompt(
|
||||
profile="period", when_utc_iso=iso_utc, lat=lat, lon=lon,
|
||||
budget=prompt_budget, from_date=from_date, to_date=to_date,
|
||||
provider=llm_provider, model=llm_model,
|
||||
)
|
||||
elif action == "horoscope":
|
||||
ctx["prompt_result"] = logic.horoscope(
|
||||
profile="period", when_utc_iso=iso_utc, lat=lat, lon=lon,
|
||||
budget=prompt_budget, provider=llm_provider,
|
||||
budget=prompt_budget, provider=llm_provider, model=llm_model,
|
||||
from_date=from_date, to_date=to_date,
|
||||
)
|
||||
else:
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// Podpowiedzi modeli zależne od wybranego dostawcy.
|
||||
//
|
||||
// Pole modelu jest CELOWO tekstowe (input + datalist), a nie zamkniętym <select>:
|
||||
// katalog to tylko wygoda, a konto może mieć dostęp do modeli, o których kod nie
|
||||
// wie. Puste pole = model domyślny dostawcy.
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const providerEl = document.getElementById('llmProvider');
|
||||
const modelEl = document.getElementById('llmModel');
|
||||
const listEl = document.getElementById('llmModelList');
|
||||
const hintEl = document.getElementById('llmModelHint');
|
||||
const dataEl = document.getElementById('llmCatalog');
|
||||
if (!providerEl || !modelEl || !listEl || !dataEl) return;
|
||||
|
||||
let catalog = {};
|
||||
let defaults = {};
|
||||
try {
|
||||
const parsed = JSON.parse(dataEl.textContent || '{}');
|
||||
catalog = parsed.providers || {};
|
||||
defaults = parsed.defaults || {};
|
||||
} catch (e) {
|
||||
return; // brak katalogu — pole nadal działa jako wolny tekst
|
||||
}
|
||||
|
||||
const fmt = n =>
|
||||
n >= 1000000 ? (n / 1000000) + 'M' : (n >= 1000 ? Math.round(n / 1000) + 'k' : String(n));
|
||||
|
||||
function refresh(resetValue) {
|
||||
const provider = providerEl.value;
|
||||
const models = catalog[provider] || [];
|
||||
|
||||
listEl.innerHTML = '';
|
||||
models.forEach(m => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = m.id;
|
||||
opt.label = m.label + ' · kontekst ' + fmt(m.context_window);
|
||||
listEl.appendChild(opt);
|
||||
});
|
||||
|
||||
const fallback = defaults[provider] || '';
|
||||
modelEl.placeholder = fallback ? 'domyślny: ' + fallback : 'domyślny dostawcy';
|
||||
|
||||
// po zmianie dostawcy stary model nie ma sensu (np. gpt-4o u Anthropica)
|
||||
if (resetValue) modelEl.value = '';
|
||||
|
||||
if (hintEl) {
|
||||
const chosen = models.find(m => m.id === modelEl.value.trim());
|
||||
hintEl.textContent = chosen
|
||||
? chosen.label + ' — okno kontekstu ' + fmt(chosen.context_window) +
|
||||
' tokenów, maksymalna odpowiedź ' + fmt(chosen.max_output) + '.'
|
||||
: 'Zostaw puste, by użyć modelu domyślnego. Możesz też wpisać dowolny ' +
|
||||
'identyfikator modelu, do którego Twoje konto ma dostęp.';
|
||||
}
|
||||
}
|
||||
|
||||
providerEl.addEventListener('change', () => refresh(true));
|
||||
modelEl.addEventListener('input', () => refresh(false));
|
||||
refresh(false);
|
||||
});
|
||||
@@ -1,3 +1,6 @@
|
||||
{# Katalog modeli wstrzykiwany przez handler — pole jest tekstowe, więc
|
||||
można wpisać dowolny model, do którego konto ma dostęp. #}
|
||||
<script type="application/json" id="llmCatalog">{{ llm_catalog | tojson }}</script>
|
||||
{# Wspólny blok: sterowanie generowaniem + prompt (LOG-29/30) + horoskop (LOG-31).
|
||||
Używany na ekranach Interpretacje i Kalendarz. #}
|
||||
<div class="opts">
|
||||
@@ -7,17 +10,26 @@
|
||||
<option value="concise" {{ 'selected' if pb == 'concise' else '' }}>zwięzły (~4 tys. znaków)</option>
|
||||
<option value="medium" {{ 'selected' if pb == 'medium' else '' }}>średni (~12 tys.)</option>
|
||||
<option value="extensive" {{ 'selected' if pb == 'extensive' else '' }}>obszerny (~30 tys.)</option>
|
||||
<option value="huge" {{ 'selected' if pb == 'huge' else '' }}>bardzo obszerny (~120 tys.)</option>
|
||||
<option value="max" {{ 'selected' if pb == 'max' else '' }}>maksymalny kontekst modelu</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>Model
|
||||
<select name="llm_provider">
|
||||
<label>Dostawca
|
||||
<select name="llm_provider" id="llmProvider">
|
||||
{% set lp = form.llm_provider or 'local' %}
|
||||
<option value="local" {{ 'selected' if lp == 'local' else '' }}>lokalny — nic nie opuszcza sieci</option>
|
||||
<option value="anthropic" {{ 'selected' if lp == 'anthropic' else '' }}>Anthropic — dane wychodzą</option>
|
||||
<option value="openai" {{ 'selected' if lp == 'openai' else '' }}>OpenAI — dane wychodzą</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>Model
|
||||
<input type="text" name="llm_model" id="llmModel" list="llmModelList"
|
||||
value="{{ form.llm_model or '' }}" placeholder="domyślny dostawcy"
|
||||
autocomplete="off">
|
||||
<datalist id="llmModelList"></datalist>
|
||||
</label>
|
||||
</div>
|
||||
<p class="muted small" id="llmModelHint"></p>
|
||||
<p class="muted small">
|
||||
Prompt zawiera <strong>oryginalne opisy z baz</strong> oraz dane urodzeniowe. Model lokalny
|
||||
przetwarza je u nas; wybór dostawcy w chmurze oznacza, że ta treść <strong>opuszcza naszą
|
||||
@@ -51,6 +63,23 @@
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if prompt_result.warnings %}
|
||||
{% for w in prompt_result.warnings %}
|
||||
<p class="muted small"><strong>Uwaga:</strong> {{ w }}</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if prompt_result.token_plan %}
|
||||
{% set tp = prompt_result.token_plan %}
|
||||
<p class="muted small">
|
||||
Tokeny: prompt {{ tp.prompt_tokens }} · okno modelu {{ tp.context_window }} ·
|
||||
zarezerwowane na odpowiedź {{ tp.max_output }}
|
||||
{% if prompt_result.usage and prompt_result.usage.turns and prompt_result.usage.turns > 1 %}
|
||||
· odpowiedź złożona z {{ prompt_result.usage.turns }} tur (model dokańczał urwany tekst)
|
||||
{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if prompt_result.llm_error %}
|
||||
<div class="error">
|
||||
Nie udało się napisać horoskopu: {{ prompt_result.llm_error }}<br>
|
||||
|
||||
@@ -90,4 +90,5 @@
|
||||
|
||||
<script src="/static/now.js"></script>
|
||||
<script src="/static/copy.js"></script>
|
||||
<script src="/static/models.js"></script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -80,4 +80,5 @@
|
||||
|
||||
<script src="/static/now.js"></script>
|
||||
<script src="/static/copy.js"></script>
|
||||
<script src="/static/models.js"></script>
|
||||
{% endblock %}
|
||||
|
||||
@@ -60,3 +60,52 @@ def test_auth_headers_helper_is_lazy():
|
||||
os.environ["INTERNAL_TOKEN"] = old
|
||||
else:
|
||||
os.environ.pop("INTERNAL_TOKEN", None)
|
||||
|
||||
|
||||
# --------------------------------------------- wybor modelu musi dojsc do logiki
|
||||
# Ta sama klasa bledu co przy tokenie: dokladajac nowa sciezke latwo zapomniec
|
||||
# przekazac parametr, a objaw (cichy powrot do modelu domyslnego) jest niewidoczny.
|
||||
|
||||
def _calls_to(path_fragment: str) -> list[int]:
|
||||
"""Linie wywolan logic.<metoda>(...) w handlerach prezentacji."""
|
||||
main = CLIENT.parent.parent / "main.py"
|
||||
tree = ast.parse(main.read_text(encoding="utf-8"))
|
||||
out = []
|
||||
for node in ast.walk(tree):
|
||||
if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
|
||||
and node.func.attr == path_fragment
|
||||
and isinstance(node.func.value, ast.Name) and node.func.value.id == "logic"):
|
||||
out.append(node.lineno)
|
||||
return out
|
||||
|
||||
|
||||
def _has_kwarg(main_src: str, lineno: int, name: str) -> bool:
|
||||
tree = ast.parse(main_src)
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Call) and node.lineno == lineno:
|
||||
return any(kw.arg == name for kw in node.keywords)
|
||||
return False
|
||||
|
||||
|
||||
def test_every_llm_call_passes_selected_model():
|
||||
main = CLIENT.parent.parent / "main.py"
|
||||
src = main.read_text(encoding="utf-8")
|
||||
missing = []
|
||||
for method in ("prompt", "horoscope"):
|
||||
for line in _calls_to(method):
|
||||
if not _has_kwarg(src, line, "model"):
|
||||
missing.append(f"main.py:{line} logic.{method}()")
|
||||
assert not missing, (
|
||||
"Wywołania bez wybranego modelu — po cichu użyją domyślnego: " + ", ".join(missing)
|
||||
)
|
||||
|
||||
|
||||
def test_every_llm_call_passes_provider():
|
||||
main = CLIENT.parent.parent / "main.py"
|
||||
src = main.read_text(encoding="utf-8")
|
||||
missing = []
|
||||
for method in ("prompt", "horoscope"):
|
||||
for line in _calls_to(method):
|
||||
if not _has_kwarg(src, line, "provider"):
|
||||
missing.append(f"main.py:{line} logic.{method}()")
|
||||
assert not missing, "Wywołania bez dostawcy: " + ", ".join(missing)
|
||||
|
||||
Reference in New Issue
Block a user