From 04d3ec05240251d10d4728fe23d59ca103a34ffb Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Thu, 18 Jan 2024 18:36:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../step/chat_step/impl/base_chat_step.py | 4 +- .../serializers/chat_serializers.py | 18 +- ui/src/components/ai-chat/index.vue | 185 +++++++++++------- 3 files changed, 129 insertions(+), 78 deletions(-) diff --git a/apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py b/apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py index 123574459..9fa4c511e 100644 --- a/apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py +++ b/apps/application/chat_pipeline/step/chat_step/impl/base_chat_step.py @@ -41,8 +41,6 @@ def event_content(response, yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True, 'content': chunk.content, 'is_end': False}) + "\n\n" - yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True, - 'content': '', 'is_end': True}) + "\n\n" # 获取token request_token = chat_model.get_num_tokens_from_messages(message_list) response_token = chat_model.get_num_tokens(all_text) @@ -56,6 +54,8 @@ def event_content(response, manage.context['answer_tokens'] = manage.context['answer_tokens'] + response_token post_response_handler.handler(chat_id, chat_record_id, paragraph_list, problem_text, all_text, manage, step, padding_problem_text) + yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True, + 'content': '', 'is_end': True}) + "\n\n" except Exception as e: logging.getLogger("max_kb_error").error(f'{str(e)}:{traceback.format_exc()}') yield 'data: ' + json.dumps({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True, diff --git a/apps/application/serializers/chat_serializers.py b/apps/application/serializers/chat_serializers.py index a7354b63d..3baf7783e 100644 --- a/apps/application/serializers/chat_serializers.py +++ b/apps/application/serializers/chat_serializers.py @@ -176,12 +176,22 @@ class ChatRecordSerializer(serializers.Serializer): chat_record_id = serializers.UUIDField(required=True) + def get_chat_record(self): + chat_record_id = self.data.get('chat_record_id') + chat_id = self.data.get('chat_id') + chat_info: ChatInfo = chat_cache.get(chat_id) + chat_record_list = [chat_record for chat_record in chat_info.chat_record_list if + chat_record.id == uuid.UUID(chat_record_id)] + if chat_record_list is not None and len(chat_record_list): + return chat_record_list[-1] + return QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_id).first() + def one(self, with_valid=True): if with_valid: self.is_valid(raise_exception=True) - chat_record_id = self.data.get('chat_record_id') - chat_id = self.data.get('chat_id') - chat_record = QuerySet(ChatRecord).filter(id=chat_record_id, chat_id=chat_id).first() + chat_record = self.get_chat_record() + if chat_record is None: + raise AppApiException(500, "对话不存在") dataset_list = [] paragraph_list = [] if len(chat_record.paragraph_id_list) > 0: @@ -200,7 +210,7 @@ class ChatRecordSerializer(serializers.Serializer): return { **ChatRecordSerializerModel(chat_record).data, - 'padding_problem_text': chat_record.details.get( + 'padding_problem_text': chat_record.details.get('problem_padding').get( 'padding_problem_text') if 'problem_padding' in chat_record.details else None, 'dataset_list': dataset_list, 'paragraph_list': paragraph_list} diff --git a/ui/src/components/ai-chat/index.vue b/ui/src/components/ai-chat/index.vue index ed84e1e29..7b562983e 100644 --- a/ui/src/components/ai-chat/index.vue +++ b/ui/src/components/ai-chat/index.vue @@ -160,7 +160,7 @@