
Signed-off-by: yihong0618 <zouzou0208@gmail.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: kurokobo <kuro664@gmail.com> Co-authored-by: Hiroshi Fujita <fujita-h@users.noreply.github.com> Co-authored-by: NFish <douxc512@gmail.com> Co-authored-by: Gen Sato <52241300+halogen22@users.noreply.github.com> Co-authored-by: eux <euxuuu@gmail.com> Co-authored-by: huangzhuo1949 <167434202+huangzhuo1949@users.noreply.github.com> Co-authored-by: huangzhuo <huangzhuo1@xiaomi.com> Co-authored-by: lotsik <lotsik@mail.ru> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: Wu Tianwei <30284043+WTW0313@users.noreply.github.com> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: gakkiyomi <gakkiyomi@aliyun.com> Co-authored-by: CN-P5 <heibai2006@gmail.com> Co-authored-by: CN-P5 <heibai2006@qq.com> Co-authored-by: Chuehnone <1897025+chuehnone@users.noreply.github.com> Co-authored-by: yihong <zouzou0208@gmail.com> Co-authored-by: Kevin9703 <51311316+Kevin9703@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Boris Feld <lothiraldan@gmail.com> Co-authored-by: mbo <himabo@gmail.com> Co-authored-by: mabo <mabo@aeyes.ai> Co-authored-by: Warren Chen <warren.chen830@gmail.com> Co-authored-by: KVOJJJin <jzongcode@gmail.com> Co-authored-by: JzoNgKVO <27049666+JzoNgKVO@users.noreply.github.com> Co-authored-by: jiandanfeng <chenjh3@wangsu.com> Co-authored-by: zhu-an <70234959+xhdd123321@users.noreply.github.com> Co-authored-by: zhaoqingyu.1075 <zhaoqingyu.1075@bytedance.com> Co-authored-by: 海狸大師 <86974027+yenslife@users.noreply.github.com> Co-authored-by: Xu Song <xusong.vip@gmail.com> Co-authored-by: rayshaw001 <396301947@163.com> Co-authored-by: Ding Jiatong <dingjiatong@gmail.com> Co-authored-by: Bowen Liang <liangbowen@gf.com.cn> Co-authored-by: JasonVV <jasonwangiii@outlook.com> Co-authored-by: le0zh <newlight@qq.com> Co-authored-by: zhuxinliang <zhuxinliang@didiglobal.com> Co-authored-by: k-zaku <zaku99@outlook.jp> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: luckylhb90 <luckylhb90@gmail.com> Co-authored-by: hobo.l <hobo.l@binance.com> Co-authored-by: jiangbo721 <365065261@qq.com> Co-authored-by: 刘江波 <jiangbo721@163.com> Co-authored-by: Shun Miyazawa <34241526+miya@users.noreply.github.com> Co-authored-by: EricPan <30651140+Egfly@users.noreply.github.com> Co-authored-by: crazywoola <427733928@qq.com> Co-authored-by: zxhlyh <jasonapring2015@outlook.com> Co-authored-by: sino <sino2322@gmail.com> Co-authored-by: Jhvcc <37662342+Jhvcc@users.noreply.github.com> Co-authored-by: lowell <lowell.hu@zkteco.in>
167 lines
5.9 KiB
Python
167 lines
5.9 KiB
Python
from flask import request
|
|
from flask_restful import marshal, reqparse # type: ignore
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
import services.dataset_service
|
|
from controllers.service_api import api
|
|
from controllers.service_api.dataset.error import DatasetInUseError, DatasetNameDuplicateError
|
|
from controllers.service_api.wraps import DatasetApiResource
|
|
from core.model_runtime.entities.model_entities import ModelType
|
|
from core.provider_manager import ProviderManager
|
|
from fields.dataset_fields import dataset_detail_fields
|
|
from libs.login import current_user
|
|
from models.dataset import Dataset, DatasetPermissionEnum
|
|
from services.dataset_service import DatasetService
|
|
|
|
|
|
def _validate_name(name):
|
|
if not name or len(name) < 1 or len(name) > 40:
|
|
raise ValueError("Name must be between 1 to 40 characters.")
|
|
return name
|
|
|
|
|
|
class DatasetListApi(DatasetApiResource):
|
|
"""Resource for datasets."""
|
|
|
|
def get(self, tenant_id):
|
|
"""Resource for getting datasets."""
|
|
|
|
page = request.args.get("page", default=1, type=int)
|
|
limit = request.args.get("limit", default=20, type=int)
|
|
# provider = request.args.get("provider", default="vendor")
|
|
search = request.args.get("keyword", default=None, type=str)
|
|
tag_ids = request.args.getlist("tag_ids")
|
|
include_all = request.args.get("include_all", default="false").lower() == "true"
|
|
|
|
datasets, total = DatasetService.get_datasets(
|
|
page, limit, tenant_id, current_user, search, tag_ids, include_all
|
|
)
|
|
# check embedding setting
|
|
provider_manager = ProviderManager()
|
|
configurations = provider_manager.get_configurations(tenant_id=current_user.current_tenant_id)
|
|
|
|
embedding_models = configurations.get_models(model_type=ModelType.TEXT_EMBEDDING, only_active=True)
|
|
|
|
model_names = []
|
|
for embedding_model in embedding_models:
|
|
model_names.append(f"{embedding_model.model}:{embedding_model.provider.provider}")
|
|
|
|
data = marshal(datasets, dataset_detail_fields)
|
|
for item in data:
|
|
if item["indexing_technique"] == "high_quality":
|
|
item_model = f"{item['embedding_model']}:{item['embedding_model_provider']}"
|
|
if item_model in model_names:
|
|
item["embedding_available"] = True
|
|
else:
|
|
item["embedding_available"] = False
|
|
else:
|
|
item["embedding_available"] = True
|
|
response = {"data": data, "has_more": len(datasets) == limit, "limit": limit, "total": total, "page": page}
|
|
return response, 200
|
|
|
|
def post(self, tenant_id):
|
|
"""Resource for creating datasets."""
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument(
|
|
"name",
|
|
nullable=False,
|
|
required=True,
|
|
help="type is required. Name must be between 1 to 40 characters.",
|
|
type=_validate_name,
|
|
)
|
|
parser.add_argument(
|
|
"description",
|
|
type=str,
|
|
nullable=True,
|
|
required=False,
|
|
default="",
|
|
)
|
|
parser.add_argument(
|
|
"indexing_technique",
|
|
type=str,
|
|
location="json",
|
|
choices=Dataset.INDEXING_TECHNIQUE_LIST,
|
|
help="Invalid indexing technique.",
|
|
)
|
|
parser.add_argument(
|
|
"permission",
|
|
type=str,
|
|
location="json",
|
|
choices=(DatasetPermissionEnum.ONLY_ME, DatasetPermissionEnum.ALL_TEAM, DatasetPermissionEnum.PARTIAL_TEAM),
|
|
help="Invalid permission.",
|
|
required=False,
|
|
nullable=False,
|
|
)
|
|
parser.add_argument(
|
|
"external_knowledge_api_id",
|
|
type=str,
|
|
nullable=True,
|
|
required=False,
|
|
default="_validate_name",
|
|
)
|
|
parser.add_argument(
|
|
"provider",
|
|
type=str,
|
|
nullable=True,
|
|
required=False,
|
|
default="vendor",
|
|
)
|
|
parser.add_argument(
|
|
"external_knowledge_id",
|
|
type=str,
|
|
nullable=True,
|
|
required=False,
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
dataset = DatasetService.create_empty_dataset(
|
|
tenant_id=tenant_id,
|
|
name=args["name"],
|
|
description=args["description"],
|
|
indexing_technique=args["indexing_technique"],
|
|
account=current_user,
|
|
permission=args["permission"],
|
|
provider=args["provider"],
|
|
external_knowledge_api_id=args["external_knowledge_api_id"],
|
|
external_knowledge_id=args["external_knowledge_id"],
|
|
)
|
|
except services.errors.dataset.DatasetNameDuplicateError:
|
|
raise DatasetNameDuplicateError()
|
|
|
|
return marshal(dataset, dataset_detail_fields), 200
|
|
|
|
|
|
class DatasetApi(DatasetApiResource):
|
|
"""Resource for dataset."""
|
|
|
|
def delete(self, _, dataset_id):
|
|
"""
|
|
Deletes a dataset given its ID.
|
|
|
|
Args:
|
|
dataset_id (UUID): The ID of the dataset to be deleted.
|
|
|
|
Returns:
|
|
dict: A dictionary with a key 'result' and a value 'success'
|
|
if the dataset was successfully deleted. Omitted in HTTP response.
|
|
int: HTTP status code 204 indicating that the operation was successful.
|
|
|
|
Raises:
|
|
NotFound: If the dataset with the given ID does not exist.
|
|
"""
|
|
|
|
dataset_id_str = str(dataset_id)
|
|
|
|
try:
|
|
if DatasetService.delete_dataset(dataset_id_str, current_user):
|
|
return {"result": "success"}, 204
|
|
else:
|
|
raise NotFound("Dataset not found.")
|
|
except services.errors.dataset.DatasetInUseError:
|
|
raise DatasetInUseError()
|
|
|
|
|
|
api.add_resource(DatasetListApi, "/datasets")
|
|
api.add_resource(DatasetApi, "/datasets/<uuid:dataset_id>")
|