diff --git a/resources/messages_resource.py b/resources/messages_resource.py index b5ede2e..e901390 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -121,7 +121,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): "type": "text", "text": msg_content }]} - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) # 收到的对话 input_wx_content_dialogue_message=[{"type": "text", "text": msg_content}] input_message=utils.dialogue_message(callback_to_user,wxid,input_wx_content_dialogue_message) @@ -180,7 +180,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): cache_messages = cache_messages[:-3] redis_helper.redis_helper.update_hash_field(hash_key,"data",json.dumps(cache_messages,ensure_ascii=False)) - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') reply_content=res["choices"][0]["message"]["content"] else: @@ -192,7 +192,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): reply_content=res["choices"][0]["message"]["content"] gewe_chat.wxchat.post_text(token_id,app_id,callback_to_user,reply_content) - get_messages_from_cache(hash_key, {"role": "assistant", "content": reply_content}) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "assistant", "content": reply_content}) # 回复的对话 input_wx_content_dialogue_message=[{"type": "text", "text": reply_content}] input_message=utils.dialogue_message(wxid,callback_to_user,input_wx_content_dialogue_message,True) @@ -239,7 +239,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): "type": "text", "text": msg_content }]} - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) # 收到的对话 input_wx_content_dialogue_message=[{"type": "text", "text": msg_content}] input_message=utils.dialogue_message(callback_to_user,wxid,input_wx_content_dialogue_message) @@ -298,7 +298,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): cache_messages = cache_messages[:-3] redis_helper.redis_helper.update_hash_field(hash_key,"data",json.dumps(cache_messages,ensure_ascii=False)) - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') reply_content=res["choices"][0]["message"]["content"] else: @@ -313,7 +313,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): reply_content='@'+extract_nickname(msg_push_content) + reply_content gewe_chat.wxchat.post_text(token_id,app_id,callback_to_user,reply_content) - get_messages_from_cache(hash_key, {"role": "assistant", "content": reply_content}) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "assistant", "content": reply_content}) # 回复的对话 input_wx_content_dialogue_message=[{"type": "text", "text": reply_content}] input_message=utils.dialogue_message(wxid,callback_to_user,input_wx_content_dialogue_message,True) @@ -349,7 +349,7 @@ def handle_image(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): }] } - get_messages_from_cache(hash_key, prompt) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) gewe_chat.wxchat.post_text(token_id,app_id,callback_to_user,'已经上传了图片,有什么可以为您服务') logger.info(f"上传图片 URL: {img_url}") @@ -380,7 +380,7 @@ def handle_voice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): os.remove(react_wav_path) hash_key = f'__AI_OPS_WX__:MESSAGES:{wxid}:{callback_to_user}' - messages=get_messages_from_cache(hash_key, {"role": "user", "content": react_voice_text}) + messages=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "user", "content": react_voice_text}) ai_res=fast_gpt_api(messages,f'{wxid}-{callback_to_user}') ai_res_content=ai_res["choices"][0]["message"]["content"] has_url=contains_url(ai_res_content) @@ -403,7 +403,7 @@ def handle_voice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): - get_messages_from_cache(hash_key, {"role": "assistant", "content": ai_res_content}) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "assistant", "content": ai_res_content}) # 构造对话消息并发送到 Kafka input_wx_content_dialogue_message = [{"type": "text", "text": ai_res_content}] input_message = utils.dialogue_message(wxid, callback_to_user, input_wx_content_dialogue_message,True) @@ -446,7 +446,7 @@ def handle_xml_reference(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): }]} # 收到的对话 - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) input_wx_content_dialogue_message=[{"type": "text", "text": msg_content}] input_message=utils.dialogue_message(callback_to_user,wxid,input_wx_content_dialogue_message) kafka_helper.kafka_client.produce_message(input_message) @@ -459,7 +459,7 @@ def handle_xml_reference(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): input_message=utils.dialogue_message(wxid,callback_to_user,input_wx_content_dialogue_message,True) kafka_helper.kafka_client.produce_message(input_message) logger.info("发送对话 %s",input_message) - get_messages_from_cache(hash_key, {"role": "assistant", "content": reply_content}) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "assistant", "content": reply_content}) gewe_chat.wxchat.post_text(token_id,app_id,callback_to_user,reply_content) def handle_add_friend_notice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): @@ -497,7 +497,7 @@ def handle_add_friend_notice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): # 好友发送的文字 hash_key = f'__AI_OPS_WX__:MESSAGES:{wxid}:{to_contact_wxid}' prompt={"role": "user", "content": [{"type": "text","text": msg_content}]} - messages_to_send=get_messages_from_cache(hash_key, prompt) + messages_to_send=gewe_chat.wxchat.save_session_messages_to_cache(hash_key, prompt) input_wx_content_dialogue_message=[{"type": "text", "text": msg_content}] input_message=utils.dialogue_message(to_contact_wxid,wxid,input_wx_content_dialogue_message) @@ -512,7 +512,7 @@ def handle_add_friend_notice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): gewe_chat.wxchat.save_contacts_brief_to_cache(token_id,app_id, wxid,[to_contact_wxid]) # 保存到缓存 - get_messages_from_cache(hash_key, {"role": "assistant", "content": reply_content}) + gewe_chat.wxchat.save_session_messages_to_cache(hash_key, {"role": "assistant", "content": reply_content}) # 发送信息 gewe_chat.wxchat.post_text(token_id,app_id, to_contact_wxid,reply_content) @@ -614,7 +614,10 @@ def handle_mod_contacts(token_id,app_id,wxid,msg_data): else: logger.info('群聊好友通过验证及好友资料变更的通知消息') -def get_messages_from_cache(hash_key,object:object)->object: +def get_messages_from_cache(hash_key,object:object)->list: + ''' + 对话列表 + ''' messages=redis_helper.redis_helper.get_hash(hash_key) wxid=hash_key.split(':')[-1] if not messages: diff --git a/wechat/gewe_chat.py b/wechat/gewe_chat.py index e50cbf0..15f0eb0 100644 --- a/wechat/gewe_chat.py +++ b/wechat/gewe_chat.py @@ -553,6 +553,33 @@ class GeWeChatCom: return response_object.get('ret',None),response_object.get('msg',None),response_object.get('data',None) ############################### 其他 ############################### + def save_session_messages_to_cache(hash_key,object:object)->list: + ''' + 对话列表 + ''' + messages=redis_helper.redis_helper.get_hash(hash_key) + wxid=hash_key.split(':')[-1] + if not messages: + messages=[{"role": "system", "content": ""}] + messages.append(object) + redis_helper.redis_helper.set_hash(hash_key,{"data":json.dumps(messages,ensure_ascii=False)},3600) + else: + messages_str=redis_helper.redis_helper.get_hash_field(hash_key,"data") + messages = json.loads(messages_str) if messages_str else [] + #判断是否含有图片 + last_message = messages[-1] + content = last_message.get("content", []) + if isinstance(content, list) and content: + last_content_type = content[-1].get("type") + if last_content_type == 'image_url': + content.append(object['content'][0]) + messages[-1]['content']=content + else: + messages.append(object) + else: + messages.append(object) + redis_helper.redis_helper.set_hash(hash_key,{"data":json.dumps(messages,ensure_ascii=False)},3600) + return messages def get_contacts_brief_from_cache(self, wxid)->list: """ @@ -562,7 +589,6 @@ class GeWeChatCom: cache_str = redis_helper.redis_helper.get_hash_field(hash_key, "data") return json.loads(cache_str) if cache_str else [] - def save_contacts_brief_to_cache(self, token_id, app_id, wxid, contacts_wxids: list)->list: """ 将联系人信息保存到 Redis 缓存。