ai-platform-toolbox / scripts /openai_compat.py
AnthropicallyHuggingFaces's picture
Add OpenAI-compat helper module
35ecb2d verified
Raw History Blame Contribute Delete
1.35 kB
"""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)