OpenRouter API 代理服务

基于Cloudflare Worker的AI模型API转发服务
状态: 正常运行

概述

这是一个专为解决跨域问题和API访问限制而设计的代理服务。它可以将您的请求安全地转发到OpenRouter API,让您轻松访问各种先进的AI语言模型,包括Google Gemini、Deepseek等。

主要功能

API 参考

基础 URL

https://zheng.2020classes4.dpdns.org

请求方法

方法 描述
POST 发送聊天请求,获取AI模型回复
GET 获取API文档
OPTIONS 响应预检请求 (CORS)

请求格式

请求体应为JSON格式,包含以下字段:

参数 类型 必填 描述
model String 必填 要使用的模型ID,例如 google/gemini-2.5-pro-preview-03-25
messages Array 必填 对话消息数组,格式见下文
stream Boolean 可选 是否使用流式传输 (目前仅支持false)
max_tokens Integer 可选 返回的最大标记数

消息格式

messages 数组中的每个元素应包含以下字段:

字段 类型 描述
role String 消息角色,可以是 userassistant
content Array/String 消息内容。对于用户消息,这是一个包含 typetext 的对象数组;对于助手消息,这是一个字符串
注意

为了确保API正常工作,消息数组不应超过20条消息。如果超过,系统会自动截取最新的20条消息。

使用示例

基本示例

以下是一个简单的API调用示例:

// 使用fetch API发送请求
fetch('https://zheng.2020classes4.dpdns.org', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        model: "google/gemini-2.5-pro-preview-03-25",
        messages: [
            {
                role: "user",
                content: [{ type: "text", text: "你好,请介绍一下自己" }]
            }
        ]
    })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

带上下文的对话示例

要进行连续对话,只需在请求中包含之前的消息:

// 带有上下文的对话示例
fetch('https://zheng.2020classes4.dpdns.org', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        model: "google/gemini-2.5-pro-preview-03-25",
        messages: [
            {
                role: "user",
                content: [{ type: "text", text: "你好,请介绍一下自己" }]
            },
            {
                role: "assistant",
                content: "你好!我是由Google开发的Gemini大型语言模型,我能回答问题、提供信息和帮助完成各种任务。"
            },
            {
                role: "user",
                content: [{ type: "text", text: "你有哪些主要功能?" }]
            }
        ]
    })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

响应格式

API的响应采用标准的OpenRouter响应格式:

{
    "id": "gen-xxxxxxxxxxxx",
    "model": "google/gemini-2.5-pro-preview-03-25",
    "created": 1718123456,
    "object": "chat.completion",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "这里是AI的回复内容...",
                "function_call": null,
                "tool_calls": null
            },
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 123,
        "completion_tokens": 456,
        "total_tokens": 579
    }
}

错误响应

当发生错误时,API会返回以下格式的错误响应:

{
    "error": "错误信息描述",
    "timestamp": "2023-10-15T08:30:45.123Z"
}

支持的模型

本API支持OpenRouter上的所有模型,以下是一些推荐模型:

模型ID 描述
google/gemini-2.5-pro-preview-03-25 Google Gemini 2.5 Pro - 最新的大型多模态模型
google/gemini-2.5-pro-exp-03-25:free Google Gemini 免费版 - 提供有限制的免费访问
deepseek/deepseek-chat-v3-0324:free Deepseek Chat - 优秀的开源中文大语言模型
anthropic/claude-3-sonnet:beta Anthropic Claude 3 Sonnet - 平衡性能和速度的优秀模型
anthropic/claude-3-opus:beta Anthropic Claude 3 Opus - 最先进的推理和智能模型
提示

请访问 OpenRouter模型列表 获取完整的可用模型信息。

限制与注意事项