6 / openai-caching-proxy-worker

caching proxy for OpenAI API, deployable as a Cloudflare Worker
79 stars 10 forks source link

Hashkey Returns the Same Hash for Object with Child Objects #7

Open callowaysutton opened 1 year ago

callowaysutton commented 1 year ago

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;
}
6 commented 1 year ago

Good catch, and thank you for the suggested fix! Let me add a test case for this and see if I can get it fixed shortly