From 46a221e834048a292e3204757571f7fbfe982bbb Mon Sep 17 00:00:00 2001 From: H Vs Date: Tue, 1 Apr 2025 17:05:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/endpoints/pipeline_endpoint.py | 92 +++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/app/endpoints/pipeline_endpoint.py b/app/endpoints/pipeline_endpoint.py index d88eab0..75d432b 100644 --- a/app/endpoints/pipeline_endpoint.py +++ b/app/endpoints/pipeline_endpoint.py @@ -2,6 +2,7 @@ from voice.ali.ali_voice import AliVoice from common.log import logger import xml.etree.ElementTree as ET import os,json,asyncio,aiohttp,random +from aiohttp import ClientError from voice import audio_convert @@ -118,40 +119,75 @@ async def handle_messages_async(request: Request,token_id,msg): case _: logger.warning(f'未知消息类型:{type_name}') -async def gpt_client_async(request,messages: list, wixd: str, friend_wxid: str): - c = await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wixd) - api_key = c.get('agentTokenId', "sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH") - print(f'流程key:{api_key}\n') +# async def gpt_client_async(request,messages: list, wixd: str, friend_wxid: str): +# c = await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wixd) +# api_key = c.get('agentTokenId', "sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH") +# print(f'流程key:{api_key}\n') - #api_key="sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH" #测试 - #api_key="sk-uJDBdKmJVb2cmfldGOvlIY6Qx0AzqWMPD3lS1IzgQYzHNOXv9SKNI" #开发2 +# #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}" - } +# api_url = "http://106.15.182.218:3000/api/v1/chat/completions" +# headers = { +# "Content-Type": "application/json", +# "Authorization": f"Bearer {api_key}" +# } - session_id = f'{wixd}-{friend_wxid}' - data = { - "model": "", - "messages": messages, - "chatId": session_id, - "detail": True - } +# session_id = f'{wixd}-{friend_wxid}' +# data = { +# "model": "", +# "messages": messages, +# "chatId": session_id, +# "detail": True +# } - logger.info("[CHATGPT] 请求={}".format(json.dumps(data, ensure_ascii=False))) +# logger.info("[CHATGPT] 请求={}".format(json.dumps(data, ensure_ascii=False))) - async with aiohttp.ClientSession() as session: +# async with aiohttp.ClientSession() as session: +# try: +# async with session.post(url=api_url, headers=headers, data=json.dumps(data), timeout=600) as response: +# response.raise_for_status() +# response_data = await response.json() +# logger.info("[CHATGPT] 响应={}".format(json.dumps(response_data, separators=(',', ':'), ensure_ascii=False))) +# return response_data +# except aiohttp.ClientError as e: +# logger.error(f"[CHATGPT] 请求失败: {e}") +# raise + + + +async def gpt_client_async(request, messages: list, wixd: str, friend_wxid: str): + max_retries = 3 + retry_delay = 5 # 重试间隔时间(秒) + + for attempt in range(max_retries): try: - async with session.post(url=api_url, headers=headers, data=json.dumps(data), timeout=600) as response: - response.raise_for_status() - response_data = await response.json() - logger.info("[CHATGPT] 响应={}".format(json.dumps(response_data, separators=(',', ':'), ensure_ascii=False))) - return response_data - except aiohttp.ClientError as e: - logger.error(f"[CHATGPT] 请求失败: {e}") - raise + c = await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wixd) + api_key = c.get('agentTokenId', "sk-jr69ONIehfGKe9JFphuNk4DU5Y5wooHKHhQv7oSnFzVbwCnW65fXO9kvH") + api_url = "http://106.15.182.218:3000/api/v1/chat/completions" + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {api_key}" + } + session_id = f'{wixd}-{friend_wxid}' + data = { + "model": "", + "messages": messages, + "chatId": session_id, + "detail": True + } + logger.info("[CHATGPT] 请求={}".format(json.dumps(data, ensure_ascii=False))) + async with aiohttp.ClientSession() as session: + async with session.post(url=api_url, headers=headers, data=json.dumps(data), timeout=1200) as response: + response.raise_for_status() + response_data = await response.json() + return response_data + except (ClientError, asyncio.TimeoutError) as e: + logger.error(f"[CHATGPT] 请求失败(尝试 {attempt + 1}/{max_retries}): {e}") + if attempt < max_retries - 1: + await asyncio.sleep(retry_delay) + else: + raise async def handle_add_messages_async(request: Request,token_id,msg,wxid):