This commit is contained in:
Mikan
2026-06-19 19:14:27 +03:00
parent 2167493887
commit 32575e217e
23 changed files with 1191 additions and 92 deletions

View File

@@ -16,6 +16,37 @@ from app.models import LlmCallLog
log = get_logger("llm")
# vendor-specific end-of-sequence / control tokens that some local models
# emit into the content stream. Strip them so they don't leak to the user.
_EOS_TOKENS = (
"<eos>",
"</s>",
"<|endoftext|>",
"<|im_end|>",
"<|end|>",
"<|eot_id|>",
"<|eom_id|>",
)
def _clean_model_text(text: str) -> str:
"""Remove vendor-specific end-of-sequence tokens and collapse whitespace.
Some local models leak control tokens into the visible content stream.
We strip them so they never reach the user.
"""
if not text:
return text
cleaned = text
for tok in _EOS_TOKENS:
cleaned = cleaned.replace(tok, "")
while "\n\n\n" in cleaned:
cleaned = cleaned.replace("\n\n\n", "\n\n")
return cleaned
class LlmResponse:
"""Non-streaming response wrapper."""
@@ -93,7 +124,7 @@ class LlmClient:
data = resp.json()
choice = (data.get("choices") or [{}])[0]
msg = choice.get("message", {})
text = msg.get("content") or ""
text = _clean_model_text(msg.get("content") or "")
tool_calls = msg.get("tool_calls") or []
usage = data.get("usage") or {}
except httpx.ConnectError as e:
@@ -206,8 +237,10 @@ class LlmClient:
continue
delta = choices[0].get("delta", {})
if delta.get("content"):
full_text_parts.append(delta["content"])
yield {"type": "delta", "content": delta["content"]}
piece = _clean_model_text(delta["content"])
if piece:
full_text_parts.append(piece)
yield {"type": "delta", "content": piece}
if delta.get("tool_calls"):
for tc in delta["tool_calls"]:
idx = tc.get("index", 0)