symposium 0.15.4 → 0.15.5

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.
Files changed (3) hide show
  1. package/Agent.js +82 -81
  2. package/README.md +2 -8
  3. package/package.json +1 -1
package/Agent.js CHANGED
@@ -123,84 +123,88 @@ export default class Agent {
123
123
  async execute(thread, counter = 0, existing_emitter = null) {
124
124
  const emitter = existing_emitter || new BufferedEventEmitter();
125
125
 
126
- if (counter === 0)
127
- thread = await this.beforeExecute(thread, emitter);
128
-
129
- const model = Symposium.getModelByName(thread.state.model);
126
+ const execution = new Promise(async (resolve, reject) => {
127
+ try {
128
+ if (counter === 0)
129
+ thread = await this.beforeExecute(thread, emitter);
130
+
131
+ const model = Symposium.getModelByName(thread.state.model);
132
+
133
+ const completion_options = {};
134
+ if (this.type === 'utility') {
135
+ if (['function', 'json'].includes(this.utility.type)) {
136
+ if (!this.utility.function || !this.utility.function.name || !this.utility.function.parameters)
137
+ throw new Error('Bad function definition');
138
+
139
+ let response_format = null;
140
+ if (this.utility.type === 'json' && model.supports_structured_output)
141
+ response_format = this.convertFunctionToResponseFormat(this.utility.function.parameters);
142
+
143
+ if (response_format && response_format.count <= 100) { // OpenAI does not support structured output if there are more than 100 parameters
144
+ completion_options.response_format = {
145
+ type: 'json_schema',
146
+ json_schema: {
147
+ name: this.utility.function.name,
148
+ schema: response_format.obj,
149
+ strict: true,
150
+ },
151
+ };
152
+ } else {
153
+ completion_options.functions = [
154
+ this.utility.function,
155
+ ];
156
+ completion_options.force_function = this.utility.function.name;
157
+ }
158
+ }
159
+ }
130
160
 
131
- const completion_options = {};
132
- if (this.type === 'utility') {
133
- if (['function', 'json'].includes(this.utility.type)) {
134
- if (!this.utility.function || !this.utility.function.name || !this.utility.function.parameters)
135
- throw new Error('Bad function definition');
136
-
137
- let response_format = null;
138
- if (this.utility.type === 'json' && model.supports_structured_output)
139
- response_format = this.convertFunctionToResponseFormat(this.utility.function.parameters);
140
-
141
- if (response_format && response_format.count <= 100) { // OpenAI does not support structured output if there are more than 100 parameters
142
- completion_options.response_format = {
143
- type: 'json_schema',
144
- json_schema: {
145
- name: this.utility.function.name,
146
- schema: response_format.obj,
147
- strict: true,
148
- },
149
- };
150
- } else {
151
- completion_options.functions = [
152
- this.utility.function,
153
- ];
154
- completion_options.force_function = this.utility.function.name;
161
+ let completion;
162
+ try {
163
+ completion = await this.generateCompletion(thread, completion_options);
164
+ } catch (e) {
165
+ console.error(e.message);
166
+ switch (this.type) {
167
+ case 'chat':
168
+ emitter.emit('error', e.message);
169
+ return resolve(e);
170
+
171
+ case 'utility':
172
+ throw e;
173
+
174
+ default:
175
+ throw new Error('Bad agent type');
176
+ }
155
177
  }
156
- }
157
- }
158
178
 
159
- let completion;
160
- try {
161
- completion = await this.generateCompletion(thread, completion_options);
162
- } catch (e) {
163
- console.error(e.message);
164
- switch (this.type) {
165
- case 'chat':
166
- emitter.emit('error', e.message);
167
- return emitter;
168
-
169
- case 'utility':
170
- throw e;
171
-
172
- default:
173
- throw new Error('Bad agent type');
174
- }
175
- }
179
+ try {
180
+ thread = await this.afterExecute(thread, completion, emitter);
181
+ const response = await this.handleCompletion(thread, completion, emitter);
176
182
 
177
- try {
178
- thread = await this.afterExecute(thread, completion);
179
- const response = await this.handleCompletion(thread, completion, emitter);
180
-
181
- switch (this.type) {
182
- case 'utility':
183
- return response;
184
-
185
- case 'chat':
186
- if (!existing_emitter) {
187
- emitter.on('data', data => {
188
- if (data.type === 'response' && data.content.type === 'continue')
189
- this.execute(thread, 0, emitter);
190
- }, false);
191
- }
183
+ switch (this.type) {
184
+ case 'utility':
185
+ return resolve(response);
186
+
187
+ case 'chat':
188
+ if (response.type === 'continue')
189
+ return this.execute(thread, 0, emitter);
190
+
191
+ return resolve(null);
192
192
 
193
- return emitter;
193
+ default:
194
+ throw new Error('Bad agent type');
195
+ }
196
+ } catch (e) {
197
+ console.error(e);
194
198
 
195
- default:
196
- throw new Error('Bad agent type');
199
+ if (counter < this.max_retries)
200
+ await this.execute(thread, counter + 1, emitter);
201
+ }
202
+ } catch (e) {
203
+ reject(e);
197
204
  }
198
- } catch (e) {
199
- console.error(e);
205
+ });
200
206
 
201
- if (counter < this.max_retries)
202
- await this.execute(thread, counter + 1, emitter);
203
- }
207
+ return this.type === 'chat' ? emitter : execution;
204
208
  }
205
209
 
206
210
  convertFunctionToResponseFormat(obj) {
@@ -242,7 +246,7 @@ export default class Agent {
242
246
  };
243
247
  }
244
248
 
245
- async afterExecute(thread, completion) {
249
+ async afterExecute(thread, completion, emitter) {
246
250
  return thread;
247
251
  }
248
252
 
@@ -311,9 +315,9 @@ export default class Agent {
311
315
  case 'text':
312
316
  if (this.type === 'utility') {
313
317
  if (this.utility.type === 'text')
314
- return this.afterHandle(thread, completion, 'return', m.content);
318
+ return this.afterHandle(thread, completion, m.content);
315
319
  if (this.utility.type === 'json' && model.supports_structured_output)
316
- return this.afterHandle(thread, completion, 'return', JSON.parse(m.content));
320
+ return this.afterHandle(thread, completion, JSON.parse(m.content));
317
321
  }
318
322
 
319
323
  emitter.emit('output', m.content);
@@ -330,7 +334,7 @@ export default class Agent {
330
334
  if (functions.length) {
331
335
  for (let f of functions) {
332
336
  if (this.utility && ['function', 'json'].includes(this.utility.type))
333
- return this.afterHandle(thread, completion, 'return', f.arguments);
337
+ return this.afterHandle(thread, completion, f.arguments);
334
338
 
335
339
  const function_response = await this.callFunction(thread, f, emitter);
336
340
 
@@ -344,18 +348,15 @@ export default class Agent {
344
348
  await this.log('function_response', function_response);
345
349
  }
346
350
 
347
- return this.afterHandle(thread, completion, 'continue');
351
+ return this.afterHandle(thread, completion);
348
352
  } else {
349
353
  await thread.storeState();
350
- return this.afterHandle(thread, completion, 'void');
354
+ return this.afterHandle(thread, completion);
351
355
  }
352
356
  }
353
357
 
354
- async afterHandle(thread, completion, type, value = null) {
355
- return {
356
- type,
357
- value,
358
- };
358
+ async afterHandle(thread, completion, value = null) {
359
+ return value;
359
360
  }
360
361
 
361
362
  async getFunctions(parsed = true) {
package/README.md CHANGED
@@ -82,14 +82,8 @@ async function main() {
82
82
 
83
83
  const emitter = await agent.message('Hello, who are you?');
84
84
 
85
- emitter.on('data', (data) => {
86
- if (data.type === 'output') {
87
- process.stdout.write(data.content);
88
- }
89
- });
90
-
91
- emitter.on('end', () => {
92
- console.log('\nConversation ended.');
85
+ emitter.on('output', (content) => {
86
+ process.stdout.write(content);
93
87
  });
94
88
  }
95
89
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "symposium",
4
- "version": "0.15.4",
4
+ "version": "0.15.5",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",