symposium 0.12.5 → 0.12.7

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
@@ -108,20 +108,23 @@ export default class Agent {
108
108
 
109
109
  const completion_options = {};
110
110
  if (this.utility) {
111
- if (!['function', 'text'].includes(this.utility.type))
111
+ if (!['text', 'function', 'json'].includes(this.utility.type))
112
112
  throw new Error('Bad utility definition');
113
113
 
114
- if (this.utility.type === 'function') {
114
+ if (['function', 'json'].includes(this.utility.type)) {
115
115
  if (!this.utility.function || !this.utility.function.name || !this.utility.function.parameters)
116
116
  throw new Error('Bad function definition');
117
117
 
118
- if (model.supports_structured_output) {
119
- // TODO: se ci sono più di 100 parametri, OpenAI non supporta gli structured output
118
+ let response_format = null;
119
+ if (this.utility.type === 'json' && 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
120
123
  completion_options.response_format = {
121
124
  type: 'json_schema',
122
125
  json_schema: {
123
126
  name: this.utility.function.name,
124
- schema: this.utility.function.parameters,
127
+ schema: response_format.obj,
125
128
  strict: true,
126
129
  },
127
130
  };
@@ -161,6 +164,40 @@ export default class Agent {
161
164
  }
162
165
  }
163
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
+
164
201
  async afterExecute(thread, completion) {
165
202
  return thread;
166
203
  }
@@ -250,7 +287,7 @@ export default class Agent {
250
287
  if (this.utility) {
251
288
  if (this.utility.type === 'text')
252
289
  return this.afterHandle(thread, completion, 'return', m.content);
253
- if (this.utility.type === 'function' && model.supports_structured_output)
290
+ if (this.utility.type === 'json' && model.supports_structured_output)
254
291
  return this.afterHandle(thread, completion, 'return', JSON.parse(m.content));
255
292
  }
256
293
  await this.output(thread, m.content);
@@ -266,7 +303,7 @@ export default class Agent {
266
303
 
267
304
  if (functions.length) {
268
305
  for (let f of functions) {
269
- if (this.utility && this.utility.type === 'function')
306
+ if (this.utility && ['function', 'json'].includes(this.utility.type))
270
307
  return this.afterHandle(thread, completion, 'return', f.arguments);
271
308
 
272
309
  const response = await this.callFunction(thread, f);
@@ -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.5",
4
+ "version": "0.12.7",
5
5
  "description": "Agents",
6
6
  "main": "index.js",
7
7
  "author": "Domenico Giambra",