Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Chloe Yip <[email protected]>
  • Loading branch information
cyip10 authored Nov 27, 2024
1 parent e7a974e commit 370e2f3
Showing 1 changed file with 102 additions and 14 deletions.
116 changes: 102 additions & 14 deletions examples/node/vector_search_example.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
import { Decoder, GlideClient, GlideClusterClient, GlideFt, FtSearchOptions,
FtSearchReturnType, Logger, VectorField} from "@valkey/valkey-glide";

import { v4 as uuidv4 } from "uuid";

import { GlideClient, GlideClusterClient, GlideFt, Logger, VectorField} from "@valkey/valkey-glide";

// TODO: need to test against MemoryDB instance
const DATA_PROCESSING_TIMEOUT = 1000;

async function executeVssCommands() {
// When Valkey is in standalone mode, add address of the primary node, and any replicas you'd like to be able to read from.
// JSON modules and VSS modules must be enabled for this to successfully execute, otherwise there will be request errors.
const addresses = [
{
host: "localhost",
port: 6379,
port: 6380,
},
];
// Check `GlideClientConfiguration/GlideClusterClientConfiguration` for additional options.
Expand All @@ -28,28 +29,115 @@ async function executeVssCommands() {
}

async function vssCreateAndSearch(client: GlideClient | GlideClusterClient) {
const vectorField_1: VectorField = {
const prefix = "{" + uuidv4() + "}:";
const index = prefix + "index";
const query = "*=>[KNN 2 @VEC $query_vec]";

const vectorField_1: VectorField = {
type: "VECTOR",
name: "vec",
alias: "VEC",
attributes: {
algorithm: "HNSW",
type: "FLOAT32",
dimensions: 2,
distanceMetric: "L2",
dimensions: 2,
},
};
const result = await GlideFt.create(client, "index", [vectorField_1]);
console.log(result); // 'OK' - Indicates successful creation of the index.

const result2 = await GlideFt.search(client, "index", "*=>[KNN 2 @VEC $query_vec]");
console.log(result2); // TODO: response
const createResult = await GlideFt.create(client, index, [vectorField_1], {
dataType: "HASH",
prefixes: [prefix],
});
console.log(createResult); // 'OK' - Indicates successful creation of the index.

// Binary buffers are used here for memory management
const binaryValue1 = Buffer.alloc(8);
expect(
await client.hset(Buffer.from(prefix + "0"), [
// value of <Buffer 00 00 00 00 00 00 00 00 00>
{ field: "vec", value: binaryValue1 },
]),
).toEqual(1);

const binaryValue2: Buffer = Buffer.alloc(8);
binaryValue2[6] = 0x80;
binaryValue2[7] = 0xbf;
expect(
await client.hset(Buffer.from(prefix + "1"), [
// value of <Buffer 00 00 00 00 00 00 00 80 BF>
{ field: "vec", value: binaryValue2 },
]),
).toEqual(1);

expect(result).toBe("OK");
// expect(result2).toBe(""); // TODO: response
// let server digest the data and update index
const sleep = new Promise((resolve) =>
setTimeout(resolve, DATA_PROCESSING_TIMEOUT),
);
await sleep;

// Additional search options
const optionsWithCount: FtSearchOptions = {
params: [{ key: "query_vec", value: binaryValue1 }],
timeout: 10000,
count: true,
};
const binaryResultCount: FtSearchReturnType =
await GlideFt.search(client, index, query, {
decoder: Decoder.Bytes,
...optionsWithCount,
});
expect(binaryResultCount).toEqual([2]);

const options: FtSearchOptions = {
params: [{ key: "query_vec", value: binaryValue1 }],
timeout: 10000,
};
const searchResult: FtSearchReturnType = await GlideFt.search(
client,
index,
query,
{
decoder: Decoder.Bytes,
...options,
},
);

const expectedBinaryResult: FtSearchReturnType = [
2,
[
{
key: Buffer.from(prefix + "1"),
value: [
{
key: Buffer.from("vec"),
value: binaryValue2,
},
{
key: Buffer.from("__VEC_score"),
value: Buffer.from("1"),
},
],
},
{
key: Buffer.from(prefix + "0"),
value: [
{
key: Buffer.from("vec"),
value: binaryValue1,
},
{
key: Buffer.from("__VEC_score"),
value: Buffer.from("0"),
},
],
},
],
];
expect(createResult).toBe("OK");
expect(searchResult).toEqual(expectedBinaryResult);

console.log("All examples succeeded.");
}
};

function setFileLogger() {
Logger.setLoggerConfig("warn", "glide.log");
Expand Down

0 comments on commit 370e2f3

Please sign in to comment.