Skip to main content

How to build a ChatGPT API proxy?

Background

ChatGPT is a very rare "two-way blockade" super application recently, which means it does not provide services to Chinese users itself, and if you want to use it, you have to deal with overseas credit cards, overseas phone numbers, overseas IPs. And it was almost put on the "Great Firewall (GFW)" blacklist as soon as it was released. Using the web version is very inconvenient.

However, ChatGPT also provides API services, which means we can call its services through the API. As long as you get an API key, everything will be fine and you can use it. And for you gentlemen who understand it, it can be wrapped based on the API and used as your own product.

Although you only need to get an API key to call the GPT service, due to network influence, its API service in China is very unstable, and sometimes it may not be callable at all. All things can find proxies and act as intermediaries. API is no exception. However, after searching around, suitable tutorials are not many, and the only relatively good one is in a Github project, so I came up with the idea of writing a more friendly tutorial.

Original article: Github.

Proxy principle

The method mentioned in this article is mainly to host the domain on Cloudflare first, and then use Cloudflare's Workers service for relay. The advantages are:

  • Cloudflare Workers is essentially a backend function service that does not require purchasing servers to accept and process requests
  • Cloudflare has very many nodes, and requests can get good network experience anywhere in the world
  • Cloudflare Workers service can be called for free up to ten thousand times a day, which is enough
  • Cloudflare Workers service supports custom domains, which can make your API proxy address look more professional (website routing)

Prerequisites

  • Your own domain
  • Cloudflare account
  • A bit of patience and hands-on ability

1. Host the domain on Cloudflare

Click [Cloudflare], register an account, and host your domain on Cloudflare. This step is relatively simple, just follow Cloudflare's page prompts.

2. Create Workers

The following refers to Cloudflare as CF.

Go back to the CF homepage and click "Create Worker". CF inexplicably does not provide custom options and must select its template, so you can directly select the Hello Word template first and then edit the code inside.

Go in and directly click the "Deploy" button.

Modify the Workers code

After creating, you need to go in and modify the code, modifying this part of the code:

Modify it to the following code:

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);
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', '*');
return modifiedResponse;
}

After modifying, do not hurry to send requests, directly click the "Save and Deploy" button in the upper right corner. And rename this Workers at the same time.

3. Bind domain

After creating the Workers, go to the domain you just added. Click on the "Workers Routing" in the left sidebar menu, then click "Add Route".

Now think of a name for your proxy link, for example, I want to use gptapi.wsyfin.com as the URL of my proxy, so I input gptapi.wsyfin.com/* in the routing, select the Workers just created, and click "Save".

Finally, add this address in DNS, type A, content write as 2.2.2.2, open the little cloud button, click save to complete.

Verify request success

Now you can use your domain name to request the GPT API, such as using https://gptapi.wsyfin.com/v1/chat/completions to request the GPT API. If it returns the following content, then your proxy is successfully built.

Well, this is an error message! But this error indicates that it can correctly connect to the official GPT API service address, but it still needs an API key to let GPT answer our questions. Now enter the following command in the terminal, replacing the Bearer after sk-xxxx with your own API key, and you can get the correct return result.

curl --location 'https://gptapi.wsyfin.com/v1/chat/completions' \
--header 'Authorization: Bearer sk-********' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "鲁迅为什么暴打周树人?"}]
}'

Let's enjoy how GPT3.5 fooled us: