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
|
import json
|
||||||
|
|
||||||
from .base import Base
|
from .base import Base
|
||||||
|
|
||||||
|
|
||||||
@ -51,7 +50,6 @@ class Session(Base):
|
|||||||
if not line:
|
if not line:
|
||||||
continue # Skip empty lines
|
continue # Skip empty lines
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
if line.startswith("data:"):
|
if line.startswith("data:"):
|
||||||
content = line[len("data:"):].strip()
|
content = line[len("data:"):].strip()
|
||||||
if content == "[DONE]":
|
if content == "[DONE]":
|
||||||
@ -64,21 +62,26 @@ class Session(Base):
|
|||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
continue # Skip lines that are not valid JSON
|
continue # Skip lines that are not valid JSON
|
||||||
|
|
||||||
event = json_data.get("event")
|
if (
|
||||||
if event == "message":
|
(self.__session_type == "agent" and json_data.get("event") == "message_end")
|
||||||
yield self._structure_answer(json_data)
|
or (self.__session_type == "chat" and json_data.get("data") is True)
|
||||||
elif event == "message_end":
|
):
|
||||||
return # End of message stream
|
return
|
||||||
|
|
||||||
|
yield self._structure_answer(json_data)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
json_data = res.json()
|
json_data = res.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise Exception(f"Invalid response {res}")
|
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):
|
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", {})
|
reference = json_data["data"].get("reference", {})
|
||||||
temp_dict = {
|
temp_dict = {
|
||||||
"content": answer,
|
"content": answer,
|
||||||
|
|||||||
Reference in New Issue
Block a user