Spaces:
Runtime error
Runtime error
File size: 2,553 Bytes
f3997d4 | 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | from groq import Groq
from typing import List, Dict, Optional
import json
from app.config.settings import settings
class LLMClient:
"""Client for interacting with Groq LLM API."""
def __init__(self):
"""Initialize Groq client."""
self.client = Groq(api_key=settings.GROQ_API_KEY)
self.model = settings.GROQ_MODEL
def get_completion(
self,
messages: List[Dict[str, str]],
temperature: float = 0.7,
max_tokens: int = 1024,
json_mode: bool = False
) -> str:
"""
Get completion from Groq LLM.
Args:
messages: List of message dictionaries with 'role' and 'content'
temperature: Sampling temperature (0-2)
max_tokens: Maximum tokens in response
json_mode: Whether to request JSON output
Returns:
Response content string
"""
try:
response_format = {"type": "json_object"} if json_mode else None
chat_completion = self.client.chat.completions.create(
messages=messages,
model=self.model,
temperature=temperature,
max_tokens=max_tokens,
response_format=response_format
)
return chat_completion.choices[0].message.content
except Exception as e:
print(f"Error getting LLM completion: {e}")
raise
def get_completion_with_retry(
self,
messages: List[Dict[str, str]],
temperature: float = 0.7,
max_tokens: int = 1024,
json_mode: bool = False,
max_retries: int = 3
) -> str:
"""
Get completion with retry logic.
Args:
messages: List of message dictionaries
temperature: Sampling temperature
max_tokens: Maximum tokens
json_mode: Whether to request JSON output
max_retries: Maximum number of retries
Returns:
Response content string
"""
for attempt in range(max_retries):
try:
return self.get_completion(messages, temperature, max_tokens, json_mode)
except Exception as e:
if attempt == max_retries - 1:
raise
print(f"Retry {attempt + 1}/{max_retries} after error: {e}")
continue
# Global LLM client instance
llm_client = LLMClient()
|