Use cloudflare to build OpenAI interface proxy for free

Due to the official ban of GFW and OpenAI, our domestic server cannot access the official interface of OpenAI

Before, I always used a third-party proxy domain name found on the Internet, but I was worried that using other people's proxy would be unsafe and unstable

Now, we can use cloudflare to build an OpenAI proxy service ourselves, using our own forwarding proxy

Step 1: Register cloudflare account

Go to the official website to register an account  Cloudflare China Official Website | Intelligent Cloud Service Platform | Free CDN Security Protection | Cloudflare (cloudflare-cn.com)

Step 2: Create a worker for request transfer

You can choose a name at will, click Quick Edit to enter the code editing interface

The js code is

export default {
  async fetch(request) {
    const url = new URL(request.url);
    url.host = 'api.openai.com';
    return fetch(url, { headers: request.headers, method: request.method, body: request.body });
  },
};

So far, the proxy transfer service has been set up, but we still cannot use it in China, we need to configure an accessible domain name for domain name forwarding

Step 3: Domain Forwarding

In the left menu bar ==>Website===>Add site, add our own domain name, here needs to be the root domain name

 After configuration, click on the domain name, you will be prompted to modify the DNS server of the domain name

We need to find dns management in our own domain name control panel, and modify the dns server address

For example, my domain name is on Alibaba Cloud, we can change it to the DNS address required by cloudflare in the following

Wait for cloudflare to verify that the DNS configuration is successful, and a notification email will be sent to your mailbox

Go back to the Worker configuration, add a domain name forwarding, I added a custom domain name of my own

After the above operations are completed, you can configure and use your own proxy domain name when using the official OpenAI interface

Guess you like

Origin blog.csdn.net/taoshihan/article/details/131800141