Spaces:
Runtime error
Runtime error
File size: 2,298 Bytes
4439b52 |
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 |
"""
Groq API Integration
------------------
Handles interactions with the Groq API for language model inference.
"""
import os
import logging
from typing import Dict, Any, Optional
import requests
logger = logging.getLogger(__name__)
class GroqAPI:
"""Handles interactions with the Groq API."""
def __init__(self, api_key: Optional[str] = None):
"""Initialize the Groq API client."""
self.api_key = api_key or os.getenv('GROQ_API_KEY')
self.base_url = "https://api.groq.com/v1"
self.default_model = "mixtral-8x7b-32768"
async def predict(self, prompt: str, model: Optional[str] = None) -> Dict[str, Any]:
"""Make a prediction using the Groq API."""
try:
if not self.api_key:
return {
"success": False,
"error": "GROQ_API_KEY not set in environment variables",
"answer": "Error: GROQ_API_KEY not configured"
}
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
data = {
"model": model or self.default_model,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7,
"max_tokens": 1000
}
response = requests.post(
f"{self.base_url}/chat/completions",
headers=headers,
json=data
)
if response.status_code == 200:
result = response.json()
return {
"success": True,
"answer": result["choices"][0]["message"]["content"]
}
else:
return {
"success": False,
"error": f"API Error: {response.status_code}",
"answer": f"Error: {response.text}"
}
except Exception as e:
logger.error(f"Error calling Groq API: {str(e)}")
return {
"success": False,
"error": str(e),
"answer": f"Error: {str(e)}"
}
|