symposium 0.6.0 → 0.6.2
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/Model.js +16 -0
- package/models/AnthropicModel.js +34 -24
- package/models/OpenAIModel.js +4 -9
- package/package.json +1 -1
package/Model.js
CHANGED
|
@@ -18,6 +18,22 @@ export default class Model {
|
|
|
18
18
|
throw new Error('countTokens not implemented in this model');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
parseOptions(options = {}, functions = []) {
|
|
22
|
+
options = {
|
|
23
|
+
functions: null,
|
|
24
|
+
force_function: null,
|
|
25
|
+
...options,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
if (options.functions !== null)
|
|
29
|
+
functions = options.functions;
|
|
30
|
+
|
|
31
|
+
if (options.force_function && !functions.find(f => f.name === options.force_function))
|
|
32
|
+
throw new Error('Function ' + options.force_function + ' not found.');
|
|
33
|
+
|
|
34
|
+
return {options, functions};
|
|
35
|
+
}
|
|
36
|
+
|
|
21
37
|
promptFromFunctions(options, functions) {
|
|
22
38
|
if (options.force_function)
|
|
23
39
|
functions = functions.filter(f => f.name !== options.force_function);
|
package/models/AnthropicModel.js
CHANGED
|
@@ -14,14 +14,9 @@ export default class AnthropicModel extends Model {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
async generate(thread, functions = [], options = {}) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
...options,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
if (options.functions !== null)
|
|
24
|
-
functions = options.functions;
|
|
17
|
+
const parsed = this.parseOptions(options, functions);
|
|
18
|
+
options = parsed.options;
|
|
19
|
+
functions = parsed.functions;
|
|
25
20
|
|
|
26
21
|
let [system, messages] = this.convertMessages(thread);
|
|
27
22
|
|
|
@@ -45,16 +40,16 @@ export default class AnthropicModel extends Model {
|
|
|
45
40
|
})),
|
|
46
41
|
};
|
|
47
42
|
|
|
48
|
-
if (options.force_function
|
|
49
|
-
completion_payload.messages.push({
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
{type: 'text', text: 'Use the ' + options.force_function + ' tool in your response.'},
|
|
53
|
-
],
|
|
43
|
+
if (options.force_function) {
|
|
44
|
+
completion_payload.messages[completion_payload.messages.length - 1].content.push({
|
|
45
|
+
type: 'text',
|
|
46
|
+
text: 'Usa il tool "' + options.force_function + '" nella tua prossima risposta!',
|
|
54
47
|
});
|
|
55
48
|
}
|
|
56
49
|
|
|
57
|
-
const message =
|
|
50
|
+
const message = completion_payload.tools.length ?
|
|
51
|
+
await this.getAnthropic().beta.tools.messages.create(completion_payload)
|
|
52
|
+
: await this.getAnthropic().messages.create(completion_payload);
|
|
58
53
|
|
|
59
54
|
const message_content = [];
|
|
60
55
|
if (message.content) {
|
|
@@ -86,32 +81,47 @@ export default class AnthropicModel extends Model {
|
|
|
86
81
|
}
|
|
87
82
|
|
|
88
83
|
convertMessages(thread) {
|
|
89
|
-
let system = [], messages = [];
|
|
84
|
+
let system = [], messages = [], lastMessage = null;
|
|
90
85
|
for (let message of thread.messages) {
|
|
91
86
|
if (message.role === 'system') {
|
|
92
87
|
system.push(message.content.map(c => c.content).join("\n"));
|
|
93
88
|
} else {
|
|
94
|
-
|
|
89
|
+
const parsedMessage = {
|
|
95
90
|
role: message.role === 'function' ? 'user' : message.role,
|
|
96
91
|
content: message.content.map(c => {
|
|
97
92
|
switch (c.type) {
|
|
98
93
|
case 'text':
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
94
|
+
if (message.role === 'function') {
|
|
95
|
+
return {
|
|
96
|
+
type: 'text',
|
|
97
|
+
content: c.content,
|
|
98
|
+
};
|
|
99
|
+
} else {
|
|
100
|
+
return {
|
|
101
|
+
type: 'text',
|
|
102
|
+
text: c.content.trim(),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
103
105
|
|
|
104
106
|
case 'function':
|
|
105
107
|
return {
|
|
106
|
-
type: '
|
|
107
|
-
|
|
108
|
+
type: 'tool_use',
|
|
109
|
+
name: c.content.name,
|
|
110
|
+
input: c.content.arguments,
|
|
108
111
|
};
|
|
109
112
|
|
|
110
113
|
default:
|
|
111
114
|
throw new Error('Message type "' + c.type + '" unsupported by this model');
|
|
112
115
|
}
|
|
113
116
|
}),
|
|
114
|
-
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
if (lastMessage && lastMessage.role === parsedMessage.role) {
|
|
120
|
+
lastMessage.content = lastMessage.content.concat(message.content);
|
|
121
|
+
} else {
|
|
122
|
+
messages.push(parsedMessage);
|
|
123
|
+
lastMessage = parsedMessage;
|
|
124
|
+
}
|
|
115
125
|
}
|
|
116
126
|
}
|
|
117
127
|
|
package/models/OpenAIModel.js
CHANGED
|
@@ -16,14 +16,9 @@ export default class OpenAIModel extends Model {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
async generate(thread, functions = [], options = {}) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
...options,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
if (options.functions !== null)
|
|
26
|
-
functions = options.functions;
|
|
19
|
+
const parsed = this.parseOptions(options, functions);
|
|
20
|
+
options = parsed.options;
|
|
21
|
+
functions = parsed.functions;
|
|
27
22
|
|
|
28
23
|
let messages = thread.messages;
|
|
29
24
|
|
|
@@ -57,7 +52,7 @@ export default class OpenAIModel extends Model {
|
|
|
57
52
|
functions,
|
|
58
53
|
};
|
|
59
54
|
|
|
60
|
-
if (options.force_function
|
|
55
|
+
if (options.force_function)
|
|
61
56
|
completion_payload.function_call = {name: options.force_function};
|
|
62
57
|
if (!completion_payload.functions.length)
|
|
63
58
|
delete completion_payload.functions;
|