![图片[1]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/fcdb0935-830a-417d-8212-a1134f653d4c-1024x576.png)
OpenAI于2025年4月15日凌晨正式发布了全新GPT-4.1系列模型,包括标准版GPT-4.1、轻量高效的GPT-4.1 mini以及超小型性价比之选GPT-4.1 nano。这些模型在多模态处理、代码能力、指令遵循和成本效率等方面实现了显著突破,特别是100万token的超长上下文处理能力,标志着OpenAI在大模型技术上再次引领行业创新。本文将详细介绍GPT-4.1系列的核心特性、性能提升及应用场景,帮助开发者和用户全面了解这一重大更新。
根据Sam Altman的X消息,GPT-4.1模型只会在API中提供
![图片[2]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/QQ20250415-151757.png)
关于模型的更多详细信息可前往OpenAI官网查看
https://openai.com/index/gpt-4-1
最主要的是,这次“大善人”直接提供免费一个星期的API,也就是说在这一个星期内,所有用户都可以通过API来免费使用GPT-4.1模型
不过还是有一定的速率限制的:
RPM(每分钟请求数):3 TPM(每分钟 tokens 数):10000 RPD(每天请求次数):200
对于普通用户应该是完全够用了。
GPT4.1模型官网定价:
Model | 输入 | 缓存输入 | 输出 |
---|---|---|---|
gpt-4.1 gpt-4.1-2025-04-14 | $2.00 | $0.50 | $8.00 |
gpt-4.1-mini gpt-4.1-mini-2025-04-14 | $0.40 | $0.10 | $1.60 |
gpt-4.1-nano gpt-4.1-nano-2025-04-14 | $0.10 | $0.025 | $0.40 |
尝试接入Cloudflare AI Gateway:
Cloudflare AI Gateway 可以为你的 AI 应用提供集中管理和控制,监控使用情况、成本和错误。通过缓存、速率限制、请求重试和模型回退来降低风险和成本。轻松确保可靠性、可扩展性和生产力。
要使用AI Gateway只需要一个Cloudflare账号即可
注意:AI Gateway与OpenAI API配合使用时不可以直接在中国大陆直接访问,否则会出现以下错误
{
"error": {
"code": "unsupported_country_region_territory",
"message": "Country, region, or territory not supported",
"param": null,
"type": "request_forbidden"
}
}
登录账号后在左侧菜单即可找到AI Gateway标签
![图片[3]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/QQ20250415-155019-1024x719.png)
进入到AI Gateway管理页面后点击创建网关
![图片[4]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-1024x439.png)
然后根据自己喜好填写名称,配置参数,最后点击创建即可
![图片[5]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-1-1024x842.png)
免费用户日志数量限制
计划 | 免费存储日志 | 超额定价 |
---|---|---|
FREE | 共计 100,000 份日志 | 不适用 – 升级为付费 |
创建后,点击右上角的API
![图片[6]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-2-1024x235.png)
平台选择OpenAI,之后就可以看到API端点和Curl调用的命令,理论上对于OpenAI的API只需要把API的URL替换成你的API端点就可以直接使用。
![图片[7]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-4-1024x947.png)
可以使用Postman来测试API
记得将请求头中的OPENAI_TOKEN替换成你的OpenAI API KEY(登录官网获取)。
![图片[8]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-5-1024x726.png)
AI Gateway最重要的一点就是可以监控API的用量,并且每次请求日志都会记录,可以登录到AI Gateway的控制台查看
![图片[9]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-6-1024x563.png)
可以看到刚刚的请求内容和返回结果都有记录,并且还会给出令牌的使用数量和预估成本,可谓是非常舒服了,AI Gateway支持大多数主流的AI平台,可以直接接入,支持缓存响应,这样子可以大大节约成本
Workers反向代理AI Gateway实现多OpenAI令牌轮询:
为了解决AI Gateway一些问题,这里可以直接尝试使用Workers来反向代理
这里尝试用o3模型写了一个简易的Workers
// 监听 fetch 事件,所有传入的请求都会调用 handleRequest 函数
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
// 定义 API 密钥列表(请替换成你自己的 API Token)
const API_KEYS = [
'OPENAI_TOKEN_1', // 替换为真实的 API Token
'OPENAI_TOKEN_2',
'OPENAI_TOKEN_3'
]
// 全局变量,用于记录当前使用的密钥索引(注意:Cloudflare Workers 中全局变量可能在不同请求间不一定稳定,但足以实现简单轮询)
let currentKeyIndex = 0
// 轮询选择下一个 API 密钥
function getNextApiKey() {
const apiKey = API_KEYS[currentKeyIndex]
// 更新索引,使用模运算实现循环轮询
currentKeyIndex = (currentKeyIndex + 1) % API_KEYS.length
return apiKey
}
// 主处理函数,负责接收客户端请求、调用上游 API,并返回流式响应
async function handleRequest(request) {
// 解析请求的 URL,对路径和参数进行处理
const url = new URL(request.url)
// API 端点
const targetBase = 'https://gateway.ai.cloudflare.com/v1/XXXXXXX/gpt41/openai'
// 拼接目标 URL。这里简单将请求路径拼接到目标 URL 后面,
// 如果请求路径并非完全匹配,可以在此处做额外的处理或映射。
const targetUrl = targetBase + url.pathname
// 使用轮询方式获取一个 API 密钥
const apiKey = getNextApiKey()
// 构造新的请求头,保留原始请求头,并替换 Authorization 头为轮询后的 API 密钥
const newHeaders = new Headers(request.headers)
newHeaders.set('Authorization', 'Bearer ' + apiKey)
// 构造用于转发的请求,保持方法、请求体等与原始请求一致,实现流传输
const proxyRequest = new Request(targetUrl, {
method: request.method,
headers: newHeaders,
body: request.body,
redirect: 'follow'
})
// 向上游接口发起请求,并等待响应
const response = await fetch(proxyRequest)
// 如果需要支持跨域请求,可添加 CORS 头;例如允许所有来源访问:
const newResponseHeaders = new Headers(response.headers)
newResponseHeaders.set('Access-Control-Allow-Origin', '*')
// 返回新的响应,直接使用响应体流,这样可以实现流式传输(例如处理 OpenAI 流式响应)
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newResponseHeaders
})
}
经过测试流式响应没有问题
![图片[10]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-7-1024x410.png)
可以配合nextchat食用
![图片[11]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-8-1024x898.png)
最后附上一个4.1做的天气卡片
![图片[12]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-9.png)
天气SVG
![图片[13]-Openai-GPT-4.1正式发布:一周免费api体验及Cloudflare AI Gateway接入教程-星际博客](https://theblog.cn/wp-content/uploads/2025/04/image-10.png)
代码:(SVG查看器:SVGViewer)
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="400">
<!-- 背景 -->
<rect width="600" height="400" fill="#22223b"/>
<!-- 晴天 -->
<g>
<circle cx="100" cy="100" r="40" fill="#f9d923"/>
<!-- 太阳光芒 -->
<g stroke="#f9d923" stroke-width="4">
<line x1="100" y1="40" x2="100" y2="10"/>
<line x1="100" y1="160" x2="100" y2="190"/>
<line x1="40" y1="100" x2="10" y2="100"/>
<line x1="160" y1="100" x2="190" y2="100"/>
<line x1="60" y1="60" x2="35" y2="35"/>
<line x1="140" y1="60" x2="165" y2="35"/>
<line x1="60" y1="140" x2="35" y2="165"/>
<line x1="140" y1="140" x2="165" y2="165"/>
</g>
<text x="100" y="180" font-family="Arial" font-size="22" fill="#fff" text-anchor="middle">Sunny</text>
</g>
<!-- 多云 -->
<g>
<ellipse cx="250" cy="110" rx="40" ry="30" fill="#a3cef1"/>
<ellipse cx="285" cy="100" rx="30" ry="25" fill="#b8b8d1"/>
<ellipse cx="225" cy="120" rx="25" ry="20" fill="#a3cef1"/>
<text x="250" y="180" font-family="Arial" font-size="22" fill="#fff" text-anchor="middle">Cloudy</text>
</g>
<!-- 下雨 -->
<g>
<!-- 云 -->
<ellipse cx="400" cy="110" rx="40" ry="30" fill="#b8b8d1"/>
<ellipse cx="435" cy="100" rx="30" ry="25" fill="#a3cef1"/>
<ellipse cx="375" cy="120" rx="25" ry="20" fill="#b8b8d1"/>
<!-- 雨滴 -->
<g fill="#00b4d8">
<ellipse cx="385" cy="150" rx="5" ry="10"/>
<ellipse cx="400" cy="155" rx="5" ry="10"/>
<ellipse cx="415" cy="150" rx="5" ry="10"/>
</g>
<text x="400" y="180" font-family="Arial" font-size="22" fill="#fff" text-anchor="middle">Rainy</text>
</g>
<!-- 雷阵雨 -->
<g>
<!-- 云 -->
<ellipse cx="100" cy="270" rx="40" ry="30" fill="#b8b8d1"/>
<ellipse cx="135" cy="260" rx="30" ry="25" fill="#a3cef1"/>
<ellipse cx="75" cy="280" rx="25" ry="20" fill="#b8b8d1"/>
<!-- 闪电 -->
<polygon points="90,290 110,290 100,310 120,310 100,340 110,320 90,320" fill="#f9d923"/>
<text x="100" y="360" font-family="Arial" font-size="22" fill="#fff" text-anchor="middle">Thunder</text>
</g>
<!-- 雪天 -->
<g>
<!-- 云 -->
<ellipse cx="250" cy="270" rx="40" ry="30" fill="#b8b8d1"/>
<ellipse cx="285" cy="260" rx="30" ry="25" fill="#a3cef1"/>
<ellipse cx="225" cy="280" rx="25" ry="20" fill="#b8b8d1"/>
<!-- 雪花 -->
<g stroke="#fff" stroke-width="2">
<line x1="250" y1="300" x2="250" y2="320"/>
<line x1="242" y1="305" x2="258" y2="315"/>
<line x1="258" y1="305" x2="242" y2="315"/>
</g>
<g stroke="#fff" stroke-width="2">
<line x1="270" y1="305" x2="270" y2="325"/>
<line x1="262" y1="310" x2="278" y2="320"/>
<line x1="278" y1="310" x2="262" y2="320"/>
</g>
<text x="250" y="360" font-family="Arial" font-size="22" fill="#fff" text-anchor="middle">Snowy</text>
</g>
</svg>