fix(agent): 增强 LLM 流式回复兜底

This commit is contained in:
2026-06-06 19:45:13 +08:00
parent c78ff3a1fd
commit daa0642142
3 changed files with 82 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import json
import logging
from urllib import error, request
from django.conf import settings
@@ -12,6 +13,9 @@ class LLMRequestError(RuntimeError):
"""Raised when the remote LLM provider call fails."""
logger = logging.getLogger(__name__)
def generate_reply(conversation, user_message: str) -> str:
"""Calls the SiliconFlow OpenAI-compatible chat endpoint and returns assistant text."""
@@ -130,7 +134,11 @@ def stream_reply(conversation, user_message: str):
data = line[5:].strip()
if data == "[DONE]":
break
payload = json.loads(data)
try:
payload = json.loads(data)
except json.JSONDecodeError:
logger.warning("Skipping malformed LLM stream data", extra={"data": data[:200]})
continue
delta = (
payload.get("choices", [{}])[0]
.get("delta", {})