Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] AmazonBedrockEmbeddingFunction for JS #1659

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@
"node": ">=14.17.0"
},
"dependencies": {
"isomorphic-fetch": "^3.0.0",
"cliui": "^8.0.1"
"cliui": "^8.0.1",
"isomorphic-fetch": "^3.0.0"
},
"peerDependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.496.0",
"@google/generative-ai": "^0.1.1",
"cohere-ai": "^5.0.0 || ^6.0.0 || ^7.0.0",
"openai": "^3.0.0 || ^4.0.0"
},
"peerDependenciesMeta": {
"@aws-sdk/client-bedrock-runtime": {
tazarov marked this conversation as resolved.
Show resolved Hide resolved
"optional": true
},
"@google/generative-ai": {
"optional": true
},
Expand Down
71 changes: 71 additions & 0 deletions clients/js/src/embeddings/AmazonBedrockEmbeddingFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { IEmbeddingFunction } from "./IEmbeddingFunction";

let amazonBedrockApi: any;

export class AmazonBedrockEmbeddingFunction implements IEmbeddingFunction {
private model: string;
private readonly configuration: any; // Assume this is BedrockRuntimeClientConfig
private amazonBedrockApi?: any;
private invokeCommand?: any;

constructor({ config, model }: { config: any; model?: string }) {
this.configuration = config;
this.model = model || "amazon.titan-embed-text-v1";
}

private async loadClient() {
if (this.amazonBedrockApi) return;
try {
// eslint-disable-next-line global-require,import/no-extraneous-dependencies
const { amazonBedrock } = await AmazonBedrockEmbeddingFunction.import();
amazonBedrockApi = new amazonBedrock.BedrockRuntimeClient(
this.configuration
);
this.invokeCommand = amazonBedrock.InvokeModelCommand;
} catch (_a) {
// @ts-ignore
if (_a.code === "MODULE_NOT_FOUND") {
throw new Error(
"Please install the @aws-sdk/client-bedrock-runtime package to use the AmazonBedrockEmbeddingFunction, `npm install -S @aws-sdk/client-bedrock-runtime`"
);
}
throw _a; // Re-throw other errors
}
this.amazonBedrockApi = amazonBedrockApi;
}

public async generate(texts: string[]): Promise<number[][]> {
await this.loadClient();
const td = new TextDecoder();
const embeddings: number[][] = [];
texts.forEach(async text => {
const input = {
modelId: this.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({ inputText: text }),
}
const command = new this.invokeCommand(input);
const response = await this.amazonBedrockApi.send(command);
const parsedBody = JSON.parse(td.decode(response.body));
embeddings.push(parsedBody.embedding);
});
return embeddings;
}

/** @ignore */
static async import(): Promise<{
// @ts-ignore
amazonBedrock: typeof import("@aws-sdk/client-bedrock-runtime");
}> {
try {
// @ts-ignore
const { default: amazonBedrock } = await import("@aws-sdk/client-bedrock-runtime");
return { amazonBedrock };
} catch (_a) {
throw new Error(
"Please install the @aws-sdk/client-bedrock-runtime package to use the AmazonBedrockEmbeddingFunction, `npm install -S @aws-sdk/client-bedrock-runtime`"
);
}
}
}
1 change: 1 addition & 0 deletions clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { DefaultEmbeddingFunction } from './embeddings/DefaultEmbeddingFunction'
export { HuggingFaceEmbeddingServerFunction } from './embeddings/HuggingFaceEmbeddingServerFunction';
export { JinaEmbeddingFunction } from './embeddings/JinaEmbeddingFunction';
export { GoogleGenerativeAiEmbeddingFunction } from './embeddings/GoogleGeminiEmbeddingFunction';
export { AmazonBedrockEmbeddingFunction } from './embeddings/AmazonBedrockEmbeddingFunction';

export {
IncludeEnum,
Expand Down
Loading