-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
anthropic.ts
357 lines (319 loc) · 10.5 KB
/
anthropic.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import Anthropic, { APIError } from '@anthropic-ai/sdk';
import logger from '../logger';
import type { ApiProvider, EnvOverrides, ProviderResponse, TokenUsage } from '../types.js';
import { getCache, isCacheEnabled } from '../cache';
import { parseChatPrompt } from './shared';
interface AnthropicMessageOptions {
apiKey?: string;
temperature?: number;
max_tokens?: number;
top_p?: number;
top_k?: number;
model?: string;
cost?: number;
}
function getTokenUsage(data: any, cached: boolean): Partial<TokenUsage> {
if (data.usage) {
const total_tokens = data.usage.input_tokens + data.usage.output_tokens;
if (cached) {
return { cached: total_tokens, total: total_tokens };
} else {
return {
total: total_tokens,
prompt: data.usage.input_tokens || 0,
completion: data.usage.output_tokens || 0,
};
}
}
return {};
}
function calculateCost(
modelName: string,
config: AnthropicMessageOptions,
promptTokens?: number,
completionTokens?: number,
): number | undefined {
if (!promptTokens || !completionTokens) {
return undefined;
}
const model = [...AnthropicMessagesProvider.ANTHROPIC_MODELS].find((m) => m.id === modelName);
if (!model || !model.cost) {
return undefined;
}
const inputCost = config.cost ?? model.cost.input;
const outputCost = config.cost ?? model.cost.output;
return inputCost * promptTokens + outputCost * completionTokens || undefined;
}
interface AnthropicMessageInput {
role: 'user' | 'assistant' | 'system';
content: string | Array<Anthropic.ImageBlockParam | Anthropic.TextBlockParam>;
}
export function parseMessages(messages: string) {
// We need to be able to handle the 'system' role prompts that
// are in the style of OpenAI's chat prompts.
// As a result, AnthropicMessageInput is the same as Anthropic.MessageParam
// just with the system role added on
const chats = parseChatPrompt<AnthropicMessageInput[]>(
messages,
[{ role: 'user' as const, content: messages }],
);
// Convert from OpenAI to Anthropic format
const systemMessage = chats.find((m) => m.role === 'system')?.content;
const system = typeof systemMessage === 'string' ? systemMessage : undefined;
const extractedMessages: Anthropic.MessageParam[] = chats
.filter((m) => m.role === 'user' || m.role === 'assistant')
.map((m) => {
const role = m.role as 'user' | 'assistant';
if (typeof m.content === 'string') {
const content = [{ type: 'text' as const, text: m.content }];
return { role, content };
}
else {
const content = [...m.content];
return { role, content };
}
});
return { system, extractedMessages };
}
export class AnthropicMessagesProvider implements ApiProvider {
modelName: string;
config: AnthropicMessageOptions;
env?: EnvOverrides;
apiKey?: string;
anthropic: Anthropic;
static ANTHROPIC_MODELS = [
...['claude-instant-1.2'].map((model) => ({
id: model,
cost: {
input: 0.0008 / 1000,
output: 0.0024 / 1000,
},
})),
...['claude-2.0'].map((model) => ({
id: model,
cost: {
input: 0.008 / 1000,
output: 0.024 / 1000,
},
})),
...['claude-2.1'].map((model) => ({
id: model,
cost: {
input: 0.008 / 1000,
output: 0.024 / 1000,
},
})),
...['claude-3-haiku-20240307'].map((model) => ({
id: model,
cost: {
input: 0.00025 / 1000,
output: 0.00125 / 1000,
},
})),
...['claude-3-sonnet-20240229'].map((model) => ({
id: model,
cost: {
input: 0.003 / 1000,
output: 0.015 / 1000,
},
})),
...['claude-3-opus-20240229'].map((model) => ({
id: model,
cost: {
input: 0.015 / 1000,
output: 0.075 / 1000,
},
})),
];
static ANTHROPIC_MODELS_NAMES = AnthropicMessagesProvider.ANTHROPIC_MODELS.map(
(model) => model.id,
);
constructor(
modelName: string,
options: { id?: string; config?: AnthropicMessageOptions; env?: EnvOverrides } = {},
) {
if (!AnthropicMessagesProvider.ANTHROPIC_MODELS_NAMES.includes(modelName)) {
logger.warn(`Using unknown Anthropic model: ${modelName}`);
}
const { id, config, env } = options;
this.env = env;
this.modelName = modelName;
this.id = id ? () => id : this.id;
this.config = config || {};
this.apiKey = config?.apiKey || env?.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY;
this.anthropic = new Anthropic({ apiKey: this.apiKey });
}
id(): string {
return `anthropic:messages:${this.modelName || 'claude-2.1'}`;
}
toString(): string {
return `[Anthropic Messages Provider ${this.modelName || 'claude-2.1'}]`;
}
async callApi(prompt: string): Promise<ProviderResponse> {
if (!this.apiKey) {
throw new Error(
'Anthropic API key is not set. Set the ANTHROPIC_API_KEY environment variable or add `apiKey` to the provider config.',
);
}
const { system, extractedMessages } = parseMessages(prompt);
const params: Anthropic.MessageCreateParams = {
model: this.modelName,
...(system ? { system } : {}),
messages: extractedMessages,
max_tokens: this.config?.max_tokens || 1024,
temperature: this.config.temperature || 0,
stream: false
};
logger.debug(`Calling Anthropic Messages API: ${JSON.stringify(params)}`);
const cache = await getCache();
const cacheKey = `anthropic:${JSON.stringify(params)}`;
if (isCacheEnabled()) {
// Try to get the cached response
const cachedResponse = await cache.get(cacheKey);
if (cachedResponse) {
logger.debug(`Returning cached response for ${prompt}: ${cachedResponse}`);
return {
output: JSON.parse(cachedResponse as string),
tokenUsage: {},
};
}
}
try {
const response = await this.anthropic.messages.create(params);
logger.debug(`Anthropic Messages API response: ${JSON.stringify(response)}`);
if (isCacheEnabled()) {
try {
await cache.set(cacheKey, JSON.stringify(response.content[0].text));
} catch (err) {
logger.error(`Failed to cache response: ${String(err)}`);
}
}
return {
output: response.content[0].text,
tokenUsage: getTokenUsage(response, false),
cost: calculateCost(
this.modelName,
this.config,
response.usage?.input_tokens,
response.usage?.output_tokens,
),
};
} catch (err) {
logger.error(`Anthropic Messages API call error: ${String(err)}`);
if (err instanceof APIError && err.error) {
const errorDetails = err.error as { error: { message: string; type: string } };
return {
error: `API call error: ${errorDetails.error.message}, status ${err.status}, type ${errorDetails.error.type}`,
};
}
return {
error: `API call error: ${String(err)}`,
};
}
}
}
interface AnthropicCompletionOptions {
apiKey?: string;
max_tokens_to_sample?: number;
temperature?: number;
top_p?: number;
top_k?: number;
}
export class AnthropicCompletionProvider implements ApiProvider {
static ANTHROPIC_COMPLETION_MODELS = [
'claude-1',
'claude-1-100k',
'claude-instant-1',
'claude-instant-1.2',
'claude-instant-1-100k',
'claude-2',
'claude-2.1',
];
modelName: string;
apiKey?: string;
anthropic: Anthropic;
config: AnthropicCompletionOptions;
constructor(
modelName: string,
options: { config?: AnthropicCompletionOptions; id?: string; env?: EnvOverrides } = {},
) {
const { config, id, env } = options;
this.modelName = modelName;
this.apiKey = config?.apiKey || env?.ANTHROPIC_API_KEY || process.env.ANTHROPIC_API_KEY;
this.anthropic = new Anthropic({ apiKey: this.apiKey });
this.config = config || {};
this.id = id ? () => id : this.id;
}
id(): string {
return `anthropic:${this.modelName}`;
}
toString(): string {
return `[Anthropic Provider ${this.modelName}]`;
}
async callApi(prompt: string): Promise<ProviderResponse> {
if (!this.apiKey) {
throw new Error(
'Anthropic API key is not set. Set the ANTHROPIC_API_KEY environment variable or add `apiKey` to the provider config.',
);
}
let stop: string[];
try {
stop = process.env.ANTHROPIC_STOP
? JSON.parse(process.env.ANTHROPIC_STOP)
: ['<|im_end|>', '<|endoftext|>'];
} catch (err) {
throw new Error(`ANTHROPIC_STOP is not a valid JSON string: ${err}`);
}
const params: Anthropic.CompletionCreateParams = {
model: this.modelName,
prompt: `${Anthropic.HUMAN_PROMPT} ${prompt} ${Anthropic.AI_PROMPT}`,
max_tokens_to_sample:
this.config?.max_tokens_to_sample || parseInt(process.env.ANTHROPIC_MAX_TOKENS || '1024'),
temperature: this.config.temperature ?? parseFloat(process.env.ANTHROPIC_TEMPERATURE || '0'),
stop_sequences: stop,
};
logger.debug(`Calling Anthropic API: ${JSON.stringify(params)}`);
const cache = await getCache();
const cacheKey = `anthropic:${JSON.stringify(params)}`;
if (isCacheEnabled()) {
// Try to get the cached response
const cachedResponse = await cache.get(cacheKey);
if (cachedResponse) {
logger.debug(`Returning cached response for ${prompt}: ${cachedResponse}`);
return {
output: JSON.parse(cachedResponse as string),
tokenUsage: {},
};
}
}
let response;
try {
response = await this.anthropic.completions.create(params);
} catch (err) {
return {
error: `API call error: ${String(err)}`,
};
}
logger.debug(`\tAnthropic API response: ${JSON.stringify(response)}`);
if (isCacheEnabled()) {
try {
await cache.set(cacheKey, JSON.stringify(response.completion));
} catch (err) {
logger.error(`Failed to cache response: ${String(err)}`);
}
}
try {
return {
output: response.completion,
tokenUsage: {}, // TODO: add token usage once Anthropic API supports it
};
} catch (err) {
return {
error: `API response error: ${String(err)}: ${JSON.stringify(response)}`,
};
}
}
}
export const DefaultGradingProvider = new AnthropicMessagesProvider('claude-3-opus-20240229');
export const DefaultGradingJsonProvider = new AnthropicMessagesProvider('claude-3-opus-20240229');
export const DefaultSuggestionsProvider = new AnthropicMessagesProvider('claude-3-opus-20240229');