快速开始
OpenRoute 提供统一 API,只需一个端点即可调用数百种 AI 模型,并自动处理故障转移、选择最具性价比的方案。仅需几行代码,即可通过你偏好的 SDK 或框架快速接入。
想了解免费模型和速率限制?请查看 FAQ
以下示例中的 OpenRoute 专属头部均为可选。设置后,你的应用即可出现在 OpenRoute 排行榜中。关于应用归属的详细信息,请阅读我们的 应用归属指南。
使用 OpenAI SDK
from openai import OpenAI
client = OpenAI(
base_url="https://api.openroute.cn/v1 ",
api_key="<OpenRoute_API_KEY>",
)
completion = client.chat.completions.create(
extra_headers={
"HTTP-Referer": "<YOUR_SITE_URL>", # 可选。用于在 openroute.cn 排行榜中展示你的网站 URL。
"X-Title": "<YOUR_SITE_NAME>", # 可选。用于在 openroute.cn 排行榜中展示你的网站名称。
},
model="openai/gpt-4o",
messages=[
{
"role": "user",
"content": "What is the meaning of life?"
}
]
)
print(completion.choices[0].message.content)
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://api.openroute.cn/v1 ',
apiKey: '<OpenRoute_API_KEY>',
defaultHeaders: {
'HTTP-Referer': '<YOUR_SITE_URL>', // 可选。用于在 openroute.cn 排行榜中展示你的网站 URL。
'X-Title': '<YOUR_SITE_NAME>', // 可选。用于在 openroute.cn 排行榜中展示你的网站名称。
},
});
async function main() {
const completion = await openai.chat.completions.create({
model: 'openai/gpt-4o',
messages: [
{
role: 'user',
content: 'What is the meaning of life?',
},
],
});
console.log(completion.choices[0].message);
}
main();
直接调用 OpenRoute API
你可以使用交互式 请求构建器 在任意语言中生成 OpenRoute API 请求。
import requests
import json
response = requests.post(
url="https://api.openroute.cn/v1/chat/completions ",
headers={
"Authorization": "Bearer <OpenRoute_API_KEY>",
"HTTP-Referer": "<YOUR_SITE_URL>", # 可选。用于在 openroute.cn 排行榜中展示你的网站 URL。
"X-Title": "<YOUR_SITE_NAME>", # 可选。用于在 openroute.cn 排行榜中展示你的网站名称。
},
data=json.dumps({
"model": "openai/gpt-4o", # 可选
"messages": [
{
"role": "user",
"content": "What is the meaning of life?"
}
]
})
)
fetch('https://api.openroute.cn/v1/chat/completions ', {
method: 'POST',
headers: {
Authorization: 'Bearer <OpenRoute_API_KEY>',
'HTTP-Referer': '<YOUR_SITE_URL>', // 可选。用于在 openroute.cn 排行榜中展示你的网站 URL。
'X-Title': '<YOUR_SITE_NAME>', // 可选。用于在 openroute.cn 排行榜中展示你的网站名称。
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'openai/gpt-4o',
messages: [
{
role: 'user',
content: 'What is the meaning of life?',
},
],
}),
});
curl https://api.openroute.cn/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OpenRoute_API_KEY" \
-d '{
"model": "openai/gpt-4o",
"messages": [
{
"role": "user",
"content": "What is the meaning of life?"
}
]
}'
API 同时支持 流式传输。
使用第三方 SDK
想了解如何使用第三方 SDK 和框架接入 OpenRoute,请 参阅我们的框架文档。