Giter VIP home page Giter VIP logo

cf-openai-proxy's Introduction

Cloudflare worker as a proxy to the OpenAI API

This Cloudflare worker serves as a proxy to the OpenAI API. I created it to address two issues:

  1. Some countries and territories are still not supported by the OpenAI API.
  2. There are many ChatBot clients that request the API key, even though OpenAI discourages sharing it.

By accessing the OpenAI API through the Cloudflare worker, clients from all countries can access the API. The access key mechanism also allows you to keep the real API key private.

Usage

To set up secrets for the Cloudflare worker, follow these steps:

  1. Run wrangler secret put OPENAI_API_KEY to set the OpenAI API key.
  2. Run wrangler secret put ACCESS_KEYS to set the access key.

The ACCESS_KEYS is a comma separated list of access keys. Each access key is a string of "sk-{user_name}-{random_string}", for example "sk-Tom-2Hf3aTUVlG". You could run node src/key.mjs user_name to generate the access keys.

❯ node src/key.mjs Tom
sk-Tom-wMtF9kkGDu

Deploy to Cloudflare

Run wrangler deploy to deploy the worker to Cloudflare.

Client Setting

Now you can use the URL of this worker as the base URL of the OpenAI API, and the access key as the OpenAI API key to access the OpenAI API.

import openai

openai.api_key = "sk-Tom-wMtF9kkGDu"
openai.api_base = "https://openai-proxy.yourname-8235.workers.dev"

# create a chat completion
chat_completion = openai.ChatCompletion.create(
    model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello world"}]
)

# print the chat completion
print(chat_completion.choices[0].message.content)

cf-openai-proxy's People

Contributors

flyisland avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.