2023-11-16 05:16:27 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: maxkb
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: application_serializers.py
|
|
|
|
|
|
@date:2023/11/7 10:02
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
import hashlib
|
2024-07-01 01:45:59 +00:00
|
|
|
|
import json
|
2023-11-16 05:16:27 +00:00
|
|
|
|
import os
|
2024-04-22 03:21:24 +00:00
|
|
|
|
import re
|
2023-11-16 05:16:27 +00:00
|
|
|
|
import uuid
|
2023-12-25 09:10:59 +00:00
|
|
|
|
from functools import reduce
|
2024-07-03 03:46:37 +00:00
|
|
|
|
from typing import Dict, List
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
from django.contrib.postgres.fields import ArrayField
|
2024-04-22 03:21:24 +00:00
|
|
|
|
from django.core import cache, validators
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from django.core import signing
|
|
|
|
|
|
from django.db import transaction, models
|
|
|
|
|
|
from django.db.models import QuerySet
|
2024-03-13 08:07:13 +00:00
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
from django.template import Template, Context
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
2024-07-01 01:45:59 +00:00
|
|
|
|
from application.flow.workflow_manage import Flow
|
|
|
|
|
|
from application.models import Application, ApplicationDatasetMapping, ApplicationTypeChoices, WorkFlowVersion
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from application.models.api_key_model import ApplicationAccessToken, ApplicationApiKey
|
2024-07-17 09:01:57 +00:00
|
|
|
|
from common.config.embedding_config import VectorStore
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from common.constants.authentication_type import AuthenticationType
|
|
|
|
|
|
from common.db.search import get_dynamics_model, native_search, native_page_search
|
|
|
|
|
|
from common.db.sql_execute import select_list
|
2024-04-25 06:05:59 +00:00
|
|
|
|
from common.exception.app_exception import AppApiException, NotFound404, AppUnauthorizedFailed
|
2024-04-23 11:03:34 +00:00
|
|
|
|
from common.field.common import UploadedImageField
|
2024-07-09 05:44:27 +00:00
|
|
|
|
from common.util.common import valid_license
|
2024-03-04 02:12:18 +00:00
|
|
|
|
from common.util.field_message import ErrMessage
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from common.util.file_util import get_file_content
|
2024-04-23 11:03:34 +00:00
|
|
|
|
from dataset.models import DataSet, Document, Image
|
2024-07-17 09:01:57 +00:00
|
|
|
|
from dataset.serializers.common_serializers import list_paragraph, get_embedding_model_by_dataset_id_list
|
2024-04-22 03:21:24 +00:00
|
|
|
|
from embedding.models import SearchMode
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from setting.models import AuthOperate
|
|
|
|
|
|
from setting.models.model_management import Model
|
2024-03-05 09:03:45 +00:00
|
|
|
|
from setting.serializers.provider_serializers import ModelSerializer
|
2023-11-16 05:16:27 +00:00
|
|
|
|
from smartdoc.conf import PROJECT_DIR
|
|
|
|
|
|
|
|
|
|
|
|
token_cache = cache.caches['token_cache']
|
2024-05-20 12:21:45 +00:00
|
|
|
|
chat_cache = cache.caches['model_cache']
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModelDatasetAssociation(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
2024-04-28 09:09:12 +00:00
|
|
|
|
model_id = serializers.CharField(required=False, allow_null=True, allow_blank=True,
|
|
|
|
|
|
error_messages=ErrMessage.char("模型id"))
|
2024-03-04 02:12:18 +00:00
|
|
|
|
dataset_id_list = serializers.ListSerializer(required=False, child=serializers.UUIDField(required=True,
|
|
|
|
|
|
error_messages=ErrMessage.uuid(
|
|
|
|
|
|
"知识库id")),
|
|
|
|
|
|
error_messages=ErrMessage.list("知识库列表"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
def is_valid(self, *, raise_exception=True):
|
|
|
|
|
|
super().is_valid(raise_exception=True)
|
|
|
|
|
|
model_id = self.data.get('model_id')
|
|
|
|
|
|
user_id = self.data.get('user_id')
|
2024-04-28 09:09:12 +00:00
|
|
|
|
if model_id is not None and len(model_id) > 0:
|
|
|
|
|
|
if not QuerySet(Model).filter(id=model_id).exists():
|
|
|
|
|
|
raise AppApiException(500, f'模型不存在【{model_id}】')
|
2023-11-16 05:16:27 +00:00
|
|
|
|
dataset_id_list = list(set(self.data.get('dataset_id_list')))
|
|
|
|
|
|
exist_dataset_id_list = [str(dataset.id) for dataset in
|
|
|
|
|
|
QuerySet(DataSet).filter(id__in=dataset_id_list, user_id=user_id)]
|
|
|
|
|
|
for dataset_id in dataset_id_list:
|
|
|
|
|
|
if not exist_dataset_id_list.__contains__(dataset_id):
|
2023-12-18 03:32:29 +00:00
|
|
|
|
raise AppApiException(500, f'知识库id不存在【{dataset_id}】')
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationSerializerModel(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = Application
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-24 07:03:58 +00:00
|
|
|
|
class NoReferencesChoices(models.TextChoices):
|
|
|
|
|
|
"""订单类型"""
|
|
|
|
|
|
ai_questioning = 'ai_questioning', 'ai回答'
|
|
|
|
|
|
designated_answer = 'designated_answer', '指定回答'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NoReferencesSetting(serializers.Serializer):
|
|
|
|
|
|
status = serializers.ChoiceField(required=True, choices=NoReferencesChoices.choices,
|
|
|
|
|
|
error_messages=ErrMessage.char("无引用状态"))
|
|
|
|
|
|
value = serializers.CharField(required=True, error_messages=ErrMessage.char("提示词"))
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-16 08:46:54 +00:00
|
|
|
|
class DatasetSettingSerializer(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
top_n = serializers.FloatField(required=True, max_value=100, min_value=1,
|
|
|
|
|
|
error_messages=ErrMessage.float("引用分段数"))
|
|
|
|
|
|
similarity = serializers.FloatField(required=True, max_value=1, min_value=0,
|
|
|
|
|
|
error_messages=ErrMessage.float("相识度"))
|
2024-06-13 08:20:30 +00:00
|
|
|
|
max_paragraph_char_number = serializers.IntegerField(required=True, min_value=500, max_value=100000,
|
2024-03-04 02:12:18 +00:00
|
|
|
|
error_messages=ErrMessage.integer("最多引用字符数"))
|
2024-04-22 03:21:24 +00:00
|
|
|
|
search_mode = serializers.CharField(required=True, validators=[
|
|
|
|
|
|
validators.RegexValidator(regex=re.compile("^embedding|keywords|blend$"),
|
|
|
|
|
|
message="类型只支持register|reset_password", code=500)
|
|
|
|
|
|
], error_messages=ErrMessage.char("检索模式"))
|
2024-01-16 08:46:54 +00:00
|
|
|
|
|
2024-04-24 07:03:58 +00:00
|
|
|
|
no_references_setting = NoReferencesSetting(required=True, error_messages=ErrMessage.base("未引用分段设置"))
|
|
|
|
|
|
|
2024-01-16 08:46:54 +00:00
|
|
|
|
|
|
|
|
|
|
class ModelSettingSerializer(serializers.Serializer):
|
2024-03-04 03:57:45 +00:00
|
|
|
|
prompt = serializers.CharField(required=True, max_length=2048, error_messages=ErrMessage.char("提示词"))
|
2024-01-16 08:46:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-07-01 01:45:59 +00:00
|
|
|
|
class ApplicationWorkflowSerializer(serializers.Serializer):
|
|
|
|
|
|
name = serializers.CharField(required=True, max_length=64, min_length=1, error_messages=ErrMessage.char("应用名称"))
|
|
|
|
|
|
desc = serializers.CharField(required=False, allow_null=True, allow_blank=True,
|
|
|
|
|
|
max_length=256, min_length=1,
|
|
|
|
|
|
error_messages=ErrMessage.char("应用描述"))
|
|
|
|
|
|
prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096,
|
|
|
|
|
|
error_messages=ErrMessage.char("开场白"))
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def to_application_model(user_id: str, application: Dict):
|
|
|
|
|
|
|
|
|
|
|
|
default_workflow_json = get_file_content(
|
|
|
|
|
|
os.path.join(PROJECT_DIR, "apps", "application", 'flow', 'default_workflow.json'))
|
|
|
|
|
|
default_workflow = json.loads(default_workflow_json)
|
|
|
|
|
|
for node in default_workflow.get('nodes'):
|
|
|
|
|
|
if node.get('id') == 'base-node':
|
|
|
|
|
|
node.get('properties')['node_data'] = {"desc": application.get('desc'),
|
|
|
|
|
|
"name": application.get('name'),
|
|
|
|
|
|
"prologue": application.get('prologue')}
|
|
|
|
|
|
return Application(id=uuid.uuid1(),
|
|
|
|
|
|
name=application.get('name'),
|
|
|
|
|
|
desc=application.get('desc'),
|
|
|
|
|
|
prologue="",
|
|
|
|
|
|
dialogue_number=0,
|
|
|
|
|
|
user_id=user_id, model_id=None,
|
|
|
|
|
|
dataset_setting={},
|
|
|
|
|
|
model_setting={},
|
|
|
|
|
|
problem_optimization=False,
|
|
|
|
|
|
type=ApplicationTypeChoices.WORK_FLOW,
|
|
|
|
|
|
work_flow=default_workflow
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_base_node_work_flow(work_flow):
|
|
|
|
|
|
node_list = work_flow.get('nodes')
|
|
|
|
|
|
base_node_list = [node for node in node_list if node.get('id') == 'base-node']
|
|
|
|
|
|
if len(base_node_list) > 0:
|
|
|
|
|
|
return base_node_list[-1]
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
class ApplicationSerializer(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
name = serializers.CharField(required=True, max_length=64, min_length=1, error_messages=ErrMessage.char("应用名称"))
|
|
|
|
|
|
desc = serializers.CharField(required=False, allow_null=True, allow_blank=True,
|
|
|
|
|
|
max_length=256, min_length=1,
|
|
|
|
|
|
error_messages=ErrMessage.char("应用描述"))
|
2024-04-28 09:09:12 +00:00
|
|
|
|
model_id = serializers.CharField(required=False, allow_null=True, allow_blank=True,
|
|
|
|
|
|
error_messages=ErrMessage.char("模型"))
|
2024-03-04 02:12:18 +00:00
|
|
|
|
multiple_rounds_dialogue = serializers.BooleanField(required=True, error_messages=ErrMessage.char("多轮对话"))
|
2024-05-24 03:05:07 +00:00
|
|
|
|
prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096,
|
2024-03-04 02:12:18 +00:00
|
|
|
|
error_messages=ErrMessage.char("开场白"))
|
2023-12-14 02:43:34 +00:00
|
|
|
|
dataset_id_list = serializers.ListSerializer(required=False, child=serializers.UUIDField(required=True),
|
2024-03-04 02:12:18 +00:00
|
|
|
|
allow_null=True, error_messages=ErrMessage.list("关联知识库"))
|
2024-01-16 08:46:54 +00:00
|
|
|
|
# 数据集相关设置
|
|
|
|
|
|
dataset_setting = DatasetSettingSerializer(required=True)
|
|
|
|
|
|
# 模型相关设置
|
|
|
|
|
|
model_setting = ModelSettingSerializer(required=True)
|
|
|
|
|
|
# 问题补全
|
2024-03-04 02:12:18 +00:00
|
|
|
|
problem_optimization = serializers.BooleanField(required=True, error_messages=ErrMessage.boolean("问题补全"))
|
2024-07-01 01:45:59 +00:00
|
|
|
|
# 应用类型
|
|
|
|
|
|
type = serializers.CharField(required=True, error_messages=ErrMessage.char("应用类型"),
|
|
|
|
|
|
validators=[
|
|
|
|
|
|
validators.RegexValidator(regex=re.compile("^SIMPLE|WORK_FLOW$"),
|
|
|
|
|
|
message="应用类型只支持SIMPLE|WORK_FLOW", code=500)
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
2024-01-16 08:46:54 +00:00
|
|
|
|
|
|
|
|
|
|
def is_valid(self, *, user_id=None, raise_exception=False):
|
|
|
|
|
|
super().is_valid(raise_exception=True)
|
|
|
|
|
|
ModelDatasetAssociation(data={'user_id': user_id, 'model_id': self.data.get('model_id'),
|
|
|
|
|
|
'dataset_id_list': self.data.get('dataset_id_list')}).is_valid()
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-13 08:07:13 +00:00
|
|
|
|
class Embed(serializers.Serializer):
|
|
|
|
|
|
host = serializers.CharField(required=True, error_messages=ErrMessage.char("主机"))
|
|
|
|
|
|
protocol = serializers.CharField(required=True, error_messages=ErrMessage.char("协议"))
|
|
|
|
|
|
token = serializers.CharField(required=True, error_messages=ErrMessage.char("token"))
|
|
|
|
|
|
|
2024-03-13 21:43:01 +00:00
|
|
|
|
def get_embed(self, with_valid=True):
|
2024-03-13 08:07:13 +00:00
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
index_path = os.path.join(PROJECT_DIR, 'apps', "application", 'template', 'embed.js')
|
|
|
|
|
|
file = open(index_path, "r", encoding='utf-8')
|
|
|
|
|
|
content = file.read()
|
|
|
|
|
|
file.close()
|
2024-03-14 03:46:18 +00:00
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).filter(
|
|
|
|
|
|
access_token=self.data.get('token')).first()
|
|
|
|
|
|
|
|
|
|
|
|
is_auth = 'true' if application_access_token is not None and application_access_token.is_active else 'false'
|
2024-03-13 13:50:57 +00:00
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).filter(
|
|
|
|
|
|
access_token=self.data.get('token')).first()
|
2024-03-13 08:07:13 +00:00
|
|
|
|
t = Template(content)
|
|
|
|
|
|
s = t.render(
|
|
|
|
|
|
Context(
|
2024-03-14 06:06:51 +00:00
|
|
|
|
{'is_auth': is_auth, 'protocol': self.data.get('protocol'), 'host': self.data.get('host'),
|
|
|
|
|
|
'token': self.data.get('token'),
|
2024-03-13 13:50:57 +00:00
|
|
|
|
'white_list_str': ",".join(
|
|
|
|
|
|
application_access_token.white_list),
|
|
|
|
|
|
'white_active': 'true' if application_access_token.white_active else 'false'}))
|
2024-03-13 08:07:13 +00:00
|
|
|
|
response = HttpResponse(s, status=200, headers={'Content-Type': 'text/javascript'})
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
class AccessTokenSerializer(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.boolean("应用id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class AccessTokenEditSerializer(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
access_token_reset = serializers.BooleanField(required=False,
|
|
|
|
|
|
error_messages=ErrMessage.boolean("重置Token"))
|
|
|
|
|
|
is_active = serializers.BooleanField(required=False, error_messages=ErrMessage.boolean("是否开启"))
|
2024-03-21 08:06:33 +00:00
|
|
|
|
access_num = serializers.IntegerField(required=False, max_value=10000,
|
|
|
|
|
|
min_value=0,
|
|
|
|
|
|
error_messages=ErrMessage.integer("访问次数"))
|
2024-03-12 09:16:20 +00:00
|
|
|
|
white_active = serializers.BooleanField(required=False, error_messages=ErrMessage.boolean("是否开启白名单"))
|
|
|
|
|
|
white_list = serializers.ListSerializer(required=False, child=serializers.CharField(required=True,
|
|
|
|
|
|
error_messages=ErrMessage.char(
|
|
|
|
|
|
"白名单")),
|
2024-04-25 06:05:59 +00:00
|
|
|
|
error_messages=ErrMessage.list("白名单列表")),
|
|
|
|
|
|
show_source = serializers.BooleanField(required=False,
|
|
|
|
|
|
error_messages=ErrMessage.boolean("是否显示知识来源"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
def edit(self, instance: Dict, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
ApplicationSerializer.AccessTokenSerializer.AccessTokenEditSerializer(data=instance).is_valid(
|
|
|
|
|
|
raise_exception=True)
|
|
|
|
|
|
|
|
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).get(
|
|
|
|
|
|
application_id=self.data.get('application_id'))
|
|
|
|
|
|
if 'is_active' in instance:
|
|
|
|
|
|
application_access_token.is_active = instance.get("is_active")
|
|
|
|
|
|
if 'access_token_reset' in instance and instance.get('access_token_reset'):
|
|
|
|
|
|
application_access_token.access_token = hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()[8:24]
|
2024-03-12 09:16:20 +00:00
|
|
|
|
if 'access_num' in instance and instance.get('access_num') is not None:
|
|
|
|
|
|
application_access_token.access_num = instance.get("access_num")
|
|
|
|
|
|
if 'white_active' in instance and instance.get('white_active') is not None:
|
|
|
|
|
|
application_access_token.white_active = instance.get("white_active")
|
|
|
|
|
|
if 'white_list' in instance and instance.get('white_list') is not None:
|
|
|
|
|
|
application_access_token.white_list = instance.get('white_list')
|
2024-04-25 06:05:59 +00:00
|
|
|
|
if 'show_source' in instance and instance.get('show_source') is not None:
|
|
|
|
|
|
application_access_token.show_source = instance.get('show_source')
|
2023-11-16 05:16:27 +00:00
|
|
|
|
application_access_token.save()
|
|
|
|
|
|
return self.one(with_valid=False)
|
|
|
|
|
|
|
|
|
|
|
|
def one(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).filter(
|
|
|
|
|
|
application_id=application_id).first()
|
|
|
|
|
|
if application_access_token is None:
|
|
|
|
|
|
application_access_token = ApplicationAccessToken(application_id=application_id,
|
|
|
|
|
|
access_token=hashlib.md5(
|
|
|
|
|
|
str(uuid.uuid1()).encode()).hexdigest()[
|
|
|
|
|
|
8:24], is_active=True)
|
|
|
|
|
|
application_access_token.save()
|
|
|
|
|
|
return {'application_id': application_access_token.application_id,
|
|
|
|
|
|
'access_token': application_access_token.access_token,
|
2024-03-12 09:16:20 +00:00
|
|
|
|
"is_active": application_access_token.is_active,
|
|
|
|
|
|
'access_num': application_access_token.access_num,
|
|
|
|
|
|
'white_active': application_access_token.white_active,
|
2024-04-25 06:05:59 +00:00
|
|
|
|
'white_list': application_access_token.white_list,
|
|
|
|
|
|
'show_source': application_access_token.show_source
|
2024-03-12 09:16:20 +00:00
|
|
|
|
}
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class Authentication(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
access_token = serializers.CharField(required=True, error_messages=ErrMessage.char("access_token"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-13 21:43:01 +00:00
|
|
|
|
def auth(self, request, with_valid=True):
|
2024-04-16 14:55:34 +00:00
|
|
|
|
token = request.META.get('HTTP_AUTHORIZATION')
|
2024-03-13 21:43:01 +00:00
|
|
|
|
token_details = None
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 校验token
|
|
|
|
|
|
if token is not None:
|
|
|
|
|
|
token_details = signing.loads(token)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
token = None
|
2023-11-16 05:16:27 +00:00
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
access_token = self.data.get("access_token")
|
|
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).filter(access_token=access_token).first()
|
|
|
|
|
|
if application_access_token is not None and application_access_token.is_active:
|
2024-04-02 11:32:04 +00:00
|
|
|
|
if token_details is not None and 'client_id' in token_details and token_details.get(
|
|
|
|
|
|
'client_id') is not None:
|
|
|
|
|
|
client_id = token_details.get('client_id')
|
|
|
|
|
|
else:
|
2024-03-13 21:43:01 +00:00
|
|
|
|
client_id = str(uuid.uuid1())
|
2024-04-02 11:32:04 +00:00
|
|
|
|
token = signing.dumps({'application_id': str(application_access_token.application_id),
|
|
|
|
|
|
'user_id': str(application_access_token.application.user.id),
|
|
|
|
|
|
'access_token': application_access_token.access_token,
|
|
|
|
|
|
'type': AuthenticationType.APPLICATION_ACCESS_TOKEN.value,
|
|
|
|
|
|
'client_id': client_id})
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return token
|
|
|
|
|
|
else:
|
2023-12-12 09:21:39 +00:00
|
|
|
|
raise NotFound404(404, "无效的access_token")
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class Edit(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
name = serializers.CharField(required=False, max_length=64, min_length=1,
|
|
|
|
|
|
error_messages=ErrMessage.char("应用名称"))
|
|
|
|
|
|
desc = serializers.CharField(required=False, max_length=256, min_length=1, allow_null=True, allow_blank=True,
|
|
|
|
|
|
error_messages=ErrMessage.char("应用描述"))
|
2024-04-28 09:09:12 +00:00
|
|
|
|
model_id = serializers.CharField(required=False, allow_blank=True, allow_null=True,
|
|
|
|
|
|
error_messages=ErrMessage.char("模型"))
|
2024-03-04 02:12:18 +00:00
|
|
|
|
multiple_rounds_dialogue = serializers.BooleanField(required=False,
|
|
|
|
|
|
error_messages=ErrMessage.boolean("多轮会话"))
|
2024-05-24 03:05:07 +00:00
|
|
|
|
prologue = serializers.CharField(required=False, allow_null=True, allow_blank=True, max_length=4096,
|
2024-03-04 02:12:18 +00:00
|
|
|
|
error_messages=ErrMessage.char("开场白"))
|
|
|
|
|
|
dataset_id_list = serializers.ListSerializer(required=False, child=serializers.UUIDField(required=True),
|
|
|
|
|
|
error_messages=ErrMessage.list("关联知识库")
|
|
|
|
|
|
)
|
2024-01-16 08:46:54 +00:00
|
|
|
|
# 数据集相关设置
|
2024-03-04 03:57:45 +00:00
|
|
|
|
dataset_setting = DatasetSettingSerializer(required=False, allow_null=True,
|
|
|
|
|
|
error_messages=ErrMessage.json("数据集设置"))
|
2024-01-16 08:46:54 +00:00
|
|
|
|
# 模型相关设置
|
2024-03-04 03:57:45 +00:00
|
|
|
|
model_setting = ModelSettingSerializer(required=False, allow_null=True,
|
|
|
|
|
|
error_messages=ErrMessage.json("模型设置"))
|
2024-01-16 08:46:54 +00:00
|
|
|
|
# 问题补全
|
2024-03-04 02:12:18 +00:00
|
|
|
|
problem_optimization = serializers.BooleanField(required=False, allow_null=True,
|
|
|
|
|
|
error_messages=ErrMessage.boolean("问题补全"))
|
2024-04-23 11:03:34 +00:00
|
|
|
|
icon = serializers.CharField(required=False, allow_null=True, error_messages=ErrMessage.char("icon图标"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class Create(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-07-09 05:44:27 +00:00
|
|
|
|
@valid_license(model=Application, count=5,
|
|
|
|
|
|
message='社区版最多支持 5 个应用,如需拥有更多应用,请联系我们(https://fit2cloud.com/)。')
|
2023-11-16 05:16:27 +00:00
|
|
|
|
@transaction.atomic
|
|
|
|
|
|
def insert(self, application: Dict):
|
2024-07-01 01:45:59 +00:00
|
|
|
|
application_type = application.get('type')
|
|
|
|
|
|
if 'WORK_FLOW' == application_type:
|
|
|
|
|
|
return self.insert_workflow(application)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return self.insert_simple(application)
|
|
|
|
|
|
|
|
|
|
|
|
def insert_workflow(self, application: Dict):
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
user_id = self.data.get('user_id')
|
|
|
|
|
|
ApplicationWorkflowSerializer(data=application).is_valid(raise_exception=True)
|
|
|
|
|
|
application_model = ApplicationWorkflowSerializer.to_application_model(user_id, application)
|
|
|
|
|
|
application_model.save()
|
|
|
|
|
|
# 插入认证信息
|
|
|
|
|
|
ApplicationAccessToken(application_id=application_model.id,
|
|
|
|
|
|
access_token=hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()[8:24]).save()
|
|
|
|
|
|
return ApplicationSerializerModel(application_model).data
|
|
|
|
|
|
|
|
|
|
|
|
def insert_simple(self, application: Dict):
|
2023-11-16 05:16:27 +00:00
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
user_id = self.data.get('user_id')
|
|
|
|
|
|
ApplicationSerializer(data=application).is_valid(user_id=user_id, raise_exception=True)
|
|
|
|
|
|
application_model = ApplicationSerializer.Create.to_application_model(user_id, application)
|
|
|
|
|
|
dataset_id_list = application.get('dataset_id_list', [])
|
|
|
|
|
|
application_dataset_mapping_model_list = [
|
2024-04-15 11:06:42 +00:00
|
|
|
|
ApplicationSerializer.Create.to_application_dataset_mapping(application_model.id, dataset_id) for
|
2023-11-16 05:16:27 +00:00
|
|
|
|
dataset_id in dataset_id_list]
|
|
|
|
|
|
# 插入应用
|
|
|
|
|
|
application_model.save()
|
|
|
|
|
|
# 插入认证信息
|
|
|
|
|
|
ApplicationAccessToken(application_id=application_model.id,
|
|
|
|
|
|
access_token=hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()[8:24]).save()
|
|
|
|
|
|
# 插入关联数据
|
|
|
|
|
|
QuerySet(ApplicationDatasetMapping).bulk_create(application_dataset_mapping_model_list)
|
2024-07-01 01:45:59 +00:00
|
|
|
|
return ApplicationSerializerModel(application_model).data
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def to_application_model(user_id: str, application: Dict):
|
|
|
|
|
|
return Application(id=uuid.uuid1(), name=application.get('name'), desc=application.get('desc'),
|
2024-01-16 08:46:54 +00:00
|
|
|
|
prologue=application.get('prologue'),
|
2023-11-16 05:16:27 +00:00
|
|
|
|
dialogue_number=3 if application.get('multiple_rounds_dialogue') else 0,
|
2023-12-14 02:43:34 +00:00
|
|
|
|
user_id=user_id, model_id=application.get('model_id'),
|
2024-01-16 08:46:54 +00:00
|
|
|
|
dataset_setting=application.get('dataset_setting'),
|
|
|
|
|
|
model_setting=application.get('model_setting'),
|
2024-07-01 01:45:59 +00:00
|
|
|
|
problem_optimization=application.get('problem_optimization'),
|
|
|
|
|
|
type=ApplicationTypeChoices.SIMPLE,
|
|
|
|
|
|
work_flow={}
|
2023-11-16 05:16:27 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-04-15 11:06:42 +00:00
|
|
|
|
def to_application_dataset_mapping(application_id: str, dataset_id: str):
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return ApplicationDatasetMapping(id=uuid.uuid1(), application_id=application_id, dataset_id=dataset_id)
|
|
|
|
|
|
|
2023-12-25 09:10:59 +00:00
|
|
|
|
class HitTest(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
id = serializers.CharField(required=True, error_messages=ErrMessage.uuid("应用id"))
|
|
|
|
|
|
user_id = serializers.UUIDField(required=False, error_messages=ErrMessage.uuid("用户id"))
|
|
|
|
|
|
query_text = serializers.CharField(required=True, error_messages=ErrMessage.char("查询文本"))
|
2024-05-30 02:03:01 +00:00
|
|
|
|
top_number = serializers.IntegerField(required=True, max_value=100, min_value=1,
|
2024-03-04 02:12:18 +00:00
|
|
|
|
error_messages=ErrMessage.integer("topN"))
|
2024-05-30 02:03:01 +00:00
|
|
|
|
similarity = serializers.FloatField(required=True, max_value=2, min_value=0,
|
2024-03-04 02:12:18 +00:00
|
|
|
|
error_messages=ErrMessage.float("相关度"))
|
2024-04-22 03:21:24 +00:00
|
|
|
|
search_mode = serializers.CharField(required=True, validators=[
|
|
|
|
|
|
validators.RegexValidator(regex=re.compile("^embedding|keywords|blend$"),
|
|
|
|
|
|
message="类型只支持register|reset_password", code=500)
|
|
|
|
|
|
], error_messages=ErrMessage.char("检索模式"))
|
2023-12-25 09:10:59 +00:00
|
|
|
|
|
|
|
|
|
|
def is_valid(self, *, raise_exception=False):
|
|
|
|
|
|
super().is_valid(raise_exception=True)
|
|
|
|
|
|
if not QuerySet(Application).filter(id=self.data.get('id')).exists():
|
|
|
|
|
|
raise AppApiException(500, '不存在的应用id')
|
|
|
|
|
|
|
|
|
|
|
|
def hit_test(self):
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
vector = VectorStore.get_embedding_vector()
|
2024-02-29 07:51:35 +00:00
|
|
|
|
dataset_id_list = [ad.dataset_id for ad in
|
|
|
|
|
|
QuerySet(ApplicationDatasetMapping).filter(
|
|
|
|
|
|
application_id=self.data.get('id'))]
|
|
|
|
|
|
|
|
|
|
|
|
exclude_document_id_list = [str(document.id) for document in
|
|
|
|
|
|
QuerySet(Document).filter(
|
|
|
|
|
|
dataset_id__in=dataset_id_list,
|
|
|
|
|
|
is_active=False)]
|
2024-07-17 09:01:57 +00:00
|
|
|
|
model = get_embedding_model_by_dataset_id_list(dataset_id_list)
|
2023-12-25 09:10:59 +00:00
|
|
|
|
# 向量库检索
|
2024-02-29 07:51:35 +00:00
|
|
|
|
hit_list = vector.hit_test(self.data.get('query_text'), dataset_id_list, exclude_document_id_list,
|
2023-12-25 09:10:59 +00:00
|
|
|
|
self.data.get('top_number'),
|
|
|
|
|
|
self.data.get('similarity'),
|
2024-04-22 03:21:24 +00:00
|
|
|
|
SearchMode(self.data.get('search_mode')),
|
2024-07-17 09:01:57 +00:00
|
|
|
|
model)
|
2023-12-25 09:10:59 +00:00
|
|
|
|
hit_dict = reduce(lambda x, y: {**x, **y}, [{hit.get('paragraph_id'): hit} for hit in hit_list], {})
|
|
|
|
|
|
p_list = list_paragraph([h.get('paragraph_id') for h in hit_list])
|
|
|
|
|
|
return [{**p, 'similarity': hit_dict.get(p.get('id')).get('similarity'),
|
|
|
|
|
|
'comprehensive_score': hit_dict.get(p.get('id')).get('comprehensive_score')} for p in p_list]
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
class Query(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
name = serializers.CharField(required=False, error_messages=ErrMessage.char("应用名称"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-04 02:12:18 +00:00
|
|
|
|
desc = serializers.CharField(required=False, error_messages=ErrMessage.char("应用描述"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-04 02:12:18 +00:00
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
def get_query_set(self):
|
|
|
|
|
|
user_id = self.data.get("user_id")
|
|
|
|
|
|
query_set_dict = {}
|
|
|
|
|
|
query_set = QuerySet(model=get_dynamics_model(
|
2023-12-14 02:43:34 +00:00
|
|
|
|
{'temp_application.name': models.CharField(), 'temp_application.desc': models.CharField(),
|
|
|
|
|
|
'temp_application.create_time': models.DateTimeField()}))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
if "desc" in self.data and self.data.get('desc') is not None:
|
2024-04-15 05:58:40 +00:00
|
|
|
|
query_set = query_set.filter(**{'temp_application.desc__icontains': self.data.get("desc")})
|
2023-11-16 05:16:27 +00:00
|
|
|
|
if "name" in self.data and self.data.get('name') is not None:
|
2024-04-15 05:58:40 +00:00
|
|
|
|
query_set = query_set.filter(**{'temp_application.name__icontains': self.data.get("name")})
|
2023-12-14 02:43:34 +00:00
|
|
|
|
query_set = query_set.order_by("-temp_application.create_time")
|
2023-11-16 05:16:27 +00:00
|
|
|
|
query_set_dict['default_sql'] = query_set
|
|
|
|
|
|
|
|
|
|
|
|
query_set_dict['application_custom_sql'] = QuerySet(model=get_dynamics_model(
|
|
|
|
|
|
{'application.user_id': models.CharField(),
|
|
|
|
|
|
})).filter(
|
|
|
|
|
|
**{'application.user_id': user_id}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
query_set_dict['team_member_permission_custom_sql'] = QuerySet(model=get_dynamics_model(
|
|
|
|
|
|
{'user_id': models.CharField(),
|
|
|
|
|
|
'team_member_permission.auth_target_type': models.CharField(),
|
|
|
|
|
|
'team_member_permission.operate': ArrayField(verbose_name="权限操作列表",
|
|
|
|
|
|
base_field=models.CharField(max_length=256,
|
|
|
|
|
|
blank=True,
|
|
|
|
|
|
choices=AuthOperate.choices,
|
|
|
|
|
|
default=AuthOperate.USE)
|
|
|
|
|
|
)})).filter(
|
|
|
|
|
|
**{'user_id': user_id, 'team_member_permission.operate__contains': ['USE'],
|
|
|
|
|
|
'team_member_permission.auth_target_type': 'APPLICATION'})
|
|
|
|
|
|
|
|
|
|
|
|
return query_set_dict
|
|
|
|
|
|
|
|
|
|
|
|
def list(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
return [ApplicationSerializer.Query.reset_application(a) for a in
|
|
|
|
|
|
native_search(self.get_query_set(), select_string=get_file_content(
|
|
|
|
|
|
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'list_application.sql')))]
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
def reset_application(application: Dict):
|
|
|
|
|
|
application['multiple_rounds_dialogue'] = True if application.get('dialogue_number') > 0 else False
|
|
|
|
|
|
del application['dialogue_number']
|
2024-04-23 11:03:34 +00:00
|
|
|
|
if 'dataset_setting' in application:
|
2024-04-24 07:03:58 +00:00
|
|
|
|
application['dataset_setting'] = {'search_mode': 'embedding', 'no_references_setting': {
|
|
|
|
|
|
'status': 'ai_questioning',
|
|
|
|
|
|
'value': '{question}'}, **application['dataset_setting']}
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return application
|
|
|
|
|
|
|
|
|
|
|
|
def page(self, current_page: int, page_size: int, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
return native_page_search(current_page, page_size, self.get_query_set(), select_string=get_file_content(
|
|
|
|
|
|
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'list_application.sql')),
|
|
|
|
|
|
post_records_handler=ApplicationSerializer.Query.reset_application)
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationModel(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = Application
|
2024-07-01 01:45:59 +00:00
|
|
|
|
fields = ['id', 'name', 'desc', 'prologue', 'dialogue_number', 'icon', 'type']
|
2024-04-23 11:03:34 +00:00
|
|
|
|
|
|
|
|
|
|
class IconOperate(serializers.Serializer):
|
|
|
|
|
|
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("应用id"))
|
|
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
|
|
|
|
|
image = UploadedImageField(required=True, error_messages=ErrMessage.image("图片"))
|
|
|
|
|
|
|
|
|
|
|
|
def edit(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
application = QuerySet(Application).filter(id=self.data.get('application_id')).first()
|
|
|
|
|
|
if application is None:
|
|
|
|
|
|
raise AppApiException(500, '不存在的应用id')
|
|
|
|
|
|
image_id = uuid.uuid1()
|
|
|
|
|
|
image = Image(id=image_id, image=self.data.get('image').read(), image_name=self.data.get('image').name)
|
|
|
|
|
|
image.save()
|
|
|
|
|
|
application.icon = f'/api/image/{image_id}'
|
|
|
|
|
|
application.save()
|
|
|
|
|
|
return {**ApplicationSerializer.Query.reset_application(ApplicationSerializerModel(application).data)}
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class Operate(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("应用id"))
|
|
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
def is_valid(self, *, raise_exception=False):
|
|
|
|
|
|
super().is_valid(raise_exception=True)
|
|
|
|
|
|
if not QuerySet(Application).filter(id=self.data.get('application_id')).exists():
|
|
|
|
|
|
raise AppApiException(500, '不存在的应用id')
|
|
|
|
|
|
|
2024-07-15 08:26:54 +00:00
|
|
|
|
def list_model(self, model_type=None, with_valid=True):
|
2024-03-05 09:03:45 +00:00
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
2024-07-15 08:26:54 +00:00
|
|
|
|
if model_type is None:
|
|
|
|
|
|
model_type = "LLM"
|
2024-03-05 09:03:45 +00:00
|
|
|
|
application = QuerySet(Application).filter(id=self.data.get("application_id")).first()
|
|
|
|
|
|
return ModelSerializer.Query(
|
2024-07-15 08:26:54 +00:00
|
|
|
|
data={'user_id': application.user_id, 'model_type': model_type}).list(
|
2024-03-05 09:03:45 +00:00
|
|
|
|
with_valid=True)
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def delete(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
QuerySet(Application).filter(id=self.data.get('application_id')).delete()
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
2024-07-03 04:26:07 +00:00
|
|
|
|
@transaction.atomic
|
2024-07-01 01:45:59 +00:00
|
|
|
|
def publish(self, instance, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
application = QuerySet(Application).filter(id=self.data.get("application_id")).first()
|
|
|
|
|
|
work_flow = instance.get('work_flow')
|
|
|
|
|
|
if work_flow is None:
|
|
|
|
|
|
raise AppApiException(500, "work_flow是必填字段")
|
|
|
|
|
|
Flow.new_instance(work_flow).is_valid()
|
|
|
|
|
|
base_node = get_base_node_work_flow(work_flow)
|
|
|
|
|
|
if base_node is not None:
|
|
|
|
|
|
node_data = base_node.get('properties').get('node_data')
|
|
|
|
|
|
if node_data is not None:
|
|
|
|
|
|
application.name = node_data.get('name')
|
|
|
|
|
|
application.desc = node_data.get('desc')
|
|
|
|
|
|
application.prologue = node_data.get('prologue')
|
2024-07-03 03:46:37 +00:00
|
|
|
|
dataset_list = self.list_dataset(with_valid=False)
|
2024-07-03 08:05:56 +00:00
|
|
|
|
application_dataset_id_list = [str(dataset.get('id')) for dataset in dataset_list]
|
|
|
|
|
|
dataset_id_list = self.update_reverse_search_node(work_flow, application_dataset_id_list)
|
2024-07-01 01:45:59 +00:00
|
|
|
|
application.work_flow = work_flow
|
|
|
|
|
|
application.save()
|
2024-07-03 04:26:07 +00:00
|
|
|
|
# 插入知识库关联关系
|
2024-07-03 08:05:56 +00:00
|
|
|
|
self.save_application_mapping(application_dataset_id_list, dataset_id_list, application.id)
|
2024-07-01 01:45:59 +00:00
|
|
|
|
work_flow_version = WorkFlowVersion(work_flow=work_flow, application=application)
|
|
|
|
|
|
work_flow_version.save()
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def one(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
application = QuerySet(Application).get(id=application_id)
|
|
|
|
|
|
dataset_list = self.list_dataset(with_valid=False)
|
|
|
|
|
|
mapping_dataset_id_list = [adm.dataset_id for adm in
|
|
|
|
|
|
QuerySet(ApplicationDatasetMapping).filter(application_id=application_id)]
|
|
|
|
|
|
dataset_id_list = [d.get('id') for d in
|
|
|
|
|
|
list(filter(lambda row: mapping_dataset_id_list.__contains__(row.get('id')),
|
|
|
|
|
|
dataset_list))]
|
2024-07-03 03:46:37 +00:00
|
|
|
|
self.update_search_node(application.work_flow, [str(dataset.get('id')) for dataset in dataset_list])
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return {**ApplicationSerializer.Query.reset_application(ApplicationSerializerModel(application).data),
|
|
|
|
|
|
'dataset_id_list': dataset_id_list}
|
|
|
|
|
|
|
2024-07-03 03:46:37 +00:00
|
|
|
|
def get_search_node(self, work_flow):
|
|
|
|
|
|
return [node for node in work_flow.get('nodes', []) if node.get('type', '') == 'search-dataset-node']
|
|
|
|
|
|
|
|
|
|
|
|
def update_search_node(self, work_flow, user_dataset_id_list: List):
|
|
|
|
|
|
search_node_list = self.get_search_node(work_flow)
|
|
|
|
|
|
for search_node in search_node_list:
|
|
|
|
|
|
node_data = search_node.get('properties', {}).get('node_data', {})
|
|
|
|
|
|
dataset_id_list = node_data.get('dataset_id_list', [])
|
|
|
|
|
|
node_data['source_dataset_id_list'] = dataset_id_list
|
|
|
|
|
|
node_data['dataset_id_list'] = [dataset_id for dataset_id in dataset_id_list if
|
|
|
|
|
|
user_dataset_id_list.__contains__(dataset_id)]
|
|
|
|
|
|
|
|
|
|
|
|
def update_reverse_search_node(self, work_flow, user_dataset_id_list: List):
|
|
|
|
|
|
search_node_list = self.get_search_node(work_flow)
|
2024-07-03 06:41:45 +00:00
|
|
|
|
result_dataset_id_list = []
|
2024-07-03 03:46:37 +00:00
|
|
|
|
for search_node in search_node_list:
|
|
|
|
|
|
node_data = search_node.get('properties', {}).get('node_data', {})
|
|
|
|
|
|
dataset_id_list = node_data.get('dataset_id_list', [])
|
|
|
|
|
|
for dataset_id in dataset_id_list:
|
|
|
|
|
|
if not user_dataset_id_list.__contains__(dataset_id):
|
|
|
|
|
|
raise AppApiException(500, f"未知的知识库id${dataset_id},无法关联")
|
|
|
|
|
|
|
|
|
|
|
|
source_dataset_id_list = node_data.get('source_dataset_id_list', [])
|
|
|
|
|
|
source_dataset_id_list = [source_dataset_id for source_dataset_id in source_dataset_id_list if
|
|
|
|
|
|
not user_dataset_id_list.__contains__(source_dataset_id)]
|
|
|
|
|
|
source_dataset_id_list = list({*source_dataset_id_list, *dataset_id_list})
|
|
|
|
|
|
node_data['source_dataset_id_list'] = []
|
|
|
|
|
|
node_data['dataset_id_list'] = source_dataset_id_list
|
2024-07-03 06:41:45 +00:00
|
|
|
|
result_dataset_id_list = [*source_dataset_id_list, *result_dataset_id_list]
|
|
|
|
|
|
return list(set(result_dataset_id_list))
|
2024-07-03 03:46:37 +00:00
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def profile(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
application = QuerySet(Application).get(id=application_id)
|
2024-04-25 06:05:59 +00:00
|
|
|
|
application_access_token = QuerySet(ApplicationAccessToken).filter(application_id=application.id).first()
|
|
|
|
|
|
if application_access_token is None:
|
|
|
|
|
|
raise AppUnauthorizedFailed(500, "非法用户")
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return ApplicationSerializer.Query.reset_application(
|
2024-04-25 06:05:59 +00:00
|
|
|
|
{**ApplicationSerializer.ApplicationModel(application).data,
|
|
|
|
|
|
'show_source': application_access_token.show_source})
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-07-03 04:26:07 +00:00
|
|
|
|
@transaction.atomic
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def edit(self, instance: Dict, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid()
|
|
|
|
|
|
ApplicationSerializer.Edit(data=instance).is_valid(
|
|
|
|
|
|
raise_exception=True)
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
|
|
|
|
|
|
application = QuerySet(Application).get(id=application_id)
|
2024-04-28 09:09:12 +00:00
|
|
|
|
if instance.get('model_id') is None or len(instance.get('model_id')) == 0:
|
|
|
|
|
|
application.model_id = None
|
|
|
|
|
|
else:
|
|
|
|
|
|
model = QuerySet(Model).filter(
|
|
|
|
|
|
id=instance.get('model_id'),
|
|
|
|
|
|
user_id=application.user_id).first()
|
|
|
|
|
|
if model is None:
|
|
|
|
|
|
raise AppApiException(500, "模型不存在")
|
2024-07-03 03:46:37 +00:00
|
|
|
|
if 'work_flow' in instance:
|
|
|
|
|
|
# 当前用户可修改关联的知识库列表
|
|
|
|
|
|
application_dataset_id_list = [str(dataset_dict.get('id')) for dataset_dict in
|
|
|
|
|
|
self.list_dataset(with_valid=False)]
|
|
|
|
|
|
self.update_reverse_search_node(instance.get('work_flow'), application_dataset_id_list)
|
|
|
|
|
|
|
2024-01-16 08:46:54 +00:00
|
|
|
|
update_keys = ['name', 'desc', 'model_id', 'multiple_rounds_dialogue', 'prologue', 'status',
|
2024-01-18 06:07:34 +00:00
|
|
|
|
'dataset_setting', 'model_setting', 'problem_optimization',
|
2024-07-01 01:45:59 +00:00
|
|
|
|
'api_key_is_active', 'icon', 'work_flow']
|
2023-11-16 05:16:27 +00:00
|
|
|
|
for update_key in update_keys:
|
|
|
|
|
|
if update_key in instance and instance.get(update_key) is not None:
|
|
|
|
|
|
if update_key == 'multiple_rounds_dialogue':
|
2024-04-30 06:28:53 +00:00
|
|
|
|
application.__setattr__('dialogue_number', 0 if not instance.get(update_key) else 3)
|
2023-11-16 05:16:27 +00:00
|
|
|
|
else:
|
|
|
|
|
|
application.__setattr__(update_key, instance.get(update_key))
|
|
|
|
|
|
application.save()
|
|
|
|
|
|
|
|
|
|
|
|
if 'dataset_id_list' in instance:
|
|
|
|
|
|
dataset_id_list = instance.get('dataset_id_list')
|
2023-12-18 03:32:29 +00:00
|
|
|
|
# 当前用户可修改关联的知识库列表
|
2023-12-04 08:32:50 +00:00
|
|
|
|
application_dataset_id_list = [str(dataset_dict.get('id')) for dataset_dict in
|
2023-11-16 05:16:27 +00:00
|
|
|
|
self.list_dataset(with_valid=False)]
|
|
|
|
|
|
for dataset_id in dataset_id_list:
|
|
|
|
|
|
if not application_dataset_id_list.__contains__(dataset_id):
|
2023-12-18 03:32:29 +00:00
|
|
|
|
raise AppApiException(500, f"未知的知识库id${dataset_id},无法关联")
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-07-03 08:05:56 +00:00
|
|
|
|
self.save_application_mapping(application_dataset_id_list, dataset_id_list, application_id)
|
2024-05-20 12:21:45 +00:00
|
|
|
|
chat_cache.clear_by_application_id(application_id)
|
2023-11-16 05:16:27 +00:00
|
|
|
|
return self.one(with_valid=False)
|
|
|
|
|
|
|
2024-07-03 04:26:07 +00:00
|
|
|
|
@staticmethod
|
2024-07-03 08:05:56 +00:00
|
|
|
|
def save_application_mapping(application_dataset_id_list, dataset_id_list, application_id):
|
2024-07-03 06:41:45 +00:00
|
|
|
|
# 需要排除已删除的数据集
|
|
|
|
|
|
dataset_id_list = [dataset.id for dataset in QuerySet(DataSet).filter(id__in=dataset_id_list)]
|
2024-07-03 04:26:07 +00:00
|
|
|
|
# 删除已经关联的id
|
2024-07-03 08:05:56 +00:00
|
|
|
|
QuerySet(ApplicationDatasetMapping).filter(dataset_id__in=application_dataset_id_list,
|
2024-07-03 04:26:07 +00:00
|
|
|
|
application_id=application_id).delete()
|
|
|
|
|
|
# 插入
|
|
|
|
|
|
QuerySet(ApplicationDatasetMapping).bulk_create(
|
|
|
|
|
|
[ApplicationDatasetMapping(application_id=application_id, dataset_id=dataset_id) for dataset_id in
|
|
|
|
|
|
dataset_id_list]) if len(dataset_id_list) > 0 else None
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def list_dataset(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
application = QuerySet(Application).get(id=self.data.get("application_id"))
|
|
|
|
|
|
return select_list(get_file_content(
|
|
|
|
|
|
os.path.join(PROJECT_DIR, "apps", "application", 'sql', 'list_application_dataset.sql')),
|
2024-02-29 07:51:35 +00:00
|
|
|
|
[self.data.get('user_id') if self.data.get('user_id') == str(application.user_id) else None,
|
|
|
|
|
|
application.user_id, self.data.get('user_id')])
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
class ApplicationKeySerializerModel(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = ApplicationApiKey
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationKeySerializer(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
user_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("用户id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-04 02:12:18 +00:00
|
|
|
|
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("应用id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-02-29 10:19:31 +00:00
|
|
|
|
def is_valid(self, *, raise_exception=False):
|
|
|
|
|
|
super().is_valid(raise_exception=True)
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
application = QuerySet(Application).filter(id=application_id).first()
|
|
|
|
|
|
if application is None:
|
|
|
|
|
|
raise AppApiException(1001, "应用不存在")
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
def generate(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
2024-02-29 10:19:31 +00:00
|
|
|
|
application = QuerySet(Application).filter(id=application_id).first()
|
2023-11-16 05:16:27 +00:00
|
|
|
|
secret_key = 'application-' + hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()
|
2024-02-29 10:19:31 +00:00
|
|
|
|
application_api_key = ApplicationApiKey(id=uuid.uuid1(), secret_key=secret_key, user_id=application.user_id,
|
2023-11-16 05:16:27 +00:00
|
|
|
|
application_id=application_id)
|
|
|
|
|
|
application_api_key.save()
|
|
|
|
|
|
return ApplicationSerializer.ApplicationKeySerializerModel(application_api_key).data
|
|
|
|
|
|
|
|
|
|
|
|
def list(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
application_id = self.data.get("application_id")
|
|
|
|
|
|
return [ApplicationSerializer.ApplicationKeySerializerModel(application_api_key).data for
|
|
|
|
|
|
application_api_key in
|
2024-02-29 10:19:31 +00:00
|
|
|
|
QuerySet(ApplicationApiKey).filter(application_id=application_id)]
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2023-12-08 05:58:21 +00:00
|
|
|
|
class Edit(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
is_active = serializers.BooleanField(required=False, error_messages=ErrMessage.boolean("是否可用"))
|
2023-12-08 05:58:21 +00:00
|
|
|
|
|
2024-05-08 09:13:13 +00:00
|
|
|
|
allow_cross_domain = serializers.BooleanField(required=False,
|
|
|
|
|
|
error_messages=ErrMessage.boolean("是否允许跨域"))
|
|
|
|
|
|
|
|
|
|
|
|
cross_domain_list = serializers.ListSerializer(required=False,
|
|
|
|
|
|
child=serializers.CharField(required=True,
|
|
|
|
|
|
error_messages=ErrMessage.char(
|
|
|
|
|
|
"跨域列表")),
|
|
|
|
|
|
error_messages=ErrMessage.char("跨域地址"))
|
|
|
|
|
|
|
2023-11-16 05:16:27 +00:00
|
|
|
|
class Operate(serializers.Serializer):
|
2024-03-04 02:12:18 +00:00
|
|
|
|
application_id = serializers.UUIDField(required=True, error_messages=ErrMessage.uuid("应用id"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
2024-03-04 02:12:18 +00:00
|
|
|
|
api_key_id = serializers.CharField(required=True, error_messages=ErrMessage.char("ApiKeyid"))
|
2023-11-16 05:16:27 +00:00
|
|
|
|
|
|
|
|
|
|
def delete(self, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
|
|
|
|
|
api_key_id = self.data.get("api_key_id")
|
|
|
|
|
|
application_id = self.data.get('application_id')
|
|
|
|
|
|
QuerySet(ApplicationApiKey).filter(id=api_key_id,
|
|
|
|
|
|
application_id=application_id).delete()
|
2023-12-08 05:58:21 +00:00
|
|
|
|
|
|
|
|
|
|
def edit(self, instance, with_valid=True):
|
|
|
|
|
|
if with_valid:
|
|
|
|
|
|
self.is_valid(raise_exception=True)
|
2024-05-08 09:13:13 +00:00
|
|
|
|
ApplicationSerializer.ApplicationKeySerializer.Edit(data=instance).is_valid(raise_exception=True)
|
|
|
|
|
|
api_key_id = self.data.get("api_key_id")
|
|
|
|
|
|
application_id = self.data.get('application_id')
|
|
|
|
|
|
application_api_key = QuerySet(ApplicationApiKey).filter(id=api_key_id,
|
|
|
|
|
|
application_id=application_id).first()
|
|
|
|
|
|
if application_api_key is None:
|
|
|
|
|
|
raise AppApiException(500, '不存在')
|
2023-12-08 05:58:21 +00:00
|
|
|
|
if 'is_active' in instance and instance.get('is_active') is not None:
|
|
|
|
|
|
application_api_key.is_active = instance.get('is_active')
|
2024-05-08 09:13:13 +00:00
|
|
|
|
if 'allow_cross_domain' in instance and instance.get('allow_cross_domain') is not None:
|
|
|
|
|
|
application_api_key.allow_cross_domain = instance.get('allow_cross_domain')
|
|
|
|
|
|
if 'cross_domain_list' in instance and instance.get('cross_domain_list') is not None:
|
|
|
|
|
|
application_api_key.cross_domain_list = instance.get('cross_domain_list')
|
|
|
|
|
|
application_api_key.save()
|