API: create update_doc method (#1341)

### What problem does this PR solve?

Adds the API method of updating documents.


### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
cecilia-uu
2024-07-03 15:14:34 +08:00
committed by GitHub
parent 65e59862e4
commit 1defc83506
4 changed files with 473 additions and 116 deletions

View File

@ -23,11 +23,11 @@ from api.settings import RetCode
class RAGFlow:
def __init__(self, user_key, base_url, version='v1'):
'''
"""
api_url: http://<host_address>/api/v1
dataset_url: http://<host_address>/api/v1/dataset
document_url: http://<host_address>/api/v1/dataset/{dataset_id}/documents
'''
"""
self.user_key = user_key
self.api_url = f"{base_url}/api/{version}"
self.dataset_url = f"{self.api_url}/dataset"
@ -50,9 +50,9 @@ class RAGFlow:
def find_dataset_id_by_name(self, dataset_name):
res = requests.get(self.dataset_url, headers=self.authorization_header)
for dataset in res.json()['data']:
if dataset['name'] == dataset_name:
return dataset['id']
for dataset in res.json()["data"]:
if dataset["name"] == dataset_name:
return dataset["id"]
return None
def list_dataset(self, offset=0, count=-1, orderby="create_time", desc=True):
@ -78,7 +78,7 @@ class RAGFlow:
response = requests.put(endpoint, json=params, headers=self.authorization_header)
return response.json()
# -------------------- content management -----------------------------------------------------
# ------------------------------- CONTENT MANAGEMENT -----------------------------------------------------
# ----------------------------upload local files-----------------------------------------------------
def upload_local_file(self, dataset_id, file_paths):
@ -86,15 +86,15 @@ class RAGFlow:
for file_path in file_paths:
if not isinstance(file_path, str):
return {'code': RetCode.ARGUMENT_ERROR, 'message': f"{file_path} is not string."}
if 'http' in file_path:
return {'code': RetCode.ARGUMENT_ERROR, 'message': "Remote files have not unsupported."}
return {"code": RetCode.ARGUMENT_ERROR, "message": f"{file_path} is not string."}
if "http" in file_path:
return {"code": RetCode.ARGUMENT_ERROR, "message": "Remote files have not unsupported."}
if os.path.isfile(file_path):
files.append(('file', open(file_path, 'rb')))
files.append(("file", open(file_path, "rb")))
else:
return {'code': RetCode.DATA_ERROR, 'message': f"The file {file_path} does not exist"}
return {"code": RetCode.DATA_ERROR, "message": f"The file {file_path} does not exist"}
res = requests.request('POST', url=f"{self.dataset_url}/{dataset_id}/documents", files=files,
res = requests.request("POST", url=f"{self.dataset_url}/{dataset_id}/documents", files=files,
headers=self.authorization_header)
result_dict = json.loads(res.text)
@ -119,9 +119,13 @@ class RAGFlow:
res = requests.get(endpoint, params=params, headers=self.authorization_header)
return res.json()
# ----------------------------download a file-----------------------------------------------------
# ----------------------------update files: enable, rename, template_type-------------------------------------------
def update_file(self, dataset_id, document_id, **params):
endpoint = f"{self.dataset_url}/{dataset_id}/documents/{document_id}"
response = requests.put(endpoint, json=params, headers=self.authorization_header)
return response.json()
# ----------------------------enable rename-----------------------------------------------------
# ----------------------------download a file-----------------------------------------------------
# ----------------------------start parsing-----------------------------------------------------
@ -137,8 +141,6 @@ class RAGFlow:
# ----------------------------insert a new chunk-----------------------------------------------------
# ----------------------------upload a file-----------------------------------------------------
# ----------------------------get a specific chunk-----------------------------------------------------
# ----------------------------retrieval test-----------------------------------------------------