-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathstructured_output.test.ts
119 lines (110 loc) · 4.33 KB
/
structured_output.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { test, expect } from "@jest/globals";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { AIMessage } from "@langchain/core/messages";
import { FunctionCallStructuredOutputParser } from "../structured_output.js";
test("structured output parser", async () => {
const parser = new FunctionCallStructuredOutputParser(
zodToJsonSchema(
z.object({
name: z.string().describe("Human name"),
surname: z.string().describe("Human surname"),
age: z.number().describe("Human age"),
appearance: z.string().describe("Human appearance description"),
shortBio: z.string().describe("Short bio secription"),
university: z
.string()
.optional()
.describe("University name if attended"),
gender: z.string().describe("Gender of the human"),
interests: z
.array(z.string())
.describe("json array of strings human interests"),
})
)
);
const result = await parser.parseResult([
{
text: "",
message: new AIMessage({
content: "",
additional_kwargs: {
function_call: {
name: "",
arguments: JSON.stringify({
name: "Anna",
surname: "Kowalska",
age: 30,
appearance:
"Anna has shoulder-length brown hair and green eyes. She has a slim build and stands at around 5'6\" tall.",
shortBio:
"Anna is a kind and compassionate person who loves to help others. She works as a nurse at a local hospital in Poland. In her free time, she enjoys reading, cooking, and spending time with her friends and family. Anna is also passionate about traveling and exploring new places.",
university: null,
gender: "female",
interests: ["reading", "cooking", "traveling"],
}),
},
},
}),
},
]);
// console.log("result", result);
expect(result.name).toEqual("Anna");
expect(result.surname).toEqual("Kowalska");
expect(result.age).toEqual(30);
expect(result).toHaveProperty("appearance");
expect(result).toHaveProperty("shortBio");
expect(result).not.toHaveProperty("university");
expect(result.gender).toEqual("female");
expect(result.interests.length).toEqual(3);
});
test("structured output parser with Zod input", async () => {
const parser = new FunctionCallStructuredOutputParser({
zodSchema: z.object({
name: z.string().describe("Human name"),
surname: z.string().describe("Human surname"),
age: z.number().describe("Human age"),
appearance: z.string().describe("Human appearance description"),
shortBio: z.string().describe("Short bio secription"),
university: z.string().optional().describe("University name if attended"),
gender: z.string().describe("Gender of the human"),
interests: z
.array(z.string())
.describe("json array of strings human interests"),
}),
});
const result = await parser.parseResult([
{
text: "",
message: new AIMessage({
content: "",
additional_kwargs: {
function_call: {
name: "",
arguments: JSON.stringify({
name: "Anna",
surname: "Kowalska",
age: 30,
appearance:
"Anna has shoulder-length brown hair and green eyes. She has a slim build and stands at around 5'6\" tall.",
shortBio:
"Anna is a kind and compassionate person who loves to help others. She works as a nurse at a local hospital in Poland. In her free time, she enjoys reading, cooking, and spending time with her friends and family. Anna is also passionate about traveling and exploring new places.",
university: null,
gender: "female",
interests: ["reading", "cooking", "traveling"],
}),
},
},
}),
},
]);
// console.log("result", result);
expect(result.name).toEqual("Anna");
expect(result.surname).toEqual("Kowalska");
expect(result.age).toEqual(30);
expect(result).toHaveProperty("appearance");
expect(result).toHaveProperty("shortBio");
expect(result).not.toHaveProperty("university");
expect(result.gender).toEqual("female");
expect(result.interests.length).toEqual(3);
});