fix: 修复流式聊天部分问题
This commit is contained in:
parent
22a4b8a4bb
commit
9edf4dac9c
|
|
@ -213,30 +213,23 @@ class ChatService:
|
|||
|
||||
# Get delta
|
||||
choices = chunk.get("choices", [])
|
||||
if not choices:
|
||||
delta = None
|
||||
|
||||
if choices:
|
||||
delta = choices[0].get("delta", {})
|
||||
# If no delta but has message (non-streaming response)
|
||||
if not delta:
|
||||
message = choices[0].get("message", {})
|
||||
if message.get("content"):
|
||||
delta = {"content": message["content"]}
|
||||
|
||||
if not delta:
|
||||
# Check if there's any content in the response (for non-standard LLM responses)
|
||||
if chunk.get("content") or chunk.get("message"):
|
||||
content = chunk.get("content") or chunk.get("message", {}).get("content", "")
|
||||
if content:
|
||||
# BUG FIX: Update full_content so it gets saved to database
|
||||
prev_content_len = len(full_content)
|
||||
full_content += content
|
||||
if prev_content_len == 0: # New text stream started
|
||||
text_step_idx = step_index
|
||||
text_step_id = f"step-{step_index}"
|
||||
step_index += 1
|
||||
yield _sse_event("process_step", {
|
||||
"step": {
|
||||
"id": text_step_id if prev_content_len == 0 else f"step-{step_index - 1}",
|
||||
"index": text_step_idx if prev_content_len == 0 else step_index - 1,
|
||||
"type": "text",
|
||||
"content": full_content # Always send accumulated content
|
||||
}
|
||||
})
|
||||
continue
|
||||
|
||||
delta = choices[0].get("delta", {})
|
||||
delta = {"content": content}
|
||||
|
||||
if delta:
|
||||
# Handle reasoning (thinking)
|
||||
reasoning = delta.get("reasoning_content", "")
|
||||
if reasoning:
|
||||
|
|
|
|||
|
|
@ -143,30 +143,23 @@ class LLMClient:
|
|||
tools: Optional[List[Dict]] = None,
|
||||
**kwargs
|
||||
) -> AsyncGenerator[str, None]:
|
||||
"""Stream call LLM API - yields raw SSE event lines
|
||||
|
||||
Yields:
|
||||
str: Raw SSE event lines for direct forwarding
|
||||
"""
|
||||
"""Stream call LLM API - yields raw SSE event lines"""
|
||||
body = self._build_body(model, messages, tools, stream=True, **kwargs)
|
||||
|
||||
logger.info(f"Starting stream_call for model: {model}, messages count: {len(messages)}")
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=120.0) as client:
|
||||
logger.info(f"Sending request to {self.api_url}")
|
||||
async with client.stream(
|
||||
"POST",
|
||||
self.api_url,
|
||||
headers=self._build_headers(),
|
||||
json=body
|
||||
) as response:
|
||||
logger.info(f"Response status: {response.status_code}")
|
||||
response.raise_for_status()
|
||||
|
||||
async for line in response.aiter_lines():
|
||||
if line.strip():
|
||||
yield line + "\n"
|
||||
|
||||
logger.info(f"response finish with : {response.status_code}")
|
||||
except httpx.HTTPStatusError as e:
|
||||
status_code = e.response.status_code if e.response else "?"
|
||||
logger.error(f"HTTP error: {status_code}")
|
||||
|
|
|
|||
Loading…
Reference in New Issue