66 @date:2025/5/26 17:03
77 @desc:
88"""
9+ import hashlib
910import re
1011from typing import Dict
1112
1617from django .utils .translation import gettext_lazy as _
1718from rest_framework import serializers
1819
19- from application .models .application import Application , ApplicationTypeChoices
20+ from application .models .application import Application , ApplicationTypeChoices , ApplicationKnowledgeMapping
21+ from application .models .application_access_token import ApplicationAccessToken
2022from common .exception .app_exception import AppApiException
2123from knowledge .models import Knowledge
2224from models_provider .models import Model
@@ -94,6 +96,11 @@ class ModelSettingSerializer(serializers.Serializer):
9496
9597
9698class ApplicationCreateSerializer (serializers .Serializer ):
99+ class ApplicationResponse (serializers .ModelSerializer ):
100+ class Meta :
101+ model = Application
102+ fields = "__all__"
103+
97104 class WorkflowRequest (serializers .Serializer ):
98105 name = serializers .CharField (required = True , max_length = 64 , min_length = 1 ,
99106 label = _ ("Application Name" ))
@@ -105,7 +112,7 @@ class WorkflowRequest(serializers.Serializer):
105112 label = _ ("Opening remarks" ))
106113
107114 @staticmethod
108- def to_application_model (user_id : str , application : Dict ):
115+ def to_application_model (user_id : str , workspace_id : str , application : Dict ):
109116 default_workflow = application .get ('work_flow' )
110117 for node in default_workflow .get ('nodes' ):
111118 if node .get ('id' ) == 'base-node' :
@@ -115,6 +122,7 @@ def to_application_model(user_id: str, application: Dict):
115122 return Application (id = uuid .uuid7 (),
116123 name = application .get ('name' ),
117124 desc = application .get ('desc' ),
125+ workspace_id = workspace_id ,
118126 prologue = "" ,
119127 dialogue_number = 0 ,
120128 user_id = user_id , model_id = None ,
@@ -176,7 +184,70 @@ def is_valid(self, *, user_id=None, raise_exception=False):
176184 ModelKnowledgeAssociation (data = {'user_id' : user_id , 'model_id' : self .data .get ('model_id' ),
177185 'knowledge_id_list' : self .data .get ('knowledge_id_list' )}).is_valid ()
178186
187+ @staticmethod
188+ def to_application_model (user_id : str , application : Dict ):
189+ return Application (id = uuid .uuid1 (), name = application .get ('name' ), desc = application .get ('desc' ),
190+ prologue = application .get ('prologue' ),
191+ dialogue_number = application .get ('dialogue_number' , 0 ),
192+ user_id = user_id , model_id = application .get ('model_id' ),
193+ dataset_setting = application .get ('dataset_setting' ),
194+ model_setting = application .get ('model_setting' ),
195+ problem_optimization = application .get ('problem_optimization' ),
196+ type = ApplicationTypeChoices .SIMPLE ,
197+ model_params_setting = application .get ('model_params_setting' , {}),
198+ problem_optimization_prompt = application .get ('problem_optimization_prompt' , None ),
199+ stt_model_enable = application .get ('stt_model_enable' , False ),
200+ stt_model_id = application .get ('stt_model' , None ),
201+ tts_model_id = application .get ('tts_model' , None ),
202+ tts_model_enable = application .get ('tts_model_enable' , False ),
203+ tts_model_params_setting = application .get ('tts_model_params_setting' , {}),
204+ tts_type = application .get ('tts_type' , None ),
205+ file_upload_enable = application .get ('file_upload_enable' , False ),
206+ file_upload_setting = application .get ('file_upload_setting' , {}),
207+ work_flow = {}
208+ )
209+
179210
180211class ApplicationSerializer (serializers .Serializer ):
181- def insert (self ):
182- pass
212+ workspace_id = serializers .CharField (required = True , label = _ ('workspace id' ))
213+ user_id = serializers .UUIDField (required = True , label = _ ("User ID" ))
214+
215+ def insert (self , instance : Dict , with_valid = True ):
216+ application_type = instance .get ('type' )
217+ if 'WORK_FLOW' == application_type :
218+ return self .insert_workflow (instance )
219+ else :
220+ return self .insert_simple (instance )
221+
222+ def insert_workflow (self , instance : Dict ):
223+ self .is_valid (raise_exception = True )
224+ user_id = self .data .get ('user_id' )
225+ ApplicationCreateSerializer .WorkflowRequest (data = instance ).is_valid (raise_exception = True )
226+ application_model = ApplicationCreateSerializer .WorkflowRequest .to_application_model (user_id , instance )
227+ application_model .save ()
228+ # 插入认证信息
229+ ApplicationAccessToken (application_id = application_model .id ,
230+ access_token = hashlib .md5 (str (uuid .uuid1 ()).encode ()).hexdigest ()[8 :24 ]).save ()
231+ return ApplicationCreateSerializer .ApplicationResponse (application_model ).data
232+
233+ @staticmethod
234+ def to_application_knowledge_mapping (application_id : str , dataset_id : str ):
235+ return ApplicationKnowledgeMapping (id = uuid .uuid1 (), application_id = application_id , dataset_id = dataset_id )
236+
237+ def insert_simple (self , instance : Dict ):
238+ self .is_valid (raise_exception = True )
239+ user_id = self .data .get ('user_id' )
240+ ApplicationCreateSerializer .SimplateRequest (data = instance ).is_valid (user_id = user_id , raise_exception = True )
241+ application_model = ApplicationCreateSerializer .SimplateRequest .to_application_model (user_id , instance )
242+ dataset_id_list = instance .get ('knowledge_id_list' , [])
243+ application_knowledge_mapping_model_list = [
244+ self .to_application_knowledge_mapping (application_model .id , dataset_id ) for
245+ dataset_id in dataset_id_list ]
246+ # 插入应用
247+ application_model .save ()
248+ # 插入认证信息
249+ ApplicationAccessToken (application_id = application_model .id ,
250+ access_token = hashlib .md5 (str (uuid .uuid1 ()).encode ()).hexdigest ()[8 :24 ]).save ()
251+ # 插入关联数据
252+ QuerySet (ApplicationKnowledgeMapping ).bulk_create (application_knowledge_mapping_model_list )
253+ return ApplicationCreateSerializer .ApplicationResponse (application_model ).data
0 commit comments