66 @date:2025/6/9 13:42
77 @desc:
88"""
9+ import json
910from typing import List
1011
1112from django .core .cache import cache
1920from application .serializers .application_chat import ChatCountSerializer
2021from common .constants .cache_version import Cache_Version
2122from common .database_model_manage .database_model_manage import DatabaseModelManage
23+ from common .encoder .encoder import SystemEncoder
2224from common .exception .app_exception import ChatException
2325from knowledge .models import Document
2426from models_provider .models import Model
@@ -234,7 +236,7 @@ def to_dict(self):
234236 'knowledge_id_list' : self .knowledge_id_list ,
235237 'exclude_document_id_list' : self .exclude_document_id_list ,
236238 'application_id' : self .application_id ,
237- 'chat_record_list' : [self .chat_record_to_map (c ) for c in self .chat_record_list ],
239+ 'chat_record_list' : [self .chat_record_to_map (c ) for c in self .chat_record_list ][ - 20 :] ,
238240 'debug' : self .debug
239241 }
240242
@@ -255,7 +257,7 @@ def chat_record_to_map(self, chat_record):
255257
256258 @staticmethod
257259 def map_to_chat_record (chat_record_dict ):
258- ChatRecord (id = chat_record_dict .get ('id' ),
260+ return ChatRecord (id = chat_record_dict .get ('id' ),
259261 chat_id = chat_record_dict .get ('chat_id' ),
260262 vote_status = chat_record_dict .get ('vote_status' ),
261263 problem_text = chat_record_dict .get ('problem_text' ),
@@ -270,21 +272,24 @@ def map_to_chat_record(chat_record_dict):
270272 index = chat_record_dict .get ('index' ), )
271273
272274 def set_cache (self ):
273- cache .set (Cache_Version .CHAT .get_key (key = self .chat_id ), self .to_dict (),
275+ cache .set (Cache_Version .CHAT .get_key (key = self .chat_id ),json . dumps ( self .to_dict (), cls = SystemEncoder ),
274276 version = Cache_Version .CHAT_INFO .get_version (),
275277 timeout = 60 * 30 )
276278
277279 @staticmethod
278280 def map_to_chat_info (chat_info_dict ):
279- return ChatInfo (chat_info_dict .get ('chat_id' ), chat_info_dict .get ('chat_user_id' ),
280- chat_info_dict .get ('chat_user_type' ), chat_info_dict .get ('knowledge_id_list' ),
281- chat_info_dict .get ('exclude_document_id_list' ),
282- chat_info_dict .get ('application_id' ),
283- [ChatInfo .map_to_chat_record (c_r ) for c_r in chat_info_dict .get ('chat_record_list' )])
281+ c = ChatInfo (chat_info_dict .get ('chat_id' ), chat_info_dict .get ('chat_user_id' ),
282+ chat_info_dict .get ('chat_user_type' ), chat_info_dict .get ('knowledge_id_list' ),
283+ chat_info_dict .get ('exclude_document_id_list' ),
284+ chat_info_dict .get ('application_id' ),
285+ debug = chat_info_dict .get ('debug' ))
286+ c .chat_record_list = [ChatInfo .map_to_chat_record (c_r ) for c_r in chat_info_dict .get ('chat_record_list' )]
287+ return c
284288
285289 @staticmethod
286290 def get_cache (chat_id ):
287- chat_info_dict = cache .get (Cache_Version .CHAT .get_key (key = chat_id ), version = Cache_Version .CHAT_INFO .get_version ())
291+ chat_info_dict = cache .get (Cache_Version .CHAT .get_key (key = chat_id ),
292+ version = Cache_Version .CHAT_INFO .get_version ())
288293 if chat_info_dict :
289- return ChatInfo .map_to_chat_info (chat_info_dict )
294+ return ChatInfo .map_to_chat_info (json . loads ( chat_info_dict ) )
290295 return None
0 commit comments