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
- Log in to your Cloudflare Dashboard and navigate to the Workers section.
- Click on the "Create a Service" button.
- Input the Service name.
- Keep the "Select a starter" as "Default handler".
- 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
- Click "Triggers" on the tab bar.
- Click "Add Custom Domain" button.
- 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. - 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
- Click "Settings" on the tab bar.
- Click "Variables" from the right part.
- In "Environment Variables", Click "Add Variable" button.
- 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.
- Key:
5. Configure the newly created service - KV Storage
- Expand "Workers" in right sidebar.
- Click "KV".
- In "Workers KV", Click "Create a namespace" button.
- Input new name for the namespace, such as
namespace_gpt
. - Click "Add" button.
- Go back to the detail page of the new created service.
- Go to step 3 of above section, enter "Environment Variables" and scroll down the page.
- In "KV Namespace Bindings" section, Click "Add binding" button.
- Input
KV
(UPPERCASE) in the left, and choose new KV namespace created in step 4. - Click "Save and deploy" button.
6. Configure the newly created service - Code
- Open the raw code of worker.js.
- Copy all of the code.
- Go back to the detail page of the new created service.
- Click "Quick edit" button at the top right.
- Replace all code with content of pasteboard.
- 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 ofhttps://api.openai.com/v1/chat/completions
- The HTTP Header
Authorization
and its value should beBearer sk-cfw****
, which is generated by above command.
Have fun!
LICENSE
This project uses the MIT license. Please see LICENSE for more information.