diff --git a/app/endpoints/pipeline_endpoint.py b/app/endpoints/pipeline_endpoint.py index da386c6..241f7a3 100644 --- a/app/endpoints/pipeline_endpoint.py +++ b/app/endpoints/pipeline_endpoint.py @@ -595,7 +595,8 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, # 判断图片url 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 request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) + await ai_post_text_split_async_async(request, token_id, app_id, callback_to_user, reply_content) await asyncio.sleep(random.uniform(1.5, 3)) for img_url in img_urls: await request.app.state.gewe_service.post_image_async(token_id, app_id, callback_to_user, img_url) @@ -604,7 +605,8 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, # 判断视频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 request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, reply_content) + await ai_post_text_split_async_async(request, 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) @@ -620,7 +622,8 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, 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.post_text_async(token_id, app_id, callback_to_user, reply_content) + await ai_post_text_split_async_async(request, 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}) # 回复的对话 @@ -633,8 +636,25 @@ async def ai_chat_text_async(request: Request,token_id, app_id, wxid, msg_data, execution_time = end_time - start_time # 计算执行时间 logger.info(f"AI回答任务完成,耗时 {execution_time:.2f} 秒") -async def ai_text_split_post_async(request: Request,token_id,app_id, wxid,msg_data,from_wxid, to_wxid): - pass +# async def ai_post_text_split_async_async(request: Request, token_id, app_id, callback_to_user, reply_content): +# parts = reply_content.split('\n\n') +# for part in parts: +# await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, part) +# await asyncio.sleep(random.uniform(1.5, 3)) + +async def ai_post_text_split_async_async(request: Request, token_id, app_id, callback_to_user, reply_content): + parts = reply_content.split('\n\n') + i = 0 + while i < len(parts): + current_part = parts[i].strip() # 去除首尾空白字符 + # 如果当前段长度小于30个汉字,则尝试与下一段合并 + while len(current_part) < 30 and i + 1 < len(parts): + i += 1 + current_part += "\n " + parts[i].strip() # 合并下一段 + # 打印合并后的内容 + await request.app.state.gewe_service.post_text_async(token_id, app_id, callback_to_user, current_part) + await asyncio.sleep(random.uniform(1.5, 3)) + i += 1 async def handle_text_group_async(request: Request,token_id,app_id, wxid,msg_data,from_wxid, to_wxid): '''