### What problem does this PR solve? discuss:https://github.com/infiniflow/ragflow/issues/1102 #### Completed 1. Integrate API Flask to generate Swagger API documentation, through http://ragflow_host:ragflow_port/v1/docs visit 2. Refactored http_token_auth ``` class AuthUser: def __init__(self, tenant_id, token): self.id = tenant_id self.token = token def get_token(self): return self.token @http_token_auth.verify_token def verify_token(token: str) -> Union[AuthUser, None]: try: objs = APIToken.query(token=token) if objs: api_token = objs[0] user = AuthUser(api_token.tenant_id, api_token.token) return user except Exception as e: server_error_response(e) return None # resources api @manager.auth_required(http_token_auth) def get_all_datasets(query_data): .... ``` 3. Refactored the Datasets (Knowledgebase) API to extract the implementation logic into the api/apps/services directory  4. Python SDK, I only added get_all_datasets as an attempt, Just to verify that SDK API and Server API can use the same method. ``` from ragflow.ragflow import RAGFLow ragflow = RAGFLow('<ACCESS_KEY>', 'http://127.0.0.1:9380') ragflow.get_all_datasets() ``` 5. Request parameter validation, as an attempt, may not be necessary as this feature is already present at the data model layer. This is mainly easier to test the API in Swagger Docs service ``` class UpdateDatasetReq(Schema): kb_id = fields.String(required=True) name = fields.String(validate=validators.Length(min=1, max=128)) description = fields.String(allow_none=True) permission = fields.String(validate=validators.OneOf(['me', 'team'])) embd_id = fields.String(validate=validators.Length(min=1, max=128)) language = fields.String(validate=validators.OneOf(['Chinese', 'English'])) parser_id = fields.String(validate=validators.OneOf([parser_type.value for parser_type in ParserType])) parser_config = fields.Dict() avatar = fields.String() ``` #### TODO 1. Simultaneously supporting multiple authentication methods, so that the Web API can use the same method as the Server API, but perhaps this feature is not important. I tried using this method, but it was not successful. It only allows token authentication when not logged in, but cannot skip token authentication when logged in 😢 ``` def http_basic_auth_required(func): @wraps(func) def decorated_view(*args, **kwargs): if 'Authorization' in flask_request.headers: # If the request header contains a token, skip username and password verification return func(*args, **kwargs) if flask_request.method in EXEMPT_METHODS or current_app.config.get("LOGIN_DISABLED"): pass elif not current_user.is_authenticated: return current_app.login_manager.unauthorized() if callable(getattr(current_app, "ensure_sync", None)): return current_app.ensure_sync(func)(*args, **kwargs) return func(*args, **kwargs) return decorated_view ``` 2. Refactoring the SDK API using the same method as the Server API is feasible and constructive, but it still requires time I see some differences between the Web and SDK APIs, such as the key_mapping handling of the returned results. Until I figure it out, I cannot modify these codes to avoid causing more problems ``` for kb in kbs: key_mapping = { "chunk_num": "chunk_count", "doc_num": "document_count", "parser_id": "parse_method", "embd_id": "embedding_model" } renamed_data = {} for key, value in kb.items(): new_key = key_mapping.get(key, key) renamed_data[new_key] = value renamed_list.append(renamed_data) return get_json_result(data=renamed_list) ``` ### Type of change - [x] Refactoring
97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
#
|
|
# 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.
|
|
#
|
|
|
|
from api.apps import http_token_auth
|
|
from api.apps.services import dataset_service
|
|
from api.utils.api_utils import server_error_response, http_basic_auth_required
|
|
|
|
|
|
@manager.post('')
|
|
@manager.input(dataset_service.CreateDatasetReq, location='json')
|
|
@manager.auth_required(http_token_auth)
|
|
def create_dataset(json_data):
|
|
"""Creates a new Dataset(Knowledgebase)."""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.create_dataset(tenant_id, json_data)
|
|
except Exception as e:
|
|
return server_error_response(e)
|
|
|
|
|
|
@manager.put('')
|
|
@manager.input(dataset_service.UpdateDatasetReq, location='json')
|
|
@manager.auth_required(http_token_auth)
|
|
def update_dataset(json_data):
|
|
"""Updates a Dataset(Knowledgebase)."""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.update_dataset(tenant_id, json_data)
|
|
except Exception as e:
|
|
return server_error_response(e)
|
|
|
|
|
|
@manager.get('/<string:kb_id>')
|
|
@manager.auth_required(http_token_auth)
|
|
def get_dataset_by_id(kb_id):
|
|
"""Query Dataset(Knowledgebase) by Dataset(Knowledgebase) ID."""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.get_dataset_by_id(tenant_id, kb_id)
|
|
except Exception as e:
|
|
return server_error_response(e)
|
|
|
|
|
|
@manager.get('/search')
|
|
@manager.input(dataset_service.SearchDatasetReq, location='query')
|
|
@manager.auth_required(http_token_auth)
|
|
def get_dataset_by_name(query_data):
|
|
"""Query Dataset(Knowledgebase) by Dataset(Knowledgebase) Name."""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.get_dataset_by_name(tenant_id, query_data["name"])
|
|
except Exception as e:
|
|
return server_error_response(e)
|
|
|
|
|
|
@manager.get('')
|
|
@manager.input(dataset_service.QueryDatasetReq, location='query')
|
|
@http_basic_auth_required
|
|
@manager.auth_required(http_token_auth)
|
|
def get_all_datasets(query_data):
|
|
"""Query all Datasets(Knowledgebase)"""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.get_all_datasets(
|
|
tenant_id,
|
|
query_data['page'],
|
|
query_data['page_size'],
|
|
query_data['orderby'],
|
|
query_data['desc'],
|
|
)
|
|
except Exception as e:
|
|
return server_error_response(e)
|
|
|
|
|
|
@manager.delete('/<string:kb_id>')
|
|
@manager.auth_required(http_token_auth)
|
|
def delete_dataset(kb_id):
|
|
"""Deletes a Dataset(Knowledgebase)."""
|
|
try:
|
|
tenant_id = http_token_auth.current_user.id
|
|
return dataset_service.delete_dataset(tenant_id, kb_id)
|
|
except Exception as e:
|
|
return server_error_response(e)
|