Refa: Chats /chat API to RESTFul (#13871)

### What problem does this PR solve?

Chats /chat API to RESTFul.

### Type of change

- [x] Refactoring
This commit is contained in:
Yongteng Lei
2026-04-01 10:50:22 +08:00
committed by GitHub
parent 00b62dd587
commit 1a608ac411
52 changed files with 2058 additions and 3598 deletions

View File

@@ -977,76 +977,13 @@ class RAGFlowClient:
def create_user_chat(self, command):
if self.server_type != "user":
print("This command is only allowed in USER mode")
'''
description
:
""
icon
:
""
language
:
"English"
llm_id
:
"glm-4-flash@ZHIPU-AI"
llm_setting
:
{}
name
:
"xx"
prompt_config
:
{empty_response: "", prologue: "Hi! I'm your assistant. What can I do for you?", quote: true,…}
empty_response
:
""
keyword
:
false
parameters
:
[{key: "knowledge", optional: false}]
prologue
:
"Hi! I'm your assistant. What can I do for you?"
quote
:
true
reasoning
:
false
refine_multiturn
:
false
system
:
"You are an intelligent assistant. Your primary function is to answer questions based strictly on the provided knowledge base.\n\n **Essential Rules:**\n - Your answer must be derived **solely** from this knowledge base: `{knowledge}`.\n - **When information is available**: Summarize the content to give a detailed answer.\n - **When information is unavailable**: Your response must contain this exact sentence: \"The answer you are looking for is not found in the knowledge base!\"\n - **Always consider** the entire conversation history."
toc_enhance
:
false
tts
:
false
use_kg
:
false
similarity_threshold
:
0.2
top_n
:
8
vector_similarity_weight
:
0.3
'''
chat_name = command["chat_name"]
default_models = self._get_default_models() or {}
payload = {
"name": chat_name,
"description": "",
"icon": "",
"language": "English",
"dataset_ids": [],
"llm_setting": {},
"prompt_config": {
"empty_response": "",
@@ -1064,16 +1001,24 @@ class RAGFlowClient:
"optional": False
}
],
"toc_enhance": False
"toc_enhance": False,
},
"similarity_threshold": 0.2,
"top_n": 8,
"vector_similarity_weight": 0.3
"top_k": 1024,
"vector_similarity_weight": 0.3,
"rerank_id": default_models.get("rerank_id", ""),
}
if default_models.get("llm_id"):
payload["llm_id"] = default_models["llm_id"]
payload.update({"name": chat_name})
response = self.http_client.request("POST", "/dialog/set", json_body=payload, use_api_base=False,
auth_kind="web")
response = self.http_client.request(
"POST",
"/chats",
json_body=payload,
use_api_base=True,
auth_kind="web",
)
res_json = response.json()
if response.status_code == 200 and res_json["code"] == 0:
print(f"Success to create chat: {chat_name}")
@@ -1158,9 +1103,14 @@ class RAGFlowClient:
for elem in res_json:
if elem["name"] == chat_name:
to_drop_chat_ids.append(elem["id"])
payload = {"dialog_ids": to_drop_chat_ids}
response = self.http_client.request("POST", "/dialog/rm", json_body=payload, use_api_base=False,
auth_kind="web")
payload = {"ids": to_drop_chat_ids}
response = self.http_client.request(
"DELETE",
"/chats",
json_body=payload,
use_api_base=True,
auth_kind="web",
)
res_json = response.json()
if response.status_code == 200 and res_json["code"] == 0:
print(f"Success to drop chat: {chat_name}")
@@ -1622,17 +1572,27 @@ class RAGFlowClient:
def _list_chats(self, command):
iterations = command.get("iterations", 1)
if iterations > 1:
response = self.http_client.request("POST", "/dialog/next", use_api_base=False, auth_kind="web",
iterations=iterations)
response = self.http_client.request(
"GET",
"/chats",
use_api_base=True,
auth_kind="web",
iterations=iterations,
)
return response
else:
response = self.http_client.request("POST", "/dialog/next", use_api_base=False, auth_kind="web",
iterations=iterations)
response = self.http_client.request(
"GET",
"/chats",
use_api_base=True,
auth_kind="web",
iterations=iterations,
)
res_json = response.json()
if response.status_code == 200 and res_json["code"] == 0:
return res_json["data"]["dialogs"]
return res_json["data"]["chats"]
else:
print(f"Fail to list datasets, code: {res_json['code']}, message: {res_json['message']}")
print(f"Fail to list chats, code: {res_json['code']}, message: {res_json['message']}")
return None
def _get_default_models(self):