Skills:
x-dr / chatgptproxyapi Goto Github PK
View Code? Open in Web Editor NEW🔥 使用cloudflare 搭建免费的 OpenAI api代理 ,解决网络无法访问问题。支持流式输出
Home Page: https://chatai.451024.xyz
License: MIT License
🔥 使用cloudflare 搭建免费的 OpenAI api代理 ,解决网络无法访问问题。支持流式输出
Home Page: https://chatai.451024.xyz
License: MIT License
rt
开头少字如何解决
async function handleRequest(request) {
const url = new URL(request.url)
let target = url.searchParams.get('target')
if (!target) {
return new Response('Target host is required', { status: 400 })
}
target = target.replace(/^https?:\/\//, '')
const apiUrl = new URL(request.url)
apiUrl.host = target
const modifiedRequest = new Request(apiUrl, request)
const response = await fetch(modifiedRequest)
const modifiedResponse = new Response(response.body, response)
modifiedResponse.headers.set('Access-Control-Allow-Origin', '*')
return modifiedResponse
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
代码小白求大神解答
按照步骤配好了,可是在项目中报错,ERROR c.r.f.w.e.GlobalExceptionHandler - [handleRuntimeException,69] - 请求地址'/gpt/chat',发生未知异常.
java.lang.RuntimeException: java.net.SocketException: Connection reset
参照CloudFlare 的命令api页面显示404
各位大神能告诉我Vercel的构建命令吗?
对nextjs不熟悉
想问下是否可以实现更换api的路径
例如把{url}/api修改为{url}/openai
因为有其他冲突
谢谢
Error: Could not access built-in Node.js modules. Please make sure that your Cloudflare Pages project has the 'nodejs_compat' compatibility flag set.
应该以哪个为准?
访问时有时候快有时候慢,怎么加快
目前登录必须要翻墙才可以,能否通过项目实现不翻墙登录
打开xxxxxxxx.pages.dev/v1/chat/completions ,网页是404
弱弱的问下, 这种方式只是能在没有科学上网的时候使用吗? 还是说为了解决 chatgpt 停留一会儿需要刷新的问题?
有把clash放到这个works里的教程吗
有个问题是只能用自定义域名,然后这个域名必须被它托管,被它dns解析后国内访问不稳定,经常链接不上。
大家都没遇到这个问题吗?
这个和阿里云函数计算直接在美区部署一个转发流程没有区别
Worker ->test-> edit 左侧到底是贴:
export default {
async fetch(request, env) {
const url = new URL(request.url);
url.host = "api.openai.com";
// openai is already set all CORS heasders
return fetch(url, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
}
}
还是:
const TELEGRAPH_URL = 'https://api.openai.com';
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const url = new URL(request.url);
const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
url.host = TELEGRAPH_URL.replace(/^https?:///, '');
const modifiedRequest = new Request(url.toString(), {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
const response = await fetch(modifiedRequest);
const modifiedResponse = new Response(response.body, response);
// 添加允许跨域访问的响应头
modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
return modifiedResponse;
}
列出 1 2 3 我以为是步骤,没想到是三种方法
我按照教程用pages搭建的转发好像没效果,能帮忙看看什么原因吗
这个是复制的代码
https://github.com/luoysuan/Forwarding/blob/main/_worker.js
代理不是流式输出的吗,为什么使用了之后要等所有回答文字都返回了才输出
我是用docker 部署的
docker run -itd --name openaiproxy
-p 3000:3000
--restart=always
gindex/openaiproxy:latest
为什么就调用了几次就返回错误
这个的作用是什么?难道就是翻墙下 ?
{
"error": {
"message": "The OpenAI API can only be accessed over HTTPS. You should access https://api.openai.com rather than the current URL.",
"type": "invalid_request_error"
}
}
请求是
curl --location 'https://chatapi.xxx.xxx/v1/chat/completions' \
--header 'Authorization: Bearer sk-xxxxxxxxxxxxxxx' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Hello, thank you for sharing this implementation method. It has inspired me to deploy a deeplx API on edge functions using a similar approach. However, I keep getting error 525 and would like to seek your help.
// src/pages/api/translate.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
interface RequestParams {
text: string;
source_lang: string;
target_lang: string;
}
interface ResponseParams {
id: number;
code: number;
data: string;
}
async function queryAPI(data: RequestParams): Promise<ResponseParams> {
const res = await fetch("https://www2.deepl.com/jsonrpc", {
headers: {
"Content-Type": "application/json; charset=utf-8",
},
method: "POST",
body: buildBodyString(data),
});
if (res.ok) {
const result = (await res.json()) as {
jsonrpc: string;
id: number;
result: {
texts: {
text: string;
}[];
};
};
return {
id: result.id,
code: 200,
data: result?.result?.texts?.[0]?.text,
};
}
return {
id: 42,
code: res.status,
data:
res.status === 429
? "Too many requests, please try again later."
: "Unknown error.",
};
}
function buildRequestParams(sourceLang: string, targetLang: string) {
return {
jsonrpc: "2.0",
method: "LMT_handle_texts",
id: Math.floor(Math.random() * 100000 + 100000) * 1000,
params: {
texts: [{ text: "", requestAlternatives: 3 }],
timestamp: 0,
splitting: "newlines",
lang: {
source_lang_user_selected: sourceLang,
target_lang: targetLang,
},
},
};
}
function getCountOfI(translateText: string) {
return translateText.split("i").length - 1;
}
function getTimestamp(iCount: number) {
let ts = new Date().getTime();
if (iCount !== 0) {
iCount = iCount + 1;
return ts - (ts % iCount) + iCount;
} else {
return ts;
}
}
function buildBodyString(data: RequestParams) {
const post_data = buildRequestParams(
data.source_lang || "AUTO",
data.target_lang || "AUTO"
);
post_data.params.texts = [{ text: data.text, requestAlternatives: 3 }];
post_data.params.timestamp = getTimestamp(getCountOfI(data.text));
let post_str = JSON.stringify(post_data);
if (
[0, 3].includes((post_data["id"] + 5) % 29) ||
(post_data["id"] + 3) % 13 === 0
) {
post_str = post_str.replace('"method":"', '"method" : "');
} else {
post_str = post_str.replace('"method":"', '"method": "');
}
return post_str;
}
export const config = {
runtime: "edge",
};
export default async function MyEdgeFunction(request: NextRequest) {
const req = (await request.json()) as RequestParams;
const res = await queryAPI(req);
return NextResponse.json(res);
}
After debugging locally and confirming normal operation, I encountered a persistent 525 error after deploying to the edge function. Can you advise me on how to solve this problem?
curl --location 'https://deeplx-edge-api.vercel.app/api/translate' \
--header 'Content-Type: application/json' \
--data '{
"text": "你好呀,请问你来自哪里",
"source_lang": "auto",
"target_lang": "en"
}'
{"id":42,"code":525,"data":"Unknown error."}
curl --location '127.0.0.1:3000/api/translate' \
--header 'Content-Type: application/json' \
--data '{
"text": "请给我一个冰淇淋",
"source_lang": "zh",
"target_lang": "en"
}'
{"id":115601000,"code":200,"data":"Please give me an ice cream"}
我看到这个网站 www.xxxx.com 在卖,试了下是一家吗?
选择的是那个CF wokers部署,查询余额的curl执行后提示这个
{
"error": {
"message": "Your request to GET /dashboard/billing/credit_grants must be made with a session key (that is, it can only be made from the browser). You made it with the following key type: secret.",
"type": "server_error",
"param": null,
"code": null
}
}
请问这个该如何解决?
技术小白完全不懂复杂的技术,盼望解答。谢谢
我预计是 OpenAI 的服务在您所在的国家/地区不可用的 的原因, 有大佬知道如何解决吗
办公环境本地电脑完全没有互联网权限;
本人已经在vercel里部署了本项目代理,并绑定了域名,正常使用。
因内部电脑无法访问互联网,我想请教一下,能否在可访问互联网的服务器上,搭建一个代理,并将openai的相关域名指向vercel部署的代理上,以此解决电脑能正常使用api接口?
相当于使用了2个api代理。
如果不可以,是否有其他建议?谢谢!
使用CloudFlare自带的域名 可以正常访问 使用自定义域的域名在没挂梯子时会报错,挂了梯子正常
报错信息如下
openai.error.APIConnectionError: Error communicating with OpenAI: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接
。', None, 10054, None))
麻烦作者看一下,盼复
请教一下, 如在已经部署好的静态网站上, 查询sk使用量的界面 输入API的sk查询后, 提示框弹出需要查询的登陆用户名和密码? 请问这个在哪里设置的, 默认是什么, 谢谢!
https://xxxx.pages.dev/
{
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
使用的是
export default {
async fetch(request, env) {
const url = new URL(request.url);
url.host = "api.openai.com";
// openai is already set all CORS heasders
return fetch(url, {
headers: request.headers,
method: request.method,
body: request.body,
redirect: 'follow'
});
}
}
这个代码部署在workers
第2种方法,然后用python按照用法里面的python运行,出现错误,使用的是自己的key。
用的系统分配的默认域名,国内还是无法访问
https://polished-fire-c950.chat-john.workers.dev/v1/chat/completions
你好,请问最后绑定的自己的域名需要时国外注册的吗?
还是说国内注册的就可以,如阿里云腾讯云注册并备案的域名
gpt-4慢一点,访问经常返回524错误,是cloudflare的超时,
cloudflare一般用户只能到100秒->https://developers.cloudflare.com/support/troubleshooting/cloudflare-errors/troubleshooting-cloudflare-5xx-errors/#error-524-a-timeout-occurred
有精通的大佬吗,怎么把timeout设置长一点
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.