mcp2 / multi_inference.py
aymnsk's picture
Update multi_inference.py
88e9276 verified
# multi_inference.py (Groq-only version with error handling)
import requests
# === HARDCODED GROQ KEY ===
GROQ_API_KEY = "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" # βœ… Active key
def try_groq(prompt):
try:
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "llama3-70b-8192",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
],
"temperature": 0.7
}
res = requests.post(url, headers=headers, json=data)
if res.status_code != 200:
return f"[ERROR] Groq HTTP {res.status_code}: {res.text}"
try:
result = res.json()
except Exception:
return "[ERROR] Groq returned invalid JSON"
if "choices" in result:
return result["choices"][0]["message"]["content"]
return f"[ERROR] Groq: {result.get('error', {}).get('message', 'Unknown error')}"
except Exception as e:
return f"[ERROR] Groq Exception: {str(e)}"
def multi_query(prompt):
return try_groq(prompt)