Index

快速开始

OpenRoute 提供统一 API,只需一个端点即可调用数百种 AI 模型,并自动处理故障转移、选择最具性价比的方案。仅需几行代码,即可通过你偏好的 SDK 或框架快速接入。

想了解免费模型和速率限制?请查看 FAQ

以下示例中的 OpenRoute 专属头部均为可选。设置后,你的应用即可出现在 OpenRoute 排行榜中。关于应用归属的详细信息,请阅读我们的 应用归属指南。

使用 OpenAI SDK

Python
from openai import OpenAI

client = OpenAI(
  base_url="https://api.openroute.cn/v1 ",
  api_key="<OntoRoute_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)

直接调用 OpenRoute API

你可以使用交互式 请求构建器 在任意语言中生成 OpenRoute API 请求。

Python
import requests
import json

response = requests.post(
  url="https://api.openroute.cn/v1/chat/completions ",
  headers={
    "Authorization": "Bearer <OntoRoute_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?"
      }
    ]
  })
)

API 同时支持 流式传输。

On this page