symposium 0.7.5 → 0.8.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 CHANGED
@@ -7,7 +7,6 @@ export default class Agent {
7
7
  options = {};
8
8
  threads;
9
9
  functions = null;
10
- middlewares = new Map();
11
10
  tools = new Map();
12
11
  default_model = 'gpt-4-turbo';
13
12
 
@@ -43,10 +42,6 @@ export default class Agent {
43
42
  this.tools.set(tool.name, tool);
44
43
  }
45
44
 
46
- addMiddleware(middleware) {
47
- this.middlewares.set(middleware.name, middleware);
48
- }
49
-
50
45
  async initThread(thread) {
51
46
  await this.doInitThread(thread);
52
47
  await thread.storeState();
@@ -97,27 +92,9 @@ export default class Agent {
97
92
  if (this.options.memory_handler)
98
93
  thread = await this.options.memory_handler.handle(thread);
99
94
 
100
- for (let middleware of this.middlewares.values()) {
101
- let proceed = await middleware.before_exec(thread);
102
- if (!proceed) {
103
- await thread.storeState();
104
- return;
105
- }
106
- }
107
-
108
95
  const completion = await this.generateCompletion(thread);
109
-
110
96
  if (completion)
111
97
  await this.handleCompletion(thread, completion);
112
-
113
- const reversedMiddlewares = [...this.middlewares.values()];
114
- reversedMiddlewares.reverse();
115
-
116
- for (let middleware of reversedMiddlewares) {
117
- let proceed = await middleware.after_exec(thread);
118
- if (!proceed)
119
- return;
120
- }
121
98
  }
122
99
 
123
100
  async generateCompletion(thread, options = {}, retry_counter = 1) {
package/Summarizer.js CHANGED
@@ -13,11 +13,16 @@ export default class Summarizer extends MemoryHandler {
13
13
  if (!model)
14
14
  return thread;
15
15
 
16
- const tokens = await model.countTokens(thread);
17
- if (tokens >= model.tokens * this.threshold)
18
- return await this.summarize(model, thread, model.tokens * this.summary_length);
19
- else
16
+ try {
17
+ const tokens = await model.countTokens(thread);
18
+ if (tokens >= model.tokens * this.threshold)
19
+ return await this.summarize(model, thread, model.tokens * this.summary_length);
20
+ else
21
+ return thread;
22
+ } catch (e) {
23
+ console.error('Summarizer error: ' + String(e));
20
24
  return thread;
25
+ }
21
26
  }
22
27
 
23
28
  async summarize(model, thread, maxLength) {
package/Symposium.js CHANGED
@@ -6,6 +6,8 @@ import Whisper from "./models/Whisper.js";
6
6
  import Claude3Haiku from "./models/Claude3Haiku.js";
7
7
  import Claude3Sonnet from "./models/Claude3Sonnet.js";
8
8
  import Claude3Opus from "./models/Claude3Opus.js";
9
+ import Llama3 from "./models/Llama3.js";
10
+ import Mixtral8 from "./models/Mixtral8.js";
9
11
 
10
12
  export default class Symposium {
11
13
  static models = new Map();
@@ -20,6 +22,9 @@ export default class Symposium {
20
22
  this.loadModel(new Claude3Sonnet());
21
23
  this.loadModel(new Claude3Opus());
22
24
 
25
+ this.loadModel(new Llama3());
26
+ this.loadModel(new Mixtral8());
27
+
23
28
  return Redis.init();
24
29
  }
25
30
 
package/index.js CHANGED
@@ -3,11 +3,9 @@
3
3
  import Symposium from "./Symposium.js";
4
4
  import Agent from "./Agent.js";
5
5
  import Thread from "./Thread.js";
6
- import Message from "./Message.js";
7
6
  import Tool from "./Tool.js";
8
7
  import Logger from "./Logger.js";
9
8
  import MemoryHandler from "./MemoryHandler.js";
10
- import Middleware from "./Middleware.js";
11
9
  import Summarizer from "./Summarizer.js";
12
10
 
13
11
  export {
@@ -17,6 +15,5 @@ export {
17
15
  Tool,
18
16
  Logger,
19
17
  MemoryHandler,
20
- Middleware,
21
18
  Summarizer,
22
19
  };
@@ -0,0 +1,156 @@
1
+ import Model from "../Model.js";
2
+ import Groq from "groq-sdk";
3
+ import Message from "../Message.js";
4
+
5
+ export default class GroqModel extends Model {
6
+ groq;
7
+ supports_functions = true;
8
+
9
+ getGroq() {
10
+ if (!this.groq)
11
+ this.groq = new Groq();
12
+
13
+ return this.groq;
14
+ }
15
+
16
+ async generate(thread, functions = [], options = {}) {
17
+ const parsed = this.parseOptions(options, functions);
18
+ options = parsed.options;
19
+ functions = parsed.functions;
20
+
21
+ let messages = thread.messages;
22
+
23
+ if (functions.length && !this.supports_functions) {
24
+ // Se il modello non supporta nativamente le funzioni, inserisco il prompt ad hoc come ultimo messaggio di sistema
25
+ const functions_prompt = this.promptFromFunctions(options, functions);
26
+ let system_messages = [], other_messages = [], first_found = false;
27
+ for (let message of messages) {
28
+ if (!first_found && message.role !== 'system')
29
+ first_found = true;
30
+
31
+ if (!first_found)
32
+ system_messages.push(message);
33
+ else
34
+ other_messages.push(message);
35
+ }
36
+
37
+ system_messages.push(new Message('system', functions_prompt));
38
+
39
+ messages = [...system_messages, ...other_messages];
40
+ functions = [];
41
+ }
42
+
43
+ const convertedMessages = [];
44
+ for (let m of messages)
45
+ convertedMessages.push(...this.convertMessage(m));
46
+
47
+ const completion_payload = {
48
+ model: this.name,
49
+ messages: convertedMessages,
50
+ tools: functions.map(f => ({
51
+ type: 'function',
52
+ function: f,
53
+ })),
54
+ };
55
+
56
+ if (options.force_function) {
57
+ completion_payload.tool_choice = {
58
+ type: 'function',
59
+ function: {name: options.force_function},
60
+ };
61
+ }
62
+
63
+ if (!completion_payload.tools.length)
64
+ delete completion_payload.tools;
65
+
66
+ const chatCompletion = await this.getGroq().chat.completions.create(completion_payload);
67
+ const completion = chatCompletion.choices[0].message;
68
+
69
+ const message_content = [];
70
+ if (completion.content)
71
+ message_content.push({type: 'text', content: completion.content});
72
+
73
+ if (completion.tool_calls?.length) {
74
+ message_content.push({
75
+ type: 'function',
76
+ content: completion.tool_calls.map(tool_call => {
77
+ if (tool_call.type !== 'function')
78
+ throw new Error('Unsupported tool type ' + tool_call.type);
79
+
80
+ return {
81
+ id: tool_call.id,
82
+ name: tool_call.function.name,
83
+ arguments: tool_call.function.arguments ? JSON.parse(tool_call.function.arguments) : {},
84
+ };
85
+ }),
86
+ });
87
+ }
88
+
89
+ return [
90
+ new Message('assistant', message_content),
91
+ ];
92
+ }
93
+
94
+ convertMessage(message) {
95
+ const messages = [];
96
+ for (let c of message.content) {
97
+ switch (c.type) {
98
+ case 'text':
99
+ messages.push({
100
+ role: message.role,
101
+ content: c.content,
102
+ name: message.name,
103
+ });
104
+ break;
105
+
106
+ case 'image':
107
+ throw new Error('Images not supported by this model');
108
+
109
+ case 'function':
110
+ if (this.supports_functions) {
111
+ messages.push({
112
+ role: message.role,
113
+ name: message.name,
114
+ tool_calls: c.content.map(tool_call => ({
115
+ id: tool_call.id,
116
+ type: 'function',
117
+ function: {
118
+ name: tool_call.name,
119
+ arguments: tool_call.arguments ? JSON.stringify(tool_call.arguments) : '{}',
120
+ },
121
+ })),
122
+ });
123
+ } else {
124
+ messages.push({
125
+ role: message.role,
126
+ content: c.content.map(f => '```CALL \n' + f.name + '\n' + JSON.stringify(f.arguments || {}) + '\n```').join("\n\n"),
127
+ name: message.name,
128
+ });
129
+ }
130
+ break;
131
+
132
+ case 'function_response':
133
+ if (this.supports_functions) {
134
+ messages.push({
135
+ role: message.role,
136
+ tool_call_id: c.content.id,
137
+ content: JSON.stringify(c.content.response),
138
+ name: message.name,
139
+ });
140
+ } else {
141
+ messages.push({
142
+ role: 'user',
143
+ content: 'FUNCTION RESPONSE:\n' + JSON.stringify(c.content.response),
144
+ name: message.name,
145
+ });
146
+ }
147
+ break;
148
+
149
+ default:
150
+ throw new Error('Message type unsupported by this model');
151
+ }
152
+ }
153
+
154
+ return messages;
155
+ }
156
+ }
@@ -0,0 +1,7 @@
1
+ import GroqModel from "./GroqModel.js";
2
+
3
+ export default class Llama3 extends GroqModel {
4
+ name = 'llama3-70b-8192';
5
+ label = 'llama-3';
6
+ tokens = 8192;
7
+ }
@@ -0,0 +1,7 @@
1
+ import GroqModel from "./GroqModel.js";
2
+
3
+ export default class Mixtral8 extends GroqModel {
4
+ name = 'mixtral-8x7b-32768';
5
+ label = 'mixtral-8';
6
+ tokens = 32768;
7
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "symposium",
4
- "version": "0.7.5",
4
+ "version": "0.8.0",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",
@@ -9,6 +9,7 @@
9
9
  "dependencies": {
10
10
  "@anthropic-ai/sdk": "^0.20.0",
11
11
  "@travio/redis": "^2.0.0",
12
+ "groq-sdk": "^0.3.2",
12
13
  "openai": "^4.12.1",
13
14
  "tiktoken": "^1.0.10"
14
15
  }
package/Middleware.js DELETED
@@ -1,16 +0,0 @@
1
- export default class Middleware {
2
- name;
3
- agent;
4
-
5
- constructor(agent) {
6
- this.agent = agent;
7
- }
8
-
9
- async before_exec(thread) {
10
- return true;
11
- }
12
-
13
- async after_exec(thread) {
14
- return true;
15
- }
16
- }