Skip to content

Commit 6d7083d

Browse files
authored
feat(js/plugins/vertexai/modelgarden): migrated modelgarden to V2 (#3681)
1 parent acbe641 commit 6d7083d

File tree

27 files changed

+3469
-309
lines changed

27 files changed

+3469
-309
lines changed
Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Google LLC
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,58 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { Genkit } from 'genkit';
18-
import { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';
19-
import { getDerivedParams } from '../common/index.js';
20-
import { SUPPORTED_ANTHROPIC_MODELS, anthropicModel } from './anthropic.js';
21-
import { SUPPORTED_MISTRAL_MODELS, mistralModel } from './mistral.js';
22-
import {
23-
SUPPORTED_OPENAI_FORMAT_MODELS,
24-
modelGardenOpenaiCompatibleModel,
25-
} from './model_garden.js';
26-
import type { PluginOptions } from './types.js';
27-
28-
/**
29-
* Add Google Cloud Vertex AI Rerankers API to Genkit.
30-
*/
31-
export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
32-
return genkitPlugin('vertexAIModelGarden', async (ai: Genkit) => {
33-
const { projectId, location, authClient } = await getDerivedParams(options);
34-
35-
options.models.forEach((m) => {
36-
const anthropicEntry = Object.entries(SUPPORTED_ANTHROPIC_MODELS).find(
37-
([_, value]) => value.name === m.name
38-
);
39-
if (anthropicEntry) {
40-
anthropicModel(ai, anthropicEntry[0], projectId, location);
41-
return;
42-
}
43-
const mistralEntry = Object.entries(SUPPORTED_MISTRAL_MODELS).find(
44-
([_, value]) => value.name === m.name
45-
);
46-
if (mistralEntry) {
47-
mistralModel(ai, mistralEntry[0], projectId, location);
48-
return;
49-
}
50-
const openaiModel = Object.entries(SUPPORTED_OPENAI_FORMAT_MODELS).find(
51-
([_, value]) => value.name === m.name
52-
);
53-
if (openaiModel) {
54-
modelGardenOpenaiCompatibleModel(
55-
ai,
56-
openaiModel[0],
57-
projectId,
58-
location,
59-
authClient,
60-
options.openAiBaseUrlTemplate
61-
);
62-
return;
63-
}
64-
throw new Error(`Unsupported model garden model: ${m.name}`);
65-
});
66-
});
67-
}
68-
6917
export {
7018
claude35Sonnet,
7119
claude35SonnetV2,
@@ -75,7 +23,16 @@ export {
7523
claudeOpus4,
7624
claudeOpus41,
7725
claudeSonnet4,
78-
} from './anthropic.js';
79-
export { codestral, mistralLarge, mistralNemo } from './mistral.js';
80-
export { llama3, llama31, llama32 } from './model_garden.js';
81-
export type { PluginOptions };
26+
codestral,
27+
llama3,
28+
llama31,
29+
llama32,
30+
mistralLarge,
31+
mistralNemo,
32+
vertexAIModelGarden,
33+
} from './legacy/index.js';
34+
export {
35+
vertexModelGarden,
36+
type PluginOptions,
37+
type VertexModelGardenPlugin,
38+
} from './v2/index.js';

js/plugins/vertexai/src/modelgarden/anthropic.ts renamed to js/plugins/vertexai/src/modelgarden/legacy/anthropic.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ import {
4545
modelRef,
4646
type ModelAction,
4747
} from 'genkit/model';
48-
import { getGenkitClientHeader } from '../common/index.js';
48+
import { getGenkitClientHeader } from '../../common/index.js';
4949

5050
export const AnthropicConfigSchema = GenerationCommonConfigSchema.extend({
5151
location: z.string().optional(),
5252
});
5353

54+
/**
55+
* @deprecated
56+
*/
5457
export const claude35SonnetV2 = modelRef({
5558
name: 'vertexai/claude-3-5-sonnet-v2',
5659
info: {
@@ -67,6 +70,9 @@ export const claude35SonnetV2 = modelRef({
6770
configSchema: AnthropicConfigSchema,
6871
});
6972

73+
/**
74+
* @deprecated
75+
*/
7076
export const claude35Sonnet = modelRef({
7177
name: 'vertexai/claude-3-5-sonnet',
7278
info: {
@@ -83,6 +89,9 @@ export const claude35Sonnet = modelRef({
8389
configSchema: AnthropicConfigSchema,
8490
});
8591

92+
/**
93+
* @deprecated
94+
*/
8695
export const claude3Sonnet = modelRef({
8796
name: 'vertexai/claude-3-sonnet',
8897
info: {
@@ -99,6 +108,9 @@ export const claude3Sonnet = modelRef({
99108
configSchema: AnthropicConfigSchema,
100109
});
101110

111+
/**
112+
* @deprecated Please use vertexModelGarden.model('claude-3-5-haiku@20241022')
113+
*/
102114
export const claude3Haiku = modelRef({
103115
name: 'vertexai/claude-3-haiku',
104116
info: {
@@ -115,6 +127,9 @@ export const claude3Haiku = modelRef({
115127
configSchema: AnthropicConfigSchema,
116128
});
117129

130+
/**
131+
* @deprecated
132+
*/
118133
export const claude3Opus = modelRef({
119134
name: 'vertexai/claude-3-opus',
120135
info: {
@@ -131,6 +146,9 @@ export const claude3Opus = modelRef({
131146
configSchema: AnthropicConfigSchema,
132147
});
133148

149+
/**
150+
* @deprecated please use vertexModelGarden.model('claude-sonnet-4@20250514')
151+
*/
134152
export const claudeSonnet4 = modelRef({
135153
name: 'vertexai/claude-sonnet-4',
136154
info: {
@@ -147,6 +165,9 @@ export const claudeSonnet4 = modelRef({
147165
configSchema: AnthropicConfigSchema,
148166
});
149167

168+
/**
169+
* @deprecated please use vertexModelGarden.model('claude-opus-4@20250514')
170+
*/
150171
export const claudeOpus4 = modelRef({
151172
name: 'vertexai/claude-opus-4',
152173
info: {
@@ -163,6 +184,9 @@ export const claudeOpus4 = modelRef({
163184
configSchema: AnthropicConfigSchema,
164185
});
165186

187+
/**
188+
* @deprecated please use vertexModelGarden.model('claude-opus-4-1@20250805')
189+
*/
166190
export const claudeOpus41 = modelRef({
167191
name: 'vertexai/claude-opus-4-1',
168192
info: {
@@ -179,6 +203,9 @@ export const claudeOpus41 = modelRef({
179203
configSchema: AnthropicConfigSchema,
180204
});
181205

206+
/**
207+
* @deprecated
208+
*/
182209
export const SUPPORTED_ANTHROPIC_MODELS: Record<
183210
string,
184211
ModelReference<typeof AnthropicConfigSchema>
@@ -193,6 +220,9 @@ export const SUPPORTED_ANTHROPIC_MODELS: Record<
193220
'claude-opus-4-1': claudeOpus41,
194221
};
195222

223+
/**
224+
* @deprecated
225+
*/
196226
export function toAnthropicRequest(
197227
model: string,
198228
input: GenerateRequest<typeof AnthropicConfigSchema>
@@ -339,6 +369,9 @@ function fromAnthropicPart(part: AnthropicContent): Part {
339369
}
340370

341371
// Converts an Anthropic response to a Genkit response.
372+
/**
373+
* @deprecated
374+
*/
342375
export function fromAnthropicResponse(
343376
input: GenerateRequest<typeof AnthropicConfigSchema>,
344377
response: Message
@@ -428,6 +461,9 @@ function toAnthropicToolResponse(part: Part): ToolResultBlockParam {
428461
};
429462
}
430463

464+
/**
465+
* @deprecated
466+
*/
431467
export function anthropicModel(
432468
ai: Genkit,
433469
modelName: string,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import type { Genkit } from 'genkit';
18+
import { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';
19+
import { getDerivedParams } from '../../common/index.js';
20+
import { SUPPORTED_ANTHROPIC_MODELS, anthropicModel } from './anthropic.js';
21+
import { SUPPORTED_MISTRAL_MODELS, mistralModel } from './mistral.js';
22+
import {
23+
SUPPORTED_OPENAI_FORMAT_MODELS,
24+
modelGardenOpenaiCompatibleModel,
25+
} from './model_garden.js';
26+
import type { PluginOptions } from './types.js';
27+
28+
/**
29+
* Add Google Cloud Vertex AI Rerankers API to Genkit.
30+
* @deprecated Please use vertexModelGarden
31+
*/
32+
export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
33+
return genkitPlugin('vertexAIModelGarden', async (ai: Genkit) => {
34+
const { projectId, location, authClient } = await getDerivedParams(options);
35+
36+
options.models.forEach((m) => {
37+
const anthropicEntry = Object.entries(SUPPORTED_ANTHROPIC_MODELS).find(
38+
([_, value]) => value.name === m.name
39+
);
40+
if (anthropicEntry) {
41+
anthropicModel(ai, anthropicEntry[0], projectId, location);
42+
return;
43+
}
44+
const mistralEntry = Object.entries(SUPPORTED_MISTRAL_MODELS).find(
45+
([_, value]) => value.name === m.name
46+
);
47+
if (mistralEntry) {
48+
mistralModel(ai, mistralEntry[0], projectId, location);
49+
return;
50+
}
51+
const openaiModel = Object.entries(SUPPORTED_OPENAI_FORMAT_MODELS).find(
52+
([_, value]) => value.name === m.name
53+
);
54+
if (openaiModel) {
55+
modelGardenOpenaiCompatibleModel(
56+
ai,
57+
openaiModel[0],
58+
projectId,
59+
location,
60+
authClient,
61+
options.openAiBaseUrlTemplate
62+
);
63+
return;
64+
}
65+
throw new Error(`Unsupported model garden model: ${m.name}`);
66+
});
67+
});
68+
}
69+
70+
export {
71+
claude35Sonnet,
72+
claude35SonnetV2,
73+
claude3Haiku,
74+
claude3Opus,
75+
claude3Sonnet,
76+
claudeOpus4,
77+
claudeOpus41,
78+
claudeSonnet4,
79+
} from './anthropic.js';
80+
export { codestral, mistralLarge, mistralNemo } from './mistral.js';
81+
export { llama3, llama31, llama32 } from './model_garden.js';
82+
//export type { PluginOptions }; // Same one will be exported by v2 now

js/plugins/vertexai/src/modelgarden/mistral.ts renamed to js/plugins/vertexai/src/modelgarden/legacy/mistral.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ import {
4646
modelRef,
4747
type ModelAction,
4848
} from 'genkit/model';
49-
import { getGenkitClientHeader } from '../common/index.js';
49+
import { getGenkitClientHeader } from '../../common/index.js';
5050

5151
/**
5252
* See https://docs.mistral.ai/api/#tag/chat/operation/chat_completion_v1_chat_completions_post
53+
* @deprecated
5354
*/
5455
export const MistralConfigSchema = GenerationCommonConfigSchema.extend({
5556
// TODO: Update this with all the parameters in
@@ -63,6 +64,9 @@ export const MistralConfigSchema = GenerationCommonConfigSchema.extend({
6364
.optional(),
6465
});
6566

67+
/**
68+
* @deprecated
69+
*/
6670
export const mistralLarge = modelRef({
6771
name: 'vertexai/mistral-large',
6872
info: {
@@ -79,6 +83,9 @@ export const mistralLarge = modelRef({
7983
configSchema: MistralConfigSchema,
8084
});
8185

86+
/**
87+
* @deprecated
88+
*/
8289
export const mistralNemo = modelRef({
8390
name: 'vertexai/mistral-nemo',
8491
info: {
@@ -95,6 +102,9 @@ export const mistralNemo = modelRef({
95102
configSchema: MistralConfigSchema,
96103
});
97104

105+
/**
106+
* @deprecated
107+
*/
98108
export const codestral = modelRef({
99109
name: 'vertexai/codestral',
100110
info: {
@@ -111,6 +121,9 @@ export const codestral = modelRef({
111121
configSchema: MistralConfigSchema,
112122
});
113123

124+
/**
125+
* @deprecated
126+
*/
114127
export const SUPPORTED_MISTRAL_MODELS: Record<
115128
string,
116129
ModelReference<typeof MistralConfigSchema>
@@ -152,6 +165,9 @@ function toMistralToolRequest(toolRequest: Record<string, any>): FunctionCall {
152165
};
153166
}
154167

168+
/**
169+
* @deprecated
170+
*/
155171
export function toMistralRequest(
156172
model: string,
157173
input: GenerateRequest<typeof MistralConfigSchema>
@@ -278,7 +294,10 @@ function fromMistralMessage(message: AssistantMessage): Part[] {
278294
return parts;
279295
}
280296

281-
// Maps Mistral finish reasons to Genkit finish reasons
297+
/**
298+
* Maps Mistral finish reasons to Genkit finish reasons
299+
* @deprecated
300+
*/
282301
export function fromMistralFinishReason(
283302
reason: ChatCompletionChoiceFinishReason | undefined
284303
): 'length' | 'unknown' | 'stop' | 'blocked' | 'other' {
@@ -297,7 +316,10 @@ export function fromMistralFinishReason(
297316
}
298317
}
299318

300-
// Converts a Mistral response to a Genkit response
319+
/**
320+
* Converts a Mistral response to a Genkit response
321+
* @deprecated
322+
*/
301323
export function fromMistralResponse(
302324
_input: GenerateRequest<typeof MistralConfigSchema>,
303325
response: ChatCompletionResponse
@@ -329,6 +351,7 @@ export function fromMistralResponse(
329351
};
330352
}
331353

354+
/** @deprecated */
332355
export function mistralModel(
333356
ai: Genkit,
334357
modelName: string,
@@ -467,6 +490,7 @@ function validateToolSequence(messages: MistralMessage[]) {
467490
});
468491
}
469492

493+
/** @deprecated */
470494
export function fromMistralCompletionChunk(chunk: CompletionChunk): Part[] {
471495
if (!chunk.choices?.[0]?.delta) return [];
472496

0 commit comments

Comments
 (0)