|
|
@@ -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: |
|
|
|