OpenRouter 注册与免费使用 Hy3 Preview 模型完整指南(实战版)

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

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每分钟请求数
TPMtoken 限制
排队高峰期延迟
fallback不保证稳定

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穆友航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值