langchain 0.1.3 → 0.1.5
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/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/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/chat_models/anthropic.cjs +2 -0
- package/dist/chat_models/anthropic.js +2 -0
- package/dist/document_transformers/openai_functions.d.ts +1 -1
- package/dist/document_transformers/openai_functions.js +1 -1
- package/dist/experimental/chat_models/anthropic_functions.cjs +11 -2
- package/dist/experimental/chat_models/anthropic_functions.d.ts +3 -0
- package/dist/experimental/chat_models/anthropic_functions.js +11 -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/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/retrievers/self_query/pinecone.d.ts +2 -2
- package/dist/runnables/remote.cjs +179 -74
- package/dist/runnables/remote.d.ts +3 -0
- package/dist/runnables/remote.js +180 -75
- package/dist/text_splitter.cjs +6 -5
- package/dist/text_splitter.js +6 -5
- 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/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 +16 -9
- package/dist/util/sql_utils.js +16 -9
- package/dist/vectorstores/pinecone.cjs +2 -0
- package/dist/vectorstores/pinecone.js +2 -0
- package/package.json +7 -7
|
@@ -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() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { VectorStoreInterface } from "@langchain/core/vectorstores";
|
|
2
2
|
import { BasicTranslator } from "./base.js";
|
|
3
3
|
/**
|
|
4
4
|
* Specialized translator class that extends the BasicTranslator. It is
|
|
@@ -21,6 +21,6 @@ import { BasicTranslator } from "./base.js";
|
|
|
21
21
|
* );
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
export declare class PineconeTranslator<T extends
|
|
24
|
+
export declare class PineconeTranslator<T extends VectorStoreInterface> extends BasicTranslator<T> {
|
|
25
25
|
constructor();
|
|
26
26
|
}
|
|
@@ -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,
|
|
@@ -158,8 +219,9 @@ class RemoteRunnable extends runnables_1.Runnable {
|
|
|
158
219
|
body: JSON.stringify(body),
|
|
159
220
|
headers: {
|
|
160
221
|
"Content-Type": "application/json",
|
|
222
|
+
...this.options?.headers,
|
|
161
223
|
},
|
|
162
|
-
signal: AbortSignal.timeout(this.options?.timeout ??
|
|
224
|
+
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
163
225
|
});
|
|
164
226
|
}
|
|
165
227
|
async invoke(input, options) {
|
|
@@ -232,5 +294,48 @@ class RemoteRunnable extends runnables_1.Runnable {
|
|
|
232
294
|
});
|
|
233
295
|
return stream_1.IterableReadableStream.fromReadableStream(stream);
|
|
234
296
|
}
|
|
297
|
+
async *streamLog(input, options, streamOptions) {
|
|
298
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
299
|
+
const stream = new log_stream_1.LogStreamCallbackHandler({
|
|
300
|
+
...streamOptions,
|
|
301
|
+
autoClose: false,
|
|
302
|
+
});
|
|
303
|
+
const { callbacks } = config;
|
|
304
|
+
if (callbacks === undefined) {
|
|
305
|
+
config.callbacks = [stream];
|
|
306
|
+
}
|
|
307
|
+
else if (Array.isArray(callbacks)) {
|
|
308
|
+
config.callbacks = callbacks.concat([stream]);
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
const copiedCallbacks = callbacks.copy();
|
|
312
|
+
copiedCallbacks.inheritableHandlers.push(stream);
|
|
313
|
+
config.callbacks = copiedCallbacks;
|
|
314
|
+
}
|
|
315
|
+
// The type is in camelCase but the API only accepts snake_case.
|
|
316
|
+
const camelCaseStreamOptions = {
|
|
317
|
+
include_names: streamOptions?.includeNames,
|
|
318
|
+
include_types: streamOptions?.includeTypes,
|
|
319
|
+
include_tags: streamOptions?.includeTags,
|
|
320
|
+
exclude_names: streamOptions?.excludeNames,
|
|
321
|
+
exclude_types: streamOptions?.excludeTypes,
|
|
322
|
+
exclude_tags: streamOptions?.excludeTags,
|
|
323
|
+
};
|
|
324
|
+
const response = await this.post("/stream_log", {
|
|
325
|
+
input,
|
|
326
|
+
config,
|
|
327
|
+
kwargs,
|
|
328
|
+
...camelCaseStreamOptions,
|
|
329
|
+
diff: false,
|
|
330
|
+
});
|
|
331
|
+
const { body } = response;
|
|
332
|
+
if (!body) {
|
|
333
|
+
throw new Error("Could not begin remote stream log. Please check the given URL and try again.");
|
|
334
|
+
}
|
|
335
|
+
const runnableStream = (0, event_source_parse_1.convertEventStreamToIterableReadableDataStream)(body);
|
|
336
|
+
for await (const log of runnableStream) {
|
|
337
|
+
yield revive(JSON.parse(log));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
235
340
|
}
|
|
236
341
|
exports.RemoteRunnable = RemoteRunnable;
|
|
@@ -1,8 +1,10 @@
|
|
|
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;
|
|
7
|
+
headers?: Record<string, unknown>;
|
|
6
8
|
};
|
|
7
9
|
export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
8
10
|
private url;
|
|
@@ -23,5 +25,6 @@ export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends Run
|
|
|
23
25
|
}): Promise<(RunOutput | Error)[]>;
|
|
24
26
|
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
25
27
|
stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
|
|
28
|
+
streamLog(input: RunInput, options?: Partial<CallOptions>, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<RunLogPatch>;
|
|
26
29
|
}
|
|
27
30
|
export {};
|
package/dist/runnables/remote.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Runnable, } from "@langchain/core/runnables";
|
|
2
2
|
import { Document } from "@langchain/core/documents";
|
|
3
3
|
import { ChatPromptValue, StringPromptValue, } from "@langchain/core/prompt_values";
|
|
4
|
+
import { LogStreamCallbackHandler, } from "@langchain/core/tracers/log_stream";
|
|
4
5
|
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessage, FunctionMessageChunk, HumanMessage, HumanMessageChunk, SystemMessage, SystemMessageChunk, ToolMessage, ToolMessageChunk, } from "@langchain/core/messages";
|
|
5
|
-
import {
|
|
6
|
+
import { GenerationChunk, ChatGenerationChunk, RUN_KEY, } from "@langchain/core/outputs";
|
|
7
|
+
import { getBytes, getLines, getMessages, convertEventStreamToIterableReadableDataStream, } from "@langchain/community/utils/event_source_parse";
|
|
6
8
|
import { IterableReadableStream } from "@langchain/core/utils/stream";
|
|
7
9
|
function isSuperset(set, subset) {
|
|
8
10
|
for (const elem of subset) {
|
|
@@ -17,94 +19,153 @@ function revive(obj) {
|
|
|
17
19
|
if (Array.isArray(obj))
|
|
18
20
|
return obj.map(revive);
|
|
19
21
|
if (typeof obj === "object") {
|
|
22
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
23
|
+
if (!obj || obj instanceof Date) {
|
|
24
|
+
return obj;
|
|
25
|
+
}
|
|
20
26
|
const keysArr = Object.keys(obj);
|
|
21
27
|
const keys = new Set(keysArr);
|
|
22
|
-
if (isSuperset(keys, new Set(["page_content", "metadata"])))
|
|
28
|
+
if (isSuperset(keys, new Set(["page_content", "metadata"]))) {
|
|
23
29
|
return new Document({
|
|
24
30
|
pageContent: obj.page_content,
|
|
25
31
|
metadata: obj.metadata,
|
|
26
32
|
});
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
content: obj.content,
|
|
95
|
-
});
|
|
96
|
-
}
|
|
33
|
+
}
|
|
34
|
+
if (isSuperset(keys, new Set(["content", "type", "additional_kwargs"]))) {
|
|
35
|
+
if (obj.type === "HumanMessage" || obj.type === "human") {
|
|
36
|
+
return new HumanMessage({
|
|
37
|
+
content: obj.content,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (obj.type === "SystemMessage" || obj.type === "system") {
|
|
41
|
+
return new SystemMessage({
|
|
42
|
+
content: obj.content,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (obj.type === "ChatMessage" || obj.type === "chat") {
|
|
46
|
+
return new ChatMessage({
|
|
47
|
+
content: obj.content,
|
|
48
|
+
role: obj.role,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (obj.type === "FunctionMessage" || obj.type === "function") {
|
|
52
|
+
return new FunctionMessage({
|
|
53
|
+
content: obj.content,
|
|
54
|
+
name: obj.name,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (obj.type === "ToolMessage" || obj.type === "tool") {
|
|
58
|
+
return new ToolMessage({
|
|
59
|
+
content: obj.content,
|
|
60
|
+
tool_call_id: obj.tool_call_id,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (obj.type === "AIMessage" || obj.type === "ai") {
|
|
64
|
+
return new AIMessage({
|
|
65
|
+
content: obj.content,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (obj.type === "HumanMessageChunk") {
|
|
69
|
+
return new HumanMessageChunk({
|
|
70
|
+
content: obj.content,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
if (obj.type === "SystemMessageChunk") {
|
|
74
|
+
return new SystemMessageChunk({
|
|
75
|
+
content: obj.content,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (obj.type === "ChatMessageChunk") {
|
|
79
|
+
return new ChatMessageChunk({
|
|
80
|
+
content: obj.content,
|
|
81
|
+
role: obj.role,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (obj.type === "FunctionMessageChunk") {
|
|
85
|
+
return new FunctionMessageChunk({
|
|
86
|
+
content: obj.content,
|
|
87
|
+
name: obj.name,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (obj.type === "ToolMessageChunk") {
|
|
91
|
+
return new ToolMessageChunk({
|
|
92
|
+
content: obj.content,
|
|
93
|
+
tool_call_id: obj.tool_call_id,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
if (obj.type === "AIMessageChunk") {
|
|
97
|
+
return new AIMessageChunk({
|
|
98
|
+
content: obj.content,
|
|
99
|
+
});
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
|
-
if (isSuperset(keys, new Set(["text"]))) {
|
|
100
|
-
|
|
102
|
+
if (isSuperset(keys, new Set(["text", "generation_info", "type"]))) {
|
|
103
|
+
if (obj.type === "ChatGenerationChunk") {
|
|
104
|
+
return new ChatGenerationChunk({
|
|
105
|
+
message: revive(obj.message),
|
|
106
|
+
text: obj.text,
|
|
107
|
+
generationInfo: obj.generation_info,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else if (obj.type === "ChatGeneration") {
|
|
111
|
+
return {
|
|
112
|
+
message: revive(obj.message),
|
|
113
|
+
text: obj.text,
|
|
114
|
+
generationInfo: obj.generation_info,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
else if (obj.type === "GenerationChunk") {
|
|
118
|
+
return new GenerationChunk({
|
|
119
|
+
text: obj.text,
|
|
120
|
+
generationInfo: obj.generation_info,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else if (obj.type === "Generation") {
|
|
124
|
+
return {
|
|
125
|
+
text: obj.text,
|
|
126
|
+
generationInfo: obj.generation_info,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (isSuperset(keys, new Set(["tool", "tool_input", "log", "type"]))) {
|
|
131
|
+
if (obj.type === "AgentAction") {
|
|
132
|
+
return {
|
|
133
|
+
tool: obj.tool,
|
|
134
|
+
toolInput: obj.tool_input,
|
|
135
|
+
log: obj.log,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (isSuperset(keys, new Set(["return_values", "log", "type"]))) {
|
|
140
|
+
if (obj.type === "AgentFinish") {
|
|
141
|
+
return {
|
|
142
|
+
returnValues: obj.return_values,
|
|
143
|
+
log: obj.log,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (isSuperset(keys, new Set(["generations", "run", "type"]))) {
|
|
148
|
+
if (obj.type === "LLMResult") {
|
|
149
|
+
return {
|
|
150
|
+
generations: revive(obj.generations),
|
|
151
|
+
llmOutput: obj.llm_output,
|
|
152
|
+
[RUN_KEY]: obj.run,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
101
155
|
}
|
|
102
156
|
if (isSuperset(keys, new Set(["messages"]))) {
|
|
157
|
+
// TODO: Start checking for type: ChatPromptValue and ChatPromptValueConcrete
|
|
158
|
+
// when LangServe bug is fixed
|
|
103
159
|
return new ChatPromptValue({
|
|
104
160
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
105
161
|
messages: obj.messages.map((msg) => revive(msg)),
|
|
106
162
|
});
|
|
107
163
|
}
|
|
164
|
+
if (isSuperset(keys, new Set(["text"]))) {
|
|
165
|
+
// TODO: Start checking for type: StringPromptValue
|
|
166
|
+
// when LangServe bug is fixed
|
|
167
|
+
return new StringPromptValue(obj.text);
|
|
168
|
+
}
|
|
108
169
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
109
170
|
const innerRevive = (key) => [
|
|
110
171
|
key,
|
|
@@ -155,8 +216,9 @@ export class RemoteRunnable extends Runnable {
|
|
|
155
216
|
body: JSON.stringify(body),
|
|
156
217
|
headers: {
|
|
157
218
|
"Content-Type": "application/json",
|
|
219
|
+
...this.options?.headers,
|
|
158
220
|
},
|
|
159
|
-
signal: AbortSignal.timeout(this.options?.timeout ??
|
|
221
|
+
signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
|
|
160
222
|
});
|
|
161
223
|
}
|
|
162
224
|
async invoke(input, options) {
|
|
@@ -229,4 +291,47 @@ export class RemoteRunnable extends Runnable {
|
|
|
229
291
|
});
|
|
230
292
|
return IterableReadableStream.fromReadableStream(stream);
|
|
231
293
|
}
|
|
294
|
+
async *streamLog(input, options, streamOptions) {
|
|
295
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
296
|
+
const stream = new LogStreamCallbackHandler({
|
|
297
|
+
...streamOptions,
|
|
298
|
+
autoClose: false,
|
|
299
|
+
});
|
|
300
|
+
const { callbacks } = config;
|
|
301
|
+
if (callbacks === undefined) {
|
|
302
|
+
config.callbacks = [stream];
|
|
303
|
+
}
|
|
304
|
+
else if (Array.isArray(callbacks)) {
|
|
305
|
+
config.callbacks = callbacks.concat([stream]);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const copiedCallbacks = callbacks.copy();
|
|
309
|
+
copiedCallbacks.inheritableHandlers.push(stream);
|
|
310
|
+
config.callbacks = copiedCallbacks;
|
|
311
|
+
}
|
|
312
|
+
// The type is in camelCase but the API only accepts snake_case.
|
|
313
|
+
const camelCaseStreamOptions = {
|
|
314
|
+
include_names: streamOptions?.includeNames,
|
|
315
|
+
include_types: streamOptions?.includeTypes,
|
|
316
|
+
include_tags: streamOptions?.includeTags,
|
|
317
|
+
exclude_names: streamOptions?.excludeNames,
|
|
318
|
+
exclude_types: streamOptions?.excludeTypes,
|
|
319
|
+
exclude_tags: streamOptions?.excludeTags,
|
|
320
|
+
};
|
|
321
|
+
const response = await this.post("/stream_log", {
|
|
322
|
+
input,
|
|
323
|
+
config,
|
|
324
|
+
kwargs,
|
|
325
|
+
...camelCaseStreamOptions,
|
|
326
|
+
diff: false,
|
|
327
|
+
});
|
|
328
|
+
const { body } = response;
|
|
329
|
+
if (!body) {
|
|
330
|
+
throw new Error("Could not begin remote stream log. Please check the given URL and try again.");
|
|
331
|
+
}
|
|
332
|
+
const runnableStream = convertEventStreamToIterableReadableDataStream(body);
|
|
333
|
+
for await (const log of runnableStream) {
|
|
334
|
+
yield revive(JSON.parse(log));
|
|
335
|
+
}
|
|
336
|
+
}
|
|
232
337
|
}
|
package/dist/text_splitter.cjs
CHANGED
|
@@ -691,13 +691,14 @@ class TokenTextSplitter extends TextSplitter {
|
|
|
691
691
|
const splits = [];
|
|
692
692
|
const input_ids = this.tokenizer.encode(text, this.allowedSpecial, this.disallowedSpecial);
|
|
693
693
|
let start_idx = 0;
|
|
694
|
-
let cur_idx = Math.min(start_idx + this.chunkSize, input_ids.length);
|
|
695
|
-
let chunk_ids = input_ids.slice(start_idx, cur_idx);
|
|
696
694
|
while (start_idx < input_ids.length) {
|
|
695
|
+
if (start_idx > 0) {
|
|
696
|
+
start_idx -= this.chunkOverlap;
|
|
697
|
+
}
|
|
698
|
+
const end_idx = Math.min(start_idx + this.chunkSize, input_ids.length);
|
|
699
|
+
const chunk_ids = input_ids.slice(start_idx, end_idx);
|
|
697
700
|
splits.push(this.tokenizer.decode(chunk_ids));
|
|
698
|
-
start_idx
|
|
699
|
-
cur_idx = Math.min(start_idx + this.chunkSize, input_ids.length);
|
|
700
|
-
chunk_ids = input_ids.slice(start_idx, cur_idx);
|
|
701
|
+
start_idx = end_idx;
|
|
701
702
|
}
|
|
702
703
|
return splits;
|
|
703
704
|
}
|