UNPKG

genkitx-aws-bedrock

Version:
146 lines 4.94 kB
/** * Copyright 2026 Xavier Portilla Edo * Copyright 2026 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. */ /* eslint-disable @typescript-eslint/no-explicit-any */ import { embedderRef as createEmbedderRef } from "genkit"; import { z } from "zod"; import { embedder } from "genkit/plugin"; import { InvokeModelCommand, } from "@aws-sdk/client-bedrock-runtime"; export const TextEmbeddingConfigSchema = z.object({ dimensions: z.number().optional(), }); export const TextEmbeddingInputSchema = z.string(); export const amazonNova2MultimodalEmbeddingsV1 = createEmbedderRef({ name: "aws-bedrock/amazon.nova-2-multimodal-embeddings-v1:0", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Amazon - nova-2-multimodal-embeddings-v1:0", supports: { input: ["text", "image", "video"], multilingual: true }, }, }); export const amazonTitanEmbedTextV2 = createEmbedderRef({ name: "aws-bedrock/amazon.titan-embed-text-v2:0", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Amazon - titan-embed-text-v2:0", supports: { input: ["text"], }, }, }); export const amazonTitanEmbedMultimodalV2 = createEmbedderRef({ name: "aws-bedrock/amazon.titan-embed-image-v1", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Amazon - titan-embed-multimodal-v2:0", supports: { input: ["text", "image"], }, }, }); export const amazonTitanEmbedTextG1V1 = createEmbedderRef({ name: "aws-bedrock/amazon.titan-embed-text-v1", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1536, label: "Amazon - titan-embed-text-v1", supports: { input: ["text"], }, }, }); export const cohereEmbedV4 = createEmbedderRef({ name: "aws-bedrock/cohere.embed-v4:0", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Cohere - Embed v4", supports: { input: ["text", "image"], multilingual: true }, }, }); export const cohereEmbedEnglishV3 = createEmbedderRef({ name: "aws-bedrock/cohere.embed-english-v3", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Cohere - embed-english-v3", supports: { input: ["text"], }, }, }); export const cohereEmbedMultilingualV3 = createEmbedderRef({ name: "aws-bedrock/cohere.embed-multilingual-v3", configSchema: TextEmbeddingConfigSchema, info: { dimensions: 1024, label: "Cohere - embed-multilingual-v3", supports: { input: ["text"], }, }, }); export const SUPPORTED_EMBEDDING_MODELS = { "amazon.titan-embed-text-v2:0": amazonTitanEmbedTextV2, "amazon.titan-embed-image-v1": amazonTitanEmbedMultimodalV2, "amazon.titan-embed-text-v1": amazonTitanEmbedTextG1V1, "cohere.embed-english-v3": cohereEmbedEnglishV3, "cohere.embed-multilingual-v3": cohereEmbedMultilingualV3, "cohere.embed-v4:0": cohereEmbedV4, "amazon.nova-2-multimodal-embeddings-v1:0": amazonNova2MultimodalEmbeddingsV1, }; export function awsBedrockEmbedder(name, client) { const modelRef = SUPPORTED_EMBEDDING_MODELS[name]; if (!modelRef) throw new Error(`Unsupported model: ${name}`); return embedder({ info: modelRef.info, configSchema: TextEmbeddingConfigSchema, name: modelRef.name, }, async (request) => { const { input, options } = request; const body = { modelId: name, contentType: "application/json", body: JSON.stringify({ inputText: input.map((d) => d.text).join(","), dimensions: options?.dimensions, }), }; const command = new InvokeModelCommand(body); const response = (await client.send(command)); const embeddings = new TextDecoder().decode(response.body) ? JSON.parse(new TextDecoder().decode(response.body)) : []; return { embeddings: [ { embedding: embeddings.embedding, }, ], }; }); } //# sourceMappingURL=aws_bedrock_embedders.js.map