瀏覽代碼

Refa: Structure Ask Message (#8276)

### What problem does this PR solve?

Refactoring codes for SDK

### Type of change

- [x] Refactoring
tags/v0.19.1
Stephen Hu 4 月之前
父節點
當前提交
545ea229b6
沒有連結到貢獻者的電子郵件帳戶。
共有 1 個檔案被更改,包括 16 行新增21 行删除
  1. 16
    21
      sdk/python/ragflow_sdk/modules/session.py

+ 16
- 21
sdk/python/ragflow_sdk/modules/session.py 查看文件

@@ -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}

Loading…
取消
儲存