symposium 0.12.4 → 0.12.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
@@ -12,7 +12,6 @@ export default class Agent {
12
12
  max_retries = 5;
13
13
  callbacks = {};
14
14
  utility = null;
15
- supports_structured_output = false;
16
15
 
17
16
  constructor(options) {
18
17
  this.options = {
@@ -105,6 +104,8 @@ export default class Agent {
105
104
  if (counter === 0)
106
105
  thread = await this.beforeExecute(thread);
107
106
 
107
+ const model = Symposium.getModelByName(thread.state.model);
108
+
108
109
  const completion_options = {};
109
110
  if (this.utility) {
110
111
  if (!['function', 'text'].includes(this.utility.type))
@@ -114,13 +115,16 @@ export default class Agent {
114
115
  if (!this.utility.function || !this.utility.function.name || !this.utility.function.parameters)
115
116
  throw new Error('Bad function definition');
116
117
 
117
- if (this.supports_structured_output) {
118
- // TODO: se ci sono più di 100 parametri, OpenAI non supporta gli structured output
118
+ let response_format = null;
119
+ if (model.supports_structured_output)
120
+ response_format = this.convertFunctionToResponseFormat(this.utility.function.parameters);
121
+
122
+ if (response_format && response_format.count <= 100) { // Se ci sono più di 100 parametri, OpenAI non supporta gli structured output
119
123
  completion_options.response_format = {
120
124
  type: 'json_schema',
121
125
  json_schema: {
122
126
  name: this.utility.function.name,
123
- schema: this.utility.function.parameters,
127
+ schema: response_format.obj,
124
128
  strict: true,
125
129
  },
126
130
  };
@@ -160,6 +164,40 @@ export default class Agent {
160
164
  }
161
165
  }
162
166
 
167
+ convertFunctionToResponseFormat(obj) {
168
+ if (obj.type !== 'object')
169
+ return {obj, count: 0};
170
+
171
+ let properties_count = 0, required = [];
172
+
173
+ for (let [key, property] of Object.entries(obj.properties || {})) {
174
+ properties_count++;
175
+ required.push(key);
176
+
177
+ if (property.type === 'object') {
178
+ const {obj: subobj, count} = this.convertFunctionToResponseFormat(property);
179
+ obj.properties[key] = subobj;
180
+ properties_count += count;
181
+ } else if (property.type === 'array' && property.items.type === 'object') {
182
+ const {obj: subobj, count} = this.convertFunctionToResponseFormat(property.items);
183
+ obj.properties[key] = {
184
+ type: 'array',
185
+ items: subobj,
186
+ };
187
+ properties_count += count;
188
+ }
189
+ }
190
+
191
+ return {
192
+ obj: {
193
+ ...obj,
194
+ additionalProperties: false,
195
+ required,
196
+ },
197
+ count: properties_count,
198
+ };
199
+ }
200
+
163
201
  async afterExecute(thread, completion) {
164
202
  return thread;
165
203
  }
@@ -236,6 +274,8 @@ export default class Agent {
236
274
  }
237
275
 
238
276
  async handleCompletion(thread, completion) {
277
+ const model = Symposium.getModelByName(thread.state.model);
278
+
239
279
  const functions = [];
240
280
  for (let message of completion) {
241
281
  thread.addDirectMessage(message);
@@ -247,7 +287,7 @@ export default class Agent {
247
287
  if (this.utility) {
248
288
  if (this.utility.type === 'text')
249
289
  return this.afterHandle(thread, completion, 'return', m.content);
250
- if (this.utility.type === 'function' && this.supports_structured_output)
290
+ if (this.utility.type === 'function' && model.supports_structured_output)
251
291
  return this.afterHandle(thread, completion, 'return', JSON.parse(m.content));
252
292
  }
253
293
  await this.output(thread, m.content);
package/Model.js CHANGED
@@ -4,6 +4,7 @@ export default class Model {
4
4
  label;
5
5
  tokens;
6
6
  supports_functions = false;
7
+ supports_structured_output = false;
7
8
 
8
9
  constructor() {
9
10
  if (!this.label)
@@ -62,6 +62,9 @@ export default class OpenAIModel extends Model {
62
62
  };
63
63
  }
64
64
 
65
+ if (options.response_format)
66
+ completion_payload.response_format = options.response_format;
67
+
65
68
  if (!completion_payload.tools.length)
66
69
  delete completion_payload.tools;
67
70
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "symposium",
4
- "version": "0.12.4",
4
+ "version": "0.12.6",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",