@langchain/core 0.1.23 → 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/messages/index.cjs +3 -0
- package/dist/messages/index.d.ts +1 -0
- package/dist/messages/index.js +3 -0
- 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/prompts/image.cjs +6 -0
- package/dist/prompts/image.d.ts +1 -0
- package/dist/prompts/image.js +6 -0
- package/dist/prompts/index.cjs +1 -0
- package/dist/prompts/index.d.ts +1 -0
- package/dist/prompts/index.js +1 -0
- package/dist/runnables/remote.cjs +378 -0
- package/dist/runnables/remote.d.ts +31 -0
- package/dist/runnables/remote.js +374 -0
- package/dist/utils/event_source_parse.cjs +210 -0
- package/dist/utils/event_source_parse.d.ts +39 -0
- package/dist/utils/event_source_parse.js +203 -0
- 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 +27 -3
- package/runnables/remote.cjs +1 -0
- package/runnables/remote.d.cts +1 -0
- package/runnables/remote.d.ts +1 -0
- package/runnables/remote.js +1 -0
- package/utils/event_source_parse.cjs +1 -0
- package/utils/event_source_parse.d.cts +1 -0
- package/utils/event_source_parse.d.ts +1 -0
- package/utils/event_source_parse.js +1 -0
package/dist/messages/index.cjs
CHANGED
package/dist/messages/index.d.ts
CHANGED
|
@@ -195,6 +195,7 @@ export declare class ToolMessage extends BaseMessage {
|
|
|
195
195
|
constructor(fields: ToolMessageFieldsWithToolCallId);
|
|
196
196
|
constructor(fields: string | BaseMessageFields, tool_call_id: string, name?: string);
|
|
197
197
|
_getType(): MessageType;
|
|
198
|
+
static isInstance(message: BaseMessage): message is ToolMessage;
|
|
198
199
|
}
|
|
199
200
|
/**
|
|
200
201
|
* Represents a chunk of a tool message, which can be concatenated
|
package/dist/messages/index.js
CHANGED
|
@@ -335,6 +335,9 @@ export class ToolMessage extends BaseMessage {
|
|
|
335
335
|
_getType() {
|
|
336
336
|
return "tool";
|
|
337
337
|
}
|
|
338
|
+
static isInstance(message) {
|
|
339
|
+
return message._getType() === "tool";
|
|
340
|
+
}
|
|
338
341
|
}
|
|
339
342
|
/**
|
|
340
343
|
* Represents a chunk of a tool message, which can be concatenated
|
|
@@ -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];
|
package/dist/prompts/image.cjs
CHANGED
|
@@ -13,6 +13,12 @@ class ImagePromptTemplate extends base_js_1.BasePromptTemplate {
|
|
|
13
13
|
}
|
|
14
14
|
constructor(input) {
|
|
15
15
|
super(input);
|
|
16
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: ["langchain_core", "prompts", "image"]
|
|
21
|
+
});
|
|
16
22
|
Object.defineProperty(this, "template", {
|
|
17
23
|
enumerable: true,
|
|
18
24
|
configurable: true,
|
package/dist/prompts/image.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export interface ImagePromptTemplateInput<RunInput extends InputValues = any, Pa
|
|
|
29
29
|
*/
|
|
30
30
|
export declare class ImagePromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BasePromptTemplate<RunInput, ImagePromptValue, PartialVariableName> {
|
|
31
31
|
static lc_name(): string;
|
|
32
|
+
lc_namespace: string[];
|
|
32
33
|
template: Record<string, unknown>;
|
|
33
34
|
templateFormat: TemplateFormat;
|
|
34
35
|
validateTemplate: boolean;
|
package/dist/prompts/image.js
CHANGED
|
@@ -10,6 +10,12 @@ export class ImagePromptTemplate extends BasePromptTemplate {
|
|
|
10
10
|
}
|
|
11
11
|
constructor(input) {
|
|
12
12
|
super(input);
|
|
13
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: ["langchain_core", "prompts", "image"]
|
|
18
|
+
});
|
|
13
19
|
Object.defineProperty(this, "template", {
|
|
14
20
|
enumerable: true,
|
|
15
21
|
configurable: true,
|
package/dist/prompts/index.cjs
CHANGED
package/dist/prompts/index.d.ts
CHANGED
package/dist/prompts/index.js
CHANGED
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoteRunnable = void 0;
|
|
4
|
+
const base_js_1 = require("./base.cjs");
|
|
5
|
+
const index_js_1 = require("../documents/index.cjs");
|
|
6
|
+
const prompt_values_js_1 = require("../prompt_values.cjs");
|
|
7
|
+
const log_stream_js_1 = require("../tracers/log_stream.cjs");
|
|
8
|
+
const index_js_2 = require("../messages/index.cjs");
|
|
9
|
+
const outputs_js_1 = require("../outputs.cjs");
|
|
10
|
+
const event_source_parse_js_1 = require("../utils/event_source_parse.cjs");
|
|
11
|
+
const stream_js_1 = require("../utils/stream.cjs");
|
|
12
|
+
function isSuperset(set, subset) {
|
|
13
|
+
for (const elem of subset) {
|
|
14
|
+
if (!set.has(elem)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
+
function revive(obj) {
|
|
22
|
+
if (Array.isArray(obj))
|
|
23
|
+
return obj.map(revive);
|
|
24
|
+
if (typeof obj === "object") {
|
|
25
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
26
|
+
if (!obj || obj instanceof Date) {
|
|
27
|
+
return obj;
|
|
28
|
+
}
|
|
29
|
+
const keysArr = Object.keys(obj);
|
|
30
|
+
const keys = new Set(keysArr);
|
|
31
|
+
if (isSuperset(keys, new Set(["page_content", "metadata"]))) {
|
|
32
|
+
return new index_js_1.Document({
|
|
33
|
+
pageContent: obj.page_content,
|
|
34
|
+
metadata: obj.metadata,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (isSuperset(keys, new Set(["content", "type", "additional_kwargs"]))) {
|
|
38
|
+
if (obj.type === "HumanMessage" || obj.type === "human") {
|
|
39
|
+
return new index_js_2.HumanMessage({
|
|
40
|
+
content: obj.content,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (obj.type === "SystemMessage" || obj.type === "system") {
|
|
44
|
+
return new index_js_2.SystemMessage({
|
|
45
|
+
content: obj.content,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (obj.type === "ChatMessage" || obj.type === "chat") {
|
|
49
|
+
return new index_js_2.ChatMessage({
|
|
50
|
+
content: obj.content,
|
|
51
|
+
role: obj.role,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (obj.type === "FunctionMessage" || obj.type === "function") {
|
|
55
|
+
return new index_js_2.FunctionMessage({
|
|
56
|
+
content: obj.content,
|
|
57
|
+
name: obj.name,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (obj.type === "ToolMessage" || obj.type === "tool") {
|
|
61
|
+
return new index_js_2.ToolMessage({
|
|
62
|
+
content: obj.content,
|
|
63
|
+
tool_call_id: obj.tool_call_id,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (obj.type === "AIMessage" || obj.type === "ai") {
|
|
67
|
+
return new index_js_2.AIMessage({
|
|
68
|
+
content: obj.content,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (obj.type === "HumanMessageChunk") {
|
|
72
|
+
return new index_js_2.HumanMessageChunk({
|
|
73
|
+
content: obj.content,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (obj.type === "SystemMessageChunk") {
|
|
77
|
+
return new index_js_2.SystemMessageChunk({
|
|
78
|
+
content: obj.content,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (obj.type === "ChatMessageChunk") {
|
|
82
|
+
return new index_js_2.ChatMessageChunk({
|
|
83
|
+
content: obj.content,
|
|
84
|
+
role: obj.role,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (obj.type === "FunctionMessageChunk") {
|
|
88
|
+
return new index_js_2.FunctionMessageChunk({
|
|
89
|
+
content: obj.content,
|
|
90
|
+
name: obj.name,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (obj.type === "ToolMessageChunk") {
|
|
94
|
+
return new index_js_2.ToolMessageChunk({
|
|
95
|
+
content: obj.content,
|
|
96
|
+
tool_call_id: obj.tool_call_id,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (obj.type === "AIMessageChunk") {
|
|
100
|
+
return new index_js_2.AIMessageChunk({
|
|
101
|
+
content: obj.content,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (isSuperset(keys, new Set(["text", "generation_info", "type"]))) {
|
|
106
|
+
if (obj.type === "ChatGenerationChunk") {
|
|
107
|
+
return new outputs_js_1.ChatGenerationChunk({
|
|
108
|
+
message: revive(obj.message),
|
|
109
|
+
text: obj.text,
|
|
110
|
+
generationInfo: obj.generation_info,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else if (obj.type === "ChatGeneration") {
|
|
114
|
+
return {
|
|
115
|
+
message: revive(obj.message),
|
|
116
|
+
text: obj.text,
|
|
117
|
+
generationInfo: obj.generation_info,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
else if (obj.type === "GenerationChunk") {
|
|
121
|
+
return new outputs_js_1.GenerationChunk({
|
|
122
|
+
text: obj.text,
|
|
123
|
+
generationInfo: obj.generation_info,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
else if (obj.type === "Generation") {
|
|
127
|
+
return {
|
|
128
|
+
text: obj.text,
|
|
129
|
+
generationInfo: obj.generation_info,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (isSuperset(keys, new Set(["tool", "tool_input", "log", "type"]))) {
|
|
134
|
+
if (obj.type === "AgentAction") {
|
|
135
|
+
return {
|
|
136
|
+
tool: obj.tool,
|
|
137
|
+
toolInput: obj.tool_input,
|
|
138
|
+
log: obj.log,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (isSuperset(keys, new Set(["return_values", "log", "type"]))) {
|
|
143
|
+
if (obj.type === "AgentFinish") {
|
|
144
|
+
return {
|
|
145
|
+
returnValues: obj.return_values,
|
|
146
|
+
log: obj.log,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (isSuperset(keys, new Set(["generations", "run", "type"]))) {
|
|
151
|
+
if (obj.type === "LLMResult") {
|
|
152
|
+
return {
|
|
153
|
+
generations: revive(obj.generations),
|
|
154
|
+
llmOutput: obj.llm_output,
|
|
155
|
+
[outputs_js_1.RUN_KEY]: obj.run,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (isSuperset(keys, new Set(["messages"]))) {
|
|
160
|
+
// TODO: Start checking for type: ChatPromptValue and ChatPromptValueConcrete
|
|
161
|
+
// when LangServe bug is fixed
|
|
162
|
+
return new prompt_values_js_1.ChatPromptValue({
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
164
|
+
messages: obj.messages.map((msg) => revive(msg)),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
if (isSuperset(keys, new Set(["text"]))) {
|
|
168
|
+
// TODO: Start checking for type: StringPromptValue
|
|
169
|
+
// when LangServe bug is fixed
|
|
170
|
+
return new prompt_values_js_1.StringPromptValue(obj.text);
|
|
171
|
+
}
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
173
|
+
const innerRevive = (key) => [
|
|
174
|
+
key,
|
|
175
|
+
revive(obj[key]),
|
|
176
|
+
];
|
|
177
|
+
const rtn = Object.fromEntries(keysArr.map(innerRevive));
|
|
178
|
+
return rtn;
|
|
179
|
+
}
|
|
180
|
+
return obj;
|
|
181
|
+
}
|
|
182
|
+
function deserialize(str) {
|
|
183
|
+
const obj = JSON.parse(str);
|
|
184
|
+
return revive(obj);
|
|
185
|
+
}
|
|
186
|
+
function removeCallbacks(options) {
|
|
187
|
+
const rest = { ...options };
|
|
188
|
+
delete rest.callbacks;
|
|
189
|
+
return rest;
|
|
190
|
+
}
|
|
191
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
192
|
+
function serialize(input) {
|
|
193
|
+
if (Array.isArray(input))
|
|
194
|
+
return input.map(serialize);
|
|
195
|
+
if ((0, index_js_2.isBaseMessage)(input)) {
|
|
196
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
197
|
+
const serializedMessage = {
|
|
198
|
+
content: input.content,
|
|
199
|
+
type: input._getType(),
|
|
200
|
+
additional_kwargs: input.additional_kwargs,
|
|
201
|
+
name: input.name,
|
|
202
|
+
example: false,
|
|
203
|
+
};
|
|
204
|
+
if (index_js_2.ToolMessage.isInstance(input)) {
|
|
205
|
+
serializedMessage.tool_call_id = input.tool_call_id;
|
|
206
|
+
}
|
|
207
|
+
else if (index_js_2.ChatMessage.isInstance(input)) {
|
|
208
|
+
serializedMessage.role = input.role;
|
|
209
|
+
}
|
|
210
|
+
return serializedMessage;
|
|
211
|
+
}
|
|
212
|
+
if (typeof input === "object") {
|
|
213
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
214
|
+
if (!input || input instanceof Date) {
|
|
215
|
+
return input;
|
|
216
|
+
}
|
|
217
|
+
const keysArr = Object.keys(input);
|
|
218
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
219
|
+
const innerSerialize = (key) => [
|
|
220
|
+
key,
|
|
221
|
+
serialize(input[key]),
|
|
222
|
+
];
|
|
223
|
+
const rtn = Object.fromEntries(keysArr.map(innerSerialize));
|
|
224
|
+
return rtn;
|
|
225
|
+
}
|
|
226
|
+
return input;
|
|
227
|
+
}
|
|
228
|
+
class RemoteRunnable extends base_js_1.Runnable {
|
|
229
|
+
constructor(fields) {
|
|
230
|
+
super(fields);
|
|
231
|
+
Object.defineProperty(this, "url", {
|
|
232
|
+
enumerable: true,
|
|
233
|
+
configurable: true,
|
|
234
|
+
writable: true,
|
|
235
|
+
value: void 0
|
|
236
|
+
});
|
|
237
|
+
Object.defineProperty(this, "options", {
|
|
238
|
+
enumerable: true,
|
|
239
|
+
configurable: true,
|
|
240
|
+
writable: true,
|
|
241
|
+
value: void 0
|
|
242
|
+
});
|
|
243
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
244
|
+
enumerable: true,
|
|
245
|
+
configurable: true,
|
|
246
|
+
writable: true,
|
|
247
|
+
value: ["langchain", "schema", "runnable", "remote"]
|
|
248
|
+
});
|
|
249
|
+
const { url, options } = fields;
|
|
250
|
+
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
251
|
+
this.options = options;
|
|
252
|
+
}
|
|
253
|
+
async post(path, body) {
|
|
254
|
+
return fetch(`${this.url}${path}`, {
|
|
255
|
+
method: "POST",
|
|
256
|
+
body: JSON.stringify(serialize(body)),
|
|
257
|
+
headers: {
|
|
258
|
+
"Content-Type": "application/json",
|
|
259
|
+
...this.options?.headers,
|
|
260
|
+
},
|
|
261
|
+
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async invoke(input, options) {
|
|
265
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
266
|
+
const response = await this.post("/invoke", {
|
|
267
|
+
input,
|
|
268
|
+
config: removeCallbacks(config),
|
|
269
|
+
kwargs: kwargs ?? {},
|
|
270
|
+
});
|
|
271
|
+
return revive((await response.json()).output);
|
|
272
|
+
}
|
|
273
|
+
async _batch(inputs, options, _, batchOptions) {
|
|
274
|
+
if (batchOptions?.returnExceptions) {
|
|
275
|
+
throw new Error("returnExceptions is not supported for remote clients");
|
|
276
|
+
}
|
|
277
|
+
const configsAndKwargsArray = options?.map((opts) => this._separateRunnableConfigFromCallOptions(opts));
|
|
278
|
+
const [configs, kwargs] = configsAndKwargsArray?.reduce(([pc, pk], [c, k]) => [
|
|
279
|
+
[...pc, c],
|
|
280
|
+
[...pk, k],
|
|
281
|
+
], [[], []]) ?? [undefined, undefined];
|
|
282
|
+
const response = await this.post("/batch", {
|
|
283
|
+
inputs,
|
|
284
|
+
config: (configs ?? [])
|
|
285
|
+
.map(removeCallbacks)
|
|
286
|
+
.map((config) => ({ ...config, ...batchOptions })),
|
|
287
|
+
kwargs,
|
|
288
|
+
});
|
|
289
|
+
const body = await response.json();
|
|
290
|
+
if (!body.output)
|
|
291
|
+
throw new Error("Invalid response from remote runnable");
|
|
292
|
+
return revive(body.output);
|
|
293
|
+
}
|
|
294
|
+
async batch(inputs, options, batchOptions) {
|
|
295
|
+
if (batchOptions?.returnExceptions) {
|
|
296
|
+
throw Error("returnExceptions is not supported for remote clients");
|
|
297
|
+
}
|
|
298
|
+
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
299
|
+
}
|
|
300
|
+
async stream(input, options) {
|
|
301
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
302
|
+
const response = await this.post("/stream", {
|
|
303
|
+
input,
|
|
304
|
+
config: removeCallbacks(config),
|
|
305
|
+
kwargs,
|
|
306
|
+
});
|
|
307
|
+
if (!response.ok) {
|
|
308
|
+
const json = await response.json();
|
|
309
|
+
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
310
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
311
|
+
error.response = response;
|
|
312
|
+
throw error;
|
|
313
|
+
}
|
|
314
|
+
const { body } = response;
|
|
315
|
+
if (!body) {
|
|
316
|
+
throw new Error("Could not begin remote stream. Please check the given URL and try again.");
|
|
317
|
+
}
|
|
318
|
+
const stream = new ReadableStream({
|
|
319
|
+
async start(controller) {
|
|
320
|
+
const enqueueLine = (0, event_source_parse_js_1.getMessages)((msg) => {
|
|
321
|
+
if (msg.data)
|
|
322
|
+
controller.enqueue(deserialize(msg.data));
|
|
323
|
+
});
|
|
324
|
+
const onLine = (line, fieldLength, flush) => {
|
|
325
|
+
enqueueLine(line, fieldLength, flush);
|
|
326
|
+
if (flush)
|
|
327
|
+
controller.close();
|
|
328
|
+
};
|
|
329
|
+
await (0, event_source_parse_js_1.getBytes)(body, (0, event_source_parse_js_1.getLines)(onLine));
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
return stream_js_1.IterableReadableStream.fromReadableStream(stream);
|
|
333
|
+
}
|
|
334
|
+
async *streamLog(input, options, streamOptions) {
|
|
335
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
336
|
+
const stream = new log_stream_js_1.LogStreamCallbackHandler({
|
|
337
|
+
...streamOptions,
|
|
338
|
+
autoClose: false,
|
|
339
|
+
});
|
|
340
|
+
const { callbacks } = config;
|
|
341
|
+
if (callbacks === undefined) {
|
|
342
|
+
config.callbacks = [stream];
|
|
343
|
+
}
|
|
344
|
+
else if (Array.isArray(callbacks)) {
|
|
345
|
+
config.callbacks = callbacks.concat([stream]);
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
const copiedCallbacks = callbacks.copy();
|
|
349
|
+
copiedCallbacks.inheritableHandlers.push(stream);
|
|
350
|
+
config.callbacks = copiedCallbacks;
|
|
351
|
+
}
|
|
352
|
+
// The type is in camelCase but the API only accepts snake_case.
|
|
353
|
+
const camelCaseStreamOptions = {
|
|
354
|
+
include_names: streamOptions?.includeNames,
|
|
355
|
+
include_types: streamOptions?.includeTypes,
|
|
356
|
+
include_tags: streamOptions?.includeTags,
|
|
357
|
+
exclude_names: streamOptions?.excludeNames,
|
|
358
|
+
exclude_types: streamOptions?.excludeTypes,
|
|
359
|
+
exclude_tags: streamOptions?.excludeTags,
|
|
360
|
+
};
|
|
361
|
+
const response = await this.post("/stream_log", {
|
|
362
|
+
input,
|
|
363
|
+
config: removeCallbacks(config),
|
|
364
|
+
kwargs,
|
|
365
|
+
...camelCaseStreamOptions,
|
|
366
|
+
diff: false,
|
|
367
|
+
});
|
|
368
|
+
const { body } = response;
|
|
369
|
+
if (!body) {
|
|
370
|
+
throw new Error("Could not begin remote stream log. Please check the given URL and try again.");
|
|
371
|
+
}
|
|
372
|
+
const runnableStream = (0, event_source_parse_js_1.convertEventStreamToIterableReadableDataStream)(body);
|
|
373
|
+
for await (const log of runnableStream) {
|
|
374
|
+
yield revive(JSON.parse(log));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
exports.RemoteRunnable = RemoteRunnable;
|