AI-Skill-Connector / groq_client.py
BasitAliii's picture
Create groq_client.py
99462d7 verified
raw
history blame contribute delete
862 Bytes
import os
import json
import re
from config import MODEL
try:
from groq import Groq
except Exception:
Groq = None
def init_groq_client():
api_key = os.getenv("GROQ_API_KEY")
if not api_key or Groq is None:
return None
return Groq(api_key=api_key)
def ask_groq_for_matches(system, user, model=MODEL):
client = init_groq_client()
if not client:
raise RuntimeError("Groq client not initialized.")
resp = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user},
],
)
content = resp.choices[0].message.content or ""
match = re.search(r"(\[\s*\{[\s\S]*?\}\s*\])", content)
if not match:
raise RuntimeError("No JSON array found.")
return json.loads(match.group(1))