symposium 0.5.7 → 0.6.0
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 +3 -3
- package/Model.js +7 -7
- package/Summarizer.js +1 -1
- package/models/AnthropicModel.js +20 -3
- package/models/OpenAIModel.js +14 -7
- package/package.json +1 -1
package/Agent.js
CHANGED
|
@@ -121,10 +121,10 @@ export default class Agent {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
-
async generateCompletion(thread,
|
|
124
|
+
async generateCompletion(thread, options = {}, retry_counter = 1) {
|
|
125
125
|
try {
|
|
126
126
|
const model = Symposium.getModelByName(thread.state.model);
|
|
127
|
-
const messages = await model.generate(thread,
|
|
127
|
+
const messages = await model.generate(thread, await this.getFunctions(), options);
|
|
128
128
|
return messages.map(m => model.supports_functions ? m : this.parseFunctions(m));
|
|
129
129
|
} catch (error) {
|
|
130
130
|
if (error.response) {
|
|
@@ -136,7 +136,7 @@ export default class Agent {
|
|
|
136
136
|
setTimeout(resolve, 1000);
|
|
137
137
|
});
|
|
138
138
|
|
|
139
|
-
return this.generateCompletion(thread,
|
|
139
|
+
return this.generateCompletion(thread, options, retry_counter + 1);
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
await thread.reply('# Errore ' + error.response.status + ': ' + JSON.stringify(error.response.data));
|
package/Model.js
CHANGED
|
@@ -10,7 +10,7 @@ export default class Model {
|
|
|
10
10
|
this.label = this.name;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
async generate(thread,
|
|
13
|
+
async generate(thread, functions = [], options = {}) {
|
|
14
14
|
return null;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -18,19 +18,19 @@ export default class Model {
|
|
|
18
18
|
throw new Error('countTokens not implemented in this model');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
promptFromFunctions(
|
|
22
|
-
if (
|
|
23
|
-
functions = functions.filter(f => f.name !==
|
|
21
|
+
promptFromFunctions(options, functions) {
|
|
22
|
+
if (options.force_function)
|
|
23
|
+
functions = functions.filter(f => f.name !== options.force_function);
|
|
24
24
|
|
|
25
25
|
if (!functions.length)
|
|
26
26
|
return '';
|
|
27
27
|
|
|
28
28
|
let message;
|
|
29
|
-
if (
|
|
29
|
+
if (options.force_function) {
|
|
30
30
|
message = "Nella prossima risposta, rispondi UNICAMENTE seguendo le seguenti istruzioni:\n";
|
|
31
31
|
message += functions[0].description + "\n";
|
|
32
32
|
delete functions[0].description;
|
|
33
|
-
message += "Rispondi con un messaggio che inizia con le parole:\nCALL " +
|
|
33
|
+
message += "Rispondi con un messaggio che inizia con le parole:\nCALL " + options.force_function + "\nE poi a capo un oggetto JSON che segue queste direttive OpenAPI:\n";
|
|
34
34
|
} else {
|
|
35
35
|
message = "Hai a disposizione alcune funzioni che puoi chiamare per ottenere risposte o compiere azioni. Ricorda che devi attendere la risposta dalla funzione per sapere se ha avuto successo. Per chiamare una funzione scrivi un messaggio che inizia con CALL nome_funzione e a capo inserisci il JSON con gli argomenti; delimitando il tutto da 3 caratteri ``` - ad esempio:\n" +
|
|
36
36
|
"```\n" +
|
|
@@ -50,7 +50,7 @@ export default class Model {
|
|
|
50
50
|
message += '=== ' + f.name + " ===\n" + JSON.stringify(f.parameters.properties) + "\n\n";
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
if (
|
|
53
|
+
if (options.force_function)
|
|
54
54
|
message += "\nNella risposta non deve esserci NIENTE ALTRO se non queste due cose, non saranno prese in considerazione dal sistema altro genere di risposte.";
|
|
55
55
|
|
|
56
56
|
return message;
|
package/Summarizer.js
CHANGED
package/models/AnthropicModel.js
CHANGED
|
@@ -13,12 +13,21 @@ export default class AnthropicModel extends Model {
|
|
|
13
13
|
return this.anthropic;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
async generate(thread,
|
|
16
|
+
async generate(thread, functions = [], options = {}) {
|
|
17
|
+
options = {
|
|
18
|
+
functions: null,
|
|
19
|
+
force_function: null,
|
|
20
|
+
...options,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (options.functions !== null)
|
|
24
|
+
functions = options.functions;
|
|
25
|
+
|
|
17
26
|
let [system, messages] = this.convertMessages(thread);
|
|
18
27
|
|
|
19
28
|
if (functions.length && !this.supports_functions) {
|
|
20
29
|
// Se il modello non supporta nativamente le funzioni, aggiungo il prompt al messaggio di sistema
|
|
21
|
-
const functions_prompt = this.promptFromFunctions(
|
|
30
|
+
const functions_prompt = this.promptFromFunctions(options, functions);
|
|
22
31
|
system += "\n\n" + functions_prompt;
|
|
23
32
|
functions = [];
|
|
24
33
|
}
|
|
@@ -34,9 +43,17 @@ export default class AnthropicModel extends Model {
|
|
|
34
43
|
input_schema: f.parameters,
|
|
35
44
|
required: f.required || undefined,
|
|
36
45
|
})),
|
|
37
|
-
...payload,
|
|
38
46
|
};
|
|
39
47
|
|
|
48
|
+
if (options.force_function && completion_payload.functions.length) {
|
|
49
|
+
completion_payload.messages.push({
|
|
50
|
+
role: 'user',
|
|
51
|
+
content: [
|
|
52
|
+
{type: 'text', text: 'Use the ' + options.force_function + ' tool in your response.'},
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
const message = await this.getAnthropic().messages.create(completion_payload);
|
|
41
58
|
|
|
42
59
|
const message_content = [];
|
package/models/OpenAIModel.js
CHANGED
|
@@ -15,12 +15,21 @@ export default class OpenAIModel extends Model {
|
|
|
15
15
|
return this.openai;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
async generate(thread,
|
|
18
|
+
async generate(thread, functions = [], options = {}) {
|
|
19
|
+
options = {
|
|
20
|
+
functions: null,
|
|
21
|
+
force_function: null,
|
|
22
|
+
...options,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (options.functions !== null)
|
|
26
|
+
functions = options.functions;
|
|
27
|
+
|
|
19
28
|
let messages = thread.messages;
|
|
20
29
|
|
|
21
30
|
if (functions.length && !this.supports_functions) {
|
|
22
31
|
// Se il modello non supporta nativamente le funzioni, inserisco il prompt ad hoc come ultimo messaggio di sistema
|
|
23
|
-
const functions_prompt = this.promptFromFunctions(
|
|
32
|
+
const functions_prompt = this.promptFromFunctions(options, functions);
|
|
24
33
|
let system_messages = [], other_messages = [], first_found = false;
|
|
25
34
|
for (let message of messages) {
|
|
26
35
|
if (!first_found && message.role !== 'system')
|
|
@@ -46,14 +55,12 @@ export default class OpenAIModel extends Model {
|
|
|
46
55
|
model: this.name,
|
|
47
56
|
messages: convertedMessages,
|
|
48
57
|
functions,
|
|
49
|
-
...payload,
|
|
50
58
|
};
|
|
51
59
|
|
|
52
|
-
if (
|
|
60
|
+
if (options.force_function && completion_payload.functions.length)
|
|
61
|
+
completion_payload.function_call = {name: options.force_function};
|
|
62
|
+
if (!completion_payload.functions.length)
|
|
53
63
|
delete completion_payload.functions;
|
|
54
|
-
if (completion_payload.hasOwnProperty('function_call'))
|
|
55
|
-
delete completion_payload.function_call;
|
|
56
|
-
}
|
|
57
64
|
|
|
58
65
|
const chatCompletion = await this.getOpenAi().chat.completions.create(completion_payload);
|
|
59
66
|
const completion = chatCompletion.choices[0].message;
|