Fix session.ask return generator bug when stream=False on python sdk (#5209)

add non-stream mode support to session.ask function

### What problem does this PR solve?

same as title, I do not know why the stream=False is not work on the
server side also, when stream=False, the response in the
session._ask_chat is a fully connnected SSE string.

This is a quick fix on the sdk side to make the response format align
with API docs

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Vela-zz 2025-02-21 12:50:08 +09:00 committed by GitHub
parent 7b3d700d5f
commit c54ec09519
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -38,6 +38,7 @@ class Session(Base):
res = self._ask_agent(question, stream)
elif self.__session_type == "chat":
res = self._ask_chat(question, stream, **kwargs)
for line in res.iter_lines():
line = line.decode("utf-8")
if line.startswith("{"):
@ -58,8 +59,11 @@ class Session(Base):
chunks = reference["chunks"]
temp_dict["reference"] = chunks
message = Message(self.rag, temp_dict)
yield message
if stream:
yield message
if not stream:
return message
def _ask_chat(self, question: str, stream: bool, **kwargs):
json_data = {"question": question, "stream": True, "session_id": self.id}
json_data.update(kwargs)