File size: 1,207 Bytes
b2efd24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import logging
from groq import AsyncGroq
from app.utils.key_manager import key_manager

logger = logging.getLogger(__name__)


async def get_groq_completion(messages: list, model: str = None) -> str:
    """
    Calls Groq API with automatic key rotation on failure.
    Retries across all available keys before raising.
    """
    if model is None:
        model = os.getenv("GROQ_MODEL", "llama3-70b-8192")

    max_retries = max(key_manager.key_count(), 1)
    last_error = None

    for attempt in range(max_retries):
        try:
            api_key = key_manager.get_next_key()
            client = AsyncGroq(api_key=api_key)
            response = await client.chat.completions.create(
                messages=messages,
                model=model,
                temperature=0.2,          # Low temp for deterministic structured output
                max_tokens=2048,
            )
            return response.choices[0].message.content

        except Exception as e:
            logger.warning(f"[Groq] Attempt {attempt + 1}/{max_retries} failed: {e}")
            last_error = e
            continue

    raise Exception(f"[Groq] All API keys exhausted. Last error: {last_error}")