File size: 798 Bytes
c8b1c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Configuration and Groq client initialization.

- Loads environment variables via python-dotenv.
- Exposes a single `get_groq_client()` helper.

Never hardcode secrets. Put `GROQ_API_KEY` in `.env`.
"""

from __future__ import annotations

import os
from functools import lru_cache

from dotenv import load_dotenv
from groq import Groq


@lru_cache(maxsize=1)
def get_groq_client() -> Groq:
    """Create and cache a Groq client.

    Raises:
        RuntimeError: if GROQ_API_KEY is missing.
    """

    # Load .env (if present) once per process.
    load_dotenv()

    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        raise RuntimeError(
            "Missing GROQ_API_KEY. Create a .env file (see .env.example) and set GROQ_API_KEY."
        )

    return Groq(api_key=api_key)