mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Fix: Chat session completion (#10851)
### What problem does this PR solve? Fix: Chat session completion #10791 ### Type of change - [X] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
@ -15,7 +15,6 @@
|
||||
#
|
||||
|
||||
import json
|
||||
|
||||
from .base import Base
|
||||
|
||||
|
||||
@ -51,7 +50,6 @@ class Session(Base):
|
||||
if not line:
|
||||
continue # Skip empty lines
|
||||
line = line.strip()
|
||||
|
||||
if line.startswith("data:"):
|
||||
content = line[len("data:"):].strip()
|
||||
if content == "[DONE]":
|
||||
@ -64,21 +62,26 @@ class Session(Base):
|
||||
except json.JSONDecodeError:
|
||||
continue # Skip lines that are not valid JSON
|
||||
|
||||
event = json_data.get("event")
|
||||
if event == "message":
|
||||
yield self._structure_answer(json_data)
|
||||
elif event == "message_end":
|
||||
return # End of message stream
|
||||
if (
|
||||
(self.__session_type == "agent" and json_data.get("event") == "message_end")
|
||||
or (self.__session_type == "chat" and json_data.get("data") is True)
|
||||
):
|
||||
return
|
||||
|
||||
yield self._structure_answer(json_data)
|
||||
else:
|
||||
try:
|
||||
json_data = res.json()
|
||||
except ValueError:
|
||||
raise Exception(f"Invalid response {res}")
|
||||
yield self._structure_answer(json_data["data"])
|
||||
yield self._structure_answer(json_data)
|
||||
|
||||
|
||||
def _structure_answer(self, json_data):
|
||||
answer = json_data["data"]["content"]
|
||||
if self.__session_type == "agent":
|
||||
answer = json_data["data"]["data"]["content"]
|
||||
elif self.__session_type == "chat":
|
||||
answer =json_data["data"]["answer"]
|
||||
reference = json_data["data"].get("reference", {})
|
||||
temp_dict = {
|
||||
"content": answer,
|
||||
|
||||
Reference in New Issue
Block a user