|
|
@@ -467,7 +467,15 @@ async def check_timeout_async(task: asyncio.Task, request: Request,token_id, wxi |
|
|
|
async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, msg_content): |
|
|
|
|
|
|
|
start_time = time.time() # 记录任务开始时间 |
|
|
|
|
|
|
|
callback_to_user = msg_data["FromUserName"]["string"] |
|
|
|
|
|
|
|
k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid) |
|
|
|
wxid_nickname=loginfo.get("nickName") |
|
|
|
|
|
|
|
contacts_brief:list=await request.app.state.gewe_service.get_contacts_brief_from_cache_async(wxid) |
|
|
|
callback_to_user_nickname=next(filter(lambda x:x.get("userName") == callback_to_user,contacts_brief),None).get("nickName") |
|
|
|
|
|
|
|
hash_key = f'__AI_OPS_WX__:MESSAGES:{wxid}:{callback_to_user}' |
|
|
|
prompt = {"role": "user", "content": [{ |
|
|
|
"type": "text", |
|
|
@@ -546,7 +554,16 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, |
|
|
|
# logger.info(f'wxid_mesh33pw13e721 不发送到微信有关{msg_content}的AI回复到微信') |
|
|
|
# else: |
|
|
|
# await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) |
|
|
|
|
|
|
|
replacements = { |
|
|
|
'{昵称}': wxid_nickname, |
|
|
|
'{好友昵称}': callback_to_user_nickname |
|
|
|
} |
|
|
|
|
|
|
|
reply_content=replace_placeholders(reply_content, replacements) |
|
|
|
|
|
|
|
img_urls,reply_content=extract_and_replace_image_urls(reply_content) |
|
|
|
|
|
|
|
if img_urls: |
|
|
|
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) |
|
|
|
await asyncio.sleep(random.uniform(1.5, 3)) |
|
|
@@ -555,7 +572,7 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, |
|
|
|
await asyncio.sleep(random.uniform(1.5, 3)) |
|
|
|
else: |
|
|
|
|
|
|
|
if reply_content in '不回复': |
|
|
|
if reply_content == '不回复': |
|
|
|
end_time = time.time() # 记录任务结束时间 |
|
|
|
execution_time = end_time - start_time # 计算执行时间 |
|
|
|
logger.warning(f"AI回答任务完成,耗时 {execution_time:.2f} 秒,AI回答<不回复>,跳过微信回复") |
|
|
@@ -563,6 +580,7 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, |
|
|
|
return |
|
|
|
else: |
|
|
|
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) |
|
|
|
|
|
|
|
|
|
|
|
await request.app.state.gewe_service.save_session_messages_to_cache_async(hash_key, {"role": "assistant", "content": reply_content}) |
|
|
|
# 回复的对话 |
|
|
|