symposium 0.4.2 → 0.4.4
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 +5 -2
- package/Model.js +24 -2
- package/Symposium.js +3 -3
- package/models/AnthropicModel.js +20 -3
- package/models/Gpt4Vision.js +1 -1
- package/models/OpenAIModel.js +24 -2
- package/package.json +1 -1
package/Agent.js
CHANGED
|
@@ -123,7 +123,7 @@ export default class Agent {
|
|
|
123
123
|
async generateCompletion(thread, payload = {}, retry_counter = 1) {
|
|
124
124
|
try {
|
|
125
125
|
const model = Symposium.getModelByName(thread.state.model);
|
|
126
|
-
return model.generate(thread, payload, await this.getFunctions())
|
|
126
|
+
return model.generate(thread, payload, await this.getFunctions());
|
|
127
127
|
} catch (error) {
|
|
128
128
|
if (error.response) {
|
|
129
129
|
console.error(error.response.status);
|
|
@@ -149,8 +149,11 @@ export default class Agent {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
async handleCompletion(thread, completion) {
|
|
152
|
-
for (let message of completion.messages)
|
|
152
|
+
for (let message of completion.messages) {
|
|
153
153
|
thread.addMessage(message);
|
|
154
|
+
await this.log('ai_message', message.text);
|
|
155
|
+
await thread.reply(message.text);
|
|
156
|
+
}
|
|
154
157
|
|
|
155
158
|
if (completion.function) {
|
|
156
159
|
thread.addAssistantMessage('', {
|
package/Model.js
CHANGED
|
@@ -4,7 +4,7 @@ export default class Model {
|
|
|
4
4
|
name_for_tiktoken;
|
|
5
5
|
label;
|
|
6
6
|
tokens;
|
|
7
|
-
|
|
7
|
+
supports_functions = false;
|
|
8
8
|
|
|
9
9
|
constructor() {
|
|
10
10
|
if (!this.label)
|
|
@@ -13,7 +13,29 @@ export default class Model {
|
|
|
13
13
|
this.name_for_tiktoken = this.name;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
async generate(thread) {
|
|
16
|
+
async generate(thread, payload = {}, functions = []) {
|
|
17
17
|
return null;
|
|
18
18
|
}
|
|
19
|
+
|
|
20
|
+
promptFromFunctions(functions) {
|
|
21
|
+
if (!functions.length)
|
|
22
|
+
return null;
|
|
23
|
+
|
|
24
|
+
let message = "Hai a disposizione le seguenti funzioni, per chiamare una funzione scrivi un messaggio che inizia con CALL nome_funzione e a capo inserisci il JSON con gli argomenti, ad esempio:\n" +
|
|
25
|
+
"CALL create_user\n" +
|
|
26
|
+
'{"name":"test"}' + "\n\n" +
|
|
27
|
+
"Lista funzioni:\n";
|
|
28
|
+
|
|
29
|
+
for (let f of functions)
|
|
30
|
+
message += '- ' + f.name + "\n " + f.description + "\n";
|
|
31
|
+
|
|
32
|
+
message += "\nOpenAPI specs:\n\n";
|
|
33
|
+
for (let f of functions) {
|
|
34
|
+
if (!f.parameters)
|
|
35
|
+
continue;
|
|
36
|
+
message += '=== ' + f.name + " ===\n" + JSON.stringify(f.parameters.properties) + "\n\n";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return message;
|
|
40
|
+
}
|
|
19
41
|
}
|
package/Symposium.js
CHANGED
|
@@ -4,9 +4,9 @@ import Gpt4 from "./models/Gpt4.js";
|
|
|
4
4
|
import Gpt4Turbo from "./models/Gpt4Turbo.js";
|
|
5
5
|
import Gpt4Vision from "./models/Gpt4Vision.js";
|
|
6
6
|
import Whisper from "./models/Whisper.js";
|
|
7
|
-
import Claude3Haiku from "
|
|
8
|
-
import Claude3Sonnet from "
|
|
9
|
-
import Claude3Opus from "
|
|
7
|
+
import Claude3Haiku from "./models/Claude3Haiku.js";
|
|
8
|
+
import Claude3Sonnet from "./models/Claude3Sonnet.js";
|
|
9
|
+
import Claude3Opus from "./models/Claude3Opus.js";
|
|
10
10
|
|
|
11
11
|
export default class Symposium {
|
|
12
12
|
static models = new Map();
|
package/models/AnthropicModel.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import Model from "../Model.js";
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
import Response from "../Response.js";
|
|
4
|
+
import Message from "../Message.js";
|
|
3
5
|
|
|
4
6
|
export default class AnthropicModel extends Model {
|
|
5
7
|
anthropic;
|
|
6
|
-
|
|
8
|
+
supports_functions = false;
|
|
7
9
|
|
|
8
10
|
getAnthropic() {
|
|
9
11
|
if (!this.anthropic)
|
|
@@ -13,7 +15,14 @@ export default class AnthropicModel extends Model {
|
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
async generate(thread, payload = {}, functions = []) {
|
|
16
|
-
|
|
18
|
+
let [system, messages] = this.convertMessages(thread);
|
|
19
|
+
|
|
20
|
+
if (functions.length && !this.supports_functions) {
|
|
21
|
+
// Se il modello non supporta nativamente le funzioni, aggiungo il prompt al messaggio di sistema
|
|
22
|
+
const functions_prompt = this.promptFromFunctions(functions);
|
|
23
|
+
system += functions_prompt;
|
|
24
|
+
functions = [];
|
|
25
|
+
}
|
|
17
26
|
|
|
18
27
|
const completion_payload = {
|
|
19
28
|
model: this.name,
|
|
@@ -24,7 +33,15 @@ export default class AnthropicModel extends Model {
|
|
|
24
33
|
};
|
|
25
34
|
|
|
26
35
|
const message = await this.getAnthropic().messages.create(completion_payload);
|
|
27
|
-
|
|
36
|
+
|
|
37
|
+
const response = new Response;
|
|
38
|
+
if (message.content) {
|
|
39
|
+
for (let m of message.content)
|
|
40
|
+
// TODO: supporto ad altri tipi oltre a text (m.type)
|
|
41
|
+
response.messages.push(new Message('assistant', m.text));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return response;
|
|
28
45
|
}
|
|
29
46
|
|
|
30
47
|
convertMessages(thread) {
|
package/models/Gpt4Vision.js
CHANGED
package/models/OpenAIModel.js
CHANGED
|
@@ -5,7 +5,7 @@ import Message from "../Message.js";
|
|
|
5
5
|
|
|
6
6
|
export default class OpenAIModel extends Model {
|
|
7
7
|
openai;
|
|
8
|
-
|
|
8
|
+
supports_functions = true;
|
|
9
9
|
|
|
10
10
|
getOpenAi() {
|
|
11
11
|
if (!this.openai)
|
|
@@ -15,9 +15,31 @@ export default class OpenAIModel extends Model {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
async generate(thread, payload = {}, functions = []) {
|
|
18
|
+
let messages = thread.getMessagesJson();
|
|
19
|
+
|
|
20
|
+
if (functions.length && !this.supports_functions) {
|
|
21
|
+
// Se il modello non supporta nativamente le funzioni, inserisco il prompt ad hoc come ultimo messaggio di sistema
|
|
22
|
+
const functions_prompt = this.promptFromFunctions(functions);
|
|
23
|
+
let system_messages = [], other_messages = [], first_found = false;
|
|
24
|
+
for (let message of messages) {
|
|
25
|
+
if (!first_found && message.role !== 'system')
|
|
26
|
+
first_found = true;
|
|
27
|
+
|
|
28
|
+
if (!first_found)
|
|
29
|
+
system_messages.push(message);
|
|
30
|
+
else
|
|
31
|
+
other_messages.push(message);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
system_messages.push({role: 'system', content: functions_prompt});
|
|
35
|
+
|
|
36
|
+
messages = [...system_messages, ...other_messages];
|
|
37
|
+
functions = [];
|
|
38
|
+
}
|
|
39
|
+
|
|
18
40
|
const completion_payload = {
|
|
19
41
|
model: this.name,
|
|
20
|
-
messages
|
|
42
|
+
messages,
|
|
21
43
|
functions,
|
|
22
44
|
...payload,
|
|
23
45
|
};
|