modelmix 3.1.2 → 3.1.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/index.js +17 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -234,25 +234,34 @@ 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
|
-
|
|
249
|
+
console.log(response);
|
|
250
|
+
return JSON.parse(this._extractBlock(response));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
_extractBlock(response) {
|
|
254
|
+
const block = response.match(/```(?:\w+)?\s*([\s\S]*?)```/);
|
|
255
|
+
return block ? block[1].trim() : response;
|
|
246
256
|
}
|
|
247
257
|
|
|
248
258
|
async block({ addSystemExtra = true } = {}) {
|
|
249
259
|
if (addSystemExtra) {
|
|
250
|
-
this.config.systemExtra = "\nReturn the result of the task between triple backtick block code tags
|
|
260
|
+
this.config.systemExtra = "\nReturn the result of the task between triple backtick block code tags ```";
|
|
251
261
|
}
|
|
252
262
|
const response = await this.message();
|
|
253
263
|
this.config.systemExtra = "";
|
|
254
|
-
|
|
255
|
-
return block ? block[1].trim() : response;
|
|
264
|
+
return this._extractBlock(response);
|
|
256
265
|
}
|
|
257
266
|
|
|
258
267
|
async raw() {
|
|
@@ -574,6 +583,8 @@ class MixAnthropic extends MixCustom {
|
|
|
574
583
|
throw new Error('Anthropic API key not found. Please provide it in config or set ANTHROPIC_API_KEY environment variable.');
|
|
575
584
|
}
|
|
576
585
|
|
|
586
|
+
delete args.options.response_format;
|
|
587
|
+
|
|
577
588
|
args.options.system = args.config.system + args.config.systemExtra;
|
|
578
589
|
return super.create(args);
|
|
579
590
|
}
|