Skip to content

feat(bedrock-sdk): allow for custom provider chain resolvers #474

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions packages/bedrock-sdk/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import assert from 'assert';
import { SignatureV4 } from '@smithy/signature-v4';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { HttpRequest } from '@smithy/protocol-http';
import { Sha256 } from '@aws-crypto/sha256-js';
import type { RequestInit } from '@anthropic-ai/sdk/_shims/index';
import { AwsCredentialIdentityProvider } from '@smithy/types';

type AuthProps = {
url: string;
regionName: string;
awsAccessKey: string | null | undefined;
awsSecretKey: string | null | undefined;
awsSessionToken: string | null | undefined;
providerChainResolver?: (() => Promise<AwsCredentialIdentityProvider>) | null;
};

const DEFAULT_PROVIDER_CHAIN_RESOLVER: () => Promise<AwsCredentialIdentityProvider> = () => import('@aws-sdk/credential-providers').then(({ fromNodeProviderChain }) => fromNodeProviderChain());

export const getAuthHeaders = async (req: RequestInit, props: AuthProps): Promise<Record<string, string>> => {
assert(req.method, 'Expected request method property to be set');

const providerChain = fromNodeProviderChain();
const providerChain = await (props.providerChainResolver ? props.providerChainResolver() : DEFAULT_PROVIDER_CHAIN_RESOLVER());

const credentials = await withTempEnv(
() => {
Expand Down
8 changes: 8 additions & 0 deletions packages/bedrock-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as Resources from '@anthropic-ai/sdk/resources/index';
import * as API from '@anthropic-ai/sdk/index';
import { getAuthHeaders } from './auth';
import { Stream } from './streaming';
import { AwsCredentialIdentityProvider } from '@smithy/types';

const DEFAULT_VERSION = 'bedrock-2023-05-31';
const MODEL_ENDPOINTS = new Set<string>(['/v1/complete', '/v1/messages']);
Expand All @@ -16,6 +17,8 @@ export type ClientOptions = Omit<API.ClientOptions, 'apiKey' | 'authToken'> & {
*/
awsRegion?: string | undefined;
awsSessionToken?: string | null | undefined;

providerChainResolver?: (() => Promise<AwsCredentialIdentityProvider>) | null;
};

/** API Client for interfacing with the Anthropic Bedrock API. */
Expand All @@ -24,6 +27,7 @@ export class AnthropicBedrock extends Core.APIClient {
awsAccessKey: string | null;
awsRegion: string;
awsSessionToken: string | null;
providerChainResolver: (() => Promise<AwsCredentialIdentityProvider>) | null;

private _options: ClientOptions;

Expand All @@ -48,13 +52,15 @@ export class AnthropicBedrock extends Core.APIClient {
awsAccessKey = null,
awsRegion = Core.readEnv('AWS_REGION') ?? 'us-east-1',
awsSessionToken = null,
providerChainResolver = null,
...opts
}: ClientOptions = {}) {
const options: ClientOptions = {
awsSecretKey,
awsAccessKey,
awsRegion,
awsSessionToken,
providerChainResolver,
...opts,
baseURL: baseURL || `https://bedrock-runtime.${awsRegion}.amazonaws.com`,
};
Expand All @@ -72,6 +78,7 @@ export class AnthropicBedrock extends Core.APIClient {
this.awsAccessKey = awsAccessKey;
this.awsRegion = awsRegion;
this.awsSessionToken = awsSessionToken;
this.providerChainResolver = providerChainResolver;
}

messages: Resources.Messages = new Resources.Messages(this);
Expand Down Expand Up @@ -105,6 +112,7 @@ export class AnthropicBedrock extends Core.APIClient {
awsAccessKey: this.awsAccessKey,
awsSecretKey: this.awsSecretKey,
awsSessionToken: this.awsSessionToken,
providerChainResolver: this.providerChainResolver
});
request.headers = { ...request.headers, ...headers };
}
Expand Down