-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrate AWS Bedrock with Claude 3.5 Sonnet, Claude 3 Sonnet, …
…and Claude 3.5 Haiku
- Loading branch information
1 parent
d2ba8d3
commit 4961d70
Showing
7 changed files
with
1,172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.