File size: 769 Bytes
7f4d69f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Groq client stub - OpenAI-compatible /chat/completions."""
import os
import requests

BASE = os.getenv("GROQ_API_BASE", "https://api.groq.com/openai/v1")
KEY = os.getenv("GROQ_API_KEY")


def chat(messages, model="llama-3.1-8b-instant", stream=False, **kwargs):
    if not KEY:
        raise RuntimeError("GROQ_API_KEY not set")
    body = {"model": model, "messages": messages, "stream": stream, **kwargs}
    return requests.post(
        f"{BASE}/chat/completions",
        headers={"Authorization": f"Bearer {KEY}"},
        json=body, timeout=60, stream=stream,
    )


def list_models():
    if not KEY:
        raise RuntimeError("GROQ_API_KEY not set")
    return requests.get(f"{BASE}/models", headers={"Authorization": f"Bearer {KEY}"}, timeout=30).json()