统一入口:所有模型都走这一个 base_url
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
client = OpenAI(
api_key=os.environ.get("THISTOKEN_API_KEY"),
base_url="https://api.thistoken.ai/v1"
)
def chat(model: str, prompt: str) -> str:
"""同一个函数,换 model 参数即可切换不同模型"""
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
if __name__ == "__main__":
想换模型?只改这个字符串,其他代码一行不动
print(chat("gpt-4o-mini", "用一句话解释什么是API网关"))
Run it:
python main.py
If you get back a Chinese explanation, congratulations—the pipeline is working. Next, do a switching experiment: change the model parameter to another model name supported by the platform, and run it again. You'll find that not a single line of the rest of your code needs to change—this is the entire point of unifying base_url.
## Step 3: Push Switching Costs Down to Nearly Zero
Once things are running, I recommend doing two small things to lock in the efficiency gains:
**1. Put the model into a config file**
import json
with open("config.json") as f:
MODEL = json.load(f)["model"]
This way, switching models doesn't even require touching code—just change the config.
**2. Build a five-minute comparison script**
Write a small script that sends the same prompt to two or three models and outputs the results side by side. From then on, evaluating a new model goes from "half a day of manual testing" to "running one script". This may be the biggest hidden benefit of unifying base_url—your willingness to try new models will noticeably increase, because the cost of experimentation approaches zero.
## Some Pitfall Warnings
- **Environment variables not taking effect**: export only works in the current terminal session; consider adding it to your shell config file or using a `.env` file with `python-dotenv`
- **Wrong model name**: if you get a 404 or "model not found" error, first check the list of supported model names in the platform documentation
- **Don't delete the old configuration right after switching**: keep the previous model's configuration so that if the new model performs poorly in certain scenarios, you can roll back within a minute
## Doing the Full Math
Let's summarize from the efficiency perspective. This base_url unification refactor requires a one-time investment of about half a day (including registration, code changes, and writing the comparison script), and in return you get:
- A single model switch drops from 1-2 days to under 10 minutes
- Provider management compresses from N accounts and N Keys to 1 account and 1 Key
- New model evaluation goes from manual testing to scripted comparison
If your product switches or evaluates models once a month, this investment pays for itself in the first month, and everything after is pure gain. More importantly, it frees you from "interface compatibility maintenance"—work with zero incremental value—so your time can be spent on the product itself.
Spend five minutes now to register and set up this infrastructure: **https://api.thistoken.ai/register**
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key