symposium 0.4.4 → 0.4.6

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 CHANGED
@@ -1,5 +1,6 @@
1
1
  import Symposium from "./Symposium.js";
2
2
  import Thread from "./Thread.js";
3
+ import Message from "./Message.js";
3
4
 
4
5
  export default class Agent {
5
6
  name = 'Agent';
@@ -88,7 +89,7 @@ export default class Agent {
88
89
 
89
90
  async message(thread, text) {
90
91
  await this.log('user_message', text);
91
- thread.addUserMessage(text);
92
+ thread.addMessage('user', 'text', text);
92
93
 
93
94
  await this.execute(thread, text);
94
95
  }
@@ -123,7 +124,16 @@ export default class Agent {
123
124
  async generateCompletion(thread, payload = {}, retry_counter = 1) {
124
125
  try {
125
126
  const model = Symposium.getModelByName(thread.state.model);
126
- return model.generate(thread, payload, await this.getFunctions());
127
+
128
+ const response = await model.generate(thread, payload, await this.getFunctions());
129
+ if (!model.supports_functions) {
130
+ const newMessages = [];
131
+ for (let message of response.messages)
132
+ newMessages.concat(...this.parseFunctions(message));
133
+ response.messages = newMessages;
134
+ }
135
+
136
+ return response;
127
137
  } catch (error) {
128
138
  if (error.response) {
129
139
  console.error(error.response.status);
@@ -148,22 +158,46 @@ export default class Agent {
148
158
  }
149
159
  }
150
160
 
151
- async handleCompletion(thread, completion) {
152
- for (let message of completion.messages) {
153
- thread.addMessage(message);
154
- await this.log('ai_message', message.text);
155
- await thread.reply(message.text);
161
+ parseFunctions(message) {
162
+ const messages = [];
163
+ if (message.type === 'text' && message.content.match(/```\nCALL [A-Za-z0-9_]+\n(\{.+\}\n)?```/)) {
164
+ const splitted = message.content.split('```');
165
+ for (let text of splitted) {
166
+ const match = text.match(/^CALL ([A-Za-z0-9_]+)\n([\s\S]*)$/);
167
+ if (match)
168
+ messages.push(new Message(message.role, 'function', {name: match[1], arguments: JSON.parse(match[2] || '{}')}));
169
+ else
170
+ messages.push(new Message(message.role, 'text', text.trim()));
171
+ }
172
+ } else {
173
+ messages.push(message);
156
174
  }
157
175
 
158
- if (completion.function) {
159
- thread.addAssistantMessage('', {
160
- name: completion.function.name,
161
- arguments: JSON.stringify(completion.function_call.args),
162
- });
176
+ return messages;
177
+ }
178
+
179
+ async handleCompletion(thread, completion) {
180
+ let call_function = null;
181
+ for (let message of completion.messages) {
182
+ thread.addDirectMessage(message);
183
+ await this.log('ai_message', message);
184
+
185
+ switch (message.type) {
186
+ case 'text':
187
+ await thread.reply(message.content);
188
+ break;
189
+
190
+ case 'function':
191
+ if (call_function)
192
+ throw new Error('The model replied with more than one function');
193
+ else
194
+ call_function = message.content;
195
+ break;
196
+ }
163
197
  }
164
198
 
165
- if (completion.function)
166
- return this.callFunction(thread, completion.function);
199
+ if (call_function)
200
+ return this.callFunction(thread, call_function);
167
201
  else
168
202
  return thread.storeState();
169
203
  }
@@ -199,11 +233,11 @@ export default class Agent {
199
233
  await this.log('function_call', function_call);
200
234
 
201
235
  try {
202
- const response = await functions.get(function_call.name).tool.callFunction(thread, function_call.name, function_call.args);
203
- thread.addFunctionMessage(response, function_call.name);
236
+ const response = await functions.get(function_call.name).tool.callFunction(thread, function_call.name, function_call.arguments);
237
+ thread.addMessage('function', 'text', JSON.stringify(response), function_call.name);
204
238
  await this.log('function_response', response);
205
239
  } catch (error) {
206
- thread.addFunctionMessage({error}, function_call.name);
240
+ thread.addMessage('function', 'text', JSON.stringify({error}), function_call.name);
207
241
  await this.log('function_response', {error});
208
242
  }
209
243
 
package/Message.js CHANGED
@@ -1,15 +1,15 @@
1
1
  export default class Message {
2
2
  role;
3
- text;
3
+ type;
4
+ content;
4
5
  name;
5
- function_call;
6
6
  tags = [];
7
7
 
8
- constructor(role, text, name = null, function_call = null, tags = []) {
8
+ constructor(role, type, content, name = null, tags = []) {
9
9
  this.role = role;
10
- this.text = text;
10
+ this.type = type;
11
+ this.content = content;
11
12
  this.name = name;
12
- this.function_call = function_call;
13
13
  this.tags = tags;
14
14
  }
15
15
  }
package/Model.js CHANGED
@@ -1,3 +1,5 @@
1
+ import Message from "./Message.js";
2
+
1
3
  export default class Model {
2
4
  type = 'llm';
3
5
  name;
@@ -19,12 +21,14 @@ export default class Model {
19
21
 
20
22
  promptFromFunctions(functions) {
21
23
  if (!functions.length)
22
- return null;
24
+ return '';
23
25
 
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" +
26
+ let message = "Hai a disposizione alcune funzioni che puoi chiamare per ottenere risposte o compiere azioni. 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" +
27
+ "```\n" +
25
28
  "CALL create_user\n" +
26
- '{"name":"test"}' + "\n\n" +
27
- "Lista funzioni:\n";
29
+ '{"name":"test"}' + "\n" +
30
+ "```\n\n" +
31
+ "Lista delle funzioni che hai a disposizione:\n";
28
32
 
29
33
  for (let f of functions)
30
34
  message += '- ' + f.name + "\n " + f.description + "\n";
package/Summarizer.js CHANGED
@@ -56,7 +56,7 @@ export default class Summarizer extends MemoryHandler {
56
56
  }
57
57
 
58
58
  async doSummarize(thread, maxLength) {
59
- thread.addSystemMessage('Summarize the conversation up to this moment.');
59
+ thread.addMessage('system', 'text', 'Summarize the conversation up to this moment.');
60
60
  const summary = await this.agent.generateCompletion(thread, {
61
61
  functions: [
62
62
  {
@@ -84,7 +84,7 @@ export default class Summarizer extends MemoryHandler {
84
84
  if (message.role === 'system' && !message.tags.includes('summary')) {
85
85
  summarizedThread.messages.push(message);
86
86
  } else {
87
- summarizedThread.addSystemMessage("This is what happened until now:\n" + summary.function_call.arguments.summary, ['summary']);
87
+ summarizedThread.addMessage('system', 'text', "This is what happened until now:\n" + summary.function_call.arguments.summary, ['summary']);
88
88
  break;
89
89
  }
90
90
  }
package/Thread.js CHANGED
@@ -33,7 +33,7 @@ export default class Thread {
33
33
  const conv = await Redis.get('thread-' + this.id);
34
34
  if (conv) {
35
35
  this.state = conv.state || {};
36
- this.messages = conv.messages.map(m => (new Message(m.role, m.text, m.name, m.function_call, m.tags || [])));
36
+ this.messages = conv.messages.map(m => new Message(m.role, m.type, m.content, m.tags));
37
37
  return true;
38
38
  } else {
39
39
  return false;
@@ -53,33 +53,12 @@ export default class Thread {
53
53
  }, 0);
54
54
  }
55
55
 
56
- getMessagesJson() {
57
- return this.messages.map(m => ({
58
- role: m.role,
59
- content: m.text,
60
- name: m.name || undefined,
61
- function_call: m.function_call || undefined,
62
- }));
63
- }
64
-
65
- addMessage(message) {
56
+ addDirectMessage(message) {
66
57
  this.messages.push(message);
67
58
  }
68
59
 
69
- addSystemMessage(text, tags = []) {
70
- this.messages.push(new Message('system', text, null, null, tags));
71
- }
72
-
73
- addUserMessage(text, name = null, tags = []) {
74
- this.messages.push(new Message('user', text, name, null, tags));
75
- }
76
-
77
- addAssistantMessage(text, function_call = null, tags = []) {
78
- this.messages.push(new Message('assistant', text, null, function_call, tags));
79
- }
80
-
81
- addFunctionMessage(response, name = null, tags = []) {
82
- this.messages.push(new Message('function', JSON.stringify(response), name, null, tags));
60
+ addMessage(role, type, content, name = null, tags = []) {
61
+ this.addDirectMessage(new Message(role, type, content, name, tags));
83
62
  }
84
63
 
85
64
  removeMessagesWithTag(tag) {
@@ -20,7 +20,7 @@ export default class AnthropicModel extends Model {
20
20
  if (functions.length && !this.supports_functions) {
21
21
  // Se il modello non supporta nativamente le funzioni, aggiungo il prompt al messaggio di sistema
22
22
  const functions_prompt = this.promptFromFunctions(functions);
23
- system += functions_prompt;
23
+ system += "\n\n" + functions_prompt;
24
24
  functions = [];
25
25
  }
26
26
 
@@ -38,7 +38,7 @@ export default class AnthropicModel extends Model {
38
38
  if (message.content) {
39
39
  for (let m of message.content)
40
40
  // TODO: supporto ad altri tipi oltre a text (m.type)
41
- response.messages.push(new Message('assistant', m.text));
41
+ response.messages.push(new Message('assistant', 'text', m.text));
42
42
  }
43
43
 
44
44
  return response;
@@ -46,11 +46,30 @@ export default class AnthropicModel extends Model {
46
46
 
47
47
  convertMessages(thread) {
48
48
  let system = [], messages = [];
49
- for (let message of thread.getMessagesJson()) {
50
- if (message.role === 'system')
49
+ for (let message of thread.messages) {
50
+ if (message.role === 'system') {
51
51
  system.push(message.content);
52
- else
53
- messages.push(message);
52
+ } else {
53
+ switch (message.type) {
54
+ case 'text':
55
+ messages.push({
56
+ role: message.role,
57
+ type: 'text',
58
+ content: message.content,
59
+ });
60
+ break;
61
+
62
+ case 'function':
63
+ messages.push({
64
+ role: message.role,
65
+ type: 'text',
66
+ content: '```CALL \n' + message.content.name + '\n' + JSON.stringify(message.content.arguments || {}) + '\n```',
67
+ });
68
+
69
+ default:
70
+ throw new Error('Message type unsupported by this model');
71
+ }
72
+ }
54
73
  }
55
74
 
56
75
  return [system.length ? system.join("\n") : undefined, messages];
@@ -15,7 +15,7 @@ export default class OpenAIModel extends Model {
15
15
  }
16
16
 
17
17
  async generate(thread, payload = {}, functions = []) {
18
- let messages = thread.getMessagesJson();
18
+ let messages = thread.messages;
19
19
 
20
20
  if (functions.length && !this.supports_functions) {
21
21
  // Se il modello non supporta nativamente le funzioni, inserisco il prompt ad hoc come ultimo messaggio di sistema
@@ -31,7 +31,7 @@ export default class OpenAIModel extends Model {
31
31
  other_messages.push(message);
32
32
  }
33
33
 
34
- system_messages.push({role: 'system', content: functions_prompt});
34
+ system_messages.push(new Message('system', 'text', functions_prompt));
35
35
 
36
36
  messages = [...system_messages, ...other_messages];
37
37
  functions = [];
@@ -39,7 +39,7 @@ export default class OpenAIModel extends Model {
39
39
 
40
40
  const completion_payload = {
41
41
  model: this.name,
42
- messages,
42
+ messages: messages.map(m => this.convertMessage(m)),
43
43
  functions,
44
44
  ...payload,
45
45
  };
@@ -51,19 +51,44 @@ export default class OpenAIModel extends Model {
51
51
  }
52
52
 
53
53
  const chatCompletion = await this.getOpenAi().chat.completions.create(completion_payload);
54
+ const completion = chatCompletion.choices[0].message;
54
55
 
55
56
  const response = new Response;
56
- const completion = chatCompletion.choices[0].message;
57
57
  if (completion.content)
58
- response.messages.push(new Message('assistant', completion.content));
59
-
60
- if (completion.function_call && completion.function_call.arguments) {
61
- response.function = {
62
- name: completion.function_call.name,
63
- args: JSON.parse(completion.function_call.arguments),
64
- };
65
- }
58
+ response.messages.push(new Message('assistant', 'text', completion.content));
59
+ if (completion.function_call && completion.function_call.arguments)
60
+ response.messages.push(new Message('assistant', 'function', completion.function_call));
66
61
 
67
62
  return response;
68
63
  }
64
+
65
+ convertMessage(message) {
66
+ switch (message.type) {
67
+ case 'text':
68
+ return {
69
+ role: message.role,
70
+ content: message.content,
71
+ name: message.name,
72
+ };
73
+
74
+ case 'function':
75
+ if (this.supports_functions) {
76
+ return {
77
+ role: message.role,
78
+ content: null,
79
+ name: message.name,
80
+ function_call: message.content,
81
+ };
82
+ } else {
83
+ return {
84
+ role: message.role,
85
+ content: message.content,
86
+ name: message.name,
87
+ };
88
+ }
89
+
90
+ default:
91
+ throw new Error('Message type unsupported by this model');
92
+ }
93
+ }
69
94
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "symposium",
4
- "version": "0.4.4",
4
+ "version": "0.4.6",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",