langchain 0.1.2 → 0.1.4
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/agents/openai_functions/index.cjs +3 -3
- package/dist/agents/openai_functions/index.js +3 -3
- package/dist/agents/openai_functions/output_parser.cjs +1 -1
- package/dist/agents/openai_functions/output_parser.d.ts +1 -1
- package/dist/agents/openai_functions/output_parser.js +1 -1
- package/dist/agents/openai_tools/index.cjs +2 -2
- package/dist/agents/openai_tools/index.js +2 -2
- package/dist/agents/openai_tools/output_parser.cjs +1 -1
- package/dist/agents/openai_tools/output_parser.d.ts +1 -1
- package/dist/agents/openai_tools/output_parser.js +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs +2 -2
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.js +1 -1
- package/dist/agents/toolkits/openapi/openapi.cjs +2 -2
- package/dist/agents/toolkits/openapi/openapi.js +1 -1
- package/dist/cache/cloudflare_kv.cjs +2 -1
- package/dist/cache/cloudflare_kv.js +2 -1
- package/dist/chains/history_aware_retriever.cjs +1 -1
- package/dist/chains/history_aware_retriever.d.ts +1 -1
- package/dist/chains/history_aware_retriever.js +1 -1
- package/dist/chains/openai_functions/base.d.ts +1 -1
- package/dist/chains/openai_functions/openapi.d.ts +1 -1
- package/dist/chains/openai_functions/structured_output.cjs +2 -0
- package/dist/chains/openai_functions/structured_output.d.ts +3 -1
- package/dist/chains/openai_functions/structured_output.js +2 -0
- package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +6 -0
- package/dist/chains/sql_db/sql_db_prompt.js +19 -0
- package/dist/chat_models/cloudflare_workersai.cjs +2 -0
- package/dist/chat_models/cloudflare_workersai.js +2 -0
- package/dist/document_transformers/openai_functions.d.ts +1 -1
- package/dist/document_transformers/openai_functions.js +1 -1
- package/dist/embeddings/cloudflare_workersai.cjs +2 -0
- package/dist/embeddings/cloudflare_workersai.js +2 -0
- package/dist/experimental/chat_models/anthropic_functions.cjs +2 -2
- package/dist/experimental/chat_models/anthropic_functions.js +2 -2
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +2 -1
- package/dist/experimental/plan_and_execute/agent_executor.d.ts +1 -2
- package/dist/experimental/plan_and_execute/prompt.d.ts +1 -2
- package/dist/llms/cloudflare_workersai.cjs +2 -0
- package/dist/llms/cloudflare_workersai.js +2 -0
- package/dist/output_parsers/index.cjs +2 -1
- package/dist/output_parsers/index.d.ts +1 -1
- package/dist/output_parsers/index.js +1 -1
- package/dist/output_parsers/openai_functions.d.ts +1 -1
- package/dist/output_parsers/openai_tools.cjs +86 -8
- package/dist/output_parsers/openai_tools.d.ts +31 -2
- package/dist/output_parsers/openai_tools.js +84 -7
- package/dist/output_parsers/structured.js +1 -1
- package/dist/runnables/remote.cjs +177 -73
- package/dist/runnables/remote.d.ts +2 -0
- package/dist/runnables/remote.js +178 -74
- package/dist/stores/message/cloudflare_d1.cjs +2 -0
- package/dist/stores/message/cloudflare_d1.js +2 -0
- package/dist/tools/chain.cjs +3 -2
- package/dist/tools/chain.d.ts +3 -1
- package/dist/tools/chain.js +2 -1
- package/dist/tools/convert_to_openai.cjs +3 -26
- package/dist/tools/convert_to_openai.d.ts +2 -8
- package/dist/tools/convert_to_openai.js +2 -24
- package/dist/tools/retriever.cjs +2 -2
- package/dist/tools/retriever.d.ts +1 -1
- package/dist/tools/retriever.js +1 -1
- package/dist/util/sql_utils.cjs +23 -0
- package/dist/util/sql_utils.js +24 -1
- package/dist/vectorstores/cloudflare_vectorize.cjs +2 -0
- package/dist/vectorstores/cloudflare_vectorize.js +2 -0
- package/package.json +7 -7
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.JsonOutputToolsParser = void 0;
|
|
3
|
+
exports.JsonOutputKeyToolsParser = exports.JsonOutputToolsParser = void 0;
|
|
4
4
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
5
|
/**
|
|
6
|
-
* Class for parsing the output of
|
|
7
|
-
* instance of `OutputToolsParser` to parse the output.
|
|
6
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
8
7
|
*/
|
|
9
8
|
class JsonOutputToolsParser extends output_parsers_1.BaseLLMOutputParser {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
static lc_name() {
|
|
10
|
+
return "JsonOutputToolsParser";
|
|
11
|
+
}
|
|
12
|
+
constructor(fields) {
|
|
13
|
+
super(fields);
|
|
14
|
+
Object.defineProperty(this, "returnId", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: false
|
|
19
|
+
});
|
|
12
20
|
Object.defineProperty(this, "lc_namespace", {
|
|
13
21
|
enumerable: true,
|
|
14
22
|
configurable: true,
|
|
@@ -21,9 +29,7 @@ class JsonOutputToolsParser extends output_parsers_1.BaseLLMOutputParser {
|
|
|
21
29
|
writable: true,
|
|
22
30
|
value: true
|
|
23
31
|
});
|
|
24
|
-
|
|
25
|
-
static lc_name() {
|
|
26
|
-
return "JsonOutputToolsParser";
|
|
32
|
+
this.returnId = fields?.returnId ?? this.returnId;
|
|
27
33
|
}
|
|
28
34
|
/**
|
|
29
35
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
@@ -45,6 +51,9 @@ class JsonOutputToolsParser extends output_parsers_1.BaseLLMOutputParser {
|
|
|
45
51
|
type: toolCall.function.name,
|
|
46
52
|
args: JSON.parse(toolCall.function.arguments),
|
|
47
53
|
};
|
|
54
|
+
if (this.returnId) {
|
|
55
|
+
parsedToolCall.id = toolCall.id;
|
|
56
|
+
}
|
|
48
57
|
// backward-compatibility with previous
|
|
49
58
|
// versions of Langchain JS, which uses `name` and `arguments`
|
|
50
59
|
Object.defineProperty(parsedToolCall, "name", {
|
|
@@ -64,3 +73,72 @@ class JsonOutputToolsParser extends output_parsers_1.BaseLLMOutputParser {
|
|
|
64
73
|
}
|
|
65
74
|
}
|
|
66
75
|
exports.JsonOutputToolsParser = JsonOutputToolsParser;
|
|
76
|
+
/**
|
|
77
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
78
|
+
* expecting only a single tool to be called.
|
|
79
|
+
*/
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
class JsonOutputKeyToolsParser extends output_parsers_1.BaseLLMOutputParser {
|
|
82
|
+
static lc_name() {
|
|
83
|
+
return "JsonOutputKeyToolsParser";
|
|
84
|
+
}
|
|
85
|
+
constructor(params) {
|
|
86
|
+
super(params);
|
|
87
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: true,
|
|
91
|
+
value: ["langchain", "output_parsers", "openai_tools"]
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: true
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperty(this, "returnId", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: false
|
|
104
|
+
});
|
|
105
|
+
/** The type of tool calls to return. */
|
|
106
|
+
Object.defineProperty(this, "keyName", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
112
|
+
/** Whether to return only the first tool call. */
|
|
113
|
+
Object.defineProperty(this, "returnSingle", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
value: false
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(this, "initialParser", {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
value: void 0
|
|
124
|
+
});
|
|
125
|
+
this.keyName = params.keyName;
|
|
126
|
+
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
127
|
+
this.initialParser = new JsonOutputToolsParser(params);
|
|
128
|
+
}
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
|
+
async parseResult(generations) {
|
|
131
|
+
const results = await this.initialParser.parseResult(generations);
|
|
132
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
134
|
+
let returnedValues = matchingResults;
|
|
135
|
+
if (!this.returnId) {
|
|
136
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
137
|
+
}
|
|
138
|
+
if (this.returnSingle) {
|
|
139
|
+
return returnedValues[0];
|
|
140
|
+
}
|
|
141
|
+
return returnedValues;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.JsonOutputKeyToolsParser = JsonOutputKeyToolsParser;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
2
2
|
import type { ChatGeneration } from "@langchain/core/outputs";
|
|
3
3
|
export type ParsedToolCall = {
|
|
4
|
+
id?: string;
|
|
4
5
|
type: string;
|
|
5
6
|
args: Record<string, any>;
|
|
6
7
|
/** @deprecated Use `type` instead. Will be removed in 0.2.0. */
|
|
@@ -8,14 +9,19 @@ export type ParsedToolCall = {
|
|
|
8
9
|
/** @deprecated Use `args` instead. Will be removed in 0.2.0. */
|
|
9
10
|
arguments: Record<string, any>;
|
|
10
11
|
};
|
|
12
|
+
export type JsonOutputToolsParserParams = {
|
|
13
|
+
/** Whether to return the tool call id. */
|
|
14
|
+
returnId?: boolean;
|
|
15
|
+
};
|
|
11
16
|
/**
|
|
12
|
-
* Class for parsing the output of
|
|
13
|
-
* instance of `OutputToolsParser` to parse the output.
|
|
17
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
14
18
|
*/
|
|
15
19
|
export declare class JsonOutputToolsParser extends BaseLLMOutputParser<ParsedToolCall[]> {
|
|
16
20
|
static lc_name(): string;
|
|
21
|
+
returnId: boolean;
|
|
17
22
|
lc_namespace: string[];
|
|
18
23
|
lc_serializable: boolean;
|
|
24
|
+
constructor(fields?: JsonOutputToolsParserParams);
|
|
19
25
|
/**
|
|
20
26
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
21
27
|
* only the arguments of the function call are returned.
|
|
@@ -24,3 +30,26 @@ export declare class JsonOutputToolsParser extends BaseLLMOutputParser<ParsedToo
|
|
|
24
30
|
*/
|
|
25
31
|
parseResult(generations: ChatGeneration[]): Promise<ParsedToolCall[]>;
|
|
26
32
|
}
|
|
33
|
+
export type JsonOutputKeyToolsParserParams = {
|
|
34
|
+
keyName: string;
|
|
35
|
+
returnSingle?: boolean;
|
|
36
|
+
/** Whether to return the tool call id. */
|
|
37
|
+
returnId?: boolean;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
41
|
+
* expecting only a single tool to be called.
|
|
42
|
+
*/
|
|
43
|
+
export declare class JsonOutputKeyToolsParser extends BaseLLMOutputParser<any> {
|
|
44
|
+
static lc_name(): string;
|
|
45
|
+
lc_namespace: string[];
|
|
46
|
+
lc_serializable: boolean;
|
|
47
|
+
returnId: boolean;
|
|
48
|
+
/** The type of tool calls to return. */
|
|
49
|
+
keyName: string;
|
|
50
|
+
/** Whether to return only the first tool call. */
|
|
51
|
+
returnSingle: boolean;
|
|
52
|
+
initialParser: JsonOutputToolsParser;
|
|
53
|
+
constructor(params: JsonOutputKeyToolsParserParams);
|
|
54
|
+
parseResult(generations: ChatGeneration[]): Promise<any>;
|
|
55
|
+
}
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
|
|
2
2
|
/**
|
|
3
|
-
* Class for parsing the output of
|
|
4
|
-
* instance of `OutputToolsParser` to parse the output.
|
|
3
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
5
4
|
*/
|
|
6
5
|
export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
static lc_name() {
|
|
7
|
+
return "JsonOutputToolsParser";
|
|
8
|
+
}
|
|
9
|
+
constructor(fields) {
|
|
10
|
+
super(fields);
|
|
11
|
+
Object.defineProperty(this, "returnId", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: false
|
|
16
|
+
});
|
|
9
17
|
Object.defineProperty(this, "lc_namespace", {
|
|
10
18
|
enumerable: true,
|
|
11
19
|
configurable: true,
|
|
@@ -18,9 +26,7 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
18
26
|
writable: true,
|
|
19
27
|
value: true
|
|
20
28
|
});
|
|
21
|
-
|
|
22
|
-
static lc_name() {
|
|
23
|
-
return "JsonOutputToolsParser";
|
|
29
|
+
this.returnId = fields?.returnId ?? this.returnId;
|
|
24
30
|
}
|
|
25
31
|
/**
|
|
26
32
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
@@ -42,6 +48,9 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
42
48
|
type: toolCall.function.name,
|
|
43
49
|
args: JSON.parse(toolCall.function.arguments),
|
|
44
50
|
};
|
|
51
|
+
if (this.returnId) {
|
|
52
|
+
parsedToolCall.id = toolCall.id;
|
|
53
|
+
}
|
|
45
54
|
// backward-compatibility with previous
|
|
46
55
|
// versions of Langchain JS, which uses `name` and `arguments`
|
|
47
56
|
Object.defineProperty(parsedToolCall, "name", {
|
|
@@ -60,3 +69,71 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
60
69
|
return parsedToolCalls;
|
|
61
70
|
}
|
|
62
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
74
|
+
* expecting only a single tool to be called.
|
|
75
|
+
*/
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
78
|
+
static lc_name() {
|
|
79
|
+
return "JsonOutputKeyToolsParser";
|
|
80
|
+
}
|
|
81
|
+
constructor(params) {
|
|
82
|
+
super(params);
|
|
83
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: ["langchain", "output_parsers", "openai_tools"]
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: true
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "returnId", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: false
|
|
100
|
+
});
|
|
101
|
+
/** The type of tool calls to return. */
|
|
102
|
+
Object.defineProperty(this, "keyName", {
|
|
103
|
+
enumerable: true,
|
|
104
|
+
configurable: true,
|
|
105
|
+
writable: true,
|
|
106
|
+
value: void 0
|
|
107
|
+
});
|
|
108
|
+
/** Whether to return only the first tool call. */
|
|
109
|
+
Object.defineProperty(this, "returnSingle", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: false
|
|
114
|
+
});
|
|
115
|
+
Object.defineProperty(this, "initialParser", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true,
|
|
118
|
+
writable: true,
|
|
119
|
+
value: void 0
|
|
120
|
+
});
|
|
121
|
+
this.keyName = params.keyName;
|
|
122
|
+
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
123
|
+
this.initialParser = new JsonOutputToolsParser(params);
|
|
124
|
+
}
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
|
+
async parseResult(generations) {
|
|
127
|
+
const results = await this.initialParser.parseResult(generations);
|
|
128
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
|
+
let returnedValues = matchingResults;
|
|
131
|
+
if (!this.returnId) {
|
|
132
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
133
|
+
}
|
|
134
|
+
if (this.returnSingle) {
|
|
135
|
+
return returnedValues[0];
|
|
136
|
+
}
|
|
137
|
+
return returnedValues;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
import { zodToJsonSchema, } from "zod-to-json-schema";
|
|
3
3
|
import { BaseOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
|
|
4
4
|
export class StructuredOutputParser extends BaseOutputParser {
|
|
5
5
|
static lc_name() {
|
|
@@ -4,7 +4,9 @@ exports.RemoteRunnable = void 0;
|
|
|
4
4
|
const runnables_1 = require("@langchain/core/runnables");
|
|
5
5
|
const documents_1 = require("@langchain/core/documents");
|
|
6
6
|
const prompt_values_1 = require("@langchain/core/prompt_values");
|
|
7
|
+
const log_stream_1 = require("@langchain/core/tracers/log_stream");
|
|
7
8
|
const messages_1 = require("@langchain/core/messages");
|
|
9
|
+
const outputs_1 = require("@langchain/core/outputs");
|
|
8
10
|
const event_source_parse_1 = require("@langchain/community/utils/event_source_parse");
|
|
9
11
|
const stream_1 = require("@langchain/core/utils/stream");
|
|
10
12
|
function isSuperset(set, subset) {
|
|
@@ -20,94 +22,153 @@ function revive(obj) {
|
|
|
20
22
|
if (Array.isArray(obj))
|
|
21
23
|
return obj.map(revive);
|
|
22
24
|
if (typeof obj === "object") {
|
|
25
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
26
|
+
if (!obj || obj instanceof Date) {
|
|
27
|
+
return obj;
|
|
28
|
+
}
|
|
23
29
|
const keysArr = Object.keys(obj);
|
|
24
30
|
const keys = new Set(keysArr);
|
|
25
|
-
if (isSuperset(keys, new Set(["page_content", "metadata"])))
|
|
31
|
+
if (isSuperset(keys, new Set(["page_content", "metadata"]))) {
|
|
26
32
|
return new documents_1.Document({
|
|
27
33
|
pageContent: obj.page_content,
|
|
28
34
|
metadata: obj.metadata,
|
|
29
35
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
content: obj.content,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
36
|
+
}
|
|
37
|
+
if (isSuperset(keys, new Set(["content", "type", "additional_kwargs"]))) {
|
|
38
|
+
if (obj.type === "HumanMessage" || obj.type === "human") {
|
|
39
|
+
return new messages_1.HumanMessage({
|
|
40
|
+
content: obj.content,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (obj.type === "SystemMessage" || obj.type === "system") {
|
|
44
|
+
return new messages_1.SystemMessage({
|
|
45
|
+
content: obj.content,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (obj.type === "ChatMessage" || obj.type === "chat") {
|
|
49
|
+
return new messages_1.ChatMessage({
|
|
50
|
+
content: obj.content,
|
|
51
|
+
role: obj.role,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (obj.type === "FunctionMessage" || obj.type === "function") {
|
|
55
|
+
return new messages_1.FunctionMessage({
|
|
56
|
+
content: obj.content,
|
|
57
|
+
name: obj.name,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
if (obj.type === "ToolMessage" || obj.type === "tool") {
|
|
61
|
+
return new messages_1.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 messages_1.AIMessage({
|
|
68
|
+
content: obj.content,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (obj.type === "HumanMessageChunk") {
|
|
72
|
+
return new messages_1.HumanMessageChunk({
|
|
73
|
+
content: obj.content,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (obj.type === "SystemMessageChunk") {
|
|
77
|
+
return new messages_1.SystemMessageChunk({
|
|
78
|
+
content: obj.content,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (obj.type === "ChatMessageChunk") {
|
|
82
|
+
return new messages_1.ChatMessageChunk({
|
|
83
|
+
content: obj.content,
|
|
84
|
+
role: obj.role,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (obj.type === "FunctionMessageChunk") {
|
|
88
|
+
return new messages_1.FunctionMessageChunk({
|
|
89
|
+
content: obj.content,
|
|
90
|
+
name: obj.name,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (obj.type === "ToolMessageChunk") {
|
|
94
|
+
return new messages_1.ToolMessageChunk({
|
|
95
|
+
content: obj.content,
|
|
96
|
+
tool_call_id: obj.tool_call_id,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (obj.type === "AIMessageChunk") {
|
|
100
|
+
return new messages_1.AIMessageChunk({
|
|
101
|
+
content: obj.content,
|
|
102
|
+
});
|
|
100
103
|
}
|
|
101
104
|
}
|
|
102
|
-
if (isSuperset(keys, new Set(["text"]))) {
|
|
103
|
-
|
|
105
|
+
if (isSuperset(keys, new Set(["text", "generation_info", "type"]))) {
|
|
106
|
+
if (obj.type === "ChatGenerationChunk") {
|
|
107
|
+
return new outputs_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_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_1.RUN_KEY]: obj.run,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
104
158
|
}
|
|
105
159
|
if (isSuperset(keys, new Set(["messages"]))) {
|
|
160
|
+
// TODO: Start checking for type: ChatPromptValue and ChatPromptValueConcrete
|
|
161
|
+
// when LangServe bug is fixed
|
|
106
162
|
return new prompt_values_1.ChatPromptValue({
|
|
107
163
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
108
164
|
messages: obj.messages.map((msg) => revive(msg)),
|
|
109
165
|
});
|
|
110
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_1.StringPromptValue(obj.text);
|
|
171
|
+
}
|
|
111
172
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
173
|
const innerRevive = (key) => [
|
|
113
174
|
key,
|
|
@@ -232,5 +293,48 @@ class RemoteRunnable extends runnables_1.Runnable {
|
|
|
232
293
|
});
|
|
233
294
|
return stream_1.IterableReadableStream.fromReadableStream(stream);
|
|
234
295
|
}
|
|
296
|
+
async *streamLog(input, options, streamOptions) {
|
|
297
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
298
|
+
const stream = new log_stream_1.LogStreamCallbackHandler({
|
|
299
|
+
...streamOptions,
|
|
300
|
+
autoClose: false,
|
|
301
|
+
});
|
|
302
|
+
const { callbacks } = config;
|
|
303
|
+
if (callbacks === undefined) {
|
|
304
|
+
config.callbacks = [stream];
|
|
305
|
+
}
|
|
306
|
+
else if (Array.isArray(callbacks)) {
|
|
307
|
+
config.callbacks = callbacks.concat([stream]);
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
const copiedCallbacks = callbacks.copy();
|
|
311
|
+
copiedCallbacks.inheritableHandlers.push(stream);
|
|
312
|
+
config.callbacks = copiedCallbacks;
|
|
313
|
+
}
|
|
314
|
+
// The type is in camelCase but the API only accepts snake_case.
|
|
315
|
+
const camelCaseStreamOptions = {
|
|
316
|
+
include_names: streamOptions?.includeNames,
|
|
317
|
+
include_types: streamOptions?.includeTypes,
|
|
318
|
+
include_tags: streamOptions?.includeTags,
|
|
319
|
+
exclude_names: streamOptions?.excludeNames,
|
|
320
|
+
exclude_types: streamOptions?.excludeTypes,
|
|
321
|
+
exclude_tags: streamOptions?.excludeTags,
|
|
322
|
+
};
|
|
323
|
+
const response = await this.post("/stream_log", {
|
|
324
|
+
input,
|
|
325
|
+
config,
|
|
326
|
+
kwargs,
|
|
327
|
+
...camelCaseStreamOptions,
|
|
328
|
+
diff: false,
|
|
329
|
+
});
|
|
330
|
+
const { body } = response;
|
|
331
|
+
if (!body) {
|
|
332
|
+
throw new Error("Could not begin remote stream log. Please check the given URL and try again.");
|
|
333
|
+
}
|
|
334
|
+
const runnableStream = (0, event_source_parse_1.convertEventStreamToIterableReadableDataStream)(body);
|
|
335
|
+
for await (const log of runnableStream) {
|
|
336
|
+
yield revive(JSON.parse(log));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
235
339
|
}
|
|
236
340
|
exports.RemoteRunnable = RemoteRunnable;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Runnable, RunnableBatchOptions, RunnableConfig } from "@langchain/core/runnables";
|
|
2
2
|
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
|
|
3
|
+
import { type LogStreamCallbackHandlerInput, type RunLogPatch } from "@langchain/core/tracers/log_stream";
|
|
3
4
|
import { IterableReadableStream } from "@langchain/core/utils/stream";
|
|
4
5
|
type RemoteRunnableOptions = {
|
|
5
6
|
timeout?: number;
|
|
@@ -23,5 +24,6 @@ export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends Run
|
|
|
23
24
|
}): Promise<(RunOutput | Error)[]>;
|
|
24
25
|
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
25
26
|
stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
|
|
27
|
+
streamLog(input: RunInput, options?: Partial<CallOptions>, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<RunLogPatch>;
|
|
26
28
|
}
|
|
27
29
|
export {};
|