一個 base_url,多個模型:切換至 OpenAI 相容端點
One base_url, many models: switching to an OpenAI-compatible endpoint
文章作者以 AIO Keys 為例,説明透過 OpenAI 相容端點切換模型供應商,只需更改 base URL 和 key,既有用戶端程式碼便可維持不變。不同供應商的模型識別名稱未必通用,應在設定中維護對照;作者亦建議按實際 token 用量計費,並在用戶端加入重試及備用方案。
Disclosure up front: I work on the gateway described below, so treat the code as practical and the conclusions as my own experience, not neutral benchmarks.
The problem
Most teams start with one model provider. Then a second need appears — cheaper bulk tasks, a model that handles a different language better, or a local option for sensitive data. Suddenly you are managing two SDKs, two key formats, and two billing dashboards.
The idea
An OpenAI-compatible endpoint keeps the request and response shape you already use. You change the base URL and the key. The rest of the client code stays put.
from openai import OpenAI
client = OpenAI(
base_url="https://your-gateway.example/v1",
api_key="YOUR_KEY",
)
resp = client.chat.completions.create(
model="chosen-model",
messages=[{"role": "user", "content": "Hello"}],
)
That is the whole migration. If your app already speaks the OpenAI protocol, it already speaks the gateway.
Three things I learned the hard way
- Model names are not portable. The same logical model can have a different identifier on each provider. Keep a mapping table in config, never hardcoded in prompts.
- Billing model matters more than headline price. Pay-as-you-go on actual tokens beat fixed packages for our uneven traffic. No seat fee, no separate authorization-server fee.
- Do not promise five-nines to your own users. No infrastructure is 100% available. Build retries and a fallback into the client regardless of provider.
Why both US and Chinese models in one place
For teams serving multiple regions, one endpoint that reaches both US and Chinese models removes a lot of account plumbing. Whether that fits your case depends on your data policy — check it against your own requirements.
Reference
The gateway I use is AIO Keys — aiokeys.com. It exposes an OpenAI-compatible endpoint, supplies official keys, and supports multiple settlement methods including ruble and USDT. Specific model lists and rates change, so verify them on the site rather than trusting any article, including this one.
來源:DEV Community · dev.to