Skip to content

Commit a356e68

Browse files
committed
Improve quantizer typings
1 parent 80294c0 commit a356e68

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

src/collections/config/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
CollectionConfig,
1717
CollectionConfigUpdate,
1818
PQConfig,
19+
QuantizerConfig,
20+
SQConfig,
1921
VectorIndexConfig,
2022
VectorIndexConfigDynamic,
2123
VectorIndexConfigFlat,
@@ -163,12 +165,15 @@ export class VectorIndex {
163165
}
164166

165167
export class Quantizer {
166-
static isPQ(config?: PQConfig | BQConfig): config is PQConfig {
168+
static isPQ(config?: QuantizerConfig): config is PQConfig {
167169
return config?.type === 'pq';
168170
}
169-
static isBQ(config?: PQConfig | BQConfig): config is BQConfig {
171+
static isBQ(config?: QuantizerConfig): config is BQConfig {
170172
return config?.type === 'bq';
171173
}
174+
static isSQ(config?: QuantizerConfig): config is SQConfig {
175+
return config?.type === 'sq';
176+
}
172177
}
173178

174179
export const configGuards = {

src/collections/config/types/vectorIndex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ export type PQEncoderDistribution = 'log-normal' | 'normal';
7373
export type VectorIndexType = 'hnsw' | 'flat' | 'dynamic' | string;
7474

7575
export type VectorIndexConfig = VectorIndexConfigHNSW | VectorIndexConfigFlat | VectorIndexConfigDynamic;
76+
77+
export type QuantizerConfig = PQConfig | BQConfig | SQConfig;

src/collections/configure/parsing.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,3 @@ export class QuantizerGuards {
3939
export function parseWithDefault<D>(value: D | undefined, defaultValue: D): D {
4040
return value !== undefined ? value : defaultValue;
4141
}
42-
43-
export const parseQuantizer = <T extends QuantizerConfig>(config?: T): T | undefined => {
44-
if (config === undefined) {
45-
return undefined;
46-
}
47-
if (QuantizerGuards.isPQCreate(config)) {
48-
return {
49-
...config,
50-
type: 'pq',
51-
} as T;
52-
} else if (QuantizerGuards.isBQCreate(config)) {
53-
return {
54-
...config,
55-
type: 'bq',
56-
} as T;
57-
}
58-
return config;
59-
};

src/collections/configure/vectorIndex.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import {
1616
VectorIndexConfigHNSWUpdate,
1717
} from './types/index.js';
1818

19-
import { parseQuantizer } from './parsing.js';
20-
2119
const isModuleConfig = <N, C>(config: ModuleConfig<N, C> | C): config is ModuleConfig<N, C> => {
2220
return config && typeof config === 'object' && 'name' in config && 'config' in config;
2321
};
@@ -40,7 +38,7 @@ const configure = {
4038
config: {
4139
distance,
4240
vectorCacheMaxObjects,
43-
quantizer: parseQuantizer(quantizer),
41+
quantizer: quantizer,
4442
},
4543
};
4644
},
@@ -62,7 +60,7 @@ const configure = {
6260
? {
6361
...rest,
6462
distance: distanceMetric,
65-
quantizer: parseQuantizer(rest.quantizer),
63+
quantizer: rest.quantizer,
6664
}
6765
: undefined,
6866
};
@@ -177,10 +175,7 @@ const reconfigure = {
177175
}): ModuleConfig<'flat', VectorIndexConfigFlatUpdate> => {
178176
return {
179177
name: 'flat',
180-
config: {
181-
vectorCacheMaxObjects: options.vectorCacheMaxObjects,
182-
quantizer: parseQuantizer(options.quantizer),
183-
},
178+
config: options,
184179
};
185180
},
186181
/**
@@ -221,8 +216,8 @@ const reconfigure = {
221216
* NOTE: If the vector index already has a quantizer configured, you cannot change its quantizer type; only its values.
222217
* So if you want to change the quantizer type, you must recreate the collection.
223218
*
224-
* @param {boolean} [options.cache] Whether to cache the quantizer. Default is false.
225-
* @param {number} [options.rescoreLimit] The rescore limit. Default is 1000.
219+
* @param {boolean} [options.cache] Whether to cache the quantizer.
220+
* @param {number} [options.rescoreLimit] The new rescore limit.
226221
* @returns {BQConfigCreate} The configuration object.
227222
*/
228223
bq: (options?: { cache?: boolean; rescoreLimit?: number }): BQConfigUpdate => {
@@ -237,11 +232,11 @@ const reconfigure = {
237232
* NOTE: If the vector index already has a quantizer configured, you cannot change its quantizer type; only its values.
238233
* So if you want to change the quantizer type, you must recreate the collection.
239234
*
240-
* @param {number} [options.centroids] The number of centroids. Default is 256.
241-
* @param {PQEncoderDistribution} [options.pqEncoderDistribution] The encoder distribution. Default is 'log-normal'.
242-
* @param {PQEncoderType} [options.pqEncoderType] The encoder type. Default is 'kmeans'.
243-
* @param {number} [options.segments] The number of segments. Default is 0.
244-
* @param {number} [options.trainingLimit] The training limit. Default is 100000.
235+
* @param {number} [options.centroids] The new number of centroids.
236+
* @param {PQEncoderDistribution} [options.pqEncoderDistribution] The new encoder distribution.
237+
* @param {PQEncoderType} [options.pqEncoderType] The new encoder type.
238+
* @param {number} [options.segments] The new number of segments.
239+
* @param {number} [options.trainingLimit] The new training limit.
245240
* @returns {PQConfigUpdate} The configuration object.
246241
*/
247242
pq: (options?: {
@@ -270,8 +265,8 @@ const reconfigure = {
270265
* NOTE: If the vector index already has a quantizer configured, you cannot change its quantizer type; only its values.
271266
* So if you want to change the quantizer type, you must recreate the collection.
272267
*
273-
* @param {number} [options.rescoreLimit] The rescore limit. Default is 1000.
274-
* @param {number} [options.trainingLimit] The training limit. Default is 100000.
268+
* @param {number} [options.rescoreLimit] The rescore limit.
269+
* @param {number} [options.trainingLimit] The training limit.
275270
* @returns {SQConfigUpdate} The configuration object.
276271
*/
277272
sq: (options?: { rescoreLimit?: number; trainingLimit?: number }): SQConfigUpdate => {

0 commit comments

Comments
 (0)