@genkit-ai/vertexai
Version:
Genkit AI framework plugin for Google Cloud Vertex AI APIs including Gemini APIs, Imagen, and more.
1 lines • 14.3 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @license\n *\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * @module /\n */\n\nimport {\n embedderRef,\n modelActionMetadata,\n modelRef,\n type EmbedderReference,\n type Genkit,\n type ModelReference,\n type z,\n} from 'genkit';\nimport { genkitPlugin, type GenkitPlugin } from 'genkit/plugin';\nimport type { ActionType } from 'genkit/registry';\nimport { getDerivedParams } from './common/index.mjs';\nimport type { PluginOptions } from './common/types.mjs';\nimport {\n SUPPORTED_EMBEDDER_MODELS,\n VertexEmbeddingConfigSchema,\n defineVertexAIEmbedder,\n geminiEmbedding001,\n multimodalEmbedding001,\n textEmbedding004,\n textEmbedding005,\n textEmbeddingGecko003,\n textEmbeddingGeckoMultilingual001,\n textMultilingualEmbedding002,\n type VertexEmbeddingConfig,\n} from './embedder.mjs';\nimport {\n GeminiConfigSchema,\n SUPPORTED_GEMINI_MODELS,\n SafetySettingsSchema,\n defineGeminiKnownModel,\n defineGeminiModel,\n gemini,\n gemini10Pro,\n gemini15Flash,\n gemini15Pro,\n gemini20Flash,\n gemini20Flash001,\n gemini20FlashLite,\n gemini20FlashLitePreview0205,\n gemini20ProExp0205,\n gemini25FlashLite,\n gemini25FlashPreview0417,\n gemini25ProExp0325,\n gemini25ProPreview0325,\n type GeminiConfig,\n type GeminiVersionString,\n} from './gemini.mjs';\nimport {\n GENERIC_IMAGEN_INFO,\n ImagenConfigSchema,\n SUPPORTED_IMAGEN_MODELS,\n defineImagenModel,\n imagen2,\n imagen3,\n imagen3Fast,\n type ACTUAL_IMAGEN_MODELS,\n} from './imagen.mjs';\nimport { listModels } from './list-models.mjs';\nexport type { PluginOptions } from './common/types.mjs';\nexport {\n GeminiConfigSchema,\n ImagenConfigSchema,\n SafetySettingsSchema,\n gemini,\n gemini10Pro,\n gemini15Flash,\n gemini15Pro,\n gemini20Flash,\n gemini20Flash001,\n gemini20FlashLite,\n gemini20FlashLitePreview0205,\n gemini20ProExp0205,\n gemini25FlashLite,\n gemini25FlashPreview0417,\n gemini25ProExp0325,\n gemini25ProPreview0325,\n geminiEmbedding001,\n imagen2,\n imagen3,\n imagen3Fast,\n multimodalEmbedding001,\n textEmbedding004,\n textEmbedding005,\n textEmbeddingGecko003,\n textEmbeddingGeckoMultilingual001,\n textMultilingualEmbedding002,\n type GeminiConfig,\n type GeminiVersionString,\n};\n\nfunction warnDeprecated() {\n console.error(\n `\\n\\x1b[41m\\x1b[37m\\x1b[1m DEPRECATION WARNING \\x1b[0m\\n` +\n `\\x1b[31mThe \\x1b[1m@genkit-ai/vertexai\\x1b[0m\\x1b[31m plugin is deprecated and will be REMOVED in a future release.\\x1b[0m\\n` +\n `\\x1b[33m👉 Please migrate to \\x1b[1m@genkit-ai/google-genai\\x1b[0m\\x1b[33m to avoid breaking changes.\\x1b[0m\\n` +\n `\\x1b[90mDocs: https://genkit.dev/docs/js/integrations/vertex-ai/ \\x1b[0m\\n`\n );\n}\n\nasync function initializer(ai: Genkit, options?: PluginOptions) {\n warnDeprecated();\n if (options?.location === 'global') {\n // Not a breaking change, it already doesn't work.\n throw new Error(\n 'The vertexAI plugin in the @genkit-ai/vertexai package does not support \"global\" location.\\n' +\n 'Support for \"global\" location is available in the new vertexAI plugin in the @genkit-ai/google-genai package.\\n\\n' +\n 'Please switch your import to use the latest features:\\n' +\n \" import { vertexAI } from '@genkit-ai/google-genai';\\n\\n\"\n );\n }\n const { projectId, location, vertexClientFactory, authClient } =\n await getDerivedParams(options);\n\n Object.keys(SUPPORTED_IMAGEN_MODELS).map((name) =>\n defineImagenModel(ai, name, authClient, { projectId, location })\n );\n Object.keys(SUPPORTED_GEMINI_MODELS).map((name) =>\n defineGeminiKnownModel(\n ai,\n name,\n vertexClientFactory,\n {\n projectId,\n location,\n },\n options?.experimental_debugTraces\n )\n );\n if (options?.models) {\n for (const modelOrRef of options?.models) {\n const modelName =\n typeof modelOrRef === 'string'\n ? modelOrRef\n : // strip out the `vertexai/` prefix\n modelOrRef.name.split('/')[1];\n const modelRef =\n typeof modelOrRef === 'string' ? gemini(modelOrRef) : modelOrRef;\n defineGeminiModel({\n ai,\n modelName: modelRef.name,\n version: modelName,\n modelInfo: modelRef.info,\n vertexClientFactory,\n options: {\n projectId,\n location,\n },\n debugTraces: options.experimental_debugTraces,\n });\n }\n }\n\n Object.keys(SUPPORTED_EMBEDDER_MODELS).map((name) =>\n defineVertexAIEmbedder(ai, name, authClient, { projectId, location })\n );\n}\n\nasync function resolver(\n ai: Genkit,\n actionType: ActionType,\n actionName: string,\n options?: PluginOptions\n) {\n warnDeprecated();\n switch (actionType) {\n case 'model':\n await resolveModel(ai, actionName, options);\n break;\n case 'embedder':\n await resolveEmbedder(ai, actionName, options);\n break;\n default:\n // no-op\n }\n}\n\nasync function resolveModel(\n ai: Genkit,\n actionName: string,\n options?: PluginOptions\n) {\n const { projectId, location, vertexClientFactory, authClient } =\n await getDerivedParams(options);\n\n if (actionName.startsWith('imagen')) {\n defineImagenModel(ai, actionName, authClient, { projectId, location });\n return;\n }\n\n const modelRef = gemini(actionName);\n defineGeminiModel({\n ai,\n modelName: modelRef.name,\n version: actionName,\n modelInfo: modelRef.info,\n vertexClientFactory,\n options: {\n projectId,\n location,\n },\n debugTraces: options?.experimental_debugTraces,\n });\n}\n\nasync function resolveEmbedder(\n ai: Genkit,\n actionName: string,\n options?: PluginOptions\n) {\n const { projectId, location, authClient } = await getDerivedParams(options);\n\n defineVertexAIEmbedder(ai, actionName, authClient, { projectId, location });\n}\n\n// Vertex AI list models still returns these and the API does not indicate in any way\n// that those models are not served anymore.\nconst KNOWN_DECOMISSIONED_MODELS = [\n 'gemini-pro-vision',\n 'gemini-pro',\n 'gemini-ultra',\n 'gemini-ultra-vision',\n];\n\nasync function listActions(options?: PluginOptions) {\n const { location, projectId, authClient } = await getDerivedParams(options);\n const models = await listModels(authClient, location, projectId);\n // Vertex has a lot of models, and no way to figure out the \"type\" of the model...\n // so, for list actions we only fetch known model \"families\".\n return [\n // Gemini\n ...models\n .filter(\n (m) =>\n m.name.includes('gemini') &&\n !KNOWN_DECOMISSIONED_MODELS.includes(m.name.split('/').at(-1)!)\n )\n .map((m) => {\n const ref = gemini(m.name.split('/').at(-1)!);\n\n return modelActionMetadata({\n name: ref.name,\n info: ref.info,\n configSchema: GeminiConfigSchema,\n });\n }),\n // Imagen\n ...models\n .filter((m) => m.name.includes('imagen'))\n .map((m) => {\n const name = m.name.split('/').at(-1)!;\n\n return modelActionMetadata({\n name: 'vertexai/' + name,\n info: {\n ...GENERIC_IMAGEN_INFO,\n label: `Vertex AI - ${name}`,\n },\n configSchema: ImagenConfigSchema,\n });\n }),\n ];\n}\n\n/**\n * Add Google Cloud Vertex AI to Genkit. Includes Gemini and Imagen models and text embedder.\n */\nfunction vertexAIPlugin(options?: PluginOptions): GenkitPlugin {\n let listActionsCache;\n return genkitPlugin(\n 'vertexai',\n async (ai: Genkit) => await initializer(ai, options),\n async (ai: Genkit, actionType: ActionType, actionName: string) =>\n await resolver(ai, actionType, actionName, options),\n async () => {\n if (listActionsCache) return listActionsCache;\n listActionsCache = await listActions(options);\n return listActionsCache;\n }\n );\n}\n\n/**\n * @deprecated\n */\nexport type VertexAIPlugin = {\n (params?: PluginOptions): GenkitPlugin;\n model(\n name: keyof typeof SUPPORTED_GEMINI_MODELS | (`gemini-${string}` & {}),\n config?: z.infer<typeof GeminiConfigSchema>\n ): ModelReference<typeof GeminiConfigSchema>;\n model(\n name: keyof typeof ACTUAL_IMAGEN_MODELS | (`imagen${string}` & {}),\n config?: z.infer<typeof ImagenConfigSchema>\n ): ModelReference<typeof ImagenConfigSchema>;\n model(name: string, config?: any): ModelReference<z.ZodTypeAny>;\n embedder(\n name: string,\n config?: VertexEmbeddingConfig\n ): EmbedderReference<typeof VertexEmbeddingConfigSchema>;\n};\n\n/**\n * Google Cloud Vertex AI plugin for Genkit.\n * Includes Gemini and Imagen models and text embedder.\n * @deprecated Please use `import { vertexAI } from '@genkit-ai/google-genai';` instead. Replace model constants with e.g. vertexAI.model('gemini-2.5-pro')\n */\nexport const vertexAI = vertexAIPlugin as VertexAIPlugin;\n// provide generic implementation for the model function overloads.\n(vertexAI as any).model = (\n name: string,\n config?: any\n): ModelReference<z.ZodTypeAny> => {\n warnDeprecated();\n if (name.startsWith('imagen')) {\n return modelRef({\n name: `vertexai/${name}`,\n config,\n configSchema: ImagenConfigSchema,\n });\n }\n return modelRef({\n name: `vertexai/${name}`,\n config,\n configSchema: GeminiConfigSchema,\n });\n};\nvertexAI.embedder = (\n name: string,\n config?: VertexEmbeddingConfig\n): EmbedderReference<typeof VertexEmbeddingConfigSchema> => {\n warnDeprecated();\n return embedderRef({\n name: `vertexai/${name}`,\n config,\n configSchema: VertexEmbeddingConfigSchema,\n });\n};\n\nexport default vertexAI;\n"],"mappings":"AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsBA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AACP,SAAS,oBAAuC;AAEhD,SAAS,wBAAwB;AAEjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;A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