modelmix 3.1.2 → 3.1.8
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/index.js +16 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -234,25 +234,33 @@ class MessageHandler {
|
|
|
234
234
|
return response.message;
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
async json(schemaExample = null, schemaDescription = {}) {
|
|
238
|
-
this.options.response_format = { type
|
|
237
|
+
async json(schemaExample = null, schemaDescription = {}, { type = 'json_object', addExample = false } = {}) {
|
|
238
|
+
this.options.response_format = { type };
|
|
239
239
|
if (schemaExample) {
|
|
240
240
|
const schema = generateJsonSchema(schemaExample, schemaDescription);
|
|
241
241
|
this.config.systemExtra = "\nOutput JSON Schema: \n```\n" + JSON.stringify(schema) + "\n```";
|
|
242
|
+
|
|
243
|
+
if (addExample) {
|
|
244
|
+
this.config.systemExtra += "\nOutput Example: \n```\n" + JSON.stringify(schemaExample) + "\n```";
|
|
245
|
+
}
|
|
242
246
|
}
|
|
243
247
|
const response = await this.message();
|
|
244
248
|
this.config.systemExtra = "";
|
|
245
|
-
return JSON.parse(response);
|
|
249
|
+
return JSON.parse(this._extractBlock(response));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
_extractBlock(response) {
|
|
253
|
+
const block = response.match(/```(?:\w+)?\s*([\s\S]*?)```/);
|
|
254
|
+
return block ? block[1].trim() : response;
|
|
246
255
|
}
|
|
247
256
|
|
|
248
257
|
async block({ addSystemExtra = true } = {}) {
|
|
249
258
|
if (addSystemExtra) {
|
|
250
|
-
this.config.systemExtra = "\nReturn the result of the task between triple backtick block code tags
|
|
259
|
+
this.config.systemExtra = "\nReturn the result of the task between triple backtick block code tags ```";
|
|
251
260
|
}
|
|
252
261
|
const response = await this.message();
|
|
253
262
|
this.config.systemExtra = "";
|
|
254
|
-
|
|
255
|
-
return block ? block[1].trim() : response;
|
|
263
|
+
return this._extractBlock(response);
|
|
256
264
|
}
|
|
257
265
|
|
|
258
266
|
async raw() {
|
|
@@ -574,6 +582,8 @@ class MixAnthropic extends MixCustom {
|
|
|
574
582
|
throw new Error('Anthropic API key not found. Please provide it in config or set ANTHROPIC_API_KEY environment variable.');
|
|
575
583
|
}
|
|
576
584
|
|
|
585
|
+
delete args.options.response_format;
|
|
586
|
+
|
|
577
587
|
args.options.system = args.config.system + args.config.systemExtra;
|
|
578
588
|
return super.create(args);
|
|
579
589
|
}
|