ajuste para verificar formdata e json na chamada groq
This commit is contained in:
parent
a4ba9d02bc
commit
a25dc9c4e7
@ -40,9 +40,10 @@ async def get_working_groq_key(storage) -> Optional[str]:
|
|||||||
storage.add_log("ERROR", "No working GROQ keys available")
|
storage.add_log("ERROR", "No working GROQ keys available")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def handle_groq_request(url: str, headers: dict, data: dict, storage) -> Tuple[bool, dict, str]:
|
async def handle_groq_request(url: str, headers: dict, data, storage, is_form_data: bool = False) -> Tuple[bool, dict, str]:
|
||||||
"""
|
"""
|
||||||
Handle GROQ API request with retries and key rotation.
|
Handle GROQ API request with retries and key rotation.
|
||||||
|
Suporta tanto JSON quanto FormData.
|
||||||
Returns: (success, response_data, error_message)
|
Returns: (success, response_data, error_message)
|
||||||
"""
|
"""
|
||||||
max_retries = len(storage.get_groq_keys())
|
max_retries = len(storage.get_groq_keys())
|
||||||
@ -50,31 +51,35 @@ async def handle_groq_request(url: str, headers: dict, data: dict, storage) -> T
|
|||||||
for attempt in range(max_retries):
|
for attempt in range(max_retries):
|
||||||
try:
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(url, headers=headers, json=data) as response:
|
if is_form_data:
|
||||||
response_data = await response.json()
|
async with session.post(url, headers=headers, data=data) as response:
|
||||||
|
response_data = await response.json()
|
||||||
if response.status == 200:
|
else:
|
||||||
# Validate response content
|
async with session.post(url, headers=headers, json=data) as response:
|
||||||
if "choices" in response_data and response_data["choices"]:
|
response_data = await response.json()
|
||||||
content = response_data["choices"][0].get("message", {}).get("content")
|
|
||||||
if content and await validate_transcription_response(content):
|
if response.status == 200:
|
||||||
return True, response_data, ""
|
# Validate response content
|
||||||
|
if "choices" in response_data and response_data["choices"]:
|
||||||
# Handle specific error cases
|
content = response_data["choices"][0].get("message", {}).get("content")
|
||||||
error_msg = response_data.get("error", {}).get("message", "")
|
if content and await validate_transcription_response(content):
|
||||||
if "organization_restricted" in error_msg or "invalid_api_key" in error_msg:
|
return True, response_data, ""
|
||||||
# Try next key
|
|
||||||
new_key = await get_working_groq_key(storage)
|
# Handle specific error cases
|
||||||
if new_key:
|
error_msg = response_data.get("error", {}).get("message", "")
|
||||||
headers["Authorization"] = f"Bearer {new_key}"
|
if "organization_restricted" in error_msg or "invalid_api_key" in error_msg:
|
||||||
storage.add_log("INFO", "Tentando nova chave GROQ após erro", {
|
# Try next key
|
||||||
"error": error_msg,
|
new_key = await get_working_groq_key(storage)
|
||||||
"attempt": attempt + 1
|
if new_key:
|
||||||
})
|
headers["Authorization"] = f"Bearer {new_key}"
|
||||||
continue
|
storage.add_log("INFO", "Tentando nova chave GROQ após erro", {
|
||||||
|
"error": error_msg,
|
||||||
return False, {}, f"API Error: {error_msg}"
|
"attempt": attempt + 1
|
||||||
|
})
|
||||||
|
continue
|
||||||
|
|
||||||
|
return False, {}, f"API Error: {error_msg}"
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Tratamento específico para erros de conexão
|
# Tratamento específico para erros de conexão
|
||||||
if "Connection" in str(e) and attempt < max_retries - 1:
|
if "Connection" in str(e) and attempt < max_retries - 1:
|
||||||
|
Loading…
Reference in New Issue
Block a user