From d0d5744a50f6477559f7dee6201e4cf10124bba0 Mon Sep 17 00:00:00 2001 From: H Vs Date: Thu, 23 Jan 2025 10:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=BE=A4=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/messages_resource.py | 59 ++++++++++++++++++++++------------ wechat/biz.py | 25 ++++++++++---- 2 files changed, 57 insertions(+), 27 deletions(-) diff --git a/resources/messages_resource.py b/resources/messages_resource.py index 180f19d..3977087 100644 --- a/resources/messages_resource.py +++ b/resources/messages_resource.py @@ -142,7 +142,7 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): else: messages_to_send=[{"role": "user", "content": msg_content}] - res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["content"] description = '' @@ -181,22 +181,13 @@ def handle_text(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): redis_helper.redis_helper.update_hash_field(hash_key,"data",json.dumps(cache_messages,ensure_ascii=False)) 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}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["content"] - # print('-------------------------------------------------') - # print(reply_content) - # print('-------------------------------------------------') + if isinstance(reply_content, list) : reply_content=reply_content[0].get('text').get("content") - # print(reply_content) - # print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') - - # try: - # json_data = json.loads(reply_content) - # reply_content=json_data[0].get('text').get("content") - # except ValueError: - # print("reply_content is a string, but not a valid JSON.") + else: reply_content=text else: @@ -274,7 +265,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): else: messages_to_send=[{"role": "user", "content": msg_content}] - res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["content"] description = '' @@ -313,7 +304,7 @@ def handle_text_group(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): redis_helper.redis_helper.update_hash_field(hash_key,"data",json.dumps(cache_messages,ensure_ascii=False)) 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}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["content"] else: reply_content=text @@ -400,7 +391,7 @@ def handle_voice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): hash_key = f'__AI_OPS_WX__:MESSAGES:{wxid}:{callback_to_user}' 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=fast_gpt_api(messages,wxid,callback_to_user) ai_res_content=ai_res["choices"][0]["message"]["content"] has_url=contains_url(ai_res_content) if not has_url: @@ -472,7 +463,7 @@ def handle_xml_reference(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): logger.info("发送对话 %s",input_message) # 回复的对话 - res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["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) @@ -524,7 +515,7 @@ def handle_add_friend_notice(token_id,app_id, wxid,msg_data,from_wxid, to_wxid): logger.info("发送对话 %s",input_message) callback_to_user=to_contact_wxid - res=fast_gpt_api(messages_to_send,f'{wxid}-{callback_to_user}') + res=fast_gpt_api(messages_to_send,wxid,callback_to_user) reply_content=res["choices"][0]["message"]["content"] #保存好友信息 @@ -661,8 +652,35 @@ def get_messages_from_cache(hash_key,object:object)->list: redis_helper.redis_helper.set_hash(hash_key,{"data":json.dumps(messages,ensure_ascii=False)},3600) return messages -def fast_gpt_api(messages:list,session_id:str): - api_key="sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH" #测试 +# def fast_gpt_api(messages:list,session_id:str): +# api_key="sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH" #测试 +# #api_key="sk-uJDBdKmJVb2cmfldGOvlIY6Qx0AzqWMPD3lS1IzgQYzHNOXv9SKNI" #开发2 +# api_url = "http://106.15.182.218:3000/api/v1/chat/completions" +# headers = { + +# "Content-Type": "application/json", +# "Authorization": f"Bearer {api_key}" +# } +# data={ +# "model": "", +# "messages":messages, +# "chatId": session_id, +# "detail": True +# } +# #print(json.dumps(data,ensure_ascii=False)) +# logger.info("[CHATGPT] 请求={}".format(json.dumps(data, ensure_ascii=False))) +# response = requests.post(url=api_url, headers=headers, data=json.dumps(data), timeout=600) +# response.raise_for_status() +# response_data = response.json() +# logger.info("[CHATGPT] 响应={}".format(json.dumps(response_data, separators=(',', ':'),ensure_ascii=False))) +# print(response_data) +# return response_data + + +def fast_gpt_api(messages:list,wixd:str,friend_wxid:str): + c=gewe_chat.wxchat.get_wxchat_config_from_cache(wixd) + api_key=c.get('agentTokenId',"sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH") + #api_key="sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH" #测试 #api_key="sk-uJDBdKmJVb2cmfldGOvlIY6Qx0AzqWMPD3lS1IzgQYzHNOXv9SKNI" #开发2 api_url = "http://106.15.182.218:3000/api/v1/chat/completions" headers = { @@ -670,6 +688,7 @@ def fast_gpt_api(messages:list,session_id:str): "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } + session_id=f'{wixd}-{friend_wxid}' data={ "model": "", "messages":messages, diff --git a/wechat/biz.py b/wechat/biz.py index ef9c3b7..5831b59 100644 --- a/wechat/biz.py +++ b/wechat/biz.py @@ -105,13 +105,24 @@ def process_group_sending(wxchat:gewe_chat.GeWeChatCom, content_data, agent_tel: # 发送消息 wx_content_list = content_data.get("wx_content", []) - for wx_content in wx_content_list: - if wx_content["type"] == "text": - send_text_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content["text"]) - elif wx_content["type"] == "image_url": - send_image_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content.get("image_url", {}).get("url")) - elif wx_content["type"] == "tts": - send_tts_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content["text"]) + # for wx_content in wx_content_list: + # if wx_content["type"] == "text": + # send_text_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content["text"]) + # elif wx_content["type"] == "image_url": + # send_image_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content.get("image_url", {}).get("url")) + # elif wx_content["type"] == "tts": + # send_tts_message(wxchat, token_id, app_id, agent_wxid, intersection_wxids, wx_content["text"]) + + for intersection_wxid in intersection_wxids: + for wx_content in wx_content_list: + if wx_content["type"] == "text": + send_text_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content["text"]) + elif wx_content["type"] == "image_url": + send_image_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content.get("image_url", {}).get("url")) + elif wx_content["type"] == "tts": + send_tts_message(wxchat, token_id, app_id, agent_wxid, [intersection_wxid], wx_content["text"]) + + def send_text_message(wxchat:gewe_chat.GeWeChatCom, token_id, app_id, agent_wxid, intersection_wxids, text): for t in intersection_wxids: