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.
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;
}
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:
Will produce the same hash as
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.