Refa: Structure Ask Message (#8276)

### What problem does this PR solve?

Refactoring codes for SDK

### Type of change

- [x] Refactoring
This commit is contained in:
Stephen Hu 2025-06-16 10:17:21 +08:00 committed by GitHub
parent df17294865
commit 545ea229b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,33 +50,28 @@ class Session(Base):
json_data = json.loads(line[5:])
if json_data["data"] is True or json_data["data"].get("running_status"):
continue
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)
message = self._structure_answer(json_data)
yield message
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 self._structure_answer(json_data)
def _structure_answer(self, json_data):
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
def _ask_chat(self, question: str, stream: bool, **kwargs):
json_data = {"question": question, "stream": stream, "session_id": self.id}