@xsai/generate-object 0.1.0-beta.6 → 0.1.0-beta.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/dist/index.d.ts +10 -5
- package/dist/index.js +61 -24
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -6,10 +6,15 @@ interface GenerateObjectOptions<T extends Schema> extends GenerateTextOptions {
|
|
|
6
6
|
schemaDescription?: string;
|
|
7
7
|
schemaName?: string;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
object:
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
type GenerateObjectResult<O> = GenerateTextResult & {
|
|
10
|
+
object: O;
|
|
11
|
+
};
|
|
12
|
+
declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T> & {
|
|
13
|
+
output: 'array';
|
|
14
|
+
}): Promise<GenerateObjectResult<Array<Infer<T>>>>;
|
|
15
|
+
declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T> & {
|
|
16
|
+
output: 'object';
|
|
17
|
+
}): Promise<GenerateObjectResult<Infer<T>>>;
|
|
18
|
+
declare function generateObject<T extends Schema>(options: GenerateObjectOptions<T>): Promise<GenerateObjectResult<Infer<T>>>;
|
|
14
19
|
|
|
15
20
|
export { type GenerateObjectOptions, type GenerateObjectResult, generateObject };
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,69 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { generateText } from "@xsai/generate-text";
|
|
3
3
|
import { toJSONSchema, validate } from "xsschema";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
},
|
|
13
|
-
type: "json_schema"
|
|
4
|
+
|
|
5
|
+
// src/wrap.ts
|
|
6
|
+
var wrap = ({ $schema, ...schema }) => ({
|
|
7
|
+
properties: {
|
|
8
|
+
elements: {
|
|
9
|
+
items: schema,
|
|
10
|
+
type: "array"
|
|
11
|
+
}
|
|
14
12
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
schemaName: void 0
|
|
18
|
-
}).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => {
|
|
19
|
-
const object = await validate(options.schema, JSON.parse(text));
|
|
20
|
-
return {
|
|
21
|
-
finishReason,
|
|
22
|
-
messages,
|
|
23
|
-
object,
|
|
24
|
-
steps,
|
|
25
|
-
toolCalls,
|
|
26
|
-
toolResults,
|
|
27
|
-
usage
|
|
28
|
-
};
|
|
13
|
+
required: ["elements"],
|
|
14
|
+
type: "object"
|
|
29
15
|
});
|
|
16
|
+
|
|
17
|
+
// src/index.ts
|
|
18
|
+
async function generateObject(options) {
|
|
19
|
+
const { schema: schemaValidator } = options;
|
|
20
|
+
let schema = await toJSONSchema(schemaValidator);
|
|
21
|
+
if (options.output === "array")
|
|
22
|
+
schema = wrap(schema);
|
|
23
|
+
return generateText({
|
|
24
|
+
...options,
|
|
25
|
+
response_format: {
|
|
26
|
+
json_schema: {
|
|
27
|
+
description: options.schemaDescription,
|
|
28
|
+
name: options.schemaName ?? "json_schema",
|
|
29
|
+
schema,
|
|
30
|
+
strict: true
|
|
31
|
+
},
|
|
32
|
+
type: "json_schema"
|
|
33
|
+
},
|
|
34
|
+
schema: void 0,
|
|
35
|
+
// Remove schema from options
|
|
36
|
+
schemaDescription: void 0,
|
|
37
|
+
// Remove schemaDescription from options
|
|
38
|
+
schemaName: void 0
|
|
39
|
+
// Remove schemaName from options
|
|
40
|
+
}).then(async ({ finishReason, messages, steps, text, toolCalls, toolResults, usage }) => {
|
|
41
|
+
const json = JSON.parse(text);
|
|
42
|
+
if (options.output === "array") {
|
|
43
|
+
return {
|
|
44
|
+
finishReason,
|
|
45
|
+
messages,
|
|
46
|
+
object: await Promise.all(json.elements.map(async (element) => validate(schemaValidator, element))),
|
|
47
|
+
steps,
|
|
48
|
+
text,
|
|
49
|
+
toolCalls,
|
|
50
|
+
toolResults,
|
|
51
|
+
usage
|
|
52
|
+
};
|
|
53
|
+
} else {
|
|
54
|
+
return {
|
|
55
|
+
finishReason,
|
|
56
|
+
messages,
|
|
57
|
+
object: await validate(options.schema, json),
|
|
58
|
+
steps,
|
|
59
|
+
text,
|
|
60
|
+
toolCalls,
|
|
61
|
+
toolResults,
|
|
62
|
+
usage
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
30
67
|
export {
|
|
31
68
|
generateObject
|
|
32
69
|
};
|
package/package.json
CHANGED