@langchain/core 0.1.4 → 0.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/README.md +1 -1
- package/dist/callbacks/manager.d.ts +0 -5
- package/dist/chat_history.d.ts +1 -0
- package/dist/language_models/base.d.ts +2 -0
- package/dist/output_parsers/index.cjs +1 -0
- package/dist/output_parsers/index.d.ts +1 -0
- package/dist/output_parsers/index.js +1 -0
- package/dist/output_parsers/json.cjs +137 -0
- package/dist/output_parsers/json.d.ts +17 -0
- package/dist/output_parsers/json.js +131 -0
- package/dist/output_parsers/list.cjs +121 -2
- package/dist/output_parsers/list.d.ts +21 -2
- package/dist/output_parsers/list.js +119 -2
- package/dist/prompt_values.cjs +3 -0
- package/dist/prompt_values.d.ts +1 -0
- package/dist/prompt_values.js +3 -0
- package/dist/prompts/chat.cjs +22 -2
- package/dist/prompts/chat.d.ts +4 -2
- package/dist/prompts/chat.js +22 -2
- package/dist/runnables/base.cjs +290 -31
- package/dist/runnables/base.d.ts +72 -20
- package/dist/runnables/base.js +289 -32
- package/dist/runnables/config.cjs +7 -3
- package/dist/runnables/config.d.ts +13 -2
- package/dist/runnables/config.js +5 -1
- package/dist/runnables/history.cjs +3 -3
- package/dist/runnables/history.d.ts +5 -6
- package/dist/runnables/history.js +3 -3
- package/dist/runnables/index.cjs +5 -2
- package/dist/runnables/index.d.ts +3 -3
- package/dist/runnables/index.js +3 -2
- package/dist/runnables/passthrough.cjs +5 -31
- package/dist/runnables/passthrough.d.ts +3 -11
- package/dist/runnables/passthrough.js +4 -29
- package/dist/utils/stream.cjs +113 -1
- package/dist/utils/stream.d.ts +13 -0
- package/dist/utils/stream.js +109 -0
- package/dist/utils/testing/index.cjs +14 -1
- package/dist/utils/testing/index.d.ts +5 -0
- package/dist/utils/testing/index.js +14 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,11 +34,6 @@ export interface BaseCallbackConfig {
|
|
|
34
34
|
* Tags are passed to all callbacks, metadata is passed to handle*Start callbacks.
|
|
35
35
|
*/
|
|
36
36
|
callbacks?: Callbacks;
|
|
37
|
-
/**
|
|
38
|
-
* Runtime values for attributes previously made configurable on this Runnable,
|
|
39
|
-
* or sub-Runnables.
|
|
40
|
-
*/
|
|
41
|
-
configurable?: Record<string, any>;
|
|
42
37
|
}
|
|
43
38
|
export declare function parseCallbackConfigArg(arg: Callbacks | BaseCallbackConfig | undefined): BaseCallbackConfig;
|
|
44
39
|
/**
|
package/dist/chat_history.d.ts
CHANGED
|
@@ -19,4 +19,5 @@ export declare abstract class BaseListChatMessageHistory extends Serializable {
|
|
|
19
19
|
abstract addMessage(message: BaseMessage): Promise<void>;
|
|
20
20
|
addUserMessage(message: string): Promise<void>;
|
|
21
21
|
addAIChatMessage(message: string): Promise<void>;
|
|
22
|
+
abstract getMessages(): Promise<BaseMessage[]>;
|
|
22
23
|
}
|
|
@@ -116,6 +116,8 @@ export interface BaseLanguageModelInterface<RunOutput = any, CallOptions extends
|
|
|
116
116
|
_identifyingParams(): Record<string, any>;
|
|
117
117
|
serialize(): SerializedLLM;
|
|
118
118
|
}
|
|
119
|
+
export type LanguageModelOutput = BaseMessage | string;
|
|
120
|
+
export type LanguageModelLike = Runnable<BaseLanguageModelInput, LanguageModelOutput>;
|
|
119
121
|
/**
|
|
120
122
|
* Base class for language models.
|
|
121
123
|
*/
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parsePartialJson = exports.parseJsonMarkdown = exports.JsonOutputParser = void 0;
|
|
4
|
+
const transform_js_1 = require("./transform.cjs");
|
|
5
|
+
const json_patch_js_1 = require("../utils/json_patch.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Class for parsing the output of an LLM into a JSON object.
|
|
8
|
+
*/
|
|
9
|
+
class JsonOutputParser extends transform_js_1.BaseCumulativeTransformOutputParser {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(...arguments);
|
|
12
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: ["langchain_core", "output_parsers"]
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: true
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
static lc_name() {
|
|
26
|
+
return "JsonOutputParser";
|
|
27
|
+
}
|
|
28
|
+
_diff(prev, next) {
|
|
29
|
+
if (!next) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (!prev) {
|
|
33
|
+
return [{ op: "replace", path: "", value: next }];
|
|
34
|
+
}
|
|
35
|
+
return (0, json_patch_js_1.compare)(prev, next);
|
|
36
|
+
}
|
|
37
|
+
async parsePartialResult(generations) {
|
|
38
|
+
return parseJsonMarkdown(generations[0].text);
|
|
39
|
+
}
|
|
40
|
+
async parse(text) {
|
|
41
|
+
return parseJsonMarkdown(text, JSON.parse);
|
|
42
|
+
}
|
|
43
|
+
getFormatInstructions() {
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.JsonOutputParser = JsonOutputParser;
|
|
48
|
+
function parseJsonMarkdown(s, parser = parsePartialJson) {
|
|
49
|
+
// eslint-disable-next-line no-param-reassign
|
|
50
|
+
s = s.trim();
|
|
51
|
+
const match = /```(json)?(.*)```/s.exec(s);
|
|
52
|
+
if (!match) {
|
|
53
|
+
return parser(s);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return parser(match[2]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.parseJsonMarkdown = parseJsonMarkdown;
|
|
60
|
+
// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
|
|
61
|
+
// MIT License
|
|
62
|
+
function parsePartialJson(s) {
|
|
63
|
+
// If the input is undefined, return null to indicate failure.
|
|
64
|
+
if (typeof s === "undefined") {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
// Attempt to parse the string as-is.
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(s);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
// Pass
|
|
73
|
+
}
|
|
74
|
+
// Initialize variables.
|
|
75
|
+
let new_s = "";
|
|
76
|
+
const stack = [];
|
|
77
|
+
let isInsideString = false;
|
|
78
|
+
let escaped = false;
|
|
79
|
+
// Process each character in the string one at a time.
|
|
80
|
+
for (let char of s) {
|
|
81
|
+
if (isInsideString) {
|
|
82
|
+
if (char === '"' && !escaped) {
|
|
83
|
+
isInsideString = false;
|
|
84
|
+
}
|
|
85
|
+
else if (char === "\n" && !escaped) {
|
|
86
|
+
char = "\\n"; // Replace the newline character with the escape sequence.
|
|
87
|
+
}
|
|
88
|
+
else if (char === "\\") {
|
|
89
|
+
escaped = !escaped;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
escaped = false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
if (char === '"') {
|
|
97
|
+
isInsideString = true;
|
|
98
|
+
escaped = false;
|
|
99
|
+
}
|
|
100
|
+
else if (char === "{") {
|
|
101
|
+
stack.push("}");
|
|
102
|
+
}
|
|
103
|
+
else if (char === "[") {
|
|
104
|
+
stack.push("]");
|
|
105
|
+
}
|
|
106
|
+
else if (char === "}" || char === "]") {
|
|
107
|
+
if (stack && stack[stack.length - 1] === char) {
|
|
108
|
+
stack.pop();
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
// Mismatched closing character; the input is malformed.
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Append the processed character to the new string.
|
|
117
|
+
new_s += char;
|
|
118
|
+
}
|
|
119
|
+
// If we're still inside a string at the end of processing,
|
|
120
|
+
// we need to close the string.
|
|
121
|
+
if (isInsideString) {
|
|
122
|
+
new_s += '"';
|
|
123
|
+
}
|
|
124
|
+
// Close any remaining open structures in the reverse order that they were opened.
|
|
125
|
+
for (let i = stack.length - 1; i >= 0; i -= 1) {
|
|
126
|
+
new_s += stack[i];
|
|
127
|
+
}
|
|
128
|
+
// Attempt to parse the modified string as JSON.
|
|
129
|
+
try {
|
|
130
|
+
return JSON.parse(new_s);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
// If we still can't parse the string as JSON, return null to indicate failure.
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.parsePartialJson = parsePartialJson;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseCumulativeTransformOutputParser } from "./transform.js";
|
|
2
|
+
import { Operation } from "../utils/json_patch.js";
|
|
3
|
+
import { ChatGeneration, Generation } from "../outputs.js";
|
|
4
|
+
/**
|
|
5
|
+
* Class for parsing the output of an LLM into a JSON object.
|
|
6
|
+
*/
|
|
7
|
+
export declare class JsonOutputParser extends BaseCumulativeTransformOutputParser<object> {
|
|
8
|
+
static lc_name(): string;
|
|
9
|
+
lc_namespace: string[];
|
|
10
|
+
lc_serializable: boolean;
|
|
11
|
+
protected _diff(prev: unknown | undefined, next: unknown): Operation[] | undefined;
|
|
12
|
+
parsePartialResult(generations: ChatGeneration[] | Generation[]): Promise<object | undefined>;
|
|
13
|
+
parse(text: string): Promise<object>;
|
|
14
|
+
getFormatInstructions(): string;
|
|
15
|
+
}
|
|
16
|
+
export declare function parseJsonMarkdown(s: string, parser?: typeof parsePartialJson): any;
|
|
17
|
+
export declare function parsePartialJson(s: string): any;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { BaseCumulativeTransformOutputParser } from "./transform.js";
|
|
2
|
+
import { compare } from "../utils/json_patch.js";
|
|
3
|
+
/**
|
|
4
|
+
* Class for parsing the output of an LLM into a JSON object.
|
|
5
|
+
*/
|
|
6
|
+
export class JsonOutputParser extends BaseCumulativeTransformOutputParser {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: ["langchain_core", "output_parsers"]
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
static lc_name() {
|
|
23
|
+
return "JsonOutputParser";
|
|
24
|
+
}
|
|
25
|
+
_diff(prev, next) {
|
|
26
|
+
if (!next) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
if (!prev) {
|
|
30
|
+
return [{ op: "replace", path: "", value: next }];
|
|
31
|
+
}
|
|
32
|
+
return compare(prev, next);
|
|
33
|
+
}
|
|
34
|
+
async parsePartialResult(generations) {
|
|
35
|
+
return parseJsonMarkdown(generations[0].text);
|
|
36
|
+
}
|
|
37
|
+
async parse(text) {
|
|
38
|
+
return parseJsonMarkdown(text, JSON.parse);
|
|
39
|
+
}
|
|
40
|
+
getFormatInstructions() {
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function parseJsonMarkdown(s, parser = parsePartialJson) {
|
|
45
|
+
// eslint-disable-next-line no-param-reassign
|
|
46
|
+
s = s.trim();
|
|
47
|
+
const match = /```(json)?(.*)```/s.exec(s);
|
|
48
|
+
if (!match) {
|
|
49
|
+
return parser(s);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return parser(match[2]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
|
|
56
|
+
// MIT License
|
|
57
|
+
export function parsePartialJson(s) {
|
|
58
|
+
// If the input is undefined, return null to indicate failure.
|
|
59
|
+
if (typeof s === "undefined") {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
// Attempt to parse the string as-is.
|
|
63
|
+
try {
|
|
64
|
+
return JSON.parse(s);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
// Pass
|
|
68
|
+
}
|
|
69
|
+
// Initialize variables.
|
|
70
|
+
let new_s = "";
|
|
71
|
+
const stack = [];
|
|
72
|
+
let isInsideString = false;
|
|
73
|
+
let escaped = false;
|
|
74
|
+
// Process each character in the string one at a time.
|
|
75
|
+
for (let char of s) {
|
|
76
|
+
if (isInsideString) {
|
|
77
|
+
if (char === '"' && !escaped) {
|
|
78
|
+
isInsideString = false;
|
|
79
|
+
}
|
|
80
|
+
else if (char === "\n" && !escaped) {
|
|
81
|
+
char = "\\n"; // Replace the newline character with the escape sequence.
|
|
82
|
+
}
|
|
83
|
+
else if (char === "\\") {
|
|
84
|
+
escaped = !escaped;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
escaped = false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
if (char === '"') {
|
|
92
|
+
isInsideString = true;
|
|
93
|
+
escaped = false;
|
|
94
|
+
}
|
|
95
|
+
else if (char === "{") {
|
|
96
|
+
stack.push("}");
|
|
97
|
+
}
|
|
98
|
+
else if (char === "[") {
|
|
99
|
+
stack.push("]");
|
|
100
|
+
}
|
|
101
|
+
else if (char === "}" || char === "]") {
|
|
102
|
+
if (stack && stack[stack.length - 1] === char) {
|
|
103
|
+
stack.pop();
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// Mismatched closing character; the input is malformed.
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Append the processed character to the new string.
|
|
112
|
+
new_s += char;
|
|
113
|
+
}
|
|
114
|
+
// If we're still inside a string at the end of processing,
|
|
115
|
+
// we need to close the string.
|
|
116
|
+
if (isInsideString) {
|
|
117
|
+
new_s += '"';
|
|
118
|
+
}
|
|
119
|
+
// Close any remaining open structures in the reverse order that they were opened.
|
|
120
|
+
for (let i = stack.length - 1; i >= 0; i -= 1) {
|
|
121
|
+
new_s += stack[i];
|
|
122
|
+
}
|
|
123
|
+
// Attempt to parse the modified string as JSON.
|
|
124
|
+
try {
|
|
125
|
+
return JSON.parse(new_s);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
// If we still can't parse the string as JSON, return null to indicate failure.
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -1,12 +1,65 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CustomListOutputParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
|
|
3
|
+
exports.MarkdownListOutputParser = exports.NumberedListOutputParser = exports.CustomListOutputParser = exports.CommaSeparatedListOutputParser = exports.ListOutputParser = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
|
+
const transform_js_1 = require("./transform.cjs");
|
|
5
6
|
/**
|
|
6
7
|
* Class to parse the output of an LLM call to a list.
|
|
7
8
|
* @augments BaseOutputParser
|
|
8
9
|
*/
|
|
9
|
-
class ListOutputParser extends
|
|
10
|
+
class ListOutputParser extends transform_js_1.BaseTransformOutputParser {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
Object.defineProperty(this, "re", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
async *_transform(inputGenerator) {
|
|
21
|
+
let buffer = "";
|
|
22
|
+
for await (const input of inputGenerator) {
|
|
23
|
+
if (typeof input === "string") {
|
|
24
|
+
// add current chunk to buffer
|
|
25
|
+
buffer += input;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// extract message content and add to buffer
|
|
29
|
+
buffer += input.content;
|
|
30
|
+
}
|
|
31
|
+
// get parts in buffer
|
|
32
|
+
if (!this.re) {
|
|
33
|
+
const parts = await this.parse(buffer);
|
|
34
|
+
if (parts.length > 1) {
|
|
35
|
+
// if there are multiple parts, yield all but the last one
|
|
36
|
+
for (const part of parts.slice(0, -1)) {
|
|
37
|
+
yield [part];
|
|
38
|
+
}
|
|
39
|
+
// keep the last part in the buffer
|
|
40
|
+
buffer = parts[parts.length - 1];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// if there is a regex, get all matches
|
|
45
|
+
const matches = [...buffer.matchAll(this.re)];
|
|
46
|
+
if (matches.length > 1) {
|
|
47
|
+
let doneIdx = 0;
|
|
48
|
+
// if there are multiple matches, yield all but the last one
|
|
49
|
+
for (const match of matches.slice(0, -1)) {
|
|
50
|
+
yield [match[1]];
|
|
51
|
+
doneIdx += (match.index ?? 0) + match[0].length;
|
|
52
|
+
}
|
|
53
|
+
// keep the last match in the buffer
|
|
54
|
+
buffer = buffer.slice(doneIdx);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// yield the last part
|
|
59
|
+
for (const part of await this.parse(buffer)) {
|
|
60
|
+
yield [part];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
10
63
|
}
|
|
11
64
|
exports.ListOutputParser = ListOutputParser;
|
|
12
65
|
/**
|
|
@@ -123,3 +176,69 @@ class CustomListOutputParser extends ListOutputParser {
|
|
|
123
176
|
}
|
|
124
177
|
}
|
|
125
178
|
exports.CustomListOutputParser = CustomListOutputParser;
|
|
179
|
+
class NumberedListOutputParser extends ListOutputParser {
|
|
180
|
+
constructor() {
|
|
181
|
+
super(...arguments);
|
|
182
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: ["langchain_core", "output_parsers", "list"]
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: true
|
|
193
|
+
});
|
|
194
|
+
Object.defineProperty(this, "re", {
|
|
195
|
+
enumerable: true,
|
|
196
|
+
configurable: true,
|
|
197
|
+
writable: true,
|
|
198
|
+
value: /\d+\.\s([^\n]+)/g
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
static lc_name() {
|
|
202
|
+
return "NumberedListOutputParser";
|
|
203
|
+
}
|
|
204
|
+
getFormatInstructions() {
|
|
205
|
+
return `Your response should be a numbered list with each item on a new line. For example: \n\n1. foo\n\n2. bar\n\n3. baz`;
|
|
206
|
+
}
|
|
207
|
+
async parse(text) {
|
|
208
|
+
return [...(text.matchAll(this.re) ?? [])].map((m) => m[1]);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.NumberedListOutputParser = NumberedListOutputParser;
|
|
212
|
+
class MarkdownListOutputParser extends ListOutputParser {
|
|
213
|
+
constructor() {
|
|
214
|
+
super(...arguments);
|
|
215
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
configurable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
value: ["langchain_core", "output_parsers", "list"]
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: true
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(this, "re", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
configurable: true,
|
|
230
|
+
writable: true,
|
|
231
|
+
value: /^\s*[-*]\s([^\n]+)$/gm
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
static lc_name() {
|
|
235
|
+
return "NumberedListOutputParser";
|
|
236
|
+
}
|
|
237
|
+
getFormatInstructions() {
|
|
238
|
+
return `Your response should be a numbered list with each item on a new line. For example: \n\n1. foo\n\n2. bar\n\n3. baz`;
|
|
239
|
+
}
|
|
240
|
+
async parse(text) {
|
|
241
|
+
return [...(text.matchAll(this.re) ?? [])].map((m) => m[1]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
exports.MarkdownListOutputParser = MarkdownListOutputParser;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseMessage } from "../messages/index.js";
|
|
2
|
+
import { BaseTransformOutputParser } from "./transform.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class to parse the output of an LLM call to a list.
|
|
4
5
|
* @augments BaseOutputParser
|
|
5
6
|
*/
|
|
6
|
-
export declare abstract class ListOutputParser extends
|
|
7
|
+
export declare abstract class ListOutputParser extends BaseTransformOutputParser<string[]> {
|
|
8
|
+
re?: RegExp;
|
|
9
|
+
_transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<string[]>;
|
|
7
10
|
}
|
|
8
11
|
/**
|
|
9
12
|
* Class to parse the output of an LLM call as a comma-separated list.
|
|
@@ -55,3 +58,19 @@ export declare class CustomListOutputParser extends ListOutputParser {
|
|
|
55
58
|
*/
|
|
56
59
|
getFormatInstructions(): string;
|
|
57
60
|
}
|
|
61
|
+
export declare class NumberedListOutputParser extends ListOutputParser {
|
|
62
|
+
static lc_name(): string;
|
|
63
|
+
lc_namespace: string[];
|
|
64
|
+
lc_serializable: boolean;
|
|
65
|
+
getFormatInstructions(): string;
|
|
66
|
+
re: RegExp;
|
|
67
|
+
parse(text: string): Promise<string[]>;
|
|
68
|
+
}
|
|
69
|
+
export declare class MarkdownListOutputParser extends ListOutputParser {
|
|
70
|
+
static lc_name(): string;
|
|
71
|
+
lc_namespace: string[];
|
|
72
|
+
lc_serializable: boolean;
|
|
73
|
+
getFormatInstructions(): string;
|
|
74
|
+
re: RegExp;
|
|
75
|
+
parse(text: string): Promise<string[]>;
|
|
76
|
+
}
|
|
@@ -1,9 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OutputParserException } from "./base.js";
|
|
2
|
+
import { BaseTransformOutputParser } from "./transform.js";
|
|
2
3
|
/**
|
|
3
4
|
* Class to parse the output of an LLM call to a list.
|
|
4
5
|
* @augments BaseOutputParser
|
|
5
6
|
*/
|
|
6
|
-
export class ListOutputParser extends
|
|
7
|
+
export class ListOutputParser extends BaseTransformOutputParser {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
Object.defineProperty(this, "re", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: void 0
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async *_transform(inputGenerator) {
|
|
18
|
+
let buffer = "";
|
|
19
|
+
for await (const input of inputGenerator) {
|
|
20
|
+
if (typeof input === "string") {
|
|
21
|
+
// add current chunk to buffer
|
|
22
|
+
buffer += input;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// extract message content and add to buffer
|
|
26
|
+
buffer += input.content;
|
|
27
|
+
}
|
|
28
|
+
// get parts in buffer
|
|
29
|
+
if (!this.re) {
|
|
30
|
+
const parts = await this.parse(buffer);
|
|
31
|
+
if (parts.length > 1) {
|
|
32
|
+
// if there are multiple parts, yield all but the last one
|
|
33
|
+
for (const part of parts.slice(0, -1)) {
|
|
34
|
+
yield [part];
|
|
35
|
+
}
|
|
36
|
+
// keep the last part in the buffer
|
|
37
|
+
buffer = parts[parts.length - 1];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// if there is a regex, get all matches
|
|
42
|
+
const matches = [...buffer.matchAll(this.re)];
|
|
43
|
+
if (matches.length > 1) {
|
|
44
|
+
let doneIdx = 0;
|
|
45
|
+
// if there are multiple matches, yield all but the last one
|
|
46
|
+
for (const match of matches.slice(0, -1)) {
|
|
47
|
+
yield [match[1]];
|
|
48
|
+
doneIdx += (match.index ?? 0) + match[0].length;
|
|
49
|
+
}
|
|
50
|
+
// keep the last match in the buffer
|
|
51
|
+
buffer = buffer.slice(doneIdx);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// yield the last part
|
|
56
|
+
for (const part of await this.parse(buffer)) {
|
|
57
|
+
yield [part];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
7
60
|
}
|
|
8
61
|
/**
|
|
9
62
|
* Class to parse the output of an LLM call as a comma-separated list.
|
|
@@ -117,3 +170,67 @@ export class CustomListOutputParser extends ListOutputParser {
|
|
|
117
170
|
return `Your response should be a list of ${this.length === undefined ? "" : `${this.length} `}items separated by "${this.separator}" (eg: \`foo${this.separator} bar${this.separator} baz\`)`;
|
|
118
171
|
}
|
|
119
172
|
}
|
|
173
|
+
export class NumberedListOutputParser extends ListOutputParser {
|
|
174
|
+
constructor() {
|
|
175
|
+
super(...arguments);
|
|
176
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
177
|
+
enumerable: true,
|
|
178
|
+
configurable: true,
|
|
179
|
+
writable: true,
|
|
180
|
+
value: ["langchain_core", "output_parsers", "list"]
|
|
181
|
+
});
|
|
182
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
183
|
+
enumerable: true,
|
|
184
|
+
configurable: true,
|
|
185
|
+
writable: true,
|
|
186
|
+
value: true
|
|
187
|
+
});
|
|
188
|
+
Object.defineProperty(this, "re", {
|
|
189
|
+
enumerable: true,
|
|
190
|
+
configurable: true,
|
|
191
|
+
writable: true,
|
|
192
|
+
value: /\d+\.\s([^\n]+)/g
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
static lc_name() {
|
|
196
|
+
return "NumberedListOutputParser";
|
|
197
|
+
}
|
|
198
|
+
getFormatInstructions() {
|
|
199
|
+
return `Your response should be a numbered list with each item on a new line. For example: \n\n1. foo\n\n2. bar\n\n3. baz`;
|
|
200
|
+
}
|
|
201
|
+
async parse(text) {
|
|
202
|
+
return [...(text.matchAll(this.re) ?? [])].map((m) => m[1]);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
export class MarkdownListOutputParser extends ListOutputParser {
|
|
206
|
+
constructor() {
|
|
207
|
+
super(...arguments);
|
|
208
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
configurable: true,
|
|
211
|
+
writable: true,
|
|
212
|
+
value: ["langchain_core", "output_parsers", "list"]
|
|
213
|
+
});
|
|
214
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
215
|
+
enumerable: true,
|
|
216
|
+
configurable: true,
|
|
217
|
+
writable: true,
|
|
218
|
+
value: true
|
|
219
|
+
});
|
|
220
|
+
Object.defineProperty(this, "re", {
|
|
221
|
+
enumerable: true,
|
|
222
|
+
configurable: true,
|
|
223
|
+
writable: true,
|
|
224
|
+
value: /^\s*[-*]\s([^\n]+)$/gm
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
static lc_name() {
|
|
228
|
+
return "NumberedListOutputParser";
|
|
229
|
+
}
|
|
230
|
+
getFormatInstructions() {
|
|
231
|
+
return `Your response should be a numbered list with each item on a new line. For example: \n\n1. foo\n\n2. bar\n\n3. baz`;
|
|
232
|
+
}
|
|
233
|
+
async parse(text) {
|
|
234
|
+
return [...(text.matchAll(this.re) ?? [])].map((m) => m[1]);
|
|
235
|
+
}
|
|
236
|
+
}
|
package/dist/prompt_values.cjs
CHANGED
|
@@ -14,6 +14,9 @@ exports.BasePromptValue = BasePromptValue;
|
|
|
14
14
|
* class and overrides the toString and toChatMessages methods.
|
|
15
15
|
*/
|
|
16
16
|
class StringPromptValue extends BasePromptValue {
|
|
17
|
+
static lc_name() {
|
|
18
|
+
return "StringPromptValue";
|
|
19
|
+
}
|
|
17
20
|
constructor(value) {
|
|
18
21
|
super({ value });
|
|
19
22
|
Object.defineProperty(this, "lc_namespace", {
|
package/dist/prompt_values.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export declare abstract class BasePromptValue extends Serializable implements Ba
|
|
|
22
22
|
* class and overrides the toString and toChatMessages methods.
|
|
23
23
|
*/
|
|
24
24
|
export declare class StringPromptValue extends BasePromptValue implements StringPromptValueInterface {
|
|
25
|
+
static lc_name(): string;
|
|
25
26
|
lc_namespace: string[];
|
|
26
27
|
lc_serializable: boolean;
|
|
27
28
|
value: string;
|
package/dist/prompt_values.js
CHANGED
|
@@ -10,6 +10,9 @@ export class BasePromptValue extends Serializable {
|
|
|
10
10
|
* class and overrides the toString and toChatMessages methods.
|
|
11
11
|
*/
|
|
12
12
|
export class StringPromptValue extends BasePromptValue {
|
|
13
|
+
static lc_name() {
|
|
14
|
+
return "StringPromptValue";
|
|
15
|
+
}
|
|
13
16
|
constructor(value) {
|
|
14
17
|
super({ value });
|
|
15
18
|
Object.defineProperty(this, "lc_namespace", {
|