Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 14 additions & 57 deletions js/plugins/vertexai/src/modelgarden/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2024 Google LLC
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,58 +14,6 @@
* limitations under the License.
*/

import type { Genkit } from 'genkit';
import { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';
import { getDerivedParams } from '../common/index.js';
import { SUPPORTED_ANTHROPIC_MODELS, anthropicModel } from './anthropic.js';
import { SUPPORTED_MISTRAL_MODELS, mistralModel } from './mistral.js';
import {
SUPPORTED_OPENAI_FORMAT_MODELS,
modelGardenOpenaiCompatibleModel,
} from './model_garden.js';
import type { PluginOptions } from './types.js';

/**
* Add Google Cloud Vertex AI Rerankers API to Genkit.
*/
export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
return genkitPlugin('vertexAIModelGarden', async (ai: Genkit) => {
const { projectId, location, authClient } = await getDerivedParams(options);

options.models.forEach((m) => {
const anthropicEntry = Object.entries(SUPPORTED_ANTHROPIC_MODELS).find(
([_, value]) => value.name === m.name
);
if (anthropicEntry) {
anthropicModel(ai, anthropicEntry[0], projectId, location);
return;
}
const mistralEntry = Object.entries(SUPPORTED_MISTRAL_MODELS).find(
([_, value]) => value.name === m.name
);
if (mistralEntry) {
mistralModel(ai, mistralEntry[0], projectId, location);
return;
}
const openaiModel = Object.entries(SUPPORTED_OPENAI_FORMAT_MODELS).find(
([_, value]) => value.name === m.name
);
if (openaiModel) {
modelGardenOpenaiCompatibleModel(
ai,
openaiModel[0],
projectId,
location,
authClient,
options.openAiBaseUrlTemplate
);
return;
}
throw new Error(`Unsupported model garden model: ${m.name}`);
});
});
}

export {
claude35Sonnet,
claude35SonnetV2,
Expand All @@ -75,7 +23,16 @@ export {
claudeOpus4,
claudeOpus41,
claudeSonnet4,
} from './anthropic.js';
export { codestral, mistralLarge, mistralNemo } from './mistral.js';
export { llama3, llama31, llama32 } from './model_garden.js';
export type { PluginOptions };
codestral,
llama3,
llama31,
llama32,
mistralLarge,
mistralNemo,
vertexAIModelGarden,
} from './legacy/index.js';
export {
vertexModelGarden,
type PluginOptions,
type VertexModelGardenPlugin,
} from './v2/index.js';
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ import {
modelRef,
type ModelAction,
} from 'genkit/model';
import { getGenkitClientHeader } from '../common/index.js';
import { getGenkitClientHeader } from '../../common/index.js';

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

/**
* @deprecated
*/
export const claude35SonnetV2 = modelRef({
name: 'vertexai/claude-3-5-sonnet-v2',
info: {
Expand All @@ -67,6 +70,9 @@ export const claude35SonnetV2 = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated
*/
export const claude35Sonnet = modelRef({
name: 'vertexai/claude-3-5-sonnet',
info: {
Expand All @@ -83,6 +89,9 @@ export const claude35Sonnet = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated
*/
export const claude3Sonnet = modelRef({
name: 'vertexai/claude-3-sonnet',
info: {
Expand All @@ -99,6 +108,9 @@ export const claude3Sonnet = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated Please use vertexModelGarden.model('claude-3-5-haiku@20241022')
*/
export const claude3Haiku = modelRef({
name: 'vertexai/claude-3-haiku',
info: {
Expand All @@ -115,6 +127,9 @@ export const claude3Haiku = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated
*/
export const claude3Opus = modelRef({
name: 'vertexai/claude-3-opus',
info: {
Expand All @@ -131,6 +146,9 @@ export const claude3Opus = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated please use vertexModelGarden.model('claude-sonnet-4@20250514')
*/
export const claudeSonnet4 = modelRef({
name: 'vertexai/claude-sonnet-4',
info: {
Expand All @@ -147,6 +165,9 @@ export const claudeSonnet4 = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated please use vertexModelGarden.model('claude-opus-4@20250514')
*/
export const claudeOpus4 = modelRef({
name: 'vertexai/claude-opus-4',
info: {
Expand All @@ -163,6 +184,9 @@ export const claudeOpus4 = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated please use vertexModelGarden.model('claude-opus-4-1@20250805')
*/
export const claudeOpus41 = modelRef({
name: 'vertexai/claude-opus-4-1',
info: {
Expand All @@ -179,6 +203,9 @@ export const claudeOpus41 = modelRef({
configSchema: AnthropicConfigSchema,
});

/**
* @deprecated
*/
export const SUPPORTED_ANTHROPIC_MODELS: Record<
string,
ModelReference<typeof AnthropicConfigSchema>
Expand All @@ -193,6 +220,9 @@ export const SUPPORTED_ANTHROPIC_MODELS: Record<
'claude-opus-4-1': claudeOpus41,
};

/**
* @deprecated
*/
export function toAnthropicRequest(
model: string,
input: GenerateRequest<typeof AnthropicConfigSchema>
Expand Down Expand Up @@ -339,6 +369,9 @@ function fromAnthropicPart(part: AnthropicContent): Part {
}

// Converts an Anthropic response to a Genkit response.
/**
* @deprecated
*/
export function fromAnthropicResponse(
input: GenerateRequest<typeof AnthropicConfigSchema>,
response: Message
Expand Down Expand Up @@ -428,6 +461,9 @@ function toAnthropicToolResponse(part: Part): ToolResultBlockParam {
};
}

/**
* @deprecated
*/
export function anthropicModel(
ai: Genkit,
modelName: string,
Expand Down
82 changes: 82 additions & 0 deletions js/plugins/vertexai/src/modelgarden/legacy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { Genkit } from 'genkit';
import { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';
import { getDerivedParams } from '../../common/index.js';
import { SUPPORTED_ANTHROPIC_MODELS, anthropicModel } from './anthropic.js';
import { SUPPORTED_MISTRAL_MODELS, mistralModel } from './mistral.js';
import {
SUPPORTED_OPENAI_FORMAT_MODELS,
modelGardenOpenaiCompatibleModel,
} from './model_garden.js';
import type { PluginOptions } from './types.js';

/**
* Add Google Cloud Vertex AI Rerankers API to Genkit.
* @deprecated Please use vertexModelGarden
*/
export function vertexAIModelGarden(options: PluginOptions): GenkitPlugin {
return genkitPlugin('vertexAIModelGarden', async (ai: Genkit) => {
const { projectId, location, authClient } = await getDerivedParams(options);

options.models.forEach((m) => {
const anthropicEntry = Object.entries(SUPPORTED_ANTHROPIC_MODELS).find(
([_, value]) => value.name === m.name
);
if (anthropicEntry) {
anthropicModel(ai, anthropicEntry[0], projectId, location);
return;
}
const mistralEntry = Object.entries(SUPPORTED_MISTRAL_MODELS).find(
([_, value]) => value.name === m.name
);
if (mistralEntry) {
mistralModel(ai, mistralEntry[0], projectId, location);
return;
}
const openaiModel = Object.entries(SUPPORTED_OPENAI_FORMAT_MODELS).find(
([_, value]) => value.name === m.name
);
if (openaiModel) {
modelGardenOpenaiCompatibleModel(
ai,
openaiModel[0],
projectId,
location,
authClient,
options.openAiBaseUrlTemplate
);
return;
}
throw new Error(`Unsupported model garden model: ${m.name}`);
});
});
}

export {
claude35Sonnet,
claude35SonnetV2,
claude3Haiku,
claude3Opus,
claude3Sonnet,
claudeOpus4,
claudeOpus41,
claudeSonnet4,
} from './anthropic.js';
export { codestral, mistralLarge, mistralNemo } from './mistral.js';
export { llama3, llama31, llama32 } from './model_garden.js';
//export type { PluginOptions }; // Same one will be exported by v2 now
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ import {
modelRef,
type ModelAction,
} from 'genkit/model';
import { getGenkitClientHeader } from '../common/index.js';
import { getGenkitClientHeader } from '../../common/index.js';

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

/**
* @deprecated
*/
export const mistralLarge = modelRef({
name: 'vertexai/mistral-large',
info: {
Expand All @@ -79,6 +83,9 @@ export const mistralLarge = modelRef({
configSchema: MistralConfigSchema,
});

/**
* @deprecated
*/
export const mistralNemo = modelRef({
name: 'vertexai/mistral-nemo',
info: {
Expand All @@ -95,6 +102,9 @@ export const mistralNemo = modelRef({
configSchema: MistralConfigSchema,
});

/**
* @deprecated
*/
export const codestral = modelRef({
name: 'vertexai/codestral',
info: {
Expand All @@ -111,6 +121,9 @@ export const codestral = modelRef({
configSchema: MistralConfigSchema,
});

/**
* @deprecated
*/
export const SUPPORTED_MISTRAL_MODELS: Record<
string,
ModelReference<typeof MistralConfigSchema>
Expand Down Expand Up @@ -152,6 +165,9 @@ function toMistralToolRequest(toolRequest: Record<string, any>): FunctionCall {
};
}

/**
* @deprecated
*/
export function toMistralRequest(
model: string,
input: GenerateRequest<typeof MistralConfigSchema>
Expand Down Expand Up @@ -278,7 +294,10 @@ function fromMistralMessage(message: AssistantMessage): Part[] {
return parts;
}

// Maps Mistral finish reasons to Genkit finish reasons
/**
* Maps Mistral finish reasons to Genkit finish reasons
* @deprecated
*/
export function fromMistralFinishReason(
reason: ChatCompletionChoiceFinishReason | undefined
): 'length' | 'unknown' | 'stop' | 'blocked' | 'other' {
Expand All @@ -297,7 +316,10 @@ export function fromMistralFinishReason(
}
}

// Converts a Mistral response to a Genkit response
/**
* Converts a Mistral response to a Genkit response
* @deprecated
*/
export function fromMistralResponse(
_input: GenerateRequest<typeof MistralConfigSchema>,
response: ChatCompletionResponse
Expand Down Expand Up @@ -329,6 +351,7 @@ export function fromMistralResponse(
};
}

/** @deprecated */
export function mistralModel(
ai: Genkit,
modelName: string,
Expand Down Expand Up @@ -467,6 +490,7 @@ function validateToolSequence(messages: MistralMessage[]) {
});
}

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

Expand Down
Loading