Skip to content

Commit 7cfb7ba

Browse files
Jinash RouniyarJinash Rouniyar
authored andcommitted
Feat: Added Contextual AI's Generative and Reranker Client
1 parent cef88c5 commit 7cfb7ba

File tree

13 files changed

+674
-1
lines changed

13 files changed

+674
-1
lines changed

src/collections/config/integration.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
PropertyConfig,
1111
RQConfig,
1212
RerankerCohereConfig,
13+
RerankerContextualAIConfig,
1314
VectorIndexConfigDynamic,
1415
VectorIndexConfigHNSW,
1516
} from './types/index.js';
@@ -787,6 +788,44 @@ describe('Testing of the collection.config namespace', () => {
787788
model: 'model',
788789
},
789790
});
791+
792+
await collection.config.update({
793+
reranker: weaviate.reconfigure.reranker.contextualai({
794+
model: 'ctxl-rerank-v2-instruct-multilingual',
795+
}),
796+
});
797+
798+
config = await collection.config.get();
799+
expect(config.reranker).toEqual<ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig>>({
800+
name: 'reranker-contextualai',
801+
config: {
802+
model: 'ctxl-rerank-v2-instruct-multilingual',
803+
},
804+
});
805+
806+
await collection.config.update({
807+
generative: weaviate.reconfigure.generative.contextualai({
808+
model: 'v2',
809+
maxTokens: 100,
810+
temperature: 0.7,
811+
topP: 0.9,
812+
systemPrompt: 'sys',
813+
avoidCommentary: false,
814+
}),
815+
});
816+
817+
config = await collection.config.get();
818+
expect(config.generative).toEqual<ModuleConfig<'generative-contextualai', any>>({
819+
name: 'generative-contextualai',
820+
config: {
821+
model: 'v2',
822+
maxTokensProperty: 100,
823+
temperatureProperty: 0.7,
824+
topPProperty: 0.9,
825+
systemPromptProperty: 'sys',
826+
avoidCommentaryProperty: false,
827+
},
828+
});
790829
});
791830

792831
requireAtLeast(1, 31, 0).it(

src/collections/config/types/generative.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,22 @@ export type GenerativeXAIConfig = {
106106
topP?: number;
107107
};
108108

109+
export type GenerativeContextualAIConfig = {
110+
model?: string;
111+
maxTokensProperty?: number;
112+
temperatureProperty?: number;
113+
topPProperty?: number;
114+
systemPromptProperty?: string;
115+
avoidCommentaryProperty?: boolean;
116+
};
117+
109118
export type GenerativeConfig =
110119
| GenerativeAnthropicConfig
111120
| GenerativeAnyscaleConfig
112121
| GenerativeAWSConfig
113122
| GenerativeAzureOpenAIConfig
114123
| GenerativeCohereConfig
124+
| GenerativeContextualAIConfig
115125
| GenerativeDatabricksConfig
116126
| GenerativeGoogleConfig
117127
| GenerativeFriendliAIConfig
@@ -133,6 +143,8 @@ export type GenerativeConfigType<G> = G extends 'generative-anthropic'
133143
? GenerativeAzureOpenAIConfig
134144
: G extends 'generative-cohere'
135145
? GenerativeCohereConfig
146+
: G extends 'generative-contextualai'
147+
? GenerativeContextualAIConfig
136148
: G extends 'generative-databricks'
137149
? GenerativeDatabricksConfig
138150
: G extends 'generative-google'
@@ -162,6 +174,7 @@ export type GenerativeSearch =
162174
| 'generative-aws'
163175
| 'generative-azure-openai'
164176
| 'generative-cohere'
177+
| 'generative-contextualai'
165178
| 'generative-databricks'
166179
| 'generative-google'
167180
| 'generative-friendliai'

src/collections/config/types/reranker.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,20 @@ export type RerankerNvidiaConfig = {
2424
model?: 'nvidia/rerank-qa-mistral-4b' | string;
2525
};
2626

27+
export type RerankerContextualAIConfig = {
28+
baseURL?: string;
29+
model?:
30+
| 'ctxl-rerank-v2-instruct-multilingual'
31+
| 'ctxl-rerank-v2-instruct-multilingual-mini'
32+
| 'ctxl-rerank-v1-instruct'
33+
| string;
34+
instruction?: string;
35+
topN?: number;
36+
};
37+
2738
export type RerankerConfig =
2839
| RerankerCohereConfig
40+
| RerankerContextualAIConfig
2941
| RerankerJinaAIConfig
3042
| RerankerNvidiaConfig
3143
| RerankerTransformersConfig
@@ -35,6 +47,7 @@ export type RerankerConfig =
3547

3648
export type Reranker =
3749
| 'reranker-cohere'
50+
| 'reranker-contextualai'
3851
| 'reranker-jinaai'
3952
| 'reranker-nvidia'
4053
| 'reranker-transformers'
@@ -48,6 +61,8 @@ export type RerankerConfigType<R> = R extends 'reranker-cohere'
4861
? RerankerJinaAIConfig
4962
: R extends 'reranker-nvidia'
5063
? RerankerNvidiaConfig
64+
: R extends 'reranker-contextualai'
65+
? RerankerContextualAIConfig
5166
: R extends 'reranker-transformers'
5267
? RerankerTransformersConfig
5368
: R extends 'reranker-voyageai'

src/collections/configure/generative.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
GenerativeAnyscaleConfig,
55
GenerativeAzureOpenAIConfig,
66
GenerativeCohereConfig,
7+
GenerativeContextualAIConfig,
78
GenerativeDatabricksConfig,
89
GenerativeFriendliAIConfig,
910
GenerativeGoogleConfig,
@@ -21,6 +22,7 @@ import {
2122
GenerativeAnyscaleConfigCreate,
2223
GenerativeAzureOpenAIConfigCreate,
2324
GenerativeCohereConfigCreate,
25+
GenerativeContextualAIConfigCreate,
2426
GenerativeDatabricksConfigCreate,
2527
GenerativeFriendliAIConfigCreate,
2628
GenerativeMistralConfigCreate,
@@ -48,6 +50,31 @@ export default {
4850
config,
4951
};
5052
},
53+
/**
54+
* Create a `ModuleConfig<'generative-contextualai', GenerativeContextualAIConfig | undefined>` object for use when performing AI generation using the `generative-contextualai` module.
55+
*
56+
* See the [documentation](https://weaviate.io/developers/weaviate/model-providers/contextualai/generative) for detailed usage.
57+
*
58+
* @param {GenerativeContextualAIConfigCreate} [config] The configuration for the `generative-contextualai` module.
59+
* @returns {ModuleConfig<'generative-contextualai', GenerativeContextualAIConfig | undefined>} The configuration object.
60+
*/
61+
contextualai: (
62+
config?: GenerativeContextualAIConfigCreate
63+
): ModuleConfig<'generative-contextualai', GenerativeContextualAIConfig | undefined> => {
64+
return {
65+
name: 'generative-contextualai',
66+
config: config
67+
? {
68+
model: config.model,
69+
maxTokensProperty: config.maxTokens,
70+
temperatureProperty: config.temperature,
71+
topPProperty: config.topP,
72+
systemPromptProperty: config.systemPrompt,
73+
avoidCommentaryProperty: config.avoidCommentary,
74+
}
75+
: undefined,
76+
};
77+
},
5178
/**
5279
* Create a `ModuleConfig<'generative-anyscale', GenerativeAnyscaleConfig | undefined>` object for use when performing AI generation using the `generative-anyscale` module.
5380
*

src/collections/configure/reranker.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
ModuleConfig,
33
RerankerCohereConfig,
4+
RerankerContextualAIConfig,
45
RerankerJinaAIConfig,
56
RerankerNvidiaConfig,
67
RerankerVoyageAIConfig,
@@ -23,6 +24,22 @@ export default {
2324
config: config,
2425
};
2526
},
27+
/**
28+
* Create a `ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig>` object for use when reranking using the `reranker-contextualai` module.
29+
*
30+
* See the [documentation](https://weaviate.io/developers/weaviate/model-providers/contextualai/reranker) for detailed usage.
31+
*
32+
* @param {RerankerContextualAIConfig} [config] The configuration for the `reranker-contextualai` module.
33+
* @returns {ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig | undefined>} The configuration object.
34+
*/
35+
contextualai: (
36+
config?: RerankerContextualAIConfig
37+
): ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig | undefined> => {
38+
return {
39+
name: 'reranker-contextualai',
40+
config: config,
41+
};
42+
},
2643
/**
2744
* Create a `ModuleConfig<'reranker-jinaai', RerankerJinaAIConfig>` object for use when reranking using the `reranker-jinaai` module.
2845
*

src/collections/configure/types/generative.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
GenerativeAWSConfig,
33
GenerativeAnthropicConfig,
44
GenerativeAnyscaleConfig,
5+
GenerativeContextualAIConfig,
56
GenerativeDatabricksConfig,
67
GenerativeFriendliAIConfig,
78
GenerativeMistralConfig,
@@ -58,12 +59,22 @@ export type GenerativePaLMConfigCreate = GenerativePaLMConfig;
5859

5960
export type GenerativeXAIConfigCreate = GenerativeXAIConfig;
6061

62+
export type GenerativeContextualAIConfigCreate = {
63+
model?: string;
64+
maxTokens?: number;
65+
temperature?: number;
66+
topP?: number;
67+
systemPrompt?: string;
68+
avoidCommentary?: boolean;
69+
};
70+
6171
export type GenerativeConfigCreate =
6272
| GenerativeAnthropicConfigCreate
6373
| GenerativeAnyscaleConfigCreate
6474
| GenerativeAWSConfigCreate
6575
| GenerativeAzureOpenAIConfigCreate
6676
| GenerativeCohereConfigCreate
77+
| GenerativeContextualAIConfigCreate
6778
| GenerativeDatabricksConfigCreate
6879
| GenerativeFriendliAIConfigCreate
6980
| GenerativeMistralConfigCreate
@@ -83,6 +94,8 @@ export type GenerativeConfigCreateType<G> = G extends 'generative-anthropic'
8394
? GenerativeAzureOpenAIConfigCreate
8495
: G extends 'generative-cohere'
8596
? GenerativeCohereConfigCreate
97+
: G extends 'generative-contextualai'
98+
? GenerativeContextualAIConfigCreate
8699
: G extends 'generative-databricks'
87100
? GenerativeDatabricksConfigCreate
88101
: G extends 'generative-friendliai'

src/collections/configure/unit.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
GenerativeAnyscaleConfig,
66
GenerativeAzureOpenAIConfig,
77
GenerativeCohereConfig,
8+
GenerativeContextualAIConfig,
89
GenerativeDatabricksConfig,
910
GenerativeFriendliAIConfig,
1011
GenerativeGoogleConfig,
@@ -14,6 +15,7 @@ import {
1415
GenerativeXAIConfig,
1516
ModuleConfig,
1617
RerankerCohereConfig,
18+
RerankerContextualAIConfig,
1719
RerankerJinaAIConfig,
1820
RerankerNvidiaConfig,
1921
RerankerTransformersConfig,
@@ -1931,6 +1933,38 @@ describe('Unit testing of the generative factory class', () => {
19311933
});
19321934
});
19331935

1936+
it('should create the correct GenerativeContextualAIConfig type with required & default values', () => {
1937+
const config = configure.generative.contextualai();
1938+
expect(config).toEqual<ModuleConfig<'generative-contextualai', GenerativeContextualAIConfig | undefined>>(
1939+
{
1940+
name: 'generative-contextualai',
1941+
config: undefined,
1942+
}
1943+
);
1944+
});
1945+
1946+
it('should create the correct GenerativeContextualAIConfig type with all values', () => {
1947+
const config = configure.generative.contextualai({
1948+
model: 'v2',
1949+
maxTokens: 100,
1950+
temperature: 0.7,
1951+
topP: 0.9,
1952+
systemPrompt: 'system',
1953+
avoidCommentary: false,
1954+
});
1955+
expect(config).toEqual<ModuleConfig<'generative-contextualai', GenerativeContextualAIConfig>>({
1956+
name: 'generative-contextualai',
1957+
config: {
1958+
model: 'v2',
1959+
maxTokensProperty: 100,
1960+
temperatureProperty: 0.7,
1961+
topPProperty: 0.9,
1962+
systemPromptProperty: 'system',
1963+
avoidCommentaryProperty: false,
1964+
},
1965+
});
1966+
});
1967+
19341968
it('should create the correct GenerativeCohereConfig type with all values', () => {
19351969
const config = configure.generative.cohere({
19361970
k: 5,
@@ -2265,6 +2299,32 @@ describe('Unit testing of the reranker factory class', () => {
22652299
});
22662300
});
22672301

2302+
it('should create the correct RerankerContextualAIConfig type using required & default values', () => {
2303+
const config = configure.reranker.contextualai();
2304+
expect(config).toEqual<ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig | undefined>>({
2305+
name: 'reranker-contextualai',
2306+
config: undefined,
2307+
});
2308+
});
2309+
2310+
it('should create the correct RerankerContextualAIConfig type with all values', () => {
2311+
const config = configure.reranker.contextualai({
2312+
baseURL: 'https://api.contextual.ai',
2313+
model: 'ctxl-rerank-v2-instruct-multilingual',
2314+
instruction: 'Custom reranking instruction',
2315+
topN: 10,
2316+
});
2317+
expect(config).toEqual<ModuleConfig<'reranker-contextualai', RerankerContextualAIConfig | undefined>>({
2318+
name: 'reranker-contextualai',
2319+
config: {
2320+
baseURL: 'https://api.contextual.ai',
2321+
model: 'ctxl-rerank-v2-instruct-multilingual',
2322+
instruction: 'Custom reranking instruction',
2323+
topN: 10,
2324+
},
2325+
});
2326+
});
2327+
22682328
it('should create the correct RerankerVoyageAIConfig type with all values', () => {
22692329
const config = configure.reranker.voyageAI({
22702330
baseURL: 'base-url',

src/collections/generate/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
GenerativeAnyscaleConfigRuntime,
77
GenerativeCohereConfigRuntime,
88
GenerativeConfigRuntimeType,
9+
GenerativeContextualAIConfigRuntime,
910
GenerativeDatabricksConfigRuntime,
1011
GenerativeFriendliAIConfigRuntime,
1112
GenerativeGoogleConfigRuntime,
@@ -297,4 +298,29 @@ export const generativeParameters = {
297298
: undefined,
298299
};
299300
},
301+
/**
302+
* Create a `ModuleConfig<'generative-contextualai', GenerativeConfigRuntimeType<'generative-contextualai'> | undefined>`
303+
* object for use when performing runtime-specific AI generation using the `generative-contextualai` module.
304+
*/
305+
contextualai(
306+
config?: GenerativeContextualAIConfigRuntime
307+
): ModuleConfig<
308+
'generative-contextualai',
309+
GenerativeConfigRuntimeType<'generative-contextualai'> | undefined
310+
> {
311+
// Contextual AI does not require special GRPC wrappers; pass primitives directly
312+
return {
313+
name: 'generative-contextualai',
314+
config: config
315+
? {
316+
model: config.model,
317+
maxTokens: config.maxTokens,
318+
temperature: config.temperature,
319+
topP: config.topP,
320+
systemPrompt: config.systemPrompt,
321+
avoidCommentary: config.avoidCommentary,
322+
}
323+
: undefined,
324+
};
325+
},
300326
};

0 commit comments

Comments
 (0)