-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathfeaturedModels.ts
More file actions
228 lines (203 loc) · 6.45 KB
/
featuredModels.ts
File metadata and controls
228 lines (203 loc) · 6.45 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { InvokeModelCommandInput } from '@aws-sdk/client-bedrock-runtime';
/** Maps AWS region to geography prefix for inference profile IDs (us/eu/global). */
export function getInferenceProfilePrefix(region: string): 'us' | 'eu' | 'global' {
if (region.startsWith('eu-')) return 'eu';
if (region.startsWith('us-') || region.startsWith('ca-')) return 'us';
return 'global';
}
export interface BedrockModel {
id: string;
name: string;
/** When set, used as modelId for InvokeModel (inference profile ID); otherwise id is used. Backward compat: existing models omit this. */
getInvokeId?: (region: string) => string;
invokeCommand: (
systemPrompt: string,
prompt: string,
maxTokens?: number,
region?: string
) => InvokeModelCommandInput;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parseResponse: (response: any) => string;
}
interface ContentBlockDeltaMsg {
type: 'content_block_delta';
delta: {
text: string;
type: 'text_delta';
};
}
interface ContentBlockStartMsg {
type: 'content_block_start';
content_block: {
text: string;
type: 'text';
};
}
class ClaudeModel implements BedrockModel {
id: string;
name: string;
getInvokeId?: (region: string) => string;
constructor(id: string, name: string, getInvokeId?: (region: string) => string) {
this.id = id;
this.name = name;
this.getInvokeId = getInvokeId;
}
invokeCommand(
systemPrompt: string,
prompt: string,
maxTokens?: number,
region?: string
): InvokeModelCommandInput {
const modelId = this.getInvokeId && region !== undefined ? this.getInvokeId(region) : this.id;
const messages = [
{
role: 'user',
content: [
{
type: 'text',
text: prompt || 'hi',
},
],
},
];
return {
modelId,
contentType: 'application/json',
body: JSON.stringify({
anthropic_version: 'bedrock-2023-05-31',
max_tokens: maxTokens || 128,
system: systemPrompt,
messages,
}),
};
}
parseResponse(response: ContentBlockDeltaMsg | ContentBlockStartMsg) {
console.log('res', response);
if (response.type == 'content_block_delta') return response.delta.text;
return '';
}
}
class LlamaModel implements BedrockModel {
id: string;
name: string;
constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
invokeCommand(
systemPrompt: string,
prompt: string,
maxTokens?: number,
region?: string
): InvokeModelCommandInput {
void region; // optional for interface compat; Llama uses direct model ID only
const completePrompt = `
${systemPrompt}
Human: ${prompt}
Assistant:`;
return {
modelId: this.id,
contentType: 'application/json',
body: JSON.stringify({
prompt: completePrompt,
...(maxTokens && { max_gen_len: maxTokens }),
}),
};
}
parseResponse(response: { generation: string }) {
return response.generation;
}
}
class MistralModel implements BedrockModel {
id: string;
name: string;
constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
invokeCommand(
systemPrompt: string,
prompt: string,
maxTokens?: number,
region?: string
): InvokeModelCommandInput {
void region; // optional for interface compat; Mistral uses direct model ID only
const completePrompt = `<s>[INST] ${systemPrompt} [/INST]
[INST] ${prompt} [/INST]`;
return {
modelId: this.id,
contentType: 'application/json',
body: JSON.stringify({
prompt: completePrompt,
...(maxTokens && { max_tokens: maxTokens }),
}),
};
}
parseResponse(response: { outputs: { text: string }[] }) {
return response['outputs'][0]['text'];
}
}
/** Inference profile IDs for models that require them (on-demand no longer supports raw model ID). See AWS docs: inference-profiles-use, inference-profiles-support. */
function inferenceProfileId(region: string, profileIdByPrefix: Record<string, string>): string {
const prefix = getInferenceProfilePrefix(region);
return profileIdByPrefix[prefix] ?? profileIdByPrefix['us'] ?? profileIdByPrefix['global'];
}
export const defaultModelId = 'anthropic.claude-sonnet-4-6';
export const defaultModelDisplayName = 'Anthropic Claude Sonnet 4.6';
export const featuredModels: BedrockModel[] = [
// Modern Claude models (use inference profiles; raw model ID causes ValidationException on-demand)
new ClaudeModel(
defaultModelId,
defaultModelDisplayName,
() => 'global.anthropic.claude-sonnet-4-6'
),
new ClaudeModel(
'anthropic.claude-sonnet-4-5-20250929-v1:0',
'Anthropic Claude Sonnet 4.5',
(region) =>
inferenceProfileId(region, {
global: 'global.anthropic.claude-sonnet-4-5-20250929-v1:0',
us: 'us.anthropic.claude-sonnet-4-5-20250929-v1:0',
eu: 'eu.anthropic.claude-sonnet-4-5-20250929-v1:0',
})
),
new ClaudeModel(
'anthropic.claude-sonnet-4-20250514-v1:0',
'Anthropic Claude Sonnet 4',
(region) =>
inferenceProfileId(region, {
global: 'global.anthropic.claude-sonnet-4-20250514-v1:0',
us: 'us.anthropic.claude-sonnet-4-20250514-v1:0',
eu: 'eu.anthropic.claude-sonnet-4-20250514-v1:0',
})
),
new ClaudeModel(
'anthropic.claude-3-5-haiku-20241022-v1:0',
'Anthropic Claude 3.5 Haiku',
(region) =>
inferenceProfileId(region, {
us: 'us.anthropic.claude-3-5-haiku-20241022-v1:0',
eu: 'eu.anthropic.claude-3-5-haiku-20241022-v1:0',
})
),
new ClaudeModel('anthropic.claude-3-haiku-20240307-v1:0', 'Anthropic Claude 3 Haiku', (region) =>
inferenceProfileId(region, {
us: 'us.anthropic.claude-3-haiku-20240307-v1:0',
eu: 'eu.anthropic.claude-3-haiku-20240307-v1:0',
})
),
// Existing models that require inference profiles (v3 Sonnet) or direct model ID (v2.1, Instant)
new ClaudeModel(
'anthropic.claude-3-sonnet-20240229-v1:0',
'Anthropic Claude v3 Sonnet',
(region) =>
inferenceProfileId(region, {
us: 'us.anthropic.claude-3-sonnet-20240229-v1:0',
eu: 'eu.anthropic.claude-3-sonnet-20240229-v1:0',
})
),
new ClaudeModel('anthropic.claude-v2:1', 'Anthropic Claude v2.1'),
new ClaudeModel('anthropic.claude-instant-v1', 'Anthropic Claude Instant v1.2'),
new LlamaModel('meta.llama2-70b-chat-v1', 'Meta Llama 2 70B'),
new MistralModel('mistral.mixtral-8x7b-instruct-v0:1', 'Mistral Mixtral 8x7B'),
];