无服务器实现openai接口反向代理和额度查询
使用Cloudflare代理openai接口,并实现简单查询额度。
安装步骤
- 将代码部署到Cloudflare的worker平台(其他支持worker的平台也可)
- 替换自己代码中的
api.openai.com
为你自己的worker域名,例如your.worker.domain
Worker 代码
/**
* @auther Rehiy
* @url https://www.rehiy.com/post/500
* @description woker service for cloudflare
*/
async function openai_get(api, key) {
const opts = {
method: 'GET',
headers: {
Authorization: 'Bearer ' + key,
},
};
const url = 'https://api.openai.com/v1/' + api;
return fetch(url, opts).then(r => r.json());
}
async function openai_usage(key) {
const today = new Date();
const formatDate = function (timestamp) {
const date = new Date(timestamp * 1000);
return [date.getFullYear(), date.getMonth() + 1, date.getDate()].join('-');
};
const subscription = await openai_get('dashboard/billing/subscription', key);
const start_date = subscription.hard_limit_usd > 20
? [today.getFullYear(), today.getMonth() + 1, '1'].join('-') : formatDate(today / 1000 - 90 * 86400);
const end_date = formatDate(today / 1000 + 86400);
const usage = await openai_get(`dashboard/billing/usage?start_date=${start_date}&end_date=${end_date}`, key);
return {
access_until: formatDate(subscription.access_until),
hard_limit_usd: subscription.hard_limit_usd.toFixed(5),
total_usage: (usage.total_usage / 100).toFixed(5),
left_quota: (subscription.hard_limit_usd - usage.total_usage / 100).toFixed(5),
start_date: start_date,
end_date: end_date,
};
}
async function openai_proxy(request) {
const url = new URL(request.url);
const auth = request.headers.get('Authorization');
const backend = request.url.replace(url.host, 'api.openai.com');
const payload = {
method: request.method,
headers: { Authorization: auth },
};
if (request.body) {
payload.body = await request.text();
payload.headers['Content-Type'] = 'application/json';
}
return fetch(backend, payload);
}
export default {
async fetch(request) {
if (request.method === 'OPTIONS') {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS',
'Access-Control-Allow-Headers': '*',
};
return new Response(null, { headers: corsHeaders });
}
const url = new URL(request.url);
if (url.pathname.startsWith('/v1/')) {
return openai_proxy(request);
}
if (url.pathname.startsWith('/usage/')) {
const [, key] = url.pathname.split('/usage/');
return new Response(JSON.stringify(openai_usage(key)));
}
return new Response('Not Found', { status: 404 });
}
}