mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 12:32: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:
@ -203,7 +203,6 @@ class Canvas(Graph):
|
|||||||
self.history = []
|
self.history = []
|
||||||
self.retrieval = []
|
self.retrieval = []
|
||||||
self.memory = []
|
self.memory = []
|
||||||
|
|
||||||
for k in self.globals.keys():
|
for k in self.globals.keys():
|
||||||
if isinstance(self.globals[k], str):
|
if isinstance(self.globals[k], str):
|
||||||
self.globals[k] = ""
|
self.globals[k] = ""
|
||||||
@ -292,7 +291,6 @@ class Canvas(Graph):
|
|||||||
"thoughts": self.get_component_thoughts(self.path[i])
|
"thoughts": self.get_component_thoughts(self.path[i])
|
||||||
})
|
})
|
||||||
_run_batch(idx, to)
|
_run_batch(idx, to)
|
||||||
|
|
||||||
# post processing of components invocation
|
# post processing of components invocation
|
||||||
for i in range(idx, to):
|
for i in range(idx, to):
|
||||||
cpn = self.get_component(self.path[i])
|
cpn = self.get_component(self.path[i])
|
||||||
@ -393,7 +391,6 @@ class Canvas(Graph):
|
|||||||
self.path = path
|
self.path = path
|
||||||
yield decorate("user_inputs", {"inputs": another_inputs, "tips": tips})
|
yield decorate("user_inputs", {"inputs": another_inputs, "tips": tips})
|
||||||
return
|
return
|
||||||
|
|
||||||
self.path = self.path[:idx]
|
self.path = self.path[:idx]
|
||||||
if not self.error:
|
if not self.error:
|
||||||
yield decorate("workflow_finished",
|
yield decorate("workflow_finished",
|
||||||
|
|||||||
@ -346,7 +346,7 @@ Respond immediately with your final comprehensive answer.
|
|||||||
|
|
||||||
return "Error occurred."
|
return "Error occurred."
|
||||||
|
|
||||||
def reset(self):
|
def reset(self, temp=False):
|
||||||
for k, cpn in self.tools.items():
|
for k, cpn in self.tools.items():
|
||||||
cpn.reset()
|
cpn.reset()
|
||||||
|
|
||||||
|
|||||||
@ -997,7 +997,7 @@ class RAGFlowPdfParser:
|
|||||||
self.__ocr(i + 1, img, chars, zoomin, id)
|
self.__ocr(i + 1, img, chars, zoomin, id)
|
||||||
|
|
||||||
if callback and i % 6 == 5:
|
if callback and i % 6 == 5:
|
||||||
callback(prog=(i + 1) * 0.6 / len(self.page_images), msg="")
|
callback((i + 1) * 0.6 / len(self.page_images), msg="")
|
||||||
|
|
||||||
async def __img_ocr_launcher():
|
async def __img_ocr_launcher():
|
||||||
def __ocr_preprocess():
|
def __ocr_preprocess():
|
||||||
|
|||||||
@ -404,8 +404,8 @@ class Parser(ProcessBase):
|
|||||||
|
|
||||||
_add_content(msg, msg.get_content_type())
|
_add_content(msg, msg.get_content_type())
|
||||||
|
|
||||||
email_content["text"] = body_text
|
email_content["text"] = "\n".join(body_text)
|
||||||
email_content["text_html"] = body_html
|
email_content["text_html"] = "\n".join(body_html)
|
||||||
# get attachment
|
# get attachment
|
||||||
if "attachments" in target_fields:
|
if "attachments" in target_fields:
|
||||||
attachments = []
|
attachments = []
|
||||||
|
|||||||
@ -33,35 +33,52 @@ class Session(Base):
|
|||||||
self.__session_type = "agent"
|
self.__session_type = "agent"
|
||||||
super().__init__(rag, res_dict)
|
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":
|
if self.__session_type == "agent":
|
||||||
res = self._ask_agent(question, stream)
|
res = self._ask_agent(question, stream)
|
||||||
elif self.__session_type == "chat":
|
elif self.__session_type == "chat":
|
||||||
res = self._ask_chat(question, stream, **kwargs)
|
res = self._ask_chat(question, stream, **kwargs)
|
||||||
|
else:
|
||||||
|
raise Exception(f"Unknown session type: {self.__session_type}")
|
||||||
|
|
||||||
if stream:
|
if stream:
|
||||||
for line in res.iter_lines():
|
for line in res.iter_lines(decode_unicode=True):
|
||||||
line = line.decode("utf-8")
|
if not line:
|
||||||
if line.startswith("{"):
|
continue # Skip empty lines
|
||||||
json_data = json.loads(line)
|
line = line.strip()
|
||||||
raise Exception(json_data["message"])
|
|
||||||
if not line.startswith("data:"):
|
if line.startswith("data:"):
|
||||||
continue
|
content = line[len("data:"):].strip()
|
||||||
json_data = json.loads(line[5:])
|
if content == "[DONE]":
|
||||||
if json_data["data"] is True or json_data["data"].get("running_status"):
|
break # End of stream
|
||||||
continue
|
else:
|
||||||
message = self._structure_answer(json_data)
|
content = line
|
||||||
yield message
|
|
||||||
|
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:
|
else:
|
||||||
try:
|
try:
|
||||||
json_data = json.loads(res.text)
|
json_data = res.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise Exception(f"Invalid response {res}")
|
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):
|
def _structure_answer(self, json_data):
|
||||||
answer = json_data["data"]["answer"]
|
answer = json_data["data"]["content"]
|
||||||
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