1

Having a vector embedding like [1,2,3,4], I can create a blob byte representation with Python using the NumPy library using something like this:

np.array([1,2,3,4]).astype(dtype=np.float32).tobytes()

This will create an output that looks like this:

\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'

How do I create such a blob string with JavaScript?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44

1 Answers1

1

From the node-redis client https://github.com/redis/node-redis/blob/master/examples/search-knn.js

function float32Buffer(arr) {
  return Buffer.from(new Float32Array(arr).buffer);
}

// Add some sample data...
// https://redis.io/commands/hset/
await Promise.all([
  client.hSet('noderedis:knn:a', { v: float32Buffer([0.1, 0.1]) }),
  client.hSet('noderedis:knn:b', { v: float32Buffer([0.1, 0.2]) }),
  client.hSet('noderedis:knn:c', { v: float32Buffer([0.1, 0.3]) }),
  client.hSet('noderedis:knn:d', { v: float32Buffer([0.1, 0.4]) }),
]);
// Perform a K-Nearest Neighbors vector similarity search
// Documentation: https://redis.io/docs/stack/search/reference/vectors/#pure-knn-queries
const results = await client.ft.search('idx:knn-example', '*=>[KNN 4 @v $BLOB AS dist]', {
  PARAMS: {
    BLOB: float32Buffer([0.1, 0.1])
  },
  SORTBY: 'dist',
  DIALECT: 2,
  RETURN: ['dist']
});
console.log(JSON.stringify(results, null, 2));
A. Guy
  • 500
  • 1
  • 3
  • 10