-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathanthropic-language-model.ts
351 lines (310 loc) · 14.7 KB
/
anthropic-language-model.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
// *****************************************************************************
// Copyright (C) 2024 EclipseSource GmbH.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import {
LanguageModel,
LanguageModelRequest,
LanguageModelMessage,
LanguageModelResponse,
LanguageModelStreamResponse,
LanguageModelStreamResponsePart,
LanguageModelTextResponse,
TokenUsageService,
TokenUsageParams,
UserRequest,
LLMImageData
} from '@theia/ai-core';
import { CancellationToken, isArray } from '@theia/core';
import { Anthropic } from '@anthropic-ai/sdk';
import { Message, MessageParam } from '@anthropic-ai/sdk/resources';
export const DEFAULT_MAX_TOKENS = 4096;
interface ToolCallback {
readonly name: string;
readonly id: string;
readonly index: number;
args: string;
}
const createMessageContent = (message: LanguageModelMessage): MessageParam['content'] => {
if (LanguageModelMessage.isTextMessage(message)) {
return message.text;
} else if (LanguageModelMessage.isThinkingMessage(message)) {
return [{ signature: message.signature, thinking: message.thinking, type: 'thinking' }];
} else if (LanguageModelMessage.isToolUseMessage(message)) {
return [{ id: message.id, input: message.input, name: message.name, type: 'tool_use' }];
} else if (LanguageModelMessage.isToolResultMessage(message)) {
return [{ type: 'tool_result', tool_use_id: message.tool_use_id }];
} else if (LanguageModelMessage.isImageMessage(message)) {
if (LLMImageData.isBase64ImageData(message.image)) {
return [{ type: 'image', source: { type: 'base64', media_type: message.image.mediaType, data: message.image.imageData } }];
}
}
throw new Error(`Unknown message type:'${JSON.stringify(message)}'`);
};
/**
* Transforms Theia language model messages to Anthropic API format
* @param messages Array of LanguageModelRequestMessage to transform
* @returns Object containing transformed messages and optional system message
*/
function transformToAnthropicParams(
messages: readonly LanguageModelMessage[]
): { messages: MessageParam[]; systemMessage?: string } {
// Extract the system message (if any), as it is a separate parameter in the Anthropic API.
const systemMessageObj = messages.find(message => message.actor === 'system');
const systemMessage = systemMessageObj && LanguageModelMessage.isTextMessage(systemMessageObj) && systemMessageObj.text || undefined;
const convertedMessages = messages
.filter(message => message.actor !== 'system')
.map(message => ({
role: toAnthropicRole(message),
content: createMessageContent(message)
}));
return {
messages: convertedMessages,
systemMessage,
};
}
export const AnthropicModelIdentifier = Symbol('AnthropicModelIdentifier');
/**
* Converts Theia message actor to Anthropic role
* @param message The message to convert
* @returns Anthropic role ('user' or 'assistant')
*/
function toAnthropicRole(message: LanguageModelMessage): 'user' | 'assistant' {
switch (message.actor) {
case 'ai':
return 'assistant';
default:
return 'user';
}
}
/**
* Implements the Anthropic language model integration for Theia
*/
export class AnthropicModel implements LanguageModel {
constructor(
public readonly id: string,
public model: string,
public enableStreaming: boolean,
public apiKey: () => string | undefined,
public maxTokens: number = DEFAULT_MAX_TOKENS,
protected readonly tokenUsageService?: TokenUsageService
) { }
protected getSettings(request: LanguageModelRequest): Readonly<Record<string, unknown>> {
return request.settings ?? {};
}
async request(request: UserRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
if (!request.messages?.length) {
throw new Error('Request must contain at least one message');
}
const anthropic = this.initializeAnthropic();
try {
if (this.enableStreaming) {
return this.handleStreamingRequest(anthropic, request, cancellationToken);
}
return this.handleNonStreamingRequest(anthropic, request);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
throw new Error(`Anthropic API request failed: ${errorMessage}`);
}
}
protected formatToolCallResult(result: unknown): string | Array<{ type: 'text', text: string }> {
if (typeof result === 'object' && result && 'content' in result && Array.isArray(result.content) &&
result.content.every(item => typeof item === 'object' && item && 'type' in item && 'text' in item)) {
return result.content;
}
if (isArray(result)) {
return result.map(r => ({ type: 'text', text: r as string }));
}
if (typeof result === 'object') {
return JSON.stringify(result);
}
return result as string;
}
protected async handleStreamingRequest(
anthropic: Anthropic,
request: UserRequest,
cancellationToken?: CancellationToken,
toolMessages?: readonly Anthropic.Messages.MessageParam[]
): Promise<LanguageModelStreamResponse> {
const settings = this.getSettings(request);
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
const tools = this.createTools(request);
const params: Anthropic.MessageCreateParams = {
max_tokens: this.maxTokens,
messages: [...messages, ...(toolMessages ?? [])],
tools,
tool_choice: tools ? { type: 'auto' } : undefined,
model: this.model,
...(systemMessage && { system: systemMessage }),
...settings
};
const stream = anthropic.messages.stream(params);
cancellationToken?.onCancellationRequested(() => {
stream.abort();
});
const that = this;
const asyncIterator = {
async *[Symbol.asyncIterator](): AsyncIterator<LanguageModelStreamResponsePart> {
const toolCalls: ToolCallback[] = [];
let toolCall: ToolCallback | undefined;
const currentMessages: Message[] = [];
let currentMessage: Message | undefined = undefined;
for await (const event of stream) {
if (event.type === 'content_block_start') {
const contentBlock = event.content_block;
if (contentBlock.type === 'thinking') {
yield { thought: contentBlock.thinking, signature: contentBlock.signature ?? '' };
}
if (contentBlock.type === 'text') {
yield { content: contentBlock.text };
}
if (contentBlock.type === 'tool_use') {
toolCall = { name: contentBlock.name!, args: '', id: contentBlock.id!, index: event.index };
yield { tool_calls: [{ finished: false, id: toolCall.id, function: { name: toolCall.name, arguments: toolCall.args } }] };
}
} else if (event.type === 'content_block_delta') {
const delta = event.delta;
if (delta.type === 'thinking_delta') {
yield { thought: delta.thinking, signature: '' };
}
if (delta.type === 'signature_delta') {
yield { thought: '', signature: delta.signature };
}
if (delta.type === 'text_delta') {
yield { content: delta.text };
}
if (toolCall && delta.type === 'input_json_delta') {
toolCall.args += delta.partial_json;
yield { tool_calls: [{ function: { arguments: delta.partial_json } }] };
}
} else if (event.type === 'content_block_stop') {
if (toolCall && toolCall.index === event.index) {
toolCalls.push(toolCall);
toolCall = undefined;
}
} else if (event.type === 'message_delta') {
if (event.delta.stop_reason === 'max_tokens') {
if (toolCall) {
yield { tool_calls: [{ finished: true, id: toolCall.id }] };
}
throw new Error(`The response was stopped because it exceeded the max token limit of ${event.usage.output_tokens}.`);
}
} else if (event.type === 'message_start') {
currentMessages.push(event.message);
currentMessage = event.message;
} else if (event.type === 'message_stop') {
if (currentMessage) {
yield { input_tokens: currentMessage.usage.input_tokens, output_tokens: currentMessage.usage.output_tokens };
// Record token usage if token usage service is available
if (that.tokenUsageService && currentMessage.usage) {
const tokenUsageParams: TokenUsageParams = {
inputTokens: currentMessage.usage.input_tokens,
outputTokens: currentMessage.usage.output_tokens,
requestId: request.requestId
};
await that.tokenUsageService.recordTokenUsage(that.id, tokenUsageParams);
}
}
}
}
if (toolCalls.length > 0) {
const toolResult = await Promise.all(toolCalls.map(async tc => {
const tool = request.tools?.find(t => t.name === tc.name);
const argsObject = tc.args.length === 0 ? '{}' : tc.args;
return { name: tc.name, result: (await tool?.handler(argsObject)), id: tc.id, arguments: argsObject };
}));
const calls = toolResult.map(tr => {
const resultAsString = typeof tr.result === 'string' ? tr.result : JSON.stringify(tr.result);
return { finished: true, id: tr.id, result: resultAsString, function: { name: tr.name, arguments: tr.arguments } };
});
yield { tool_calls: calls };
const toolResponseMessage: Anthropic.Messages.MessageParam = {
role: 'user',
content: toolResult.map(call => ({
type: 'tool_result',
tool_use_id: call.id!,
content: that.formatToolCallResult(call.result)
}))
};
const result = await that.handleStreamingRequest(
anthropic,
request,
cancellationToken,
[
...(toolMessages ?? []),
...currentMessages.map(m => ({ role: m.role, content: m.content })),
toolResponseMessage
]);
for await (const nestedEvent of result.stream) {
yield nestedEvent;
}
}
},
};
stream.on('error', (error: Error) => {
console.error('Error in Anthropic streaming:', error);
});
return { stream: asyncIterator };
}
private createTools(request: LanguageModelRequest): Anthropic.Messages.Tool[] | undefined {
if (request.tools?.length === 0) {
return undefined;
}
return request.tools?.map(tool => ({
name: tool.name,
description: tool.description,
input_schema: tool.parameters
} as Anthropic.Messages.Tool));
}
protected async handleNonStreamingRequest(
anthropic: Anthropic,
request: UserRequest
): Promise<LanguageModelTextResponse> {
const settings = this.getSettings(request);
const { messages, systemMessage } = transformToAnthropicParams(request.messages);
const params: Anthropic.MessageCreateParams = {
max_tokens: this.maxTokens,
messages,
model: this.model,
...(systemMessage && { system: systemMessage }),
...settings,
};
try {
const response = await anthropic.messages.create(params);
const textContent = response.content[0];
// Record token usage if token usage service is available
if (this.tokenUsageService && response.usage) {
const tokenUsageParams: TokenUsageParams = {
inputTokens: response.usage.input_tokens,
outputTokens: response.usage.output_tokens,
requestId: request.requestId
};
await this.tokenUsageService.recordTokenUsage(this.id, tokenUsageParams);
}
if (textContent?.type === 'text') {
return { text: textContent.text };
}
return { text: '' };
} catch (error) {
throw new Error(`Failed to get response from Anthropic API: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
protected initializeAnthropic(): Anthropic {
const apiKey = this.apiKey();
if (!apiKey) {
throw new Error('Please provide ANTHROPIC_API_KEY in preferences or via environment variable');
}
return new Anthropic({ apiKey });
}
}