Browse Source

调整

d1
H Vs 3 weeks ago
parent
commit
46a221e834
1 changed files with 64 additions and 28 deletions
  1. +64
    -28
      app/endpoints/pipeline_endpoint.py

+ 64
- 28
app/endpoints/pipeline_endpoint.py View File

@@ -2,6 +2,7 @@ from voice.ali.ali_voice import AliVoice
from common.log import logger from common.log import logger
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import os,json,asyncio,aiohttp,random import os,json,asyncio,aiohttp,random
from aiohttp import ClientError


from voice import audio_convert from voice import audio_convert


@@ -118,40 +119,75 @@ async def handle_messages_async(request: Request,token_id,msg):
case _: case _:
logger.warning(f'未知消息类型:{type_name}') 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: 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): async def handle_add_messages_async(request: Request,token_id,msg,wxid):




Loading…
Cancel
Save