非流式:一次性拿完整结果
·ThisToken.AI·
Tutorials入门教程ThisToken.AI
resp = client.chat.completions.create(
model="gpt-4o-mini",
stream=False,
messages=[{"role": "user", "content": "用一句话介绍SSE"}]
)
print("非流式结果:", resp.choices[0].message.content)
流式:逐块打印增量内容
stream = client.chat.completions.create(
model="gpt-4o-mini",
stream=True,
messages=[{"role": "user", "content": "从1数到5"}]
)
print("流式结果:", end="")
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
print()
There are two key points: first, `base_url="https://api.thistoken.ai/v1"` must be correct—this was the direct lesson from my third failure; second, the code demonstrates both `stream=False` and `stream=True`, so you can compare the two modes and choose based on your needs later—use streaming for typewriter effects and real-time feedback scenarios, and non-streaming when you need complete structured output.
## A Few Time-Saving Additional Tips
- **Don't hardcode your Key.** Use environment variables (e.g., `export THISTOKEN_API_KEY=...`) combined with `os.environ.get("THISTOKEN_API_KEY")` to avoid leaks.
- **Change only one variable at a time.** If you change the model name, don't change the stream parameter simultaneously, otherwise your logs won't match up during troubleshooting.
- **Check the HTTP status code before parsing.** 401 is a Key problem, 404 is most likely a base_url or path problem, 429 is rate limiting—categorize first, then act.
- **Don't guess about costs.** Refer to the official pricing page for specific model pricing, and during debugging, use a lightweight model first to get the pipeline working.
## Final Thoughts
Looking back, the evening I wasted was fundamentally due to charging ahead without any sense of layering. Debugging directly against the gateway with curl is actually the fastest way to verify the connection—the prerequisite is knowing which layer you're verifying. If you want to run through this process yourself, go register an account, get your own Key, and you can verify the first non-streaming code snippet in five minutes: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.Vous voulez essayer Token.AI ?
Créez une API Key au niveau du projet, activez les canaux dans la console et configurez le routage, les budgets et les journaux d'audit.
注册 ThisToken.AI 并获取 API Key