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`
This commit is contained in:
Arnaud Sahuguet 2024-09-06 16:15:52 -04:00 • committed by GitHub
parent d60617fa34
commit 4eea793943
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -49,8 +49,10 @@ class Bedrock extends BaseLLM {
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
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);