教你实现在国内正常访问chatgpt接口

教你实现在国内正常访问chatgpt接口,chatgpt将人工智能带进了一个新的时期,上百亿的参数模型训练出来的人工智能模型就是不一样,不仅能写作、分析、对话、写代码、写诗,好像没有他不行的,但是chatgpt屏蔽了国内的ip地址,所以无法访问,api接口也无法访问,那么我们通过其他的办法来访问,国内有很多镜像套壳网站,具体可以看这个链接https://ask.bfw.wiki/question/16781815042207030064.html

教你实现在国内正常访问chatgpt接口

那么如何向自己正常使用chatgpt,我们还可以通过cloudflare来实现中转,方法如下:

一、注册域名

打开这个网站注册域名:https://www.godaddy.com/

curl 教你实现在国内正常访问chatgpt接口

看这个域名非常便宜,最便宜的一年才1美分,随便注册一个之后,将域名的dns服务地址要改成cloudflare的,就这两个:

javier.ns.cloudflare.comlove.ns.cloudflare.com

二、cloudflare代理

注册录https://dash.cloudflare.com/

在控制面板下 点击worker,然后点击create service

教你实现在国内正常访问chatgpt接口

随便取一个名字

教你实现在国内正常访问chatgpt接口

创建后点击quick edit

教你实现在国内正常访问chatgpt接口

左边代码栏中输入下面的请求代理代码,这段代码的意思就是将cloudflare作为一个代理跳板请求openai接口,输入完代码后点击下面的save and deploy进行保存和部署。

教你实现在国内正常访问chatgpt接口

addEventListener('fetch', event => {
    event.respondWith(fetchAndApply(event.request));
})

async function fetchAndApply(request) {

    let response = null;
    let method = request.method;
    let request_headers = request.headers;



    let url = new URL(request.url);
    let url_hostname = url.hostname;
    url.protocol = 'https:';
    url.host = 'api.openai.com';


    
    let new_request_headers = new Headers(request_headers);
    new_request_headers.set('Host', url.host);
    new_request_headers.set('Referer', url.protocol + '//' + url_hostname);

   

    let original_response = await fetch(url.href, {
        method: method,
        headers: new_request_headers,
        body: request.body
    })
  

    let original_response_clone = original_response.clone();
    let original_text = null;
    let response_headers = original_response.headers;
    let new_response_headers = new Headers(response_headers);
    let status = original_response.status;

    new_response_headers.set('Cache-Control', 'no-store');
    new_response_headers.set('access-control-allow-origin', '*');
    new_response_headers.set('access-control-allow-credentials', true);
    new_response_headers.delete('content-security-policy');
    new_response_headers.delete('content-security-policy-report-only');
    new_response_headers.delete('clear-site-data');

    original_text = original_response_clone.body
    response = new Response(original_text, {
        status,
        headers: new_response_headers
    })

    return response

}

部署成功后还要添加一个域名,在worker下面的triggers点击add custom domino

教你实现在国内正常访问chatgpt接口

按照提示将自己域名的dns服务器改成cloudflare就可,域名不需要备案就行使用,非常方便。

一切都好了以后,就是改地址了

三、接口地址修改

这个时候就将openai的原域名地址更换成自己的域名

curl https://api.openai.com/v1/chat/completions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "What is the OpenAI mission?"}] }'

上面改成

curl https://自己的域名/v1/chat/completions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "What is the OpenAI mission?"}] }'还有whisper地址也是

curl https://自己的域名/v1/audio/transcriptions \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -H "Content-Type: multipart/form-data" \ -F model="whisper-1" \ -F file="@/path/to/file/openai.mp3

{{collectdata}}

网友评论1

  1. # 108
    还可以试试这个快捷指令:https://www.icloud.com/shortcuts/08b02bf88171454387dd4e44539d575b
    thinkfuture 2023-03-10回复