genkitx-aws-bedrock
Version:
Firebase Genkit AI framework plugin for AWS Bedrock APIs.
128 lines • 5.22 kB
JavaScript
;
/**
* Copyright 2024 The Fire Company
*
* 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 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SUPPORTED_EMBEDDING_MODELS = exports.cohereEmbedMultilingualV3 = exports.cohereEmbedEnglishV3 = exports.amazonTitanEmbedTextG1V1 = exports.amazonTitanEmbedMultimodalV2 = exports.amazonTitanEmbedTextV2 = exports.TextEmbeddingInputSchema = exports.TextEmbeddingConfigSchema = void 0;
exports.awsBedrockEmbedder = awsBedrockEmbedder;
const genkit_1 = require("genkit");
const zod_1 = require("zod");
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
exports.TextEmbeddingConfigSchema = zod_1.z.object({
dimensions: zod_1.z.number().optional(),
});
exports.TextEmbeddingInputSchema = zod_1.z.string();
exports.amazonTitanEmbedTextV2 = (0, genkit_1.embedderRef)({
name: "aws-bedrock/amazon.titan-embed-text-v2:0",
configSchema: exports.TextEmbeddingConfigSchema,
info: {
dimensions: 1024,
label: "Amazon - titan-embed-text-v2:0",
supports: {
input: ["text"],
},
},
});
exports.amazonTitanEmbedMultimodalV2 = (0, genkit_1.embedderRef)({
name: "aws-bedrock/amazon.titan-embed-image-v1",
configSchema: exports.TextEmbeddingConfigSchema,
info: {
dimensions: 1024,
label: "Amazon - titan-embed-multimodal-v2:0",
supports: {
input: ["text", "image"],
},
},
});
exports.amazonTitanEmbedTextG1V1 = (0, genkit_1.embedderRef)({
name: "aws-bedrock/amazon.titan-embed-text-v1",
configSchema: exports.TextEmbeddingConfigSchema,
info: {
dimensions: 1536,
label: "Amazon - titan-embed-text-v1",
supports: {
input: ["text"],
},
},
});
exports.cohereEmbedEnglishV3 = (0, genkit_1.embedderRef)({
name: "aws-bedrock/cohere.embed-english-v3",
configSchema: exports.TextEmbeddingConfigSchema,
info: {
dimensions: 1024,
label: "Cohere - embed-english-v3",
supports: {
input: ["text"],
},
},
});
exports.cohereEmbedMultilingualV3 = (0, genkit_1.embedderRef)({
name: "aws-bedrock/cohere.embed-multilingual-v3",
configSchema: exports.TextEmbeddingConfigSchema,
info: {
dimensions: 1024,
label: "Cohere - embed-multilingual-v3",
supports: {
input: ["text"],
},
},
});
exports.SUPPORTED_EMBEDDING_MODELS = {
"amazon.titan-embed-text-v2:0": exports.amazonTitanEmbedTextV2,
"amazon.titan-embed-image-v1": exports.amazonTitanEmbedMultimodalV2,
"amazon.titan-embed-text-v1": exports.amazonTitanEmbedTextG1V1,
"cohere.embed-english-v3": exports.cohereEmbedEnglishV3,
"cohere.embed-multilingual-v3": exports.cohereEmbedMultilingualV3,
};
function awsBedrockEmbedder(name, ai, client) {
const model = exports.SUPPORTED_EMBEDDING_MODELS[name];
return ai.defineEmbedder({
info: model.info,
configSchema: exports.TextEmbeddingConfigSchema,
name: model.name,
}, (input, options) => __awaiter(this, void 0, void 0, function* () {
const body = {
modelId: name,
contentType: "application/json",
body: JSON.stringify({
inputText: input.map((d) => d.text).join(","),
dimensions: options === null || options === void 0 ? void 0 : options.dimensions,
}),
};
const command = new client_bedrock_runtime_1.InvokeModelCommand(body);
const response = (yield 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