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 +44 -7
- package/models/OpenAIModel.js +3 -0
- package/package.json +1 -1
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', '
|
|
111
|
+
if (!['text', 'function', 'json'].includes(this.utility.type))
|
|
112
112
|
throw new Error('Bad utility definition');
|
|
113
113
|
|
|
114
|
-
if (this.utility.type
|
|
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
|
-
|
|
119
|
-
|
|
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:
|
|
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 === '
|
|
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
|
|
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);
|
package/models/OpenAIModel.js
CHANGED