Giter VIP home page Giter VIP logo

openai-caching-proxy-worker's Introduction

openai-caching-proxy-worker

Basic caching proxy for OpenAI API, deployable as a Cloudflare Worker.

This can help you reduce your OpenAI costs (and get faster results) by returning cached responses for repeated requests.

The proxy server supports specifying cache TTL on a per-request basis, so you could configure this based on your needs. For example, the text-davinci-003 model is 10x the cost of text-curie-001 so you could choose to cache results for longer for davinci.

Client compatibility:

  • openai/openai-node: full compatibility, takes just a few lines of config to use
  • openai/openai-python: partial compatibility, supports caching but no TTL options so you'll need a cache eviction policy
  • alexrudall/ruby-openai: partial compatibility, supports caching but no TTL options so you'll need a cache eviction policy

It only caches POST requests that have a JSON request body, as these tend to be the slowest and are the only ones that cost money (for now).

Setup

Clone the repo and install dependencies.

You will need to sign up for two services (which both have free tiers):

  • Cloudflare: Where our worker will be hosted.
  • Upstash: We use Upstash's redis-over-HTTP service for storing cached OpenAI responses.

Finally, set up your redis secrets based on instructions in wrangler.toml.

Depending on your usage, you may try replacing Redis with Cloudflare KV instead which is eventually consistent but will likely provide better read latency. Check wrangler.toml for setup instructions.

Usage

Start the proxy server at http://localhost:8787 with:

yarn start

Then, in your separate project where you have your openai/openai-node configuration, pass in the new basePath so that it sends requests through your proxy rather than directly to OpenAI:

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
+ // Point this to your local instance or Cloudflare deployment:
+ basePath: 'http://localhost:8787/proxy',
});
const openai = new OpenAIApi(configuration);

You can then try a few sample requests. The first will be proxied to OpenAI since a cached response isn't yet saved for it, but the second repeated/duplicate request will return the cached result instead.

const options = { model: 'text-ada-001', prompt: 'write a poem about computers' };

// This first request will be proxied as-is to OpenAI API, since a cached
// response does not yet exist for it:
const completion = await openai.createCompletion(options);
console.log('completion:', completion);

// This second request uses the same options, so it returns nearly instantly from
// local cache and does not make a request to OpenAI:
const completionCached = await openai.createCompletion(options);
console.log('completionCached:', completionCached);

Specifying a cache TTL

If you don't want to indefinitely cache results, or you don't have an eviction policy set up on your redis instance, you can specify a TTL in seconds using the X-Proxy-TTL header.

const configuration = new Configuration({
  ...
+ baseOptions: {
+   // In this example, we specify a cache TTL of 24 hours before it expires:
+   headers: { 'X-Proxy-TTL': 60 * 60 * 24 }
+ }
});

Refreshing the cache

If you need to force refresh the cache, you can use the header X-Proxy-Refresh. This will fetch a new response from OpenAI and cache this new response.

const configuration = new Configuration({
  ...
+ baseOptions: {
+   headers: { 'X-Proxy-Refresh': 'true' }
+ }
});

Example client usage

See /examples/ directory for a full example of how to call this proxy with your openai client.

This includes both Node.js, Python and Ruby client usage examples.

openai-caching-proxy-worker's People

Contributors

6 avatar dlackty avatar ebar0n avatar uyriq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

openai-caching-proxy-worker's Issues

Hashkey Returns the Same Hash for Object with Child Objects

The new OpenAI APIs require a new "messages" field in the body which contain an array of objects, however the current implementation produces the same hash key for the same amount of fields but with different values.

For example:

{
...
messages: [{"role": "user", "content": "message1"}]
}

Will produce the same hash as

{
...
messages: [{"role": "user", "content": "message2"}]
}

The following code should fix this issue but you may have to modify it to fit the environment. It goes through and recursively sorts any other objects found in the original object.

const sortJSON = function (json: any): any {
  if (Array.isArray(json)) {
    return json.map(sortJSON);
  } else if (typeof json === 'object' && json !== null) {
    json = Object.fromEntries(
      Object.entries(json)
        .filter(([_, value]) => value !== undefined && value !== null && value !== '')
    );
    return Object.keys(json)
      .sort()
      .reduce((acc, key) => {
        const value = json[key];
        if (value === undefined) {
          return acc;
        }
        return {
          ...acc,
          [key]: sortJSON(value),
        };
      }, {});
  } else {
    return json;
  }
}

export const getCacheKey = async function (props: any): Promise<string> {
  const propsWithoutUndefined = sortJSON(props)
  const hash = objectHash(propsWithoutUndefined);
  return hash;
}

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.