Skip to content

Commit

Permalink
feat: Integrate AWS Bedrock with Claude 3.5 Sonnet, Claude 3 Sonnet, …
Browse files Browse the repository at this point in the history
…and Claude 3.5 Haiku
  • Loading branch information
kunjabijukchhe committed Jan 1, 2025
1 parent d2ba8d3 commit 4961d70
Show file tree
Hide file tree
Showing 7 changed files with 1,172 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ XAI_API_KEY=
# You only need this environment variable set if you want to use Perplexity models
PERPLEXITY_API_KEY=

# Get your AWS Bedrock configuration as a JSON string.
# The JSON should include the following keys:
# - region: The AWS region where Bedrock is available.
# - accessKeyId: Your AWS access key ID.
# - secretAccessKey: Your AWS secret access key.
# - sessionToken (optional): Temporary session token if using an IAM role or temporary credentials.
# Example JSON:
# {"region": "us-east-1", "accessKeyId": "yourAccessKeyId", "secretAccessKey": "yourSecretAccessKey", "sessionToken": "yourSessionToken"}
AWS_BEDROCK_CONFIG=

# Include this environment variable if you want more logging for debugging locally
VITE_LOG_LEVEL=debug

Expand Down
1 change: 1 addition & 0 deletions app/components/settings/data/DataTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const API_KEY_PROVIDERS = [
'Perplexity',
'Cohere',
'AzureOpenAI',
'AmazonBedrock',
] as const;

interface ApiKeys {
Expand Down
82 changes: 82 additions & 0 deletions app/lib/modules/llm/providers/amazon-bedrock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { BaseProvider } from '~/lib/modules/llm/base-provider';
import type { ModelInfo } from '~/lib/modules/llm/types';
import type { LanguageModelV1 } from 'ai';
import type { IProviderSetting } from '~/types/model';
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';

interface AWSBedRockConfig {
region: string;
accessKeyId: string;
secretAccessKey: string;
sessionToken?: string;
}

export default class AmazonBedrockProvider extends BaseProvider {
name = 'AmazonBedrock';
getApiKeyLink = 'https://console.aws.amazon.com/iam/home';

config = {
apiTokenKey: 'AWS_BEDROCK_CONFIG',
};

staticModels: ModelInfo[] = [
{
name: 'anthropic.claude-3-5-sonnet-20240620-v1:0',
label: 'Claude 3.5 Sonnet (Bedrock)',
provider: 'AmazonBedrock',
maxTokenAllowed: 8000,
},
{
name: 'anthropic.claude-3-sonnet-20240229-v1:0',
label: 'Claude 3 Sonnet (Bedrock)',
provider: 'AmazonBedrock',
maxTokenAllowed: 8000,
},
{
name: 'anthropic.claude-3-5-haiku-20241022-v1:0',
label: 'Claude 3.5 Haiku (Bedrock)',
provider: 'AmazonBedrock',
maxTokenAllowed: 8000,
},
];

getModelInstance(options: {
model: string;
serverEnv: any;
apiKeys?: Record<string, string>;
providerSettings?: Record<string, IProviderSetting>;
}): LanguageModelV1 {
const { model, serverEnv, apiKeys, providerSettings } = options;

const { apiKey } = this.getProviderBaseUrlAndKey({
apiKeys,
providerSettings: providerSettings?.[this.name],
serverEnv: serverEnv as any,
defaultBaseUrlKey: '',
defaultApiTokenKey: 'AWS_BEDROCK_CONFIG',
});

if (!apiKey) {
throw new Error(`Missing API key for ${this.name} provider`);
}

const parsedConfig: AWSBedRockConfig = JSON.parse(apiKey);

const { region, accessKeyId, secretAccessKey } = parsedConfig;

if (!region || !accessKeyId || !secretAccessKey) {
throw new Error(
`The provided API configuration is incomplete. Ensure that 'region', 'accessKeyId', and 'secretAccessKey' are included.`,
);
}

const bedrock = createAmazonBedrock({
region: parsedConfig.region,
accessKeyId: parsedConfig.accessKeyId,
secretAccessKey: parsedConfig.secretAccessKey,
sessionToken: parsedConfig.sessionToken,
});

return bedrock(model);
}
}
2 changes: 2 additions & 0 deletions app/lib/modules/llm/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import PerplexityProvider from './providers/perplexity';
import TogetherProvider from './providers/together';
import XAIProvider from './providers/xai';
import HyperbolicProvider from './providers/hyperbolic';
import AmazonBedrockProvider from './providers/amazon-bedrock';

export {
AnthropicProvider,
Expand All @@ -32,4 +33,5 @@ export {
XAIProvider,
TogetherProvider,
LMStudioProvider,
AmazonBedrockProvider,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@ai-sdk/google": "^0.0.52",
"@ai-sdk/mistral": "^0.0.43",
"@ai-sdk/openai": "^0.0.66",
"@ai-sdk/amazon-bedrock": "1.0.6",
"@codemirror/autocomplete": "^6.18.3",
"@codemirror/commands": "^6.7.1",
"@codemirror/lang-cpp": "^6.0.2",
Expand Down
Loading

0 comments on commit 4961d70

Please sign in to comment.