mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
Fix: Agent.reset() argument wrong #10463 & Unable to converse with agent through Python API. #10415 (#10472)
### What problem does this PR solve? Fix: Agent.reset() argument wrong #10463 & Unable to converse with agent through Python API. #10415 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
@ -33,35 +33,52 @@ class Session(Base):
|
||||
self.__session_type = "agent"
|
||||
super().__init__(rag, res_dict)
|
||||
|
||||
def ask(self, question="", stream=True, **kwargs):
|
||||
|
||||
def ask(self, question="", stream=False, **kwargs):
|
||||
"""
|
||||
Ask a question to the session. If stream=True, yields Message objects as they arrive (SSE streaming).
|
||||
If stream=False, returns a single Message object for the final answer.
|
||||
"""
|
||||
if self.__session_type == "agent":
|
||||
res = self._ask_agent(question, stream)
|
||||
elif self.__session_type == "chat":
|
||||
res = self._ask_chat(question, stream, **kwargs)
|
||||
else:
|
||||
raise Exception(f"Unknown session type: {self.__session_type}")
|
||||
|
||||
if stream:
|
||||
for line in res.iter_lines():
|
||||
line = line.decode("utf-8")
|
||||
if line.startswith("{"):
|
||||
json_data = json.loads(line)
|
||||
raise Exception(json_data["message"])
|
||||
if not line.startswith("data:"):
|
||||
continue
|
||||
json_data = json.loads(line[5:])
|
||||
if json_data["data"] is True or json_data["data"].get("running_status"):
|
||||
continue
|
||||
message = self._structure_answer(json_data)
|
||||
yield message
|
||||
for line in res.iter_lines(decode_unicode=True):
|
||||
if not line:
|
||||
continue # Skip empty lines
|
||||
line = line.strip()
|
||||
|
||||
if line.startswith("data:"):
|
||||
content = line[len("data:"):].strip()
|
||||
if content == "[DONE]":
|
||||
break # End of stream
|
||||
else:
|
||||
content = line
|
||||
|
||||
try:
|
||||
json_data = json.loads(content)
|
||||
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
|
||||
else:
|
||||
try:
|
||||
json_data = json.loads(res.text)
|
||||
json_data = res.json()
|
||||
except ValueError:
|
||||
raise Exception(f"Invalid response {res}")
|
||||
return self._structure_answer(json_data)
|
||||
yield self._structure_answer(json_data["data"])
|
||||
|
||||
|
||||
def _structure_answer(self, json_data):
|
||||
answer = json_data["data"]["answer"]
|
||||
answer = json_data["data"]["content"]
|
||||
reference = json_data["data"].get("reference", {})
|
||||
temp_dict = {
|
||||
"content": answer,
|
||||
|
||||
Reference in New Issue
Block a user