Spaces:
Configuration error
Configuration error
Download scripts/openai_compat.py from AnthropicallyHuggingFaces/ai-platform-toolbox: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/resolve/main/scripts/openai_compat.py
- Command line
-
hf download hf://spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/scripts/openai_compat.py
-
curl -L -o openai_compat.py https://huggingface.co/spaces/AnthropicallyHuggingFaces/ai-platform-toolbox/resolve/main/scripts/openai_compat.py
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) | |