mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-07-11 03:31:47 +08:00
Fix: add support for non-stream response with session.ask_without_stream (#6207)
Add support for non-stream response with session.ask_without_stream and fix a typo mistake in python API doc There are requirements for non-stream response, especially for commands exection, e.g. text2SQL. The commands have to be completed before the agent is triggered. ### What problem does this PR solve? It's to fix the [Issue: 6206](https://github.com/infiniflow/ragflow/issues/6206) ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [ ] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe): --------- Co-authored-by: Howard WU <yuanhao.wu@ifudata.com> Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com>
This commit is contained in:
parent
57cbefa589
commit
897fe85b5c
@ -1485,8 +1485,8 @@ The parameters in `begin` component.
|
|||||||
from ragflow_sdk import RAGFlow, Agent
|
from ragflow_sdk import RAGFlow, Agent
|
||||||
|
|
||||||
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
|
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
|
||||||
AGENT_ID = "AGENT_ID"
|
agent_id = "AGENT_ID"
|
||||||
agent = rag_object.list_agents(id = AGENT_id)[0]
|
agent = rag_object.list_agents(id = agent_id)[0]
|
||||||
session = agent.create_session()
|
session = agent.create_session()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -39,6 +39,7 @@ class Session(Base):
|
|||||||
elif self.__session_type == "chat":
|
elif self.__session_type == "chat":
|
||||||
res = self._ask_chat(question, stream, **kwargs)
|
res = self._ask_chat(question, stream, **kwargs)
|
||||||
|
|
||||||
|
if stream:
|
||||||
for line in res.iter_lines():
|
for line in res.iter_lines():
|
||||||
line = line.decode("utf-8")
|
line = line.decode("utf-8")
|
||||||
if line.startswith("{"):
|
if line.startswith("{"):
|
||||||
@ -59,9 +60,22 @@ class Session(Base):
|
|||||||
chunks = reference["chunks"]
|
chunks = reference["chunks"]
|
||||||
temp_dict["reference"] = chunks
|
temp_dict["reference"] = chunks
|
||||||
message = Message(self.rag, temp_dict)
|
message = Message(self.rag, temp_dict)
|
||||||
if stream:
|
|
||||||
yield message
|
yield message
|
||||||
if not stream:
|
else:
|
||||||
|
try:
|
||||||
|
json_data = json.loads(res.text)
|
||||||
|
except ValueError:
|
||||||
|
raise Exception(f"Invalid response {res}")
|
||||||
|
answer = json_data["data"]["answer"]
|
||||||
|
reference = json_data["data"].get("reference", {})
|
||||||
|
temp_dict = {
|
||||||
|
"content": answer,
|
||||||
|
"role": "assistant"
|
||||||
|
}
|
||||||
|
if reference and "chunks" in reference:
|
||||||
|
chunks = reference["chunks"]
|
||||||
|
temp_dict["reference"] = chunks
|
||||||
|
message = Message(self.rag, temp_dict)
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def _ask_chat(self, question: str, stream: bool, **kwargs):
|
def _ask_chat(self, question: str, stream: bool, **kwargs):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user