mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-03 14:27:32 +08:00
Implement Delete in GO and refactor functions (#13974)
### What problem does this PR solve? Implement Delete in GO and refactor functions ### Type of change - [x] Refactoring <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a remove_chunks command to delete specific or all chunks from a document. * Added new endpoints for chunk removal and chunk update. * **Refactor** * Renamed index commands to dataset/metadata table terminology and updated REST routes accordingly. * Updated chunk update flow to a JSON POST style and improved metadata error messages. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1029,7 +1029,7 @@ class RAGFlowClient:
|
||||
else:
|
||||
print(f"Fail to create chat {chat_name}, code: {res_json['code']}, message: {res_json['message']}")
|
||||
|
||||
def create_index(self, command):
|
||||
def create_dataset_table(self, command):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
return
|
||||
@@ -1045,15 +1045,15 @@ class RAGFlowClient:
|
||||
# Build payload
|
||||
payload = {"kb_id": dataset_id, "vector_size": vector_size}
|
||||
# Call API
|
||||
response = self.http_client.request("POST", "/kb/index", json_body=payload,
|
||||
response = self.http_client.request("POST", "/kb/doc_engine_table", json_body=payload,
|
||||
use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200 and res_json.get("code") == 0:
|
||||
print(f"Success to create index for dataset: {dataset_name}")
|
||||
print(f"Success to create table for dataset: {dataset_name}")
|
||||
else:
|
||||
print(f"Fail to create index for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
print(f"Fail to create table for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
|
||||
def drop_index(self, command):
|
||||
def drop_dataset_table(self, command):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
return
|
||||
@@ -1062,41 +1062,41 @@ class RAGFlowClient:
|
||||
dataset_id = self._get_dataset_id(dataset_name)
|
||||
if dataset_id is None:
|
||||
return
|
||||
# Call API to delete index
|
||||
# Call API to delete table
|
||||
payload = {"kb_id": dataset_id}
|
||||
response = self.http_client.request("DELETE", "/kb/index", json_body=payload,
|
||||
response = self.http_client.request("DELETE", "/kb/doc_engine_table", json_body=payload,
|
||||
use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200 and res_json.get("code") == 0:
|
||||
print(f"Success to drop index for dataset: {dataset_name}")
|
||||
print(f"Success to drop table for dataset: {dataset_name}")
|
||||
else:
|
||||
print(f"Fail to drop index for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
print(f"Fail to drop table for dataset {dataset_name}, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
|
||||
def create_doc_meta_index(self, command):
|
||||
def create_metadata_table(self, command):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
return
|
||||
# Call API to create doc meta index
|
||||
response = self.http_client.request("POST", "/tenant/doc_meta_index",
|
||||
# Call API to create metadata table
|
||||
response = self.http_client.request("POST", "/tenant/doc_engine_metadata_table",
|
||||
use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200 and res_json.get("code") == 0:
|
||||
print("Success to create doc meta index")
|
||||
print("Success to create metadata table")
|
||||
else:
|
||||
print(f"Fail to create doc meta index, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
print(f"Fail to create metadata table, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
|
||||
def drop_doc_meta_index(self, command):
|
||||
def drop_metadata_table(self, command):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
return
|
||||
# Call API to delete doc meta index
|
||||
response = self.http_client.request("DELETE", "/tenant/doc_meta_index",
|
||||
# Call API to delete metadata table
|
||||
response = self.http_client.request("DELETE", "/tenant/doc_engine_metadata_table",
|
||||
use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200 and res_json.get("code") == 0:
|
||||
print("Success to drop doc meta index")
|
||||
print("Success to drop metadata table")
|
||||
else:
|
||||
print(f"Fail to drop doc meta index, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
print(f"Fail to drop metadata table, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
|
||||
def drop_user_chat(self, command):
|
||||
if self.server_type != "user":
|
||||
@@ -1548,9 +1548,13 @@ class RAGFlowClient:
|
||||
print(f"Invalid JSON body: {e}")
|
||||
return
|
||||
|
||||
# Call PUT /datasets/{dataset_id}/documents/{doc_id}/chunks/{chunk_id}
|
||||
path = f"/datasets/{dataset_id}/documents/{doc_id}/chunks/{chunk_id}"
|
||||
response = self.http_client.request("PUT", path, json_body=payload, use_api_base=True, auth_kind="api")
|
||||
# Add IDs to payload
|
||||
payload["dataset_id"] = dataset_id
|
||||
payload["document_id"] = doc_id
|
||||
payload["chunk_id"] = chunk_id
|
||||
|
||||
# Call POST /v1/chunk/update
|
||||
response = self.http_client.request("POST", "/chunk/update", json_body=payload, use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200:
|
||||
if res_json.get("code") == 0:
|
||||
@@ -1583,7 +1587,7 @@ class RAGFlowClient:
|
||||
else:
|
||||
print(f"Fail to set metadata, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
else:
|
||||
print(f"Fail to set metadata, HTTP {response.status_code}")
|
||||
print(f"Fail to set metadata, HTTP {response.status_code}: {res_json.get('message', 'no message')}")
|
||||
|
||||
def remove_tags(self, command_dict):
|
||||
if self.server_type != "user":
|
||||
@@ -1613,6 +1617,31 @@ class RAGFlowClient:
|
||||
else:
|
||||
print(f"Fail to remove tags, HTTP {response.status_code}")
|
||||
|
||||
def remove_chunks(self, command_dict):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
return
|
||||
|
||||
doc_id = command_dict["doc_id"]
|
||||
payload = {"doc_id": doc_id}
|
||||
|
||||
if command_dict.get("delete_all"):
|
||||
payload["delete_all"] = True
|
||||
elif command_dict.get("chunk_ids"):
|
||||
payload["chunk_ids"] = command_dict["chunk_ids"]
|
||||
|
||||
response = self.http_client.request("POST", "/chunk/rm", json_body=payload,
|
||||
use_api_base=False, auth_kind="web")
|
||||
res_json = response.json()
|
||||
if response.status_code == 200:
|
||||
if res_json.get("code") == 0:
|
||||
deleted_count = res_json.get("data", 0)
|
||||
print(f"Success to remove chunks from document {doc_id}: {deleted_count} chunks deleted")
|
||||
else:
|
||||
print(f"Fail to remove chunks, code: {res_json.get('code')}, message: {res_json.get('message')}")
|
||||
else:
|
||||
print(f"Fail to remove chunks, HTTP {response.status_code}")
|
||||
|
||||
def list_chunks(self, command_dict):
|
||||
if self.server_type != "user":
|
||||
print("This command is only allowed in USER mode")
|
||||
@@ -1976,14 +2005,14 @@ def run_command(client: RAGFlowClient, command_dict: dict):
|
||||
client.create_user_chat(command_dict)
|
||||
case "drop_user_chat":
|
||||
client.drop_user_chat(command_dict)
|
||||
case "create_index":
|
||||
client.create_index(command_dict)
|
||||
case "drop_index":
|
||||
client.drop_index(command_dict)
|
||||
case "create_doc_meta_index":
|
||||
client.create_doc_meta_index(command_dict)
|
||||
case "drop_doc_meta_index":
|
||||
client.drop_doc_meta_index(command_dict)
|
||||
case "create_dataset_table":
|
||||
client.create_dataset_table(command_dict)
|
||||
case "drop_dataset_table":
|
||||
client.drop_dataset_table(command_dict)
|
||||
case "create_metadata_table":
|
||||
client.create_metadata_table(command_dict)
|
||||
case "drop_metadata_table":
|
||||
client.drop_metadata_table(command_dict)
|
||||
case "create_chat_session":
|
||||
client.create_chat_session(command_dict)
|
||||
case "drop_chat_session":
|
||||
@@ -2016,6 +2045,8 @@ def run_command(client: RAGFlowClient, command_dict: dict):
|
||||
return client.set_metadata(command_dict)
|
||||
case "remove_tags":
|
||||
return client.remove_tags(command_dict)
|
||||
case "remove_chunks":
|
||||
return client.remove_chunks(command_dict)
|
||||
case "list_chunks":
|
||||
return client.list_chunks(command_dict)
|
||||
case "meta":
|
||||
@@ -2077,10 +2108,6 @@ LIST METADATA OF DATASETS <dataset>[, <dataset>]*
|
||||
LIST METADATA SUMMARY OF DATASET <dataset> DOCUMENTS <doc_id>[, <doc_id>]*
|
||||
GET CHUNK <chunk_id>
|
||||
LIST CHUNKS OF DOCUMENT <doc_id> [PAGE <page>] [SIZE <size>] [KEYWORDS <keywords>] [AVAILABLE <0|1>]
|
||||
CREATE INDEX FOR DATASET <dataset> VECTOR_SIZE <vector_size>
|
||||
DROP INDEX FOR DATASET <dataset>
|
||||
CREATE INDEX DOC_META
|
||||
DROP INDEX DOC_META
|
||||
|
||||
Meta Commands:
|
||||
\\?, \\h, \\help Show this help
|
||||
|
||||
Reference in New Issue
Block a user