OpenRouter 是一个统一的 LLM API 聚合平台,它把多个大模型(OpenAI、Claude、Gemini、DeepSeek 等)封装成一个 OpenAI 兼容接口,让开发者可以像切换模型一样简单地使用不同 AI。
一、 OpenRouter
OpenRouter 本质是一个:
LLM API Router(大模型路由网关)
它做三件核心事情:
1. 统一 API
所有模型统一成:
https://openrouter.ai/api/v1/chat/completions
2. 聚合模型
包括:
- GPT 系列
- Claude 系列
- Gemini
- DeepSeek
- Qwen
- Tencent Hy3
- Mistral 等
3. 自动路由 & 计费
- 自动选择 provider
- 自动 fallback
- 按 token 计费
- 部分模型提供 free tier
二、注册 OpenRouter
Step 1:访问官网
Step 2:登录方式
支持:
- GitHub 登录
- Google 登录
- 邮箱注册
Step 3:创建 API Key
进入:
Dashboard → Keys → Create Key
得到:
sk-or-xxxxxxxxxxxx
⚠️ 这个就是你后面调用 API 的 key
三、免费模型 Hy3 Preview
模型名:
tencent/hy3-preview:free
特点:
-
腾讯系推理模型
-
支持 reasoning(思维链)
-
免费额度
-
OpenRouter 托管调用
-
适合:
- 推理任务
- 数学题
- 代码分析
四、安装 Python SDK
pip install openai
OpenRouter 直接复用 OpenAI SDK。
五、基础调用
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="你的API_KEY"
)
response = client.chat.completions.create(
model="tencent/hy3-preview:free",
messages=[
{"role": "user", "content": "How many r's are in strawberry?"}
]
)
print(response.choices[0].message.content)
六、启用 reasoning
Hy3 preview 支持推理模式:
response = client.chat.completions.create(
model="tencent/hy3-preview:free",
messages=[
{"role": "user", "content": "Solve step by step: 23 * 47"}
],
extra_body={
"reasoning": {
"enabled": True
}
}
)
七、完整工程版
包含:
- ⏱ 请求耗时
- 🔢 token 统计
- 🧠 reasoning 传递
- 🔁 多轮对话
import time
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR_API_KEY"
)
def call_model(messages):
start = time.time()
resp = client.chat.completions.create(
model="tencent/hy3-preview:free",
messages=messages,
extra_body={"reasoning": {"enabled": True}}
)
end = time.time()
usage = resp.usage
print("\n===== API STATS =====")
print(f"Time: {end - start:.2f}s")
print(f"Prompt tokens: {usage.prompt_tokens}")
print(f"Completion tokens: {usage.completion_tokens}")
print(f"Total tokens: {usage.total_tokens}")
return resp.choices[0].message
# 第一次请求
msg1 = call_model([
{"role": "user", "content": "How many r's are in strawberry?"}
])
# 带 reasoning 的上下文
messages = [
{"role": "user", "content": "How many r's are in strawberry?"},
{
"role": "assistant",
"content": msg1.content,
"reasoning_details": getattr(msg1, "reasoning_details", None)
},
{"role": "user", "content": "Are you sure? Think carefully."}
]
# 第二次请求
msg2 = call_model(messages)
print("\nFINAL ANSWER:")
print(msg2.content)
八、OpenRouter 免费模型机制
1. free 模型来源
通常来自:
- provider 补贴
- 限速 API
- 开源模型推理节点
- promotional traffic
2. 免费限制
一般包括:
| 限制类型 | 说明 |
|---|---|
| RPM | 每分钟请求数 |
| TPM | token 限制 |
| 排队 | 高峰期延迟 |
| fallback | 不保证稳定 |
5842

被折叠的 条评论
为什么被折叠?



