2020from django .db import transaction , models
2121from django .db .models import QuerySet , Q
2222from django .http import StreamingHttpResponse
23+ from django .utils .translation import gettext_lazy as _ , gettext
2324from openpyxl .cell .cell import ILLEGAL_CHARACTERS_RE
2425from rest_framework import serializers
2526from rest_framework .utils .formatting import lazy_format
4546from setting .models import Model
4647from setting .models_provider import get_model_credential
4748from smartdoc .conf import PROJECT_DIR
48- from django .utils .translation import gettext_lazy as _ , gettext as __
4949
5050chat_cache = caches ['chat_cache' ]
5151
@@ -224,11 +224,13 @@ def stream_response():
224224 worksheet = workbook .active
225225 worksheet .title = 'Sheet1'
226226
227- headers = [__ ('Conversation ID' ), __ ('summary' ), __ ('User Questions' ), __ ('Problem after optimization' ),
228- __ ('answer' ), __ ('User feedback' ),
229- __ ('Reference segment number' ),
230- __ ('Section title + content' ),
231- __ ('Annotation' ), __ ('Consuming tokens' ), __ ('Time consumed (s)' ), __ ('Question Time' )]
227+ headers = [gettext ('Conversation ID' ), gettext ('summary' ), gettext ('User Questions' ),
228+ gettext ('Problem after optimization' ),
229+ gettext ('answer' ), gettext ('User feedback' ),
230+ gettext ('Reference segment number' ),
231+ gettext ('Section title + content' ),
232+ gettext ('Annotation' ), gettext ('Consuming tokens' ), gettext ('Time consumed (s)' ),
233+ gettext ('Question Time' )]
232234 for col_idx , header in enumerate (headers , 1 ):
233235 cell = worksheet .cell (row = 1 , column = col_idx )
234236 cell .value = header
@@ -272,7 +274,7 @@ def is_valid(self, *, raise_exception=False):
272274 user_id = self .data .get ('user_id' )
273275 application_id = self .data .get ('application_id' )
274276 if not QuerySet (Application ).filter (id = application_id , user_id = user_id ).exists ():
275- raise AppApiException (500 , __ ('Application does not exist' ))
277+ raise AppApiException (500 , gettext ('Application does not exist' ))
276278
277279 def open (self ):
278280 self .is_valid (raise_exception = True )
@@ -291,7 +293,8 @@ def open_work_flow(self, application):
291293 '-create_time' )[0 :1 ].first ()
292294 if work_flow_version is None :
293295 raise AppApiException (500 ,
294- __ ("The application has not been published. Please use it after publishing." ))
296+ gettext (
297+ "The application has not been published. Please use it after publishing." ))
295298 chat_cache .set (chat_id ,
296299 ChatInfo (chat_id , [],
297300 [],
@@ -373,7 +376,7 @@ def get_user_id(self):
373376 if 'id' in self .data and self .data .get ('id' ) is not None :
374377 application = QuerySet (Application ).filter (id = self .data .get ('id' )).first ()
375378 if application is None :
376- raise AppApiException (500 , __ ("Application does not exist" ))
379+ raise AppApiException (500 , gettext ("Application does not exist" ))
377380 return application .user_id
378381 return self .data .get ('user_id' )
379382
@@ -426,9 +429,9 @@ def is_valid(self, *, current_role=None, raise_exception=False):
426429 application_access_token = QuerySet (ApplicationAccessToken ).filter (
427430 application_id = self .data .get ('application_id' )).first ()
428431 if application_access_token is None :
429- raise AppApiException (500 , __ ('Application authentication information does not exist' ))
432+ raise AppApiException (500 , gettext ('Application authentication information does not exist' ))
430433 if not application_access_token .show_source and current_role == RoleConstants .APPLICATION_ACCESS_TOKEN .value :
431- raise AppApiException (500 , __ ('Displaying knowledge sources is not enabled' ))
434+ raise AppApiException (500 , gettext ('Displaying knowledge sources is not enabled' ))
432435
433436 def get_chat_record (self ):
434437 chat_record_id = self .data .get ('chat_record_id' )
@@ -446,7 +449,7 @@ def one(self, current_role: RoleConstants, with_valid=True):
446449 self .is_valid (current_role = current_role , raise_exception = True )
447450 chat_record = self .get_chat_record ()
448451 if chat_record is None :
449- raise AppApiException (500 , __ ("Conversation does not exist" ))
452+ raise AppApiException (500 , gettext ("Conversation does not exist" ))
450453 return ChatRecordSerializer .Query .reset_chat_record (chat_record )
451454
452455 class Query (serializers .Serializer ):
@@ -522,12 +525,13 @@ def vote(self, with_valid=True):
522525 self .is_valid (raise_exception = True )
523526 if not try_lock (self .data .get ('chat_record_id' )):
524527 raise AppApiException (500 ,
525- __ ("Voting on the current session minutes, please do not send repeated requests" ))
528+ gettext (
529+ "Voting on the current session minutes, please do not send repeated requests" ))
526530 try :
527531 chat_record_details_model = QuerySet (ChatRecord ).get (id = self .data .get ('chat_record_id' ),
528532 chat_id = self .data .get ('chat_id' ))
529533 if chat_record_details_model is None :
530- raise AppApiException (500 , __ ("Non-existent conversation chat_record_id" ))
534+ raise AppApiException (500 , gettext ("Non-existent conversation chat_record_id" ))
531535 vote_status = self .data .get ("vote_status" )
532536 if chat_record_details_model .vote_status == VoteChoices .UN_VOTE :
533537 if vote_status == VoteChoices .STAR :
@@ -544,7 +548,7 @@ def vote(self, with_valid=True):
544548 chat_record_details_model .vote_status = VoteChoices .UN_VOTE
545549 chat_record_details_model .save ()
546550 else :
547- raise AppApiException (500 , __ ("Already voted, please cancel first and then vote again" ))
551+ raise AppApiException (500 , gettext ("Already voted, please cancel first and then vote again" ))
548552 finally :
549553 un_lock (self .data .get ('chat_record_id' ))
550554 return True
@@ -575,7 +579,7 @@ def get(self, with_valid=True):
575579 chat_id = self .data .get ('chat_id' )
576580 chat_record = QuerySet (ChatRecord ).filter (id = chat_record_id , chat_id = chat_id ).first ()
577581 if chat_record is None :
578- raise AppApiException (500 , __ ('Conversation record does not exist' ))
582+ raise AppApiException (500 , gettext ('Conversation record does not exist' ))
579583 if chat_record .improve_paragraph_id_list is None or len (chat_record .improve_paragraph_id_list ) == 0 :
580584 return []
581585
@@ -602,7 +606,7 @@ def is_valid(self, *, raise_exception=False):
602606 super ().is_valid (raise_exception = True )
603607 if not QuerySet (Document ).filter (id = self .data .get ('document_id' ),
604608 dataset_id = self .data .get ('dataset_id' )).exists ():
605- raise AppApiException (500 , __ ("The document id is incorrect" ))
609+ raise AppApiException (500 , gettext ("The document id is incorrect" ))
606610
607611 @staticmethod
608612 def post_embedding_paragraph (chat_record , paragraph_id , dataset_id ):
@@ -621,7 +625,7 @@ def improve(self, instance: Dict, with_valid=True):
621625 chat_id = self .data .get ('chat_id' )
622626 chat_record = QuerySet (ChatRecord ).filter (id = chat_record_id , chat_id = chat_id ).first ()
623627 if chat_record is None :
624- raise AppApiException (500 , __ ('Conversation record does not exist' ))
628+ raise AppApiException (500 , gettext ('Conversation record does not exist' ))
625629
626630 document_id = self .data .get ("document_id" )
627631 dataset_id = self .data .get ("dataset_id" )
@@ -670,7 +674,7 @@ def delete(self, with_valid=True):
670674 paragraph_id = self .data .get ('paragraph_id' )
671675 chat_record = QuerySet (ChatRecord ).filter (id = chat_record_id , chat_id = chat_id ).first ()
672676 if chat_record is None :
673- raise AppApiException (500 , __ ( '不存在的对话记录 ' ))
677+ raise AppApiException (500 , gettext ( 'Conversation record does not exist ' ))
674678 if not chat_record .improve_paragraph_id_list .__contains__ (uuid .UUID (paragraph_id )):
675679 message = lazy_format (
676680 _ ('The paragraph id is wrong. The current conversation record does not exist. [{paragraph_id}] paragraph id' ),
@@ -693,7 +697,7 @@ class PostImprove(serializers.Serializer):
693697 def is_valid (self , * , raise_exception = False ):
694698 super ().is_valid (raise_exception = True )
695699 if not Document .objects .filter (id = self .data ['document_id' ], dataset_id = self .data ['dataset_id' ]).exists ():
696- raise AppApiException (500 , __ ("The document id is incorrect" ))
700+ raise AppApiException (500 , gettext ("The document id is incorrect" ))
697701
698702 @staticmethod
699703 def post_embedding_paragraph (paragraph_ids , dataset_id ):
@@ -712,7 +716,7 @@ def post_improve(self, instance: Dict):
712716 # 获取所有聊天记录
713717 chat_record_list = list (ChatRecord .objects .filter (chat_id__in = chat_ids ))
714718 if len (chat_record_list ) < len (chat_ids ):
715- raise AppApiException (500 , __ ("Conversation records that do not exist" ))
719+ raise AppApiException (500 , gettext ("Conversation records that do not exist" ))
716720
717721 # 批量创建段落和问题映射
718722 paragraphs = []
0 commit comments