Skip to content

janlay/openai-cloudflare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

openai-cloudflare

An OpenAI API proxy running with Cloudflare worker.

Features

  • Support all APIs provided by OpenAI
  • Fully compliant with the format requirements of OpenAI's API payload and key
  • Works with mainstream OpenAI/ChatGPT GUI apps
  • Streaming content
  • Uniq key for users
  • Create / delete users
  • Reset user's key
  • User can reset the key independently
  • Time-limited
  • Stats for usage

Setup

1. Prepare your domain name

  • Please make sure that the nameservers of your domain is set to the nameservers provided by Cloudflare first. Manual

2. Create a new service

  1. Log in to your Cloudflare Dashboard and navigate to the Workers section.
  2. Click on the "Create a Service" button.
  3. Input the Service name.
  4. Keep the "Select a starter" as "Default handler".
  5. Click on the "Create" button

Now you have the new service created and it shows you the detail of the service.

3. Configure the newly created service - Trigger

  1. Click "Triggers" on the tab bar.
  2. Click "Add Custom Domain" button.
  3. Input the domain you want to use, such as gpt.mydomainname.com. Don't worry, Cloudflare can automatically configure the proper DNS settings for this.
  4. Click "Add Custom Domain" button to finish the Triggers setting.

Don't leave the detail page and go on.

4. Configure the newly created service - Environment Variables

  1. Click "Settings" on the tab bar.
  2. Click "Variables" from the right part.
  3. In "Environment Variables", Click "Add Variable" button.
  4. Input two important items. Enable "Encrypt" because they are sensitive.
    • Key: OPENAPI_API_KEY, value is your own OpenAPI key.
    • Key ACCESS_TOKEN, value is any random string like a password. Again, both of these pieces of information are very sensitive, so it is strongly recommended to turn on the "Encrypt" option. This way, after you save them, no one will be able to see their values again.

5. Configure the newly created service - KV Storage

  1. Expand "Workers" in right sidebar.
  2. Click "KV".
  3. In "Workers KV", Click "Create a namespace" button.
  4. Input new name for the namespace, such as namespace_gpt.
  5. Click "Add" button.
  6. Go back to the detail page of the new created service.
  7. Go to step 3 of above section, enter "Environment Variables" and scroll down the page.
  8. In "KV Namespace Bindings" section, Click "Add binding" button.
  9. Input KV (UPPERCASE) in the left, and choose new KV namespace created in step 4.
  10. Click "Save and deploy" button.

6. Configure the newly created service - Code

  1. Open the raw code of worker.js.
  2. Copy all of the code.
  3. Go back to the detail page of the new created service.
  4. Click "Quick edit" button at the top right.
  5. Replace all code with content of pasteboard.
  6. Click "Save and deploy".

Around one minute later, the new serivce should serve.

Manage

Here assume your domain name is gpt.mydomainname.com and the Admin's password (ACCESS_TOKEN) is Une9f2ijwe

Task command
Create new user with name janlay curl https://gpt.mydomainname.com/Une9f2ijwe/register/janlay
Reset user janlay's key curl https://gpt.mydomainname.com/Une9f2ijwe/reset/janlay

Both of these commands output the user's Key. Please be aware that this key always starts with sk-cfw and may look like a valid OpenAI Key, but it can only be used for this service.

If you want to delete a user, try this:

curl -X DELETE https://gpt.mydomainname.com/Une9f2ijwe/janlay

It's ok if you see "OK".

Use the service

Here assume your domain name is gpt.mydomainname.com.

Users can invoke a standard OpenAI API request guided by its document. The only changes would be:

  • The target URL is https://gpt.mydomainname.com/v1/chat/completions instead of https://api.openai.com/v1/chat/completions
  • The HTTP Header Authorization and its value should be Bearer sk-cfw****, which is generated by above command.

Have fun!

LICENSE

This project uses the MIT license. Please see LICENSE for more information.

About

An OpenAI API proxy running with Cloudflare worker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published