symposium 1.2.7 → 1.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Agent.js +2 -0
- package/models/AnthropicModel.js +85 -72
- package/package.json +1 -1
package/Agent.js
CHANGED
package/models/AnthropicModel.js
CHANGED
|
@@ -14,84 +14,97 @@ export default class AnthropicModel extends Model {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
async generate(thread, functions = [], options = {}) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
try {
|
|
18
|
+
const parsed = this.parseOptions(options, functions);
|
|
19
|
+
options = parsed.options;
|
|
20
|
+
functions = parsed.functions;
|
|
21
|
+
|
|
22
|
+
let [system, messages] = await this.convertMessages(thread);
|
|
23
|
+
|
|
24
|
+
if (functions.length && !this.supports_functions) {
|
|
25
|
+
// Se il modello non supporta nativamente le funzioni, aggiungo il prompt al messaggio di sistema
|
|
26
|
+
const functions_prompt = this.promptFromFunctions(options, functions);
|
|
27
|
+
system += "\n\n" + functions_prompt;
|
|
28
|
+
functions = [];
|
|
29
|
+
}
|
|
20
30
|
|
|
21
|
-
|
|
31
|
+
const completion_payload = {
|
|
32
|
+
model: this.name,
|
|
33
|
+
system,
|
|
34
|
+
max_tokens: 16000,
|
|
35
|
+
thinking: {
|
|
36
|
+
type: "enabled",
|
|
37
|
+
budget_tokens: 10000,
|
|
38
|
+
},
|
|
39
|
+
betas: ["interleaved-thinking-2025-05-14"],
|
|
40
|
+
messages,
|
|
41
|
+
tools: functions.map(f => ({
|
|
42
|
+
name: f.name,
|
|
43
|
+
description: f.description,
|
|
44
|
+
input_schema: f.parameters,
|
|
45
|
+
required: f.required || undefined,
|
|
46
|
+
})),
|
|
47
|
+
};
|
|
22
48
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
49
|
+
if (options.force_function) {
|
|
50
|
+
completion_payload.tool_choice = {
|
|
51
|
+
type: 'tool',
|
|
52
|
+
name: options.force_function,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
29
55
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
const message = await this.getAnthropic().beta.messages.create(completion_payload);
|
|
57
|
+
|
|
58
|
+
const message_content = [];
|
|
59
|
+
if (message.content) {
|
|
60
|
+
for (let m of message.content) {
|
|
61
|
+
switch (m.type) {
|
|
62
|
+
case 'text':
|
|
63
|
+
message_content.push({type: 'text', content: m.text});
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
case 'tool_use':
|
|
67
|
+
message_content.push({
|
|
68
|
+
type: 'function',
|
|
69
|
+
content: [
|
|
70
|
+
{
|
|
71
|
+
id: m.id,
|
|
72
|
+
name: m.name,
|
|
73
|
+
arguments: m.input,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
break;
|
|
78
|
+
|
|
79
|
+
case 'thinking':
|
|
80
|
+
message_content.push({
|
|
81
|
+
type: 'reasoning',
|
|
82
|
+
content: m.thinking,
|
|
83
|
+
original: m,
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
54
86
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (message.content) {
|
|
59
|
-
for (let m of message.content) {
|
|
60
|
-
switch (m.type) {
|
|
61
|
-
case 'text':
|
|
62
|
-
message_content.push({type: 'text', content: m.text});
|
|
63
|
-
break;
|
|
64
|
-
|
|
65
|
-
case 'tool_use':
|
|
66
|
-
message_content.push({
|
|
67
|
-
type: 'function',
|
|
68
|
-
content: [
|
|
69
|
-
{
|
|
70
|
-
id: m.id,
|
|
71
|
-
name: m.name,
|
|
72
|
-
arguments: m.input,
|
|
73
|
-
},
|
|
74
|
-
],
|
|
75
|
-
});
|
|
76
|
-
break;
|
|
77
|
-
|
|
78
|
-
case 'thinking':
|
|
79
|
-
message_content.push({
|
|
80
|
-
type: 'reasoning',
|
|
81
|
-
content: m.thinking,
|
|
82
|
-
original: m,
|
|
83
|
-
});
|
|
84
|
-
break;
|
|
85
|
-
|
|
86
|
-
default:
|
|
87
|
-
throw new Error('Unrecognized message type in Anthropic response');
|
|
87
|
+
default:
|
|
88
|
+
throw new Error('Unrecognized message type in Anthropic response');
|
|
89
|
+
}
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
|
-
}
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
return [
|
|
94
|
+
new Message('assistant', message_content),
|
|
95
|
+
];
|
|
96
|
+
} catch (e) {
|
|
97
|
+
if (e.error?.error?.type === 'rate_limit_error') {
|
|
98
|
+
console.error('Rate limite exceeded for Anthropic API, waiting 60 seconds...');
|
|
99
|
+
await new Promise(resolve => setTimeout(resolve, 60000));
|
|
100
|
+
if ((options.counter || 0) < 3)
|
|
101
|
+
return this.generate(thread, functions, {...options, counter: (options.counter || 0) + 1});
|
|
102
|
+
else
|
|
103
|
+
throw new Error('Rate limit exceeded for Anthropic API, aborting.');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw e;
|
|
107
|
+
}
|
|
95
108
|
}
|
|
96
109
|
|
|
97
110
|
async convertMessages(thread) {
|
|
@@ -187,7 +200,7 @@ export default class AnthropicModel extends Model {
|
|
|
187
200
|
};
|
|
188
201
|
|
|
189
202
|
if (lastMessage && lastMessage.role === parsedMessage.role) {
|
|
190
|
-
lastMessage.content = lastMessage.content.concat(
|
|
203
|
+
lastMessage.content = lastMessage.content.concat(parsedMessage.content);
|
|
191
204
|
} else {
|
|
192
205
|
messages.push(parsedMessage);
|
|
193
206
|
lastMessage = parsedMessage;
|