From 4eea793943499c6ed0b46e08a317266df8982dde Mon Sep 17 00:00:00 2001 From: Arnaud Sahuguet Date: Fri, 6 Sep 2024 16:15:52 -0400 Subject: [PATCH] Update Bedrock.ts to support apiBase and headers from config.json file We extend the code for the BedrockRuntimeClient to - support `endpoint` in the constructor in order to pass an `apiBase` parameter - support new headers defined on the config.json file, using `middlewareStack.add` --- core/llm/llms/Bedrock.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index 415ec0f10..74c8f29e6 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -49,8 +49,10 @@ class Bedrock extends BaseLLM { options: CompletionOptions, ): AsyncGenerator { const credentials = await this._getCredentials(); + const client = new BedrockRuntimeClient({ region: this.region, + endpoint: this.apiBase, credentials: { accessKeyId: credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, @@ -58,6 +60,24 @@ class Bedrock extends BaseLLM { }, }); + let config_headers = this.requestOptions && this.requestOptions.headers ? this.requestOptions.headers : {}; + // AWS SigV4 requires strict canonicalization of headers. + // DO NOT USE "_" in your header name. It will return an error like below. + // "The request signature we calculated does not match the signature you provided." + + client.middlewareStack.add( + (next) => async (args) => { + args.request.headers = { + ...args.request.headers, + ...config_headers + }; + return next(args); + }, + { + step: 'build' + } + ); + const input = this._generateConverseInput(messages, options); const command = new ConverseStreamCommand(input); const response = await client.send(command);