Spaces:
No application file
No application file
| 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}") | |