瀏覽代碼

调整视频

1257
H Vs 1 周之前
父節點
當前提交
6e023abef6
共有 2 個檔案被更改,包括 46 行新增18 行删除
  1. +34
    -18
      app/endpoints/pipeline_endpoint.py
  2. +12
    -0
      common/utils.py

+ 34
- 18
app/endpoints/pipeline_endpoint.py 查看文件

@@ -574,38 +574,54 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data,
} }
reply_content = remove_markdown_symbol(res["choices"][0]["message"]["content"]) reply_content = remove_markdown_symbol(res["choices"][0]["message"]["content"])


# if callback_to_user == 'wxid_mesh33pw13e721':
# 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)


if reply_content == '不回复':
end_time = time.time() # 记录任务结束时间
execution_time = end_time - start_time # 计算执行时间
logger.warning(f"AI回答任务完成,耗时 {execution_time:.2f} 秒,AI回答<不回复>,跳过微信回复")
await request.app.state.gewe_service.save_session_messages_to_cache_async(hash_key, {"role": "assistant", "content": reply_content})
return

# 昵称替换
replacements = { replacements = {
'{昵称}': wxid_nickname, '{昵称}': wxid_nickname,
'{好友昵称}': callback_to_user_nickname '{好友昵称}': callback_to_user_nickname
} }

reply_content=replace_placeholders(reply_content, replacements) reply_content=replace_placeholders(reply_content, replacements)
img_urls,reply_content=extract_and_replace_image_urls(reply_content)


# 判断图片url
img_urls,reply_content=extract_and_replace_image_urls(reply_content)
if img_urls: if img_urls:
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) 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)) await asyncio.sleep(random.uniform(1.5, 3))
for img_url in img_urls: for img_url in img_urls:
await request.app.state.gewe_service.post_image_async(token_id, app_id, callback_to_user, img_url) await request.app.state.gewe_service.post_image_async(token_id, app_id, callback_to_user, img_url)
await asyncio.sleep(random.uniform(1.5, 3)) await asyncio.sleep(random.uniform(1.5, 3))
else:
if reply_content == '不回复':
end_time = time.time() # 记录任务结束时间
execution_time = end_time - start_time # 计算执行时间
logger.warning(f"AI回答任务完成,耗时 {execution_time:.2f} 秒,AI回答<不回复>,跳过微信回复")
await request.app.state.gewe_service.save_session_messages_to_cache_async(hash_key, {"role": "assistant", "content": reply_content})
return
else:
await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content)


# 判断视频url
video_urls,reply_content=extract_and_replace_video_urls(reply_content)
if video_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))
for video_url in video_urls:
parsed_url = urlparse(video_url)
filename = os.path.basename(parsed_url.path)
tmp_file_path = os.path.join(os.getcwd(),'tmp', filename) # 拼接完整路径
thumbnail_path=tmp_file_path.replace('.mp4','.jpg')
video_thumb_url,video_duration =download_video_and_get_thumbnail(video_url,thumbnail_path)
logger.info(f'{wxid} 视频缩略图 {video_thumb_url} 时长 {video_duration}')
ret,ret_msg,res = await request.app.state.gewe_service.post_video_async(token_id, app_id, callback_to_user, video_url,video_thumb_url,video_duration)
if ret!=200:
logger.warning(f'{wxid} 发送视频{video_url} 到 {callback_to_user} 失败,{ret_msg}')
await asyncio.sleep(random.uniform(1.5, 3))
# 发送AI微信回复
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}) await request.app.state.gewe_service.save_session_messages_to_cache_async(hash_key, {"role": "assistant", "content": reply_content})
# 回复的对话 # 回复的对话
input_wx_content_dialogue_message = [{"type": "text", "text": reply_content}] input_wx_content_dialogue_message = [{"type": "text", "text": reply_content}]


+ 12
- 0
common/utils.py 查看文件

@@ -585,6 +585,18 @@ def extract_and_replace_image_urls(text):
return image_urls, updated_text return image_urls, updated_text


def extract_and_replace_video_urls(text):

pattern = r'https?://\S+\.(?:mp4)'
# 使用正则表达式提取所有视频链接
video_links = re.findall(pattern, text)
# 将包含视频链接的部分替换为 "如下视频"
updated_text = re.sub(pattern, '如下视频', text)
return video_links, updated_text


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

Loading…
取消
儲存