@langchain/core 0.1.24 → 0.1.25-rc.0
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/output_parsers/xml.cjs +10 -19
- package/dist/output_parsers/xml.d.ts +2 -4
- package/dist/output_parsers/xml.js +10 -16
- package/dist/utils/event_source_parse.cjs +6 -22
- package/dist/utils/event_source_parse.js +6 -22
- package/dist/utils/sax-js/sax.cjs +1557 -0
- package/dist/utils/sax-js/sax.d.ts +2 -0
- package/dist/utils/sax-js/sax.js +1554 -0
- package/package.json +1 -3
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.parseXMLMarkdown = exports.XMLOutputParser = exports.XML_FORMAT_INSTRUCTIONS = void 0;
|
|
7
|
-
const sax_1 = __importDefault(require("sax"));
|
|
8
4
|
const transform_js_1 = require("./transform.cjs");
|
|
9
5
|
const json_patch_js_1 = require("../utils/json_patch.cjs");
|
|
6
|
+
const sax_js_1 = require("../utils/sax-js/sax.cjs");
|
|
10
7
|
exports.XML_FORMAT_INSTRUCTIONS = `The output should be formatted as a XML file.
|
|
11
8
|
1. Output should conform to the tags below.
|
|
12
9
|
2. If tags are not given, make them on your own.
|
|
@@ -23,21 +20,13 @@ Here are the output tags:
|
|
|
23
20
|
\`\`\``;
|
|
24
21
|
class XMLOutputParser extends transform_js_1.BaseCumulativeTransformOutputParser {
|
|
25
22
|
constructor(fields) {
|
|
26
|
-
|
|
27
|
-
const { tags, ...saxOptions } = f;
|
|
28
|
-
super(f);
|
|
23
|
+
super(fields);
|
|
29
24
|
Object.defineProperty(this, "tags", {
|
|
30
25
|
enumerable: true,
|
|
31
26
|
configurable: true,
|
|
32
27
|
writable: true,
|
|
33
28
|
value: void 0
|
|
34
29
|
});
|
|
35
|
-
Object.defineProperty(this, "saxOptions", {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
configurable: true,
|
|
38
|
-
writable: true,
|
|
39
|
-
value: void 0
|
|
40
|
-
});
|
|
41
30
|
Object.defineProperty(this, "lc_namespace", {
|
|
42
31
|
enumerable: true,
|
|
43
32
|
configurable: true,
|
|
@@ -50,8 +39,7 @@ class XMLOutputParser extends transform_js_1.BaseCumulativeTransformOutputParser
|
|
|
50
39
|
writable: true,
|
|
51
40
|
value: true
|
|
52
41
|
});
|
|
53
|
-
this.tags = tags;
|
|
54
|
-
this.saxOptions = saxOptions;
|
|
42
|
+
this.tags = fields?.tags;
|
|
55
43
|
}
|
|
56
44
|
static lc_name() {
|
|
57
45
|
return "XMLOutputParser";
|
|
@@ -66,10 +54,10 @@ class XMLOutputParser extends transform_js_1.BaseCumulativeTransformOutputParser
|
|
|
66
54
|
return (0, json_patch_js_1.compare)(prev, next);
|
|
67
55
|
}
|
|
68
56
|
async parsePartialResult(generations) {
|
|
69
|
-
return parseXMLMarkdown(generations[0].text
|
|
57
|
+
return parseXMLMarkdown(generations[0].text);
|
|
70
58
|
}
|
|
71
59
|
async parse(text) {
|
|
72
|
-
return parseXMLMarkdown(text
|
|
60
|
+
return parseXMLMarkdown(text);
|
|
73
61
|
}
|
|
74
62
|
getFormatInstructions() {
|
|
75
63
|
const withTags = !!(this.tags && this.tags.length > 0);
|
|
@@ -98,11 +86,12 @@ const parseParsedResult = (input) => {
|
|
|
98
86
|
return result;
|
|
99
87
|
}
|
|
100
88
|
};
|
|
101
|
-
function parseXMLMarkdown(s
|
|
89
|
+
function parseXMLMarkdown(s) {
|
|
102
90
|
const cleanedString = strip(s);
|
|
103
|
-
const parser =
|
|
91
|
+
const parser = sax_js_1.sax.parser(true);
|
|
104
92
|
let parsedResult = {};
|
|
105
93
|
const elementStack = [];
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
95
|
parser.onopentag = (node) => {
|
|
107
96
|
const element = {
|
|
108
97
|
name: node.name,
|
|
@@ -130,12 +119,14 @@ function parseXMLMarkdown(s, saxOptions) {
|
|
|
130
119
|
}
|
|
131
120
|
}
|
|
132
121
|
};
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
123
|
parser.ontext = (text) => {
|
|
134
124
|
if (elementStack.length > 0) {
|
|
135
125
|
const currentElement = elementStack[elementStack.length - 1];
|
|
136
126
|
currentElement.text += text;
|
|
137
127
|
}
|
|
138
128
|
};
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
139
130
|
parser.onattribute = (attr) => {
|
|
140
131
|
if (elementStack.length > 0) {
|
|
141
132
|
const currentElement = elementStack[elementStack.length - 1];
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { SAXOptions } from "sax";
|
|
2
1
|
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "./transform.js";
|
|
3
2
|
import { Operation } from "../utils/json_patch.js";
|
|
4
3
|
import { ChatGeneration, Generation } from "../outputs.js";
|
|
5
4
|
export declare const XML_FORMAT_INSTRUCTIONS = "The output should be formatted as a XML file.\n1. Output should conform to the tags below. \n2. If tags are not given, make them on your own.\n3. Remember to always open and close all the tags.\n\nAs an example, for the tags [\"foo\", \"bar\", \"baz\"]:\n1. String \"<foo>\n <bar>\n <baz></baz>\n </bar>\n</foo>\" is a well-formatted instance of the schema. \n2. String \"<foo>\n <bar>\n </foo>\" is a badly-formatted instance.\n3. String \"<foo>\n <tag>\n </tag>\n</foo>\" is a badly-formatted instance.\n\nHere are the output tags:\n```\n{tags}\n```";
|
|
6
|
-
export interface XMLOutputParserFields extends
|
|
5
|
+
export interface XMLOutputParserFields extends BaseCumulativeTransformOutputParserInput {
|
|
7
6
|
/**
|
|
8
7
|
* Optional list of tags that the output should conform to.
|
|
9
8
|
* Only used in formatting of the prompt.
|
|
@@ -18,7 +17,6 @@ export type XMLResult = {
|
|
|
18
17
|
};
|
|
19
18
|
export declare class XMLOutputParser extends BaseCumulativeTransformOutputParser<XMLResult> {
|
|
20
19
|
tags?: string[];
|
|
21
|
-
saxOptions?: SAXOptions;
|
|
22
20
|
constructor(fields?: XMLOutputParserFields);
|
|
23
21
|
static lc_name(): string;
|
|
24
22
|
lc_namespace: string[];
|
|
@@ -28,4 +26,4 @@ export declare class XMLOutputParser extends BaseCumulativeTransformOutputParser
|
|
|
28
26
|
parse(text: string): Promise<XMLResult>;
|
|
29
27
|
getFormatInstructions(): string;
|
|
30
28
|
}
|
|
31
|
-
export declare function parseXMLMarkdown(s: string
|
|
29
|
+
export declare function parseXMLMarkdown(s: string): XMLResult;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import sax from "sax";
|
|
2
1
|
import { BaseCumulativeTransformOutputParser, } from "./transform.js";
|
|
3
2
|
import { compare } from "../utils/json_patch.js";
|
|
3
|
+
import { sax } from "../utils/sax-js/sax.js";
|
|
4
4
|
export const XML_FORMAT_INSTRUCTIONS = `The output should be formatted as a XML file.
|
|
5
5
|
1. Output should conform to the tags below.
|
|
6
6
|
2. If tags are not given, make them on your own.
|
|
@@ -17,21 +17,13 @@ Here are the output tags:
|
|
|
17
17
|
\`\`\``;
|
|
18
18
|
export class XMLOutputParser extends BaseCumulativeTransformOutputParser {
|
|
19
19
|
constructor(fields) {
|
|
20
|
-
|
|
21
|
-
const { tags, ...saxOptions } = f;
|
|
22
|
-
super(f);
|
|
20
|
+
super(fields);
|
|
23
21
|
Object.defineProperty(this, "tags", {
|
|
24
22
|
enumerable: true,
|
|
25
23
|
configurable: true,
|
|
26
24
|
writable: true,
|
|
27
25
|
value: void 0
|
|
28
26
|
});
|
|
29
|
-
Object.defineProperty(this, "saxOptions", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: void 0
|
|
34
|
-
});
|
|
35
27
|
Object.defineProperty(this, "lc_namespace", {
|
|
36
28
|
enumerable: true,
|
|
37
29
|
configurable: true,
|
|
@@ -44,8 +36,7 @@ export class XMLOutputParser extends BaseCumulativeTransformOutputParser {
|
|
|
44
36
|
writable: true,
|
|
45
37
|
value: true
|
|
46
38
|
});
|
|
47
|
-
this.tags = tags;
|
|
48
|
-
this.saxOptions = saxOptions;
|
|
39
|
+
this.tags = fields?.tags;
|
|
49
40
|
}
|
|
50
41
|
static lc_name() {
|
|
51
42
|
return "XMLOutputParser";
|
|
@@ -60,10 +51,10 @@ export class XMLOutputParser extends BaseCumulativeTransformOutputParser {
|
|
|
60
51
|
return compare(prev, next);
|
|
61
52
|
}
|
|
62
53
|
async parsePartialResult(generations) {
|
|
63
|
-
return parseXMLMarkdown(generations[0].text
|
|
54
|
+
return parseXMLMarkdown(generations[0].text);
|
|
64
55
|
}
|
|
65
56
|
async parse(text) {
|
|
66
|
-
return parseXMLMarkdown(text
|
|
57
|
+
return parseXMLMarkdown(text);
|
|
67
58
|
}
|
|
68
59
|
getFormatInstructions() {
|
|
69
60
|
const withTags = !!(this.tags && this.tags.length > 0);
|
|
@@ -91,11 +82,12 @@ const parseParsedResult = (input) => {
|
|
|
91
82
|
return result;
|
|
92
83
|
}
|
|
93
84
|
};
|
|
94
|
-
export function parseXMLMarkdown(s
|
|
85
|
+
export function parseXMLMarkdown(s) {
|
|
95
86
|
const cleanedString = strip(s);
|
|
96
|
-
const parser = sax.parser(true
|
|
87
|
+
const parser = sax.parser(true);
|
|
97
88
|
let parsedResult = {};
|
|
98
89
|
const elementStack = [];
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
99
91
|
parser.onopentag = (node) => {
|
|
100
92
|
const element = {
|
|
101
93
|
name: node.name,
|
|
@@ -123,12 +115,14 @@ export function parseXMLMarkdown(s, saxOptions) {
|
|
|
123
115
|
}
|
|
124
116
|
}
|
|
125
117
|
};
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
119
|
parser.ontext = (text) => {
|
|
127
120
|
if (elementStack.length > 0) {
|
|
128
121
|
const currentElement = elementStack[elementStack.length - 1];
|
|
129
122
|
currentElement.text += text;
|
|
130
123
|
}
|
|
131
124
|
};
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
126
|
parser.onattribute = (attr) => {
|
|
133
127
|
if (elementStack.length > 0) {
|
|
134
128
|
const currentElement = elementStack[elementStack.length - 1];
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertEventStreamToIterableReadableDataStream = exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
4
|
+
/* eslint-disable prefer-template */
|
|
5
|
+
/* eslint-disable default-case */
|
|
6
|
+
/* eslint-disable no-plusplus */
|
|
7
|
+
// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts
|
|
8
|
+
// due to a packaging issue in the original.
|
|
9
|
+
// MIT License
|
|
4
10
|
const stream_js_1 = require("./stream.cjs");
|
|
5
11
|
exports.EventStreamContentType = "text/event-stream";
|
|
6
|
-
function isNodeJSReadable(x) {
|
|
7
|
-
return x != null && typeof x === "object" && "on" in x;
|
|
8
|
-
}
|
|
9
12
|
/**
|
|
10
13
|
* Converts a ReadableStream into a callback pattern.
|
|
11
14
|
* @param stream The input ReadableStream.
|
|
@@ -13,25 +16,6 @@ function isNodeJSReadable(x) {
|
|
|
13
16
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
14
17
|
*/
|
|
15
18
|
async function getBytes(stream, onChunk) {
|
|
16
|
-
// stream is a Node.js Readable / PassThrough stream
|
|
17
|
-
// this can happen if node-fetch is polyfilled
|
|
18
|
-
if (isNodeJSReadable(stream)) {
|
|
19
|
-
return new Promise((resolve) => {
|
|
20
|
-
stream.on("readable", () => {
|
|
21
|
-
let chunk;
|
|
22
|
-
// eslint-disable-next-line no-constant-condition
|
|
23
|
-
while (true) {
|
|
24
|
-
chunk = stream.read();
|
|
25
|
-
if (chunk == null) {
|
|
26
|
-
onChunk(new Uint8Array(), true);
|
|
27
|
-
break;
|
|
28
|
-
}
|
|
29
|
-
onChunk(chunk);
|
|
30
|
-
}
|
|
31
|
-
resolve();
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
19
|
const reader = stream.getReader();
|
|
36
20
|
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
37
21
|
// This change is essential to ensure that we capture every last piece of information from streams,
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
/* eslint-disable prefer-template */
|
|
2
|
+
/* eslint-disable default-case */
|
|
3
|
+
/* eslint-disable no-plusplus */
|
|
4
|
+
// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts
|
|
5
|
+
// due to a packaging issue in the original.
|
|
6
|
+
// MIT License
|
|
1
7
|
import { IterableReadableStream } from "./stream.js";
|
|
2
8
|
export const EventStreamContentType = "text/event-stream";
|
|
3
|
-
function isNodeJSReadable(x) {
|
|
4
|
-
return x != null && typeof x === "object" && "on" in x;
|
|
5
|
-
}
|
|
6
9
|
/**
|
|
7
10
|
* Converts a ReadableStream into a callback pattern.
|
|
8
11
|
* @param stream The input ReadableStream.
|
|
@@ -10,25 +13,6 @@ function isNodeJSReadable(x) {
|
|
|
10
13
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
11
14
|
*/
|
|
12
15
|
export async function getBytes(stream, onChunk) {
|
|
13
|
-
// stream is a Node.js Readable / PassThrough stream
|
|
14
|
-
// this can happen if node-fetch is polyfilled
|
|
15
|
-
if (isNodeJSReadable(stream)) {
|
|
16
|
-
return new Promise((resolve) => {
|
|
17
|
-
stream.on("readable", () => {
|
|
18
|
-
let chunk;
|
|
19
|
-
// eslint-disable-next-line no-constant-condition
|
|
20
|
-
while (true) {
|
|
21
|
-
chunk = stream.read();
|
|
22
|
-
if (chunk == null) {
|
|
23
|
-
onChunk(new Uint8Array(), true);
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
onChunk(chunk);
|
|
27
|
-
}
|
|
28
|
-
resolve();
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
16
|
const reader = stream.getReader();
|
|
33
17
|
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
34
18
|
// This change is essential to ensure that we capture every last piece of information from streams,
|