KevinHuSh
remove unused codes, seperate layout detection out as a new api. Add new rag methed 'table' (#55)
407b252
# | |
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License | |
# | |
# | |
import base64 | |
import pathlib | |
import flask | |
from elasticsearch_dsl import Q | |
from flask import request | |
from flask_login import login_required, current_user | |
from api.db.db_models import Task | |
from api.db.services.task_service import TaskService | |
from rag.nlp import search | |
from rag.utils import ELASTICSEARCH | |
from api.db.services import duplicate_name | |
from api.db.services.knowledgebase_service import KnowledgebaseService | |
from api.utils.api_utils import server_error_response, get_data_error_result, validate_request | |
from api.utils import get_uuid | |
from api.db import FileType, TaskStatus | |
from api.db.services.document_service import DocumentService | |
from api.settings import RetCode | |
from api.utils.api_utils import get_json_result | |
from rag.utils.minio_conn import MINIO | |
from api.utils.file_utils import filename_type | |
def upload(): | |
kb_id = request.form.get("kb_id") | |
if not kb_id: | |
return get_json_result( | |
data=False, retmsg='Lack of "KB ID"', retcode=RetCode.ARGUMENT_ERROR) | |
if 'file' not in request.files: | |
return get_json_result( | |
data=False, retmsg='No file part!', retcode=RetCode.ARGUMENT_ERROR) | |
file = request.files['file'] | |
if file.filename == '': | |
return get_json_result( | |
data=False, retmsg='No file selected!', retcode=RetCode.ARGUMENT_ERROR) | |
try: | |
e, kb = KnowledgebaseService.get_by_id(kb_id) | |
if not e: | |
return get_data_error_result( | |
retmsg="Can't find this knowledgebase!") | |
filename = duplicate_name( | |
DocumentService.query, | |
name=file.filename, | |
kb_id=kb.id) | |
location = filename | |
while MINIO.obj_exist(kb_id, location): | |
location += "_" | |
blob = request.files['file'].read() | |
MINIO.put(kb_id, location, blob) | |
doc = DocumentService.insert({ | |
"id": get_uuid(), | |
"kb_id": kb.id, | |
"parser_id": kb.parser_id, | |
"parser_config": kb.parser_config, | |
"created_by": current_user.id, | |
"type": filename_type(filename), | |
"name": filename, | |
"location": location, | |
"size": len(blob) | |
}) | |
return get_json_result(data=doc.to_json()) | |
except Exception as e: | |
return server_error_response(e) | |
def create(): | |
req = request.json | |
kb_id = req["kb_id"] | |
if not kb_id: | |
return get_json_result( | |
data=False, retmsg='Lack of "KB ID"', retcode=RetCode.ARGUMENT_ERROR) | |
try: | |
e, kb = KnowledgebaseService.get_by_id(kb_id) | |
if not e: | |
return get_data_error_result( | |
retmsg="Can't find this knowledgebase!") | |
if DocumentService.query(name=req["name"], kb_id=kb_id): | |
return get_data_error_result( | |
retmsg="Duplicated document name in the same knowledgebase.") | |
doc = DocumentService.insert({ | |
"id": get_uuid(), | |
"kb_id": kb.id, | |
"parser_id": kb.parser_id, | |
"parser_config": kb.parser_config, | |
"created_by": current_user.id, | |
"type": FileType.VIRTUAL, | |
"name": req["name"], | |
"location": "", | |
"size": 0 | |
}) | |
return get_json_result(data=doc.to_json()) | |
except Exception as e: | |
return server_error_response(e) | |
def list(): | |
kb_id = request.args.get("kb_id") | |
if not kb_id: | |
return get_json_result( | |
data=False, retmsg='Lack of "KB ID"', retcode=RetCode.ARGUMENT_ERROR) | |
keywords = request.args.get("keywords", "") | |
page_number = int(request.args.get("page", 1)) | |
items_per_page = int(request.args.get("page_size", 15)) | |
orderby = request.args.get("orderby", "create_time") | |
desc = request.args.get("desc", True) | |
try: | |
docs, tol = DocumentService.get_by_kb_id( | |
kb_id, page_number, items_per_page, orderby, desc, keywords) | |
return get_json_result(data={"total":tol, "docs": docs}) | |
except Exception as e: | |
return server_error_response(e) | |
def change_status(): | |
req = request.json | |
if str(req["status"]) not in ["0", "1"]: | |
get_json_result( | |
data=False, | |
retmsg='"Status" must be either 0 or 1!', | |
retcode=RetCode.ARGUMENT_ERROR) | |
try: | |
e, doc = DocumentService.get_by_id(req["doc_id"]) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
e, kb = KnowledgebaseService.get_by_id(doc.kb_id) | |
if not e: | |
return get_data_error_result( | |
retmsg="Can't find this knowledgebase!") | |
if not DocumentService.update_by_id( | |
req["doc_id"], {"status": str(req["status"])}): | |
return get_data_error_result( | |
retmsg="Database error (Document update)!") | |
if str(req["status"]) == "0": | |
ELASTICSEARCH.updateScriptByQuery(Q("term", doc_id=req["doc_id"]), | |
scripts="ctx._source.available_int=0;", | |
idxnm=search.index_name( | |
kb.tenant_id) | |
) | |
else: | |
ELASTICSEARCH.updateScriptByQuery(Q("term", doc_id=req["doc_id"]), | |
scripts="ctx._source.available_int=1;", | |
idxnm=search.index_name( | |
kb.tenant_id) | |
) | |
return get_json_result(data=True) | |
except Exception as e: | |
return server_error_response(e) | |
def rm(): | |
req = request.json | |
try: | |
e, doc = DocumentService.get_by_id(req["doc_id"]) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
tenant_id = DocumentService.get_tenant_id(req["doc_id"]) | |
if not tenant_id: | |
return get_data_error_result(retmsg="Tenant not found!") | |
ELASTICSEARCH.deleteByQuery(Q("match", doc_id=doc.id), idxnm=search.index_name(tenant_id)) | |
DocumentService.increment_chunk_num(doc.id, doc.kb_id, doc.token_num*-1, doc.chunk_num*-1, 0) | |
if not DocumentService.delete_by_id(req["doc_id"]): | |
return get_data_error_result( | |
retmsg="Database error (Document removal)!") | |
MINIO.rm(doc.kb_id, doc.location) | |
return get_json_result(data=True) | |
except Exception as e: | |
return server_error_response(e) | |
def run(): | |
req = request.json | |
try: | |
for id in req["doc_ids"]: | |
info = {"run": str(req["run"]), "progress": 0} | |
if str(req["run"]) == TaskStatus.RUNNING.value:info["progress_msg"] = "" | |
DocumentService.update_by_id(id, info) | |
if str(req["run"]) == TaskStatus.CANCEL.value: | |
tenant_id = DocumentService.get_tenant_id(id) | |
if not tenant_id: | |
return get_data_error_result(retmsg="Tenant not found!") | |
ELASTICSEARCH.deleteByQuery(Q("match", doc_id=id), idxnm=search.index_name(tenant_id)) | |
return get_json_result(data=True) | |
except Exception as e: | |
return server_error_response(e) | |
def rename(): | |
req = request.json | |
try: | |
e, doc = DocumentService.get_by_id(req["doc_id"]) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
if pathlib.Path(req["name"].lower()).suffix != pathlib.Path(doc.name.lower()).suffix: | |
return get_json_result( | |
data=False, | |
retmsg="The extension of file can't be changed", | |
retcode=RetCode.ARGUMENT_ERROR) | |
if DocumentService.query(name=req["name"], kb_id=doc.kb_id): | |
return get_data_error_result( | |
retmsg="Duplicated document name in the same knowledgebase.") | |
if not DocumentService.update_by_id( | |
req["doc_id"], {"name": req["name"]}): | |
return get_data_error_result( | |
retmsg="Database error (Document rename)!") | |
return get_json_result(data=True) | |
except Exception as e: | |
return server_error_response(e) | |
def get(): | |
doc_id = request.args["doc_id"] | |
try: | |
e, doc = DocumentService.get_by_id(doc_id) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
blob = MINIO.get(doc.kb_id, doc.location) | |
return get_json_result(data={"base64": base64.b64decode(blob)}) | |
except Exception as e: | |
return server_error_response(e) | |
def change_parser(): | |
req = request.json | |
try: | |
e, doc = DocumentService.get_by_id(req["doc_id"]) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
if doc.parser_id.lower() == req["parser_id"].lower(): | |
return get_json_result(data=True) | |
e = DocumentService.update_by_id(doc.id, {"parser_id": req["parser_id"], "progress":0, "progress_msg": ""}) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
if doc.token_num>0: | |
e = DocumentService.increment_chunk_num(doc.id, doc.kb_id, doc.token_num*-1, doc.chunk_num*-1, doc.process_duation*-1) | |
if not e: | |
return get_data_error_result(retmsg="Document not found!") | |
return get_json_result(data=True) | |
except Exception as e: | |
return server_error_response(e) | |
#@login_required | |
def get_image(image_id): | |
try: | |
bkt, nm = image_id.split("-") | |
response = flask.make_response(MINIO.get(bkt, nm)) | |
response.headers.set('Content-Type', 'image/JPEG') | |
return response | |
except Exception as e: | |
return server_error_response(e) | |