File size: 1,353 Bytes
35ecb2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""openai_compat.py - OpenAI-SDK drop-in client for the HF single-key router.

This is the BACKUP path: any code that already speaks the OpenAI SDK can point
at the HF router with one HF_TOKEN and keep working unchanged.

    from openai_compat import build_client
    client = build_client()                      # OpenAI(base_url=ROUTER_BASE, api_key=HF_TOKEN)
    r = client.chat.completions.create(
        model="meta-llama/Llama-3.1-8B-Instruct",
        messages=[{"role": "user", "content": "hi"}],
    )
"""
from __future__ import annotations
import os

ROUTER_BASE = os.getenv("HF_INFERENCE_API", "https://router.huggingface.co/v1")
DEFAULT_MODEL = os.getenv("HF_INFERENCE_API_MODEL", "meta-llama/Llama-3.1-8B-Instruct")


def build_client(api_key: str | None = None, base_url: str | None = None):
    """Return an OpenAI(client) pointed at the HF router, auth via HF_TOKEN."""
    try:
        from openai import OpenAI
    except ImportError:
        raise SystemExit(
            "openai SDK not installed. Install with: pip install 'openai>=1.0' "
            "(or `pip install -e \".[dev]\"`)"
        )
    key = api_key or os.getenv("HF_TOKEN") or os.getenv("OPENAI_API_KEY")
    if not key:
        raise SystemExit("HF_TOKEN missing - single-key fallback requires it")
    return OpenAI(base_url=base_url or ROUTER_BASE, api_key=key)