Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
callowaysutton opened this issue May 10, 2023 · 1 comment
Open

Comments

@callowaysutton
Copy link

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
Copy link
Owner

6 commented May 18, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants