Skip to content

Commit f2ddb1d

Browse files
committed
improve the vadd api
1 parent 339065f commit f2ddb1d

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

packages/client/lib/commands/VADD.spec.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,43 @@ import { parseArgs } from './generic-transformers';
55

66
describe('VADD', () => {
77
describe('transformArguments', () => {
8-
it('with VALUES', () => {
8+
it('basic usage', () => {
99
assert.deepEqual(
1010
parseArgs(VADD, 'key', [1.0, 2.0, 3.0], 'element'),
1111
['VADD', 'key', 'VALUES', '3', '1', '2', '3', 'element']
1212
);
1313
});
1414

15-
it('with FP32', () => {
15+
it('with REDUCE option', () => {
1616
assert.deepEqual(
17-
parseArgs(VADD, 'key', Buffer.from([0x3f, 0x80, 0x00, 0x00]), 'element'),
18-
['VADD', 'key', 'FP32', Buffer.from([0x3f, 0x80, 0x00, 0x00]), 'element']
17+
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { REDUCE: 50 }),
18+
['VADD', 'key', 'REDUCE', '50', 'VALUES', '2', '1', '2', 'element']
1919
);
2020
});
2121

22-
it('with options', () => {
22+
it('with quantization options', () => {
23+
assert.deepEqual(
24+
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'Q8' }),
25+
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'Q8']
26+
);
27+
28+
assert.deepEqual(
29+
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'BIN' }),
30+
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'BIN']
31+
);
32+
33+
assert.deepEqual(
34+
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'NOQUANT' }),
35+
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'NOQUANT']
36+
);
37+
});
38+
39+
it('with all options', () => {
2340
assert.deepEqual(
2441
parseArgs(VADD, 'key', [1.0, 2.0], 'element', {
2542
REDUCE: 50,
2643
CAS: true,
27-
Q8: true,
44+
QUANT: 'Q8',
2845
EF: 200,
2946
SETATTR: { name: 'test', value: 42 },
3047
M: 16

packages/client/lib/commands/VADD.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import { transformDoubleArgument } from './generic-transformers';
55
export interface VAddOptions {
66
REDUCE?: number;
77
CAS?: boolean;
8-
NOQUANT?: boolean;
9-
Q8?: boolean;
10-
BIN?: boolean;
8+
QUANT?: 'NOQUANT' | 'BIN' | 'Q8',
119
EF?: number;
1210
SETATTR?: Record<string, any>;
1311
M?: number;
@@ -19,15 +17,15 @@ export default {
1917
*
2018
* @param parser - The command parser
2119
* @param key - The name of the key that will hold the vector set data
22-
* @param vector - The vector data as FP32 blob or array of numbers
20+
* @param vector - The vector data as array of numbers
2321
* @param element - The name of the element being added to the vector set
2422
* @param options - Optional parameters for vector addition
2523
* @see https://redis.io/commands/vadd/
2624
*/
2725
parseCommand(
2826
parser: CommandParser,
2927
key: RedisArgument,
30-
vector: RedisArgument | Array<number>,
28+
vector: Array<number>,
3129
element: RedisArgument,
3230
options?: VAddOptions
3331
) {
@@ -38,13 +36,9 @@ export default {
3836
parser.push('REDUCE', options.REDUCE.toString());
3937
}
4038

41-
if (Array.isArray(vector)) {
42-
parser.push('VALUES', vector.length.toString());
43-
for (const value of vector) {
44-
parser.push(transformDoubleArgument(value));
45-
}
46-
} else {
47-
parser.push('FP32', vector);
39+
parser.push('VALUES', vector.length.toString());
40+
for (const value of vector) {
41+
parser.push(transformDoubleArgument(value));
4842
}
4943

5044
parser.push(element);
@@ -53,13 +47,7 @@ export default {
5347
parser.push('CAS');
5448
}
5549

56-
if (options?.NOQUANT) {
57-
parser.push('NOQUANT');
58-
} else if (options?.Q8) {
59-
parser.push('Q8');
60-
} else if (options?.BIN) {
61-
parser.push('BIN');
62-
}
50+
options?.QUANT && parser.push(options.QUANT);
6351

6452
if (options?.EF !== undefined) {
6553
parser.push('EF', options.EF.toString());

0 commit comments

Comments
 (0)