-
Notifications
You must be signed in to change notification settings - Fork 2k
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
No inputImage
property on Bedrock embedding
#5971
Comments
Hey there, @bmichotte! I'm here to help you with any bugs, questions, or contributions you might have. Let's tackle this together! To address the issue of embedding images using the
Here is the modified code: import {
BedrockRuntimeClient,
InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
import type { CredentialType } from "../utils/bedrock/index.js";
/**
* Interface that extends EmbeddingsParams and defines additional
* parameters specific to the BedrockEmbeddings class.
*/
export interface BedrockEmbeddingsParams extends EmbeddingsParams {
model?: string;
client?: BedrockRuntimeClient;
region?: string;
credentials?: CredentialType;
}
/**
* Class that extends the Embeddings class and provides methods for
* generating embeddings using the Bedrock API.
*/
export class BedrockEmbeddings
extends Embeddings
implements BedrockEmbeddingsParams
{
model: string;
client: BedrockRuntimeClient;
batchSize = 512;
constructor(fields?: BedrockEmbeddingsParams) {
super(fields ?? {});
this.model = fields?.model ?? "amazon.titan-embed-text-v1";
this.client =
fields?.client ??
new BedrockRuntimeClient({
region: fields?.region,
credentials: fields?.credentials,
});
}
protected async _embedText(text: string): Promise<number[]> {
return this.caller.call(async () => {
try {
const cleanedText = text.replace(/\n/g, " ");
const res = await this.client.send(
new InvokeModelCommand({
modelId: this.model,
body: JSON.stringify({
inputText: cleanedText,
}),
contentType: "application/json",
accept: "application/json",
})
);
const body = new TextDecoder().decode(res.body);
return JSON.parse(body).embedding;
} catch (e) {
console.error({ error: e });
if (e instanceof Error) {
throw new Error(
`An error occurred while embedding documents with Bedrock: ${e.message}`
);
}
throw new Error(
"An error occurred while embedding documents with Bedrock"
);
}
});
}
protected async _embedImage(imageBase64: string): Promise<number[]> {
return this.caller.call(async () => {
try {
const res = await this.client.send(
new InvokeModelCommand({
modelId: this.model,
body: JSON.stringify({
inputImage: imageBase64,
}),
contentType: "application/json",
accept: "application/json",
})
);
const body = new TextDecoder().decode(res.body);
return JSON.parse(body).embedding;
} catch (e) {
console.error({ error: e });
if (e instanceof Error) {
throw new Error(
`An error occurred while embedding images with Bedrock: ${e.message}`
);
}
throw new Error(
"An error occurred while embedding images with Bedrock"
);
}
});
}
embedQuery(document: string): Promise<number[]> {
return this.caller.callWithOptions(
{},
this._embedText.bind(this),
document
);
}
embedImage(imageBase64: string): Promise<number[]> {
return this.caller.callWithOptions(
{},
this._embedImage.bind(this),
imageBase64
);
}
async embedDocuments(documents: string[]): Promise<number[][]> {
return Promise.all(documents.map((document) => this._embedText(document)));
}
} In this code, the |
Checked other resources
Example Code
Based on https://js.langchain.com/v0.2/docs/integrations/text_embedding/bedrock
Error Message and Stack Trace (if applicable)
No response
Description
Bedrock gives the possibility to get embeddings for images using the amazon.titan-embed-image-v1 model
Atm, there's no way to give that
inputImage
On https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/embeddings/bedrock.ts we could add a new parameter image to
embedQuery
such asand modify the
_embedText
forSystem Info
langchain 0.2.8
platform mac
Node version : 20.12.2
pnpm version : 9.4.0
The text was updated successfully, but these errors were encountered: