-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathbedrock-setting-util.ts
More file actions
170 lines (148 loc) · 6.6 KB
/
bedrock-setting-util.ts
File metadata and controls
170 lines (148 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { BedrockClient, ListInferenceProfilesCommand, ListFoundationModelsCommand } from '@aws-sdk/client-bedrock'
import type { ModelProvider, ProviderModelInfo, ProviderSettings, SessionType } from 'src/shared/types'
import { ModelProviderEnum } from 'src/shared/types'
import BaseConfig from './base-config'
import { ModelSettingUtil } from './interface'
export default class BedrockSettingUtil extends BaseConfig implements ModelSettingUtil {
public provider: ModelProvider = ModelProviderEnum.Bedrock
async getCurrentModelDisplayName(
model: string,
sessionType: SessionType,
providerSettings?: ProviderSettings
): Promise<string> {
return `AWS Bedrock (${providerSettings?.models?.find((m) => m.modelId === model)?.nickname || model})`
}
protected async listProviderModels(providerSettings?: ProviderSettings): Promise<ProviderModelInfo[]> {
try {
// v3.0.73 only supports: accessKeyId, secretAccessKey, sessionToken (optional)
// Create Bedrock client with credentials from settings
if (!providerSettings?.awsAccessKeyId || !providerSettings?.awsSecretAccessKey) {
console.warn('Bedrock: AWS Access Key ID and Secret Access Key are required')
return []
}
const clientConfig: any = {
region: providerSettings.awsRegion || 'us-east-1',
credentials: {
accessKeyId: providerSettings.awsAccessKeyId,
secretAccessKey: providerSettings.awsSecretAccessKey,
},
}
if (providerSettings.awsSessionToken) {
clientConfig.credentials.sessionToken = providerSettings.awsSessionToken
}
const client = new BedrockClient(clientConfig)
// Step 1: Fetch all foundation models to get capabilities info
const foundationModelsCommand = new ListFoundationModelsCommand({})
const foundationModelsResponse = await client.send(foundationModelsCommand)
// Build a map of foundation model ID to capabilities and limits
const modelCapabilitiesMap = new Map<
string,
{
hasVision: boolean
hasToolUse: boolean
hasReasoning: boolean
maxOutput: number
contextWindow: number
}
>()
foundationModelsResponse.modelSummaries?.forEach((model) => {
// Include models that support streaming and are ACTIVE/LEGACY
// Note: Claude 4+ models have inferenceTypesSupported: ["INFERENCE_PROFILE"] instead of ["ON_DEMAND"]
if (
model.modelId &&
model.responseStreamingSupported === true &&
(model.modelLifecycle?.status === 'ACTIVE' || model.modelLifecycle?.status === 'LEGACY')
) {
// Use detailed info from 'converse' field if available
const hasImageInput =
model.inputModalities?.includes('IMAGE') ||
(model as any).converse?.userImageTypesSupported?.length > 0 ||
false
const hasTextOutput = model.outputModalities?.includes('TEXT') || false
const hasToolUse = hasTextOutput // Most text models support tool use
const hasReasoning =
(model as any).converse?.reasoningSupported !== undefined ||
model.modelId.includes('sonnet-4') ||
model.modelId.includes('opus-4') ||
model.modelId.includes('claude-3-7')
// Extract token limits from converse field
const maxTokens = (model as any).converse?.maxTokensMaximum || 8_192
// Parse context window from description or use defaults
let contextWindow = 200_000 // Default
const maxContextStr = (model as any).description?.maxContextWindow
if (maxContextStr === '1M') {
contextWindow = 1_000_000
} else if (model.modelId.includes('claude-4') || model.modelId.includes('claude-3-7')) {
contextWindow = 200_000
} else if (model.modelId.includes('nova')) {
contextWindow = 300_000
}
modelCapabilitiesMap.set(model.modelId, {
hasVision: hasImageInput && hasTextOutput,
hasToolUse,
hasReasoning,
maxOutput: maxTokens,
contextWindow,
})
}
})
// Step 2: Fetch all inference profiles with pagination
const allProfiles: any[] = []
let nextToken: string | undefined = undefined
do {
const command: ListInferenceProfilesCommand = new ListInferenceProfilesCommand({
maxResults: 1000, // Max allowed by AWS API
nextToken,
})
const response: Awaited<ReturnType<typeof client.send>> = await client.send(command)
if (response.inferenceProfileSummaries) {
allProfiles.push(...response.inferenceProfileSummaries)
}
nextToken = (response as any).nextToken
} while (nextToken)
// Step 3: Convert inference profiles to ProviderModelInfo
const models: ProviderModelInfo[] = allProfiles
.filter((profile) => {
// Only include ACTIVE profiles
return profile.status === 'ACTIVE'
})
.map((profile) => {
// Extract foundation model ID from the first model ARN
// ARN format: arn:aws:bedrock:region::foundation-model/model.id
const firstModelArn = profile.models?.[0]?.modelArn
let foundationModelId: string | undefined
if (firstModelArn) {
const match = firstModelArn.match(/foundation-model\/(.+)$/)
if (match) {
foundationModelId = match[1]
}
}
// Get capabilities and limits from foundation model
const capabilities: ('vision' | 'tool_use' | 'reasoning')[] = []
let contextWindow = 200_000 // Default
let maxOutput = 8_192 // Default
if (foundationModelId && modelCapabilitiesMap.has(foundationModelId)) {
const caps = modelCapabilitiesMap.get(foundationModelId)!
if (caps.hasVision) capabilities.push('vision')
if (caps.hasToolUse) capabilities.push('tool_use')
if (caps.hasReasoning) capabilities.push('reasoning')
contextWindow = caps.contextWindow
maxOutput = caps.maxOutput
}
return {
modelId: profile.inferenceProfileId!,
nickname: profile.inferenceProfileName || profile.inferenceProfileId,
type: 'chat' as const,
capabilities,
contextWindow,
maxOutput,
}
})
return models
} catch (error) {
console.error('Failed to list Bedrock models:', error)
// Return empty array on error, let the UI show default models
return []
}
}
}