Skip to content

Commit 91a3f1a

Browse files
authored
Merge pull request #227 from weaviate/modules/add-support-for-multi2vec-cohere
Add `configure.multi2VecCohere` factory method and types for the module
2 parents d1c02be + f2a9d47 commit 91a3f1a

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

src/collections/config/types/vectorizer.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Text2VecPalmVectorizer = 'text2vec-palm';
2020
export type Vectorizer =
2121
| 'img2vec-neural'
2222
| 'multi2vec-clip'
23+
| 'multi2vec-cohere'
2324
| 'multi2vec-bind'
2425
| Multi2VecPalmVectorizer
2526
| 'multi2vec-google'
@@ -81,6 +82,33 @@ export type Multi2VecClipConfig = {
8182
};
8283
};
8384

85+
/**
86+
* The configuration for multi-media vectorization using the Cohere module.
87+
*
88+
* See the [documentation](https://weaviate.io/developers/weaviate/model-providers/cohere/embeddings-multimodal) for detailed usage.
89+
*/
90+
export type Multi2VecCohereConfig = {
91+
/** The base URL to use where API requests should go. */
92+
baseURL?: string;
93+
/** The image fields used when vectorizing. */
94+
imageFields?: string[];
95+
/** The specific model to use. */
96+
model?: string;
97+
/** The text fields used when vectorizing. */
98+
textFields?: string[];
99+
/** The truncation strategy to use. */
100+
truncate?: string;
101+
/** Whether the collection name is vectorized. */
102+
vectorizeCollectionName?: boolean;
103+
/** The weights of the fields used for vectorization. */
104+
weights?: {
105+
/** The weights of the image fields. */
106+
imageFields?: number[];
107+
/** The weights of the text fields. */
108+
textFields?: number[];
109+
};
110+
};
111+
84112
/** The configuration for multi-media vectorization using the Bind module.
85113
*
86114
* See the [documentation](https://weaviate.io/developers/weaviate/model-providers/imagebind/embeddings-multimodal) for detailed usage.
@@ -421,6 +449,8 @@ export type VectorizerConfigType<V> = V extends 'img2vec-neural'
421449
? Img2VecNeuralConfig | undefined
422450
: V extends 'multi2vec-clip'
423451
? Multi2VecClipConfig | undefined
452+
: V extends 'multi2vec-cohere'
453+
? Multi2VecCohereConfig | undefined
424454
: V extends 'multi2vec-bind'
425455
? Multi2VecBindConfig | undefined
426456
: V extends 'multi2vec-google'

src/collections/configure/types/vectorizer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ export type Multi2VecBindConfigCreate = {
111111
vectorizeCollectionName?: boolean;
112112
};
113113

114+
/** The configuration for the `multi2vec-cohere` vectorizer. */
115+
export type Multi2VecCohereConfigCreate = {
116+
/** The base URL to use where API requests should go. */
117+
baseURL?: string;
118+
/** The image fields to use in vectorization. Can be string of `Multi2VecField` type. If string, weight 0 will be assumed. */
119+
imageFields?: string[] | Multi2VecField[];
120+
/** The specific model to use. */
121+
model?: string;
122+
/** The text fields to use in vectorization. Can be string of `Multi2VecField` type. If string, weight 0 will be assumed. */
123+
textFields?: string[] | Multi2VecField[];
124+
/** The truncation strategy to use. */
125+
truncate?: string;
126+
/** Whether to vectorize the collection name. */
127+
vectorizeCollectionName?: boolean;
128+
};
129+
114130
/** @deprecated Use `Multi2VecGoogleConfigCreate` instead.*/
115131
export type Multi2VecPalmConfigCreate = Multi2VecGoogleConfigCreate;
116132

@@ -173,6 +189,8 @@ export type VectorizerConfigCreateType<V> = V extends 'img2vec-neural'
173189
? Img2VecNeuralConfigCreate | undefined
174190
: V extends 'multi2vec-clip'
175191
? Multi2VecClipConfigCreate | undefined
192+
: V extends 'multi2vec-cohere'
193+
? Multi2VecCohereConfigCreate | undefined
176194
: V extends 'multi2vec-bind'
177195
? Multi2VecBindConfigCreate | undefined
178196
: V extends 'multi2vec-palm'

src/collections/configure/unit.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,79 @@ describe('Unit testing of the vectorizer factory class', () => {
259259
});
260260
});
261261

262+
it('should create the correct Multi2VecCohereConfig type with defaults', () => {
263+
const config = configure.vectorizer.multi2VecCohere();
264+
expect(config).toEqual<VectorConfigCreate<never, undefined, 'hnsw', 'multi2vec-cohere'>>({
265+
name: undefined,
266+
vectorIndex: {
267+
name: 'hnsw',
268+
config: undefined,
269+
},
270+
vectorizer: {
271+
name: 'multi2vec-cohere',
272+
config: undefined,
273+
},
274+
});
275+
});
276+
277+
it('should create the correct Multi2VecCohereConfig type with all values', () => {
278+
const config = configure.vectorizer.multi2VecCohere({
279+
name: 'test',
280+
model: 'model',
281+
vectorizeCollectionName: true,
282+
});
283+
expect(config).toEqual<VectorConfigCreate<never, 'test', 'hnsw', 'multi2vec-cohere'>>({
284+
name: 'test',
285+
vectorIndex: {
286+
name: 'hnsw',
287+
config: undefined,
288+
},
289+
vectorizer: {
290+
name: 'multi2vec-cohere',
291+
config: {
292+
model: 'model',
293+
vectorizeCollectionName: true,
294+
},
295+
},
296+
});
297+
});
298+
299+
it('should create the correct Multi2VecCohereConfig type with all values and weights', () => {
300+
const config = configure.vectorizer.multi2VecCohere({
301+
name: 'test',
302+
model: 'model',
303+
imageFields: [
304+
{ name: 'field1', weight: 0.1 },
305+
{ name: 'field2', weight: 0.2 },
306+
],
307+
textFields: [
308+
{ name: 'field3', weight: 0.3 },
309+
{ name: 'field4', weight: 0.4 },
310+
],
311+
vectorizeCollectionName: true,
312+
});
313+
expect(config).toEqual<VectorConfigCreate<never, 'test', 'hnsw', 'multi2vec-cohere'>>({
314+
name: 'test',
315+
vectorIndex: {
316+
name: 'hnsw',
317+
config: undefined,
318+
},
319+
vectorizer: {
320+
name: 'multi2vec-cohere',
321+
config: {
322+
model: 'model',
323+
imageFields: ['field1', 'field2'],
324+
textFields: ['field3', 'field4'],
325+
vectorizeCollectionName: true,
326+
weights: {
327+
imageFields: [0.1, 0.2],
328+
textFields: [0.3, 0.4],
329+
},
330+
},
331+
},
332+
});
333+
});
334+
262335
it('should create the correct Multi2VecClipConfig type with defaults', () => {
263336
const config = configure.vectorizer.multi2VecClip();
264337
expect(config).toEqual<VectorConfigCreate<never, undefined, 'hnsw', 'multi2vec-clip'>>({

src/collections/configure/vectorizer.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,39 @@ export const vectorizer = {
129129
},
130130
});
131131
},
132+
/**
133+
* Create a `VectorConfigCreate` object with the vectorizer set to `'multi2vec-cohere'`.
134+
*
135+
* See the [documentation](https://weaviate.io/developers/weaviate/model-providers/cohere/embeddings) for detailed usage.
136+
*
137+
* @param {ConfigureNonTextVectorizerOptions<N, I, 'multi2vec-cohere'>} [opts] The configuration options for the `multi2vec-cohere` vectorizer.
138+
* @returns {VectorConfigCreate<PrimitiveKeys<T>[], N, I, 'multi2vec-cohere'>} The configuration object.
139+
*/
140+
multi2VecCohere: <N extends string | undefined = undefined, I extends VectorIndexType = 'hnsw'>(
141+
opts?: ConfigureNonTextVectorizerOptions<N, I, 'multi2vec-cohere'>
142+
): VectorConfigCreate<never, N, I, 'multi2vec-cohere'> => {
143+
const { name, vectorIndexConfig, ...config } = opts || {};
144+
const imageFields = config.imageFields?.map(mapMulti2VecField);
145+
const textFields = config.textFields?.map(mapMulti2VecField);
146+
let weights: Multi2VecBindConfig['weights'] = {};
147+
weights = formatMulti2VecFields(weights, 'imageFields', imageFields);
148+
weights = formatMulti2VecFields(weights, 'textFields', textFields);
149+
return makeVectorizer(name, {
150+
vectorIndexConfig,
151+
vectorizerConfig: {
152+
name: 'multi2vec-cohere',
153+
config:
154+
Object.keys(config).length === 0
155+
? undefined
156+
: {
157+
...config,
158+
imageFields: imageFields?.map((f) => f.name),
159+
textFields: textFields?.map((f) => f.name),
160+
weights: Object.keys(weights).length === 0 ? undefined : weights,
161+
},
162+
},
163+
});
164+
},
132165
/**
133166
* Create a `VectorConfigCreate` object with the vectorizer set to `'multi2vec-clip'`.
134167
*

0 commit comments

Comments
 (0)