2023-11-16 05:16:27 +00:00
# coding=utf-8
"""
@project : maxkb
@Author : 虎
2024-03-13 21:43:01 +00:00
@file : application_key . py
2023-11-16 05:16:27 +00:00
@date : 2023 / 11 / 7 10 : 50
@desc :
"""
from drf_yasg import openapi
from common . mixins . api_mixin import ApiMixin
2025-01-13 11:05:08 +00:00
from django . utils . translation import gettext_lazy as _
2023-11-16 05:16:27 +00:00
class ApplicationApi ( ApiMixin ) :
2024-04-23 11:03:34 +00:00
class EditApplicationIcon ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [
openapi . Parameter ( name = ' file ' ,
in_ = openapi . IN_FORM ,
type = openapi . TYPE_FILE ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Upload files ' ) )
2024-04-23 11:03:34 +00:00
]
2023-11-16 05:16:27 +00:00
class Authentication ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ' access_token ' , ] ,
properties = {
2025-01-13 11:05:08 +00:00
' access_token ' : openapi . Schema ( type = openapi . TYPE_STRING ,
title = _ ( " Application authentication token " ) ,
description = _ ( " Application authentication token " ) ) ,
2023-11-16 05:16:27 +00:00
}
)
@staticmethod
def get_response_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
2024-09-14 13:48:45 +00:00
required = [ ' id ' , ' name ' , ' desc ' , ' model_id ' , ' dialogue_number ' , ' user_id ' , ' status ' , ' create_time ' ,
2023-11-16 05:16:27 +00:00
' update_time ' ] ,
properties = {
2025-01-13 11:05:08 +00:00
' id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = " " , description = _ ( " Primary key id " ) ) ,
' name ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Name " ) ,
description = _ ( " Application Name " ) ) ,
' desc ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Description " ) ,
description = _ ( " Application Description " ) ) ,
' model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Model id " ) , description = _ ( " Model id " ) ) ,
" dialogue_number " : openapi . Schema ( type = openapi . TYPE_NUMBER ,
title = _ ( " Number of multi-round conversations " ) ,
description = _ ( " Number of multi-round conversations " ) ) ,
' prologue ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Opening remarks " ) ,
description = _ ( " Opening remarks " ) ) ,
2023-11-16 05:16:27 +00:00
' example ' : openapi . Schema ( type = openapi . TYPE_ARRAY , items = openapi . Schema ( type = openapi . TYPE_STRING ) ,
2025-01-13 11:05:08 +00:00
title = _ ( " Example List " ) , description = _ ( " Example List " ) ) ,
' user_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Affiliation user " ) ,
description = _ ( " Affiliation user " ) ) ,
2023-11-16 05:16:27 +00:00
2025-01-13 11:05:08 +00:00
' status ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Is publish " ) , description = _ ( ' Is publish ' ) ) ,
2023-11-16 05:16:27 +00:00
2025-01-13 11:05:08 +00:00
' create_time ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Creation time " ) ,
description = _ ( ' Creation time ' ) ) ,
2023-11-16 05:16:27 +00:00
2025-01-13 11:05:08 +00:00
' update_time ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Modification time " ) ,
description = _ ( ' Modification time ' ) ) ,
2024-01-16 08:46:54 +00:00
2023-11-16 05:16:27 +00:00
' dataset_id_list ' : openapi . Schema ( type = openapi . TYPE_ARRAY ,
items = openapi . Schema ( type = openapi . TYPE_STRING ) ,
2025-01-13 11:05:08 +00:00
title = _ ( " List of associated knowledge base IDs " ) ,
description = _ (
" List of associated knowledge base IDs (returned when querying details) " ) )
2023-11-16 05:16:27 +00:00
}
)
2024-07-15 08:26:54 +00:00
class Model ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) ) ,
2024-07-15 08:26:54 +00:00
openapi . Parameter ( name = ' model_type ' , in_ = openapi . IN_QUERY ,
type = openapi . TYPE_STRING ,
required = False ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Model Type ' ) ) ,
2024-07-15 08:26:54 +00:00
]
2023-11-16 05:16:27 +00:00
class ApiKey ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) )
2023-11-16 05:16:27 +00:00
]
class Operate ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) ) ,
2023-11-16 05:16:27 +00:00
openapi . Parameter ( name = ' api_key_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application api_key id ' ) )
2023-11-16 05:16:27 +00:00
]
2023-12-08 05:58:21 +00:00
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ] ,
properties = {
2025-01-13 11:05:08 +00:00
' is_active ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Is activation " ) ,
description = _ ( " Is activation " ) ) ,
' allow_cross_domain ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN ,
title = _ ( " Is cross-domain allowed " ) ,
description = _ ( " Is cross-domain allowed " ) ) ,
' cross_domain_list ' : openapi . Schema ( type = openapi . TYPE_ARRAY , title = _ ( ' Cross-domain list ' ) ,
2024-05-08 09:13:13 +00:00
items = openapi . Schema ( type = openapi . TYPE_STRING ) )
2023-12-08 05:58:21 +00:00
}
)
2023-11-16 05:16:27 +00:00
class AccessToken ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) )
2023-11-16 05:16:27 +00:00
]
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ] ,
properties = {
2025-01-13 11:05:08 +00:00
' access_token_reset ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Reset Token " ) ,
description = _ ( " Reset Token " ) ) ,
' is_active ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Is activation " ) ,
description = _ ( " Is activation " ) ) ,
' access_num ' : openapi . Schema ( type = openapi . TYPE_NUMBER , title = _ ( " Number of visits " ) ,
description = _ ( " Number of visits " ) ) ,
' white_active ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Whether to enable whitelist " ) ,
description = _ ( " Whether to enable whitelist " ) ) ,
2024-03-12 09:16:20 +00:00
' white_list ' : openapi . Schema ( type = openapi . TYPE_ARRAY ,
2025-01-13 11:05:08 +00:00
items = openapi . Schema ( type = openapi . TYPE_STRING ) , title = _ ( " Whitelist " ) ,
description = _ ( " Whitelist " ) ) ,
' show_source ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN ,
title = _ ( " Whether to display knowledge sources " ) ,
description = _ ( " Whether to display knowledge sources " ) ) ,
2023-11-16 05:16:27 +00:00
}
)
2023-12-05 07:58:30 +00:00
class Edit ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
2024-01-16 08:46:54 +00:00
required = [ ] ,
2023-12-05 07:58:30 +00:00
properties = {
2025-01-13 11:05:08 +00:00
' name ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Name " ) ,
description = _ ( " Application Name " ) ) ,
' desc ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Description " ) ,
description = _ ( " Application Description " ) ) ,
' model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Model id " ) ,
description = _ ( " Model id " ) ) ,
" dialogue_number " : openapi . Schema ( type = openapi . TYPE_NUMBER ,
title = _ ( " Number of multi-round conversations " ) ,
description = _ ( " Number of multi-round conversations " ) ) ,
' prologue ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Opening remarks " ) ,
description = _ ( " Opening remarks " ) ) ,
2023-12-05 07:58:30 +00:00
' dataset_id_list ' : openapi . Schema ( type = openapi . TYPE_ARRAY ,
items = openapi . Schema ( type = openapi . TYPE_STRING ) ,
2025-01-13 11:05:08 +00:00
title = _ ( " List of associated knowledge base IDs " ) ,
description = _ ( " List of associated knowledge base IDs " ) ) ,
2024-01-16 08:46:54 +00:00
' dataset_setting ' : ApplicationApi . DatasetSetting . get_request_body_api ( ) ,
' model_setting ' : ApplicationApi . ModelSetting . get_request_body_api ( ) ,
2025-01-13 11:05:08 +00:00
' problem_optimization ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Problem Optimization " ) ,
description = _ ( " Whether to enable problem optimization " ) ,
default = True ) ,
2024-04-23 11:03:34 +00:00
' icon ' : openapi . Schema ( type = openapi . TYPE_STRING , title = " icon " ,
2024-07-01 01:45:59 +00:00
description = " icon " , default = " /ui/favicon.ico " ) ,
2025-01-13 11:05:08 +00:00
' type ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Type " ) ,
description = _ ( " Application Type SIMPLE | WORK_FLOW " ) ) ,
2024-09-14 13:48:45 +00:00
' work_flow ' : ApplicationApi . WorkFlow . get_request_body_api ( ) ,
2025-01-13 11:05:08 +00:00
' problem_optimization_prompt ' : openapi . Schema ( type = openapi . TYPE_STRING ,
title = _ ( ' Question optimization tips ' ) ,
description = _ ( " Question optimization tips " ) ,
default = _ (
" () contains the user ' s question. Answer the guessed user ' s question based on the context ( {question} ) Requirement: Output a complete question and put it in the <data></data> tag " ) ) ,
' tts_model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Text-to-speech model ID " ) ,
description = _ ( " Text-to-speech model ID " ) ) ,
' stt_model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Speech-to-text model id " ) ,
description = _ ( " Speech-to-text model id " ) ) ,
' stt_model_enable ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Is speech-to-text enabled " ) ,
description = _ ( " Is speech-to-text enabled " ) ) ,
' tts_model_enable ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Is text-to-speech enabled " ) ,
description = _ ( " Is text-to-speech enabled " ) ) ,
' tts_type ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Text-to-speech type " ) ,
description = _ ( " Text-to-speech type " ) )
2024-07-01 01:45:59 +00:00
}
)
class WorkFlow ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ' ' ] ,
properties = {
' nodes ' : openapi . Schema ( type = openapi . TYPE_ARRAY , items = openapi . Schema ( type = openapi . TYPE_OBJECT ) ,
2025-01-13 11:05:08 +00:00
title = _ ( " Node List " ) , description = _ ( " Node List " ) ,
2024-07-01 01:45:59 +00:00
default = [ ] ) ,
' edges ' : openapi . Schema ( type = openapi . TYPE_ARRAY , items = openapi . Schema ( type = openapi . TYPE_OBJECT ) ,
2025-01-13 11:05:08 +00:00
title = _ ( ' Connection List ' ) , description = _ ( " Connection List " ) ,
2024-07-01 01:45:59 +00:00
default = { } ) ,
2024-01-16 08:46:54 +00:00
}
)
class DatasetSetting ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ' ' ] ,
properties = {
2025-01-13 11:05:08 +00:00
' top_n ' : openapi . Schema ( type = openapi . TYPE_NUMBER , title = _ ( " Reference segment number " ) ,
description = _ ( " Reference segment number " ) ,
2024-01-16 08:46:54 +00:00
default = 5 ) ,
2025-01-13 11:05:08 +00:00
' similarity ' : openapi . Schema ( type = openapi . TYPE_NUMBER , title = _ ( ' Similarity ' ) ,
description = _ ( " Similarity " ) ,
2024-01-16 08:46:54 +00:00
default = 0.6 ) ,
2025-01-13 11:05:08 +00:00
' max_paragraph_char_number ' : openapi . Schema ( type = openapi . TYPE_NUMBER ,
title = _ ( ' Maximum number of quoted characters ' ) ,
description = _ ( " Maximum number of quoted characters " ) ,
default = 3000 ) ,
' search_mode ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( ' Retrieval Mode ' ) ,
2024-04-22 03:21:24 +00:00
description = " embedding|keywords|blend " , default = ' embedding ' ) ,
2025-01-13 11:05:08 +00:00
' no_references_setting ' : openapi . Schema ( type = openapi . TYPE_OBJECT ,
title = _ ( ' No reference segment settings ' ) ,
2024-04-24 07:03:58 +00:00
required = [ ' status ' , ' value ' ] ,
properties = {
' status ' : openapi . Schema ( type = openapi . TYPE_STRING ,
2025-01-13 11:05:08 +00:00
title = _ ( " state " ) ,
description = _ (
" ai_questioning|designated_answer " ) ,
2024-04-24 07:03:58 +00:00
default = ' ai_questioning ' ) ,
' value ' : openapi . Schema ( type = openapi . TYPE_STRING ,
2025-01-13 11:05:08 +00:00
title = _ ( " value " ) ,
description = _ (
" ai_questioning: is the title, designated_answer: is the designated answer content " ) ,
2024-04-24 07:03:58 +00:00
default = ' {question} ' ) ,
} ) ,
2024-01-16 08:46:54 +00:00
}
)
class ModelSetting ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ' prompt ' ] ,
properties = {
2025-01-13 11:05:08 +00:00
' prompt ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Prompt word " ) ,
description = _ ( " Prompt word " ) ,
default = _ ( """
Known information :
{ data }
Answer requirements :
- If you don ' t know the answer or don ' t get the answer , please answer " No relevant information found in the knowledge base, it is recommended to consult relevant technical support or refer to official documents for operation " .
- Avoid mentioning that you got the knowledge from < data > < / data > .
- Please keep the answer consistent with the description in < data > < / data > .
- Please use markdown syntax to optimize the format of the answer .
- Please return the image link , link address and script language in < data > < / data > completely .
- Please answer in the same language as the question .
Question :
{ question }
""" )),
' system ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " System prompt words (role) " ) ,
description = _ ( " System prompt words (role) " ) ) ,
' no_references_prompt ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " No citation segmentation prompt " ) ,
default = " {question} " , description = _ ( " No citation segmentation prompt " ) )
2024-09-14 13:48:45 +00:00
2023-12-05 07:58:30 +00:00
}
)
2024-07-01 01:45:59 +00:00
class Publish ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
required = [ ] ,
properties = {
' work_flow ' : ApplicationApi . WorkFlow . get_request_body_api ( )
}
)
2023-11-16 05:16:27 +00:00
class Create ( ApiMixin ) :
@staticmethod
def get_request_body_api ( ) :
return openapi . Schema (
type = openapi . TYPE_OBJECT ,
2024-09-14 13:48:45 +00:00
required = [ ' name ' , ' desc ' , ' model_id ' , ' dialogue_number ' , ' dataset_setting ' , ' model_setting ' ,
' problem_optimization ' , ' stt_model_enable ' , ' stt_model_enable ' , ' tts_type ' ] ,
2023-11-16 05:16:27 +00:00
properties = {
2025-01-13 11:05:08 +00:00
' name ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Name " ) , description = _ ( " Application Name " ) ) ,
' desc ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Description " ) , description = _ ( " Application Description " ) ) ,
' model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Model id " ) , description = _ ( " Model id " ) ) ,
" dialogue_number " : openapi . Schema ( type = openapi . TYPE_NUMBER , title = _ ( " Number of multi-round conversations " ) ,
description = _ ( " Number of multi-round conversations " ) ) ,
' prologue ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Opening remarks " ) , description = _ ( " Opening remarks " ) ) ,
2023-11-16 05:16:27 +00:00
' dataset_id_list ' : openapi . Schema ( type = openapi . TYPE_ARRAY ,
items = openapi . Schema ( type = openapi . TYPE_STRING ) ,
2025-01-13 11:05:08 +00:00
title = _ ( " List of associated knowledge base IDs " ) , description = _ ( " List of associated knowledge base IDs " ) ) ,
2024-01-16 08:46:54 +00:00
' dataset_setting ' : ApplicationApi . DatasetSetting . get_request_body_api ( ) ,
' model_setting ' : ApplicationApi . ModelSetting . get_request_body_api ( ) ,
2025-01-13 11:05:08 +00:00
' problem_optimization ' : openapi . Schema ( type = openapi . TYPE_BOOLEAN , title = _ ( " Problem Optimization " ) ,
description = _ ( " Problem Optimization " ) , default = True ) ,
' type ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Application Type " ) ,
description = _ ( " Application Type SIMPLE | WORK_FLOW " ) ) ,
' problem_optimization_prompt ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( ' Question optimization tips ' ) ,
description = _ ( " Question optimization tips " ) ,
default = _ (
" () contains the user ' s question. Answer the guessed user ' s question based on the context ( {question} ) Requirement: Output a complete question and put it in the <data></data> tag " ) ) ,
' tts_model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Text-to-speech model ID " ) ,
description = _ ( " Text-to-speech model ID " ) ) ,
' stt_model_id ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Speech-to-text model id " ) ,
description = _ ( " Speech-to-text model id " ) ) ,
' stt_model_enable ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Is speech-to-text enabled " ) ,
description = _ ( " Is speech-to-text enabled " ) ) ,
' tts_model_enable ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Is text-to-speech enabled " ) ,
description = _ ( " Is text-to-speech enabled " ) ) ,
' tts_type ' : openapi . Schema ( type = openapi . TYPE_STRING , title = _ ( " Text-to-speech type " ) ,
description = _ ( " Text-to-speech type " ) )
2023-11-16 05:16:27 +00:00
}
)
class Query ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' name ' ,
in_ = openapi . IN_QUERY ,
type = openapi . TYPE_STRING ,
required = False ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application Name ' ) ) ,
2023-11-16 05:16:27 +00:00
openapi . Parameter ( name = ' desc ' ,
in_ = openapi . IN_QUERY ,
type = openapi . TYPE_STRING ,
required = False ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application Description ' ) )
2023-11-16 05:16:27 +00:00
]
2024-12-16 06:19:57 +00:00
class Export ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) ) ,
2024-12-16 06:19:57 +00:00
]
class Import ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' file ' ,
in_ = openapi . IN_FORM ,
type = openapi . TYPE_FILE ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Upload image files ' ) )
2024-12-16 06:19:57 +00:00
]
2023-11-16 05:16:27 +00:00
class Operate ( ApiMixin ) :
@staticmethod
def get_request_params_api ( ) :
return [ openapi . Parameter ( name = ' application_id ' ,
in_ = openapi . IN_PATH ,
type = openapi . TYPE_STRING ,
required = True ,
2025-01-13 11:05:08 +00:00
description = _ ( ' Application ID ' ) ) ,
2023-11-16 05:16:27 +00:00
]