소스 검색

昵称替换

1257
H Vs 1 주 전
부모
커밋
188b09a6c8
2개의 변경된 파일25개의 추가작업 그리고 2개의 파일을 삭제
  1. +19
    -1
      app/endpoints/pipeline_endpoint.py
  2. +6
    -1
      common/utils.py

+ 19
- 1
app/endpoints/pipeline_endpoint.py 파일 보기

@@ -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})
# 回复的对话


+ 6
- 1
common/utils.py 파일 보기

@@ -583,4 +583,9 @@ def extract_and_replace_image_urls(text):
# 用 "如下图" 替换所有匹配的图片地址
updated_text = re.sub(pattern, "如下图", text)
return image_urls, updated_text
return image_urls, updated_text

def replace_placeholders(reply_content, replacements:dict)->str:
for placeholder, value in replacements.items():
reply_content:str = reply_content.replace(placeholder, value)
return reply_content

Loading…
취소
저장