@langchain/core 0.2.18 → 0.2.20
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/callbacks/manager.cjs +94 -19
- package/dist/callbacks/manager.d.ts +3 -1
- package/dist/callbacks/manager.js +94 -19
- package/dist/language_models/base.d.ts +0 -10
- package/dist/language_models/chat_models.cjs +5 -6
- package/dist/language_models/chat_models.d.ts +9 -10
- package/dist/language_models/chat_models.js +5 -6
- package/dist/language_models/llms.cjs +5 -6
- package/dist/language_models/llms.d.ts +10 -12
- package/dist/language_models/llms.js +5 -6
- package/dist/messages/base.cjs +16 -1
- package/dist/messages/base.d.ts +9 -0
- package/dist/messages/base.js +14 -0
- package/dist/messages/tool.cjs +23 -0
- package/dist/messages/tool.d.ts +15 -0
- package/dist/messages/tool.js +24 -1
- package/dist/messages/utils.cjs +1 -1
- package/dist/messages/utils.js +1 -1
- package/dist/output_parsers/string.cjs +1 -0
- package/dist/output_parsers/string.js +1 -0
- package/dist/runnables/base.cjs +35 -12
- package/dist/runnables/base.js +35 -12
- package/dist/runnables/config.cjs +41 -0
- package/dist/runnables/config.js +41 -0
- package/dist/runnables/remote.cjs +17 -14
- package/dist/runnables/remote.js +17 -14
- package/dist/runnables/types.d.ts +10 -0
- package/dist/tools/index.cjs +15 -6
- package/dist/tools/index.d.ts +21 -12
- package/dist/tools/index.js +15 -6
- package/dist/utils/math.cjs +6 -4
- package/dist/utils/math.js +6 -4
- package/dist/utils/ml-distance/distances.cjs +18 -0
- package/dist/utils/ml-distance/distances.d.ts +8 -0
- package/dist/utils/ml-distance/distances.js +14 -0
- package/dist/utils/ml-distance/similarities.cjs +21 -0
- package/dist/utils/ml-distance/similarities.d.ts +7 -0
- package/dist/utils/ml-distance/similarities.js +17 -0
- package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
- package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
- package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
- package/dist/utils/signal.cjs +28 -0
- package/dist/utils/signal.d.ts +1 -0
- package/dist/utils/signal.js +24 -0
- package/dist/utils/stream.cjs +19 -4
- package/dist/utils/stream.d.ts +3 -1
- package/dist/utils/stream.js +19 -4
- package/dist/utils/testing/index.cjs +168 -4
- package/dist/utils/testing/index.d.ts +100 -6
- package/dist/utils/testing/index.js +167 -4
- package/package.json +4 -4
- package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
- package/dist/caches/tests/in_memory_cache.test.js +0 -33
- package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
- package/dist/callbacks/tests/callbacks.test.js +0 -495
- package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
- package/dist/callbacks/tests/manager.int.test.js +0 -29
- package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
- package/dist/callbacks/tests/run_collector.test.js +0 -58
- package/dist/language_models/tests/chat_models.test.d.ts +0 -1
- package/dist/language_models/tests/chat_models.test.js +0 -204
- package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
- package/dist/language_models/tests/count_tokens.test.js +0 -19
- package/dist/language_models/tests/llms.test.d.ts +0 -1
- package/dist/language_models/tests/llms.test.js +0 -52
- package/dist/messages/tests/base_message.test.d.ts +0 -1
- package/dist/messages/tests/base_message.test.js +0 -245
- package/dist/messages/tests/message_utils.test.d.ts +0 -1
- package/dist/messages/tests/message_utils.test.js +0 -434
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
- package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
- package/dist/output_parsers/tests/json.test.d.ts +0 -1
- package/dist/output_parsers/tests/json.test.js +0 -427
- package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
- package/dist/output_parsers/tests/output_parser.test.js +0 -78
- package/dist/output_parsers/tests/string.test.d.ts +0 -1
- package/dist/output_parsers/tests/string.test.js +0 -68
- package/dist/output_parsers/tests/structured.test.d.ts +0 -1
- package/dist/output_parsers/tests/structured.test.js +0 -166
- package/dist/output_parsers/tests/xml.test.d.ts +0 -1
- package/dist/output_parsers/tests/xml.test.js +0 -81
- package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/chat.mustache.test.js +0 -129
- package/dist/prompts/tests/chat.test.d.ts +0 -1
- package/dist/prompts/tests/chat.test.js +0 -557
- package/dist/prompts/tests/few_shot.test.d.ts +0 -1
- package/dist/prompts/tests/few_shot.test.js +0 -224
- package/dist/prompts/tests/pipeline.test.d.ts +0 -1
- package/dist/prompts/tests/pipeline.test.js +0 -101
- package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.mustache.test.js +0 -105
- package/dist/prompts/tests/prompt.test.d.ts +0 -1
- package/dist/prompts/tests/prompt.test.js +0 -78
- package/dist/prompts/tests/structured.test.d.ts +0 -1
- package/dist/prompts/tests/structured.test.js +0 -37
- package/dist/prompts/tests/template.test.d.ts +0 -1
- package/dist/prompts/tests/template.test.js +0 -24
- package/dist/runnables/tests/runnable.test.d.ts +0 -1
- package/dist/runnables/tests/runnable.test.js +0 -491
- package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_binding.test.js +0 -46
- package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_branch.test.js +0 -116
- package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_graph.test.js +0 -100
- package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_history.test.js +0 -177
- package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_interface.test.js +0 -209
- package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_map.test.js +0 -238
- package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
- package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
- package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_remote.test.js +0 -200
- package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_retry.test.js +0 -125
- package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
- package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
- package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
- package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tools.test.js +0 -149
- package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
- package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
- package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
- package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
- package/dist/singletons/tests/async_local_storage.test.js +0 -153
- package/dist/structured_query/tests/utils.test.d.ts +0 -1
- package/dist/structured_query/tests/utils.test.js +0 -47
- package/dist/tools/tests/tools.test.d.ts +0 -1
- package/dist/tools/tests/tools.test.js +0 -85
- package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
- package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
- package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
- package/dist/tracers/tests/langsmith_interop.test.js +0 -551
- package/dist/tracers/tests/tracer.test.d.ts +0 -1
- package/dist/tracers/tests/tracer.test.js +0 -378
- package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
- package/dist/utils/testing/tests/chatfake.test.js +0 -112
- package/dist/utils/tests/async_caller.test.d.ts +0 -1
- package/dist/utils/tests/async_caller.test.js +0 -27
- package/dist/utils/tests/enviroment.test.d.ts +0 -1
- package/dist/utils/tests/enviroment.test.js +0 -6
- package/dist/utils/tests/function_calling.test.d.ts +0 -1
- package/dist/utils/tests/function_calling.test.js +0 -107
- package/dist/utils/tests/math_utils.test.d.ts +0 -1
- package/dist/utils/tests/math_utils.test.js +0 -139
- package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
- package/dist/utils/tests/polyfill_stream.test.js +0 -15
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { expect, test } from "@jest/globals";
|
|
3
|
-
import { OutputParserException } from "../base.js";
|
|
4
|
-
import { StructuredOutputParser } from "../structured.js";
|
|
5
|
-
test("StructuredOutputParser.fromNamesAndDescriptions", async () => {
|
|
6
|
-
const parser = StructuredOutputParser.fromNamesAndDescriptions({
|
|
7
|
-
url: "A link to the resource",
|
|
8
|
-
});
|
|
9
|
-
expect(await parser.parse('```\n{"url": "value"}```')).toEqual({
|
|
10
|
-
url: "value",
|
|
11
|
-
});
|
|
12
|
-
expect(parser.getFormatInstructions()).toMatchInlineSnapshot(`
|
|
13
|
-
"You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
14
|
-
|
|
15
|
-
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
16
|
-
|
|
17
|
-
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
18
|
-
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
19
|
-
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
20
|
-
|
|
21
|
-
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
|
|
22
|
-
|
|
23
|
-
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
24
|
-
\`\`\`json
|
|
25
|
-
{"type":"object","properties":{"url":{"type":"string","description":"A link to the resource"}},"required":["url"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
|
|
26
|
-
\`\`\`
|
|
27
|
-
"
|
|
28
|
-
`);
|
|
29
|
-
});
|
|
30
|
-
var StateProvinceEnum;
|
|
31
|
-
(function (StateProvinceEnum) {
|
|
32
|
-
StateProvinceEnum["Alabama"] = "AL";
|
|
33
|
-
StateProvinceEnum["Alaska"] = "AK";
|
|
34
|
-
StateProvinceEnum["Arizona"] = "AZ";
|
|
35
|
-
})(StateProvinceEnum || (StateProvinceEnum = {}));
|
|
36
|
-
test("StructuredOutputParser.fromZodSchema", async () => {
|
|
37
|
-
const parser = StructuredOutputParser.fromZodSchema(z.object({ url: z.string().describe("A link to the resource") }));
|
|
38
|
-
expect(await parser.parse('```\n{"url": "value"}```')).toEqual({
|
|
39
|
-
url: "value",
|
|
40
|
-
});
|
|
41
|
-
expect(parser.getFormatInstructions()).toMatchInlineSnapshot(`
|
|
42
|
-
"You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
43
|
-
|
|
44
|
-
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
45
|
-
|
|
46
|
-
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
47
|
-
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
48
|
-
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
49
|
-
|
|
50
|
-
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
|
|
51
|
-
|
|
52
|
-
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
53
|
-
\`\`\`json
|
|
54
|
-
{"type":"object","properties":{"url":{"type":"string","description":"A link to the resource"}},"required":["url"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
|
|
55
|
-
\`\`\`
|
|
56
|
-
"
|
|
57
|
-
`);
|
|
58
|
-
});
|
|
59
|
-
test("StructuredOutputParser.fromZodSchema", async () => {
|
|
60
|
-
const parser = StructuredOutputParser.fromZodSchema(z.object({ answer: z.enum(["yes", "no"]).describe("yes or no") }));
|
|
61
|
-
await expect(parser.parse('```\n{"answer": "YES"}```')).rejects.toThrow(OutputParserException);
|
|
62
|
-
});
|
|
63
|
-
test("StructuredOutputParser.fromZodSchema", async () => {
|
|
64
|
-
const parser = StructuredOutputParser.fromZodSchema(z.object({
|
|
65
|
-
answer: z.string().describe("answer to the user's question"),
|
|
66
|
-
sources: z
|
|
67
|
-
.array(z.string())
|
|
68
|
-
.describe("sources used to answer the question, should be websites."),
|
|
69
|
-
}));
|
|
70
|
-
expect(await parser.parse('```\n{"answer": "value", "sources": ["this-source"]}```')).toEqual({
|
|
71
|
-
answer: "value",
|
|
72
|
-
sources: ["this-source"],
|
|
73
|
-
});
|
|
74
|
-
expect(await parser.parse('```json\n{"answer": "value", "sources": ["this-source"]}```')).toEqual({
|
|
75
|
-
answer: "value",
|
|
76
|
-
sources: ["this-source"],
|
|
77
|
-
});
|
|
78
|
-
expect(await parser.parse('some other stuff```json\n{"answer": "value", "sources": ["this-source"]}```some other stuff at the end')).toEqual({
|
|
79
|
-
answer: "value",
|
|
80
|
-
sources: ["this-source"],
|
|
81
|
-
});
|
|
82
|
-
expect(parser.getFormatInstructions()).toMatchInlineSnapshot(`
|
|
83
|
-
"You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
84
|
-
|
|
85
|
-
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
86
|
-
|
|
87
|
-
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
88
|
-
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
89
|
-
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
90
|
-
|
|
91
|
-
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
|
|
92
|
-
|
|
93
|
-
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
94
|
-
\`\`\`json
|
|
95
|
-
{"type":"object","properties":{"answer":{"type":"string","description":"answer to the user's question"},"sources":{"type":"array","items":{"type":"string"},"description":"sources used to answer the question, should be websites."}},"required":["answer","sources"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
|
|
96
|
-
\`\`\`
|
|
97
|
-
"
|
|
98
|
-
`);
|
|
99
|
-
});
|
|
100
|
-
test("StructuredOutputParser.fromZodSchema", async () => {
|
|
101
|
-
const parser = StructuredOutputParser.fromZodSchema(z
|
|
102
|
-
.object({
|
|
103
|
-
url: z.string().describe("A link to the resource"),
|
|
104
|
-
title: z.string().describe("A title for the resource"),
|
|
105
|
-
year: z.number().describe("The year the resource was created"),
|
|
106
|
-
createdAt: z
|
|
107
|
-
.string()
|
|
108
|
-
.datetime()
|
|
109
|
-
.describe("The date and time the resource was created"),
|
|
110
|
-
createdAtDate: z.coerce
|
|
111
|
-
.date()
|
|
112
|
-
.describe("The date the resource was created")
|
|
113
|
-
.optional(),
|
|
114
|
-
authors: z.array(z.object({
|
|
115
|
-
name: z.string().describe("The name of the author"),
|
|
116
|
-
email: z.string().describe("The email of the author"),
|
|
117
|
-
type: z.enum(["author", "editor"]).optional(),
|
|
118
|
-
address: z
|
|
119
|
-
.string()
|
|
120
|
-
.optional()
|
|
121
|
-
.describe("The address of the author"),
|
|
122
|
-
stateProvince: z
|
|
123
|
-
.nativeEnum(StateProvinceEnum)
|
|
124
|
-
.optional()
|
|
125
|
-
.describe("The state or province of the author"),
|
|
126
|
-
})),
|
|
127
|
-
})
|
|
128
|
-
.describe("Only One object"));
|
|
129
|
-
expect(await parser.parse('```\n{"url": "value", "title": "value", "year": 2011, "createdAt": "2023-03-29T16:07:09.600Z", "createdAtDate": "2023-03-29", "authors": [{"name": "value", "email": "value", "stateProvince": "AZ"}]}```')).toEqual({
|
|
130
|
-
url: "value",
|
|
131
|
-
title: "value",
|
|
132
|
-
year: 2011,
|
|
133
|
-
createdAt: "2023-03-29T16:07:09.600Z",
|
|
134
|
-
createdAtDate: new Date("2023-03-29T00:00:00.000Z"),
|
|
135
|
-
authors: [{ name: "value", email: "value", stateProvince: "AZ" }],
|
|
136
|
-
});
|
|
137
|
-
expect(parser.getFormatInstructions()).toMatchInlineSnapshot(`
|
|
138
|
-
"You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
|
|
139
|
-
|
|
140
|
-
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
|
|
141
|
-
|
|
142
|
-
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
|
|
143
|
-
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
|
|
144
|
-
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
|
|
145
|
-
|
|
146
|
-
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
|
|
147
|
-
|
|
148
|
-
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
149
|
-
\`\`\`json
|
|
150
|
-
{"type":"object","properties":{"url":{"type":"string","description":"A link to the resource"},"title":{"type":"string","description":"A title for the resource"},"year":{"type":"number","description":"The year the resource was created"},"createdAt":{"type":"string","format":"date-time","description":"The date and time the resource was created"},"createdAtDate":{"type":"string","format":"date-time","description":"The date the resource was created"},"authors":{"type":"array","items":{"type":"object","properties":{"name":{"type":"string","description":"The name of the author"},"email":{"type":"string","description":"The email of the author"},"type":{"type":"string","enum":["author","editor"]},"address":{"type":"string","description":"The address of the author"},"stateProvince":{"type":"string","enum":["AL","AK","AZ"],"description":"The state or province of the author"}},"required":["name","email"],"additionalProperties":false}}},"required":["url","title","year","createdAt","authors"],"additionalProperties":false,"description":"Only One object","$schema":"http://json-schema.org/draft-07/schema#"}
|
|
151
|
-
\`\`\`
|
|
152
|
-
"
|
|
153
|
-
`);
|
|
154
|
-
});
|
|
155
|
-
test("StructuredOutputParser.fromZodSchema parsing newlines", async () => {
|
|
156
|
-
const parser = StructuredOutputParser.fromZodSchema(z
|
|
157
|
-
.object({
|
|
158
|
-
url: z.string().describe("A link to the resource"),
|
|
159
|
-
summary: z.string().describe("A summary"),
|
|
160
|
-
})
|
|
161
|
-
.describe("Only One object"));
|
|
162
|
-
expect(await parser.parse('```\n{"url": "value", "summary": "line1,\nline2,\nline3"}```')).toEqual({
|
|
163
|
-
url: "value",
|
|
164
|
-
summary: "line1,\nline2,\nline3",
|
|
165
|
-
});
|
|
166
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { FakeStreamingLLM } from "../../utils/testing/index.js";
|
|
2
|
-
import { XMLOutputParser } from "../xml.js";
|
|
3
|
-
const XML_EXAMPLE = `<?xml version="1.0" encoding="UTF-8"?>
|
|
4
|
-
<userProfile>
|
|
5
|
-
<userID>12345</userID>
|
|
6
|
-
<email>john.doe@example.com</email>
|
|
7
|
-
<roles>
|
|
8
|
-
<role>Admin</role>
|
|
9
|
-
<role>User</role>
|
|
10
|
-
</roles>
|
|
11
|
-
<preferences>
|
|
12
|
-
<theme>Dark</theme>
|
|
13
|
-
<notifications>
|
|
14
|
-
<email>true</email>
|
|
15
|
-
</notifications>
|
|
16
|
-
</preferences>
|
|
17
|
-
</userProfile>`;
|
|
18
|
-
const BACKTICK_WRAPPED_XML = `\`\`\`xml\n${XML_EXAMPLE}\n\`\`\``;
|
|
19
|
-
const expectedResult = {
|
|
20
|
-
userProfile: [
|
|
21
|
-
{
|
|
22
|
-
userID: "12345",
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
email: "john.doe@example.com",
|
|
26
|
-
},
|
|
27
|
-
{
|
|
28
|
-
roles: [
|
|
29
|
-
{
|
|
30
|
-
role: "Admin",
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
role: "User",
|
|
34
|
-
},
|
|
35
|
-
],
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
preferences: [
|
|
39
|
-
{
|
|
40
|
-
theme: "Dark",
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
notifications: [
|
|
44
|
-
{
|
|
45
|
-
email: "true",
|
|
46
|
-
},
|
|
47
|
-
],
|
|
48
|
-
},
|
|
49
|
-
],
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
};
|
|
53
|
-
test("Can parse XML", async () => {
|
|
54
|
-
const parser = new XMLOutputParser();
|
|
55
|
-
const result = await parser.invoke(XML_EXAMPLE);
|
|
56
|
-
expect(result).toStrictEqual(expectedResult);
|
|
57
|
-
});
|
|
58
|
-
test("Can parse backtick wrapped XML", async () => {
|
|
59
|
-
const parser = new XMLOutputParser();
|
|
60
|
-
const result = await parser.invoke(BACKTICK_WRAPPED_XML);
|
|
61
|
-
expect(result).toStrictEqual(expectedResult);
|
|
62
|
-
});
|
|
63
|
-
test("Can format instructions with passed tags.", async () => {
|
|
64
|
-
const tags = ["tag1", "tag2", "tag3"];
|
|
65
|
-
const parser = new XMLOutputParser({ tags });
|
|
66
|
-
const formatInstructions = parser.getFormatInstructions();
|
|
67
|
-
expect(formatInstructions).toContain("tag1, tag2, tag3");
|
|
68
|
-
});
|
|
69
|
-
test("Can parse streams", async () => {
|
|
70
|
-
const parser = new XMLOutputParser();
|
|
71
|
-
const streamingLlm = new FakeStreamingLLM({
|
|
72
|
-
responses: [XML_EXAMPLE],
|
|
73
|
-
}).pipe(parser);
|
|
74
|
-
const result = await streamingLlm.stream(XML_EXAMPLE);
|
|
75
|
-
let finalResult = {};
|
|
76
|
-
for await (const chunk of result) {
|
|
77
|
-
console.log(chunk);
|
|
78
|
-
finalResult = chunk;
|
|
79
|
-
}
|
|
80
|
-
expect(finalResult).toStrictEqual(expectedResult);
|
|
81
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { test, expect } from "@jest/globals";
|
|
2
|
-
import { AIMessage } from "../../messages/ai.js";
|
|
3
|
-
import { HumanMessage } from "../../messages/human.js";
|
|
4
|
-
import { SystemMessage } from "../../messages/system.js";
|
|
5
|
-
import { ChatPromptTemplate, HumanMessagePromptTemplate } from "../chat.js";
|
|
6
|
-
test("Test creating a chat prompt template from role string messages", async () => {
|
|
7
|
-
const template = ChatPromptTemplate.fromMessages([
|
|
8
|
-
["system", "You are a helpful AI bot. Your name is {{name}}."],
|
|
9
|
-
["human", "Hello, how are you doing?"],
|
|
10
|
-
["ai", "I'm doing well, thanks!"],
|
|
11
|
-
["human", "{{userInput}}"],
|
|
12
|
-
], {
|
|
13
|
-
templateFormat: "mustache",
|
|
14
|
-
});
|
|
15
|
-
const messages = await template.formatMessages({
|
|
16
|
-
name: "Bob",
|
|
17
|
-
userInput: "What is your name?",
|
|
18
|
-
});
|
|
19
|
-
expect(messages).toEqual([
|
|
20
|
-
new SystemMessage({
|
|
21
|
-
content: "You are a helpful AI bot. Your name is Bob.",
|
|
22
|
-
}),
|
|
23
|
-
new HumanMessage({
|
|
24
|
-
content: "Hello, how are you doing?",
|
|
25
|
-
}),
|
|
26
|
-
new AIMessage({
|
|
27
|
-
content: "I'm doing well, thanks!",
|
|
28
|
-
}),
|
|
29
|
-
new HumanMessage({
|
|
30
|
-
content: "What is your name?",
|
|
31
|
-
}),
|
|
32
|
-
]);
|
|
33
|
-
});
|
|
34
|
-
test("Multiple input variables with repeats.", async () => {
|
|
35
|
-
const template = "This {{bar}} is a {{foo}} test {{foo}}.";
|
|
36
|
-
const prompt = ChatPromptTemplate.fromTemplate(template, {
|
|
37
|
-
templateFormat: "mustache",
|
|
38
|
-
});
|
|
39
|
-
expect(prompt.inputVariables).toEqual(["bar", "foo"]);
|
|
40
|
-
const formattedPrompt = await prompt.formatPromptValue({
|
|
41
|
-
bar: "baz",
|
|
42
|
-
foo: "bar",
|
|
43
|
-
});
|
|
44
|
-
expect(formattedPrompt.toChatMessages()).toEqual([
|
|
45
|
-
new HumanMessage("This baz is a bar test bar."),
|
|
46
|
-
]);
|
|
47
|
-
});
|
|
48
|
-
test("Ignores f-string inputs input variables with repeats.", async () => {
|
|
49
|
-
const template = "This {bar} is a {foo} test {foo}.";
|
|
50
|
-
const prompt = ChatPromptTemplate.fromTemplate(template, {
|
|
51
|
-
templateFormat: "mustache",
|
|
52
|
-
});
|
|
53
|
-
expect(prompt.inputVariables).toEqual([]);
|
|
54
|
-
const formattedPrompt = await prompt.formatPromptValue({
|
|
55
|
-
bar: "baz",
|
|
56
|
-
foo: "bar",
|
|
57
|
-
});
|
|
58
|
-
expect(formattedPrompt.toChatMessages()).toEqual([
|
|
59
|
-
new HumanMessage("This {bar} is a {foo} test {foo}."),
|
|
60
|
-
]);
|
|
61
|
-
});
|
|
62
|
-
test("Mustache template with image and chat prompts inside one template (fromMessages)", async () => {
|
|
63
|
-
const template = ChatPromptTemplate.fromMessages([
|
|
64
|
-
[
|
|
65
|
-
"human",
|
|
66
|
-
[
|
|
67
|
-
{
|
|
68
|
-
type: "image_url",
|
|
69
|
-
image_url: "{{image_url}}",
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
type: "text",
|
|
73
|
-
text: "{{other_var}}",
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
],
|
|
77
|
-
["human", "hello {{name}}"],
|
|
78
|
-
], {
|
|
79
|
-
templateFormat: "mustache",
|
|
80
|
-
});
|
|
81
|
-
const messages = await template.formatMessages({
|
|
82
|
-
name: "Bob",
|
|
83
|
-
image_url: "https://foo.com/bar.png",
|
|
84
|
-
other_var: "bar",
|
|
85
|
-
});
|
|
86
|
-
expect(messages).toEqual([
|
|
87
|
-
new HumanMessage({
|
|
88
|
-
content: [
|
|
89
|
-
{ type: "image_url", image_url: { url: "https://foo.com/bar.png" } },
|
|
90
|
-
{ type: "text", text: "bar" },
|
|
91
|
-
],
|
|
92
|
-
}),
|
|
93
|
-
new HumanMessage({
|
|
94
|
-
content: "hello Bob",
|
|
95
|
-
}),
|
|
96
|
-
]);
|
|
97
|
-
expect(template.inputVariables.sort()).toEqual([
|
|
98
|
-
"image_url",
|
|
99
|
-
"name",
|
|
100
|
-
"other_var",
|
|
101
|
-
]);
|
|
102
|
-
});
|
|
103
|
-
test("Mustache image template with nested URL and chat prompts HumanMessagePromptTemplate.fromTemplate", async () => {
|
|
104
|
-
const template = HumanMessagePromptTemplate.fromTemplate([
|
|
105
|
-
{
|
|
106
|
-
text: "{{name}}",
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
image_url: {
|
|
110
|
-
url: "{{image_url}}",
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
], {
|
|
114
|
-
templateFormat: "mustache",
|
|
115
|
-
});
|
|
116
|
-
const messages = await template.formatMessages({
|
|
117
|
-
name: "Bob",
|
|
118
|
-
image_url: "https://foo.com/bar.png",
|
|
119
|
-
});
|
|
120
|
-
expect(messages).toEqual([
|
|
121
|
-
new HumanMessage({
|
|
122
|
-
content: [
|
|
123
|
-
{ type: "text", text: "Bob" },
|
|
124
|
-
{ type: "image_url", image_url: { url: "https://foo.com/bar.png" } },
|
|
125
|
-
],
|
|
126
|
-
}),
|
|
127
|
-
]);
|
|
128
|
-
expect(template.inputVariables.sort()).toEqual(["image_url", "name"]);
|
|
129
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|