Giter VIP home page Giter VIP logo

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.