OpenAI SDK
只要改掉 Base URL,OpenAI SDK 就能直接配合网关使用。它可以调用模型库里的所有模型:文本模型走 Chat Completions,图像、语音和向量嵌入模型走各自的调用。
pip install openainpm install openai把客户端指向网关
Section titled “把客户端指向网关”只需要改两处:Base URL(以 /v1 结尾)和密钥。
import osfrom openai import OpenAI
client = OpenAI( base_url="https://api.exoticrouter.com/v1", api_key=os.environ["EXOTIC_API_KEY"],)
reply = client.chat.completions.create( model="anthropic/claude-sonnet-5-5", messages=[{"role": "user", "content": "Say hello in five words."}],)print(reply.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.exoticrouter.com/v1", apiKey: process.env.EXOTIC_API_KEY,});
const reply = await client.chat.completions.create({ model: "anthropic/claude-sonnet-5-5", messages: [{ role: "user", content: "Say hello in five words." }],});console.log(reply.choices[0].message.content);AsyncOpenAI 接受同样的两个参数。
或者通过环境变量设置
Section titled “或者通过环境变量设置”SDK 会从两个环境变量里读取 Base URL 和密钥,所以直接写 OpenAI() 的代码完全不用改。很多基于这个 SDK 的工具读取的也是这两个变量。
export OPENAI_BASE_URL="https://api.exoticrouter.com/v1"export OPENAI_API_KEY="sk-er-..."$env:OPENAI_BASE_URL = "https://api.exoticrouter.com/v1"$env:OPENAI_API_KEY = "sk-er-..."可以使用的调用
Section titled “可以使用的调用”| SDK 调用 | 模型 | 文档 |
|---|---|---|
client.chat.completions.create |
所有文本模型 | Chat Completions |
client.responses.create |
所有文本模型 | Responses |
client.embeddings.create |
向量嵌入模型 | Embeddings |
client.images.generate, client.images.edit |
图像模型 | Images |
client.audio.speech.create |
语音合成模型 | Audio |
client.audio.transcriptions.create |
语音转写模型 | Audio |
client.models.list, client.models.retrieve |
当前的模型库 | Models and ids |
那些需要在 OpenAI 一侧保存数据的调用不提供:文件、批处理、内容审核、微调、assistants、实时会话,以及获取已存储的 response。
用 ID 调用任意模型
Section titled “用 ID 调用任意模型”model 可以填模型库里的任意 ID,无论模型来自哪家厂商。同一个客户端可以调用所有模型:
for model in ["anthropic/claude-sonnet-5-5", "openai/gpt-6.2-astra", "google/gemini-4-neon", "deepseek/deepseek-v4-pro"]: reply = client.chat.completions.create( model=model, messages=[{"role": "user", "content": "Name yourself in three words."}], ) print(model, "->", reply.choices[0].message.content)for (const model of ["anthropic/claude-sonnet-5-5", "openai/gpt-6.2-astra", "google/gemini-4-neon", "deepseek/deepseek-v4-pro"]) { const reply = await client.chat.completions.create({ model, messages: [{ role: "user", content: "Name yourself in three words." }], }); console.log(model, "->", reply.choices[0].message.content);}- SDK 会自动重试失败的请求两次:遇到 429、5xx 状态码或连接中断时。可以用
max_retries调整。 - 默认超时时间是十分钟。耗时较长的图像生成可能需要这么久;较长的文本回答建议使用流式输出。
- Chat Completions:流式输出、工具调用、图像和 JSON。
- 集成:基于这个 SDK 的框架和工具。