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,22 +50,17 @@ class Session(Base):
json_data = json.loads(line[5:]) json_data = json.loads(line[5:])
if json_data["data"] is True or json_data["data"].get("running_status"): if json_data["data"] is True or json_data["data"].get("running_status"):
continue continue
answer = json_data["data"]["answer"] message = self._structure_answer(json_data)
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)
yield message yield message
else: else:
try: try:
json_data = json.loads(res.text) json_data = json.loads(res.text)
except ValueError: except ValueError:
raise Exception(f"Invalid response {res}") raise Exception(f"Invalid response {res}")
return self._structure_answer(json_data)
def _structure_answer(self, json_data):
answer = json_data["data"]["answer"] answer = json_data["data"]["answer"]
reference = json_data["data"].get("reference", {}) reference = json_data["data"].get("reference", {})
temp_dict = { temp_dict = {