diff --git a/langchain/src/embeddings/bedrock.ts b/langchain/src/embeddings/bedrock.ts index 5e0bbe9ce5a3..3f6332e73783 100644 --- a/langchain/src/embeddings/bedrock.ts +++ b/langchain/src/embeddings/bedrock.ts @@ -57,27 +57,40 @@ export class BedrockEmbeddings /** @deprecated */ protected async _embedText(text: string): Promise { - // replace newlines, which can negatively affect performance. - 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", - }) - ); + return this.caller.call(async () => { + try { + // replace newlines, which can negatively affect performance. + const cleanedText = inputText.replace(/\n/g, " "); - try { - const body = new TextDecoder().decode(res.body); + const res = await this.client.send( + new InvokeModelCommand({ + modelId: this.model, + body: JSON.stringify({ + inputText: cleanedText, + }), + contentType: "application/json", + accept: "application/json", + }) + ); - return JSON.parse(body).embedding; - } catch (e) { - throw new Error("An invalid response was returned by Bedrock."); - } + const body = new TextDecoder().decode(res.body); + return JSON.parse(body).embedding; + } catch (e) { + console.error({ + error: e, + }); + // eslint-disable-next-line no-instanceof/no-instanceof + 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" + ); + } + }); } /**