langchain 0.0.160 → 0.0.162
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/callbacks/base.d.ts +10 -10
- package/dist/callbacks/handlers/log_stream.cjs +23 -11
- package/dist/callbacks/handlers/log_stream.d.ts +3 -2
- package/dist/callbacks/handlers/log_stream.js +23 -11
- package/dist/callbacks/manager.cjs +13 -12
- package/dist/callbacks/manager.d.ts +9 -5
- package/dist/callbacks/manager.js +13 -12
- package/dist/chains/base.cjs +1 -1
- package/dist/chains/base.js +1 -1
- package/dist/chat_models/base.cjs +2 -2
- package/dist/chat_models/base.js +2 -2
- package/dist/embeddings/bedrock.cjs +73 -0
- package/dist/embeddings/bedrock.d.ts +47 -0
- package/dist/embeddings/bedrock.js +69 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.cjs +297 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +183 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.js +291 -0
- package/dist/llms/base.cjs +2 -2
- package/dist/llms/base.js +2 -2
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +4 -1
- package/dist/load/import_map.d.ts +3 -0
- package/dist/load/import_map.js +3 -0
- package/dist/prompts/chat.cjs +4 -3
- package/dist/prompts/chat.js +2 -1
- package/dist/retrievers/chaindesk.cjs +71 -0
- package/dist/retrievers/chaindesk.d.ts +18 -0
- package/dist/retrievers/chaindesk.js +67 -0
- package/dist/retrievers/contextual_compression.cjs +2 -2
- package/dist/retrievers/contextual_compression.js +2 -2
- package/dist/retrievers/databerry.cjs +1 -0
- package/dist/retrievers/databerry.d.ts +1 -0
- package/dist/retrievers/databerry.js +1 -0
- package/dist/retrievers/document_compressors/embeddings_filter.cjs +78 -0
- package/dist/retrievers/document_compressors/embeddings_filter.d.ts +39 -0
- package/dist/retrievers/document_compressors/embeddings_filter.js +74 -0
- package/dist/retrievers/document_compressors/index.cjs +33 -1
- package/dist/retrievers/document_compressors/index.d.ts +14 -1
- package/dist/retrievers/document_compressors/index.js +31 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.cjs +28 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.d.ts +1 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.js +26 -0
- package/dist/retrievers/tavily_search_api.cjs +144 -0
- package/dist/retrievers/tavily_search_api.d.ts +38 -0
- package/dist/retrievers/tavily_search_api.js +140 -0
- package/dist/schema/retriever.cjs +1 -1
- package/dist/schema/retriever.js +1 -1
- package/dist/schema/runnable/base.cjs +29 -10
- package/dist/schema/runnable/base.d.ts +5 -1
- package/dist/schema/runnable/base.js +29 -10
- package/dist/stores/message/dynamodb.cjs +9 -6
- package/dist/stores/message/dynamodb.d.ts +3 -2
- package/dist/stores/message/dynamodb.js +9 -6
- package/dist/text_splitter.cjs +22 -8
- package/dist/text_splitter.d.ts +1 -0
- package/dist/text_splitter.js +22 -8
- package/dist/tools/base.cjs +1 -1
- package/dist/tools/base.js +1 -1
- package/dist/types/googlevertexai-types.d.ts +7 -5
- package/dist/util/googlevertexai-connection.cjs +35 -27
- package/dist/util/googlevertexai-connection.d.ts +12 -8
- package/dist/util/googlevertexai-connection.js +33 -26
- package/dist/util/googlevertexai-webauth.d.ts +2 -2
- package/dist/vectorstores/googlevertexai.d.ts +4 -4
- package/embeddings/bedrock.cjs +1 -0
- package/embeddings/bedrock.d.ts +1 -0
- package/embeddings/bedrock.js +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.cjs +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.js +1 -0
- package/package.json +46 -1
- package/retrievers/chaindesk.cjs +1 -0
- package/retrievers/chaindesk.d.ts +1 -0
- package/retrievers/chaindesk.js +1 -0
- package/retrievers/document_compressors/embeddings_filter.cjs +1 -0
- package/retrievers/document_compressors/embeddings_filter.d.ts +1 -0
- package/retrievers/document_compressors/embeddings_filter.js +1 -0
- package/retrievers/tavily_search_api.cjs +1 -0
- package/retrievers/tavily_search_api.d.ts +1 -0
- package/retrievers/tavily_search_api.js +1 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
2
|
+
import { BaseRetriever } from "../schema/retriever.js";
|
|
3
|
+
import { getEnvironmentVariable } from "../util/env.js";
|
|
4
|
+
/**
|
|
5
|
+
* A class for retrieving documents related to a given search term
|
|
6
|
+
* using the Tavily Search API.
|
|
7
|
+
*/
|
|
8
|
+
export class TavilySearchAPIRetriever extends BaseRetriever {
|
|
9
|
+
static lc_name() {
|
|
10
|
+
return "TavilySearchAPIRetriever";
|
|
11
|
+
}
|
|
12
|
+
get lc_namespace() {
|
|
13
|
+
return ["langchain", "retrievers", "tavily_search_api"];
|
|
14
|
+
}
|
|
15
|
+
constructor(fields) {
|
|
16
|
+
super(fields);
|
|
17
|
+
Object.defineProperty(this, "k", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: 10
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "includeGeneratedAnswer", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: false
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "includeRawContent", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: false
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "includeImages", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: false
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "searchDepth", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: "basic"
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "includeDomains", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "excludeDomains", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: void 0
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(this, "kwargs", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: {}
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(this, "apiKey", {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: true,
|
|
68
|
+
writable: true,
|
|
69
|
+
value: void 0
|
|
70
|
+
});
|
|
71
|
+
this.k = fields?.k ?? this.k;
|
|
72
|
+
this.includeGeneratedAnswer =
|
|
73
|
+
fields?.includeGeneratedAnswer ?? this.includeGeneratedAnswer;
|
|
74
|
+
this.includeRawContent =
|
|
75
|
+
fields?.includeRawContent ?? this.includeRawContent;
|
|
76
|
+
this.includeImages = fields?.includeImages ?? this.includeImages;
|
|
77
|
+
this.searchDepth = fields?.searchDepth ?? this.searchDepth;
|
|
78
|
+
this.includeDomains = fields?.includeDomains ?? this.includeDomains;
|
|
79
|
+
this.excludeDomains = fields?.excludeDomains ?? this.excludeDomains;
|
|
80
|
+
this.kwargs = fields?.kwargs ?? this.kwargs;
|
|
81
|
+
this.apiKey = fields?.apiKey ?? getEnvironmentVariable("TAVILY_API_KEY");
|
|
82
|
+
if (this.apiKey === undefined) {
|
|
83
|
+
throw new Error(`No Tavily API key found. Either set an environment variable named "TAVILY_API_KEY" or pass an API key as "apiKey".`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async _getRelevantDocuments(query, _runManager) {
|
|
87
|
+
const body = {
|
|
88
|
+
query,
|
|
89
|
+
include_answer: this.includeGeneratedAnswer,
|
|
90
|
+
include_raw_content: this.includeRawContent,
|
|
91
|
+
include_images: this.includeImages,
|
|
92
|
+
max_results: this.k,
|
|
93
|
+
search_depth: this.searchDepth,
|
|
94
|
+
api_key: this.apiKey,
|
|
95
|
+
};
|
|
96
|
+
if (this.includeDomains) {
|
|
97
|
+
body.include_domains = this.includeDomains;
|
|
98
|
+
}
|
|
99
|
+
if (this.excludeDomains) {
|
|
100
|
+
body.exclude_domains = this.excludeDomains;
|
|
101
|
+
}
|
|
102
|
+
const response = await fetch("https://api.tavily.com/search", {
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers: {
|
|
105
|
+
"content-type": "application/json",
|
|
106
|
+
},
|
|
107
|
+
body: JSON.stringify({ ...body, ...this.kwargs }),
|
|
108
|
+
});
|
|
109
|
+
const json = await response.json();
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
throw new Error(`Request failed with status code ${response.status}: ${json.error}`);
|
|
112
|
+
}
|
|
113
|
+
if (!Array.isArray(json.results)) {
|
|
114
|
+
throw new Error(`Could not parse Tavily results. Please try again.`);
|
|
115
|
+
}
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
+
const docs = json.results.map((result) => {
|
|
118
|
+
const pageContent = this.includeRawContent
|
|
119
|
+
? result.raw_content
|
|
120
|
+
: result.content;
|
|
121
|
+
const metadata = {
|
|
122
|
+
title: result.title,
|
|
123
|
+
source: result.url,
|
|
124
|
+
...Object.fromEntries(Object.entries(result).filter(([k]) => !["content", "title", "url", "raw_content"].includes(k))),
|
|
125
|
+
images: json.images,
|
|
126
|
+
};
|
|
127
|
+
return new Document({ pageContent, metadata });
|
|
128
|
+
});
|
|
129
|
+
if (this.includeGeneratedAnswer) {
|
|
130
|
+
docs.push(new Document({
|
|
131
|
+
pageContent: json.answer,
|
|
132
|
+
metadata: {
|
|
133
|
+
title: "Suggested Answer",
|
|
134
|
+
source: "https://tavily.com/",
|
|
135
|
+
},
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
return docs;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -64,7 +64,7 @@ class BaseRetriever extends index_js_1.Runnable {
|
|
|
64
64
|
async getRelevantDocuments(query, config) {
|
|
65
65
|
const parsedConfig = (0, manager_js_1.parseCallbackConfigArg)(config);
|
|
66
66
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
67
|
-
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query);
|
|
67
|
+
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
68
68
|
try {
|
|
69
69
|
const results = await this._getRelevantDocuments(query, runManager);
|
|
70
70
|
await runManager?.handleRetrieverEnd(results);
|
package/dist/schema/retriever.js
CHANGED
|
@@ -61,7 +61,7 @@ export class BaseRetriever extends Runnable {
|
|
|
61
61
|
async getRelevantDocuments(query, config) {
|
|
62
62
|
const parsedConfig = parseCallbackConfigArg(config);
|
|
63
63
|
const callbackManager_ = await CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
64
|
-
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query);
|
|
64
|
+
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
65
65
|
try {
|
|
66
66
|
const results = await this._getRelevantDocuments(query, runManager);
|
|
67
67
|
await runManager?.handleRetrieverEnd(results);
|
|
@@ -143,16 +143,18 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
143
143
|
callbacks: options.callbacks,
|
|
144
144
|
tags: options.tags,
|
|
145
145
|
metadata: options.metadata,
|
|
146
|
+
runName: options.runName,
|
|
146
147
|
};
|
|
147
148
|
const callOptions = { ...options };
|
|
148
149
|
delete callOptions.callbacks;
|
|
149
150
|
delete callOptions.tags;
|
|
150
151
|
delete callOptions.metadata;
|
|
152
|
+
delete callOptions.runName;
|
|
151
153
|
return [runnableConfig, callOptions];
|
|
152
154
|
}
|
|
153
155
|
async _callWithConfig(func, input, options) {
|
|
154
156
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
155
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
|
|
157
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
|
|
156
158
|
let output;
|
|
157
159
|
try {
|
|
158
160
|
output = await func.bind(this)(input, options, runManager);
|
|
@@ -176,7 +178,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
176
178
|
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
177
179
|
const configs = this._getOptionsList((options ?? {}), inputs.length);
|
|
178
180
|
const callbackManagers = await Promise.all(configs.map(config_js_1.getCallbackMangerForConfig));
|
|
179
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
181
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, configs[i].runType, undefined, undefined, configs[i].runName)));
|
|
180
182
|
let outputs;
|
|
181
183
|
try {
|
|
182
184
|
outputs = await func(inputs, configs, runManagers, batchOptions);
|
|
@@ -206,7 +208,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
206
208
|
if (!runManager) {
|
|
207
209
|
// Start the run manager AFTER the iterator starts to preserve
|
|
208
210
|
// tracing order
|
|
209
|
-
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType);
|
|
211
|
+
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
|
|
210
212
|
}
|
|
211
213
|
if (finalInputSupported) {
|
|
212
214
|
if (finalInput === undefined) {
|
|
@@ -257,7 +259,16 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
257
259
|
await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
|
|
258
260
|
}
|
|
259
261
|
_patchConfig(config = {}, callbackManager = undefined) {
|
|
260
|
-
|
|
262
|
+
const newConfig = { ...config };
|
|
263
|
+
if (callbackManager !== undefined) {
|
|
264
|
+
/**
|
|
265
|
+
* If we're replacing callbacks we need to unset runName
|
|
266
|
+
* since that should apply only to the same run as the original callbacks
|
|
267
|
+
*/
|
|
268
|
+
delete newConfig.runName;
|
|
269
|
+
return { ...newConfig, callbacks: callbackManager };
|
|
270
|
+
}
|
|
271
|
+
return newConfig;
|
|
261
272
|
}
|
|
262
273
|
/**
|
|
263
274
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
@@ -690,7 +701,7 @@ class RunnableSequence extends Runnable {
|
|
|
690
701
|
}
|
|
691
702
|
async invoke(input, options) {
|
|
692
703
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
693
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
704
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
694
705
|
let nextStepInput = input;
|
|
695
706
|
let finalOutput;
|
|
696
707
|
try {
|
|
@@ -712,7 +723,7 @@ class RunnableSequence extends Runnable {
|
|
|
712
723
|
async batch(inputs, options, batchOptions) {
|
|
713
724
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
714
725
|
const callbackManagers = await Promise.all(configList.map(config_js_1.getCallbackMangerForConfig));
|
|
715
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
726
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
716
727
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
717
728
|
let nextStepInputs = inputs;
|
|
718
729
|
let finalOutputs;
|
|
@@ -733,7 +744,7 @@ class RunnableSequence extends Runnable {
|
|
|
733
744
|
}
|
|
734
745
|
async *_streamIterator(input, options) {
|
|
735
746
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
736
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
747
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
737
748
|
let nextStepInput = input;
|
|
738
749
|
const steps = [this.first, ...this.middle, this.last];
|
|
739
750
|
// Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
|
|
@@ -858,13 +869,16 @@ class RunnableMap extends Runnable {
|
|
|
858
869
|
this.steps[key] = _coerceToRunnable(value);
|
|
859
870
|
}
|
|
860
871
|
}
|
|
872
|
+
static from(steps) {
|
|
873
|
+
return new RunnableMap({ steps });
|
|
874
|
+
}
|
|
861
875
|
async invoke(input, options
|
|
862
876
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
863
877
|
) {
|
|
864
878
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
865
879
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
|
|
866
880
|
input,
|
|
867
|
-
});
|
|
881
|
+
}, undefined, undefined, undefined, undefined, options?.runName);
|
|
868
882
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
869
883
|
const output = {};
|
|
870
884
|
try {
|
|
@@ -904,6 +918,11 @@ class RunnableLambda extends Runnable {
|
|
|
904
918
|
});
|
|
905
919
|
this.func = fields.func;
|
|
906
920
|
}
|
|
921
|
+
static from(func) {
|
|
922
|
+
return new RunnableLambda({
|
|
923
|
+
func,
|
|
924
|
+
});
|
|
925
|
+
}
|
|
907
926
|
async _invoke(input, config, runManager) {
|
|
908
927
|
let output = await this.func(input);
|
|
909
928
|
if (output && Runnable.isRunnable(output)) {
|
|
@@ -960,7 +979,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
960
979
|
}
|
|
961
980
|
async invoke(input, options) {
|
|
962
981
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
|
|
963
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
982
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
964
983
|
let firstError;
|
|
965
984
|
for (const runnable of this.runnables()) {
|
|
966
985
|
try {
|
|
@@ -986,7 +1005,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
986
1005
|
}
|
|
987
1006
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
988
1007
|
const callbackManagers = await Promise.all(configList.map((config) => manager_js_1.CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
|
|
989
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
1008
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
990
1009
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
991
1010
|
let firstError;
|
|
992
1011
|
for (const runnable of this.runnables()) {
|
|
@@ -57,7 +57,9 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
57
57
|
withFallbacks(fields: {
|
|
58
58
|
fallbacks: Runnable<RunInput, RunOutput>[];
|
|
59
59
|
}): RunnableWithFallbacks<RunInput, RunOutput>;
|
|
60
|
-
protected _getOptionsList(options: Partial<CallOptions> | Partial<CallOptions>[], length?: number): Partial<CallOptions
|
|
60
|
+
protected _getOptionsList(options: Partial<CallOptions> | Partial<CallOptions>[], length?: number): Partial<CallOptions & {
|
|
61
|
+
runType?: string;
|
|
62
|
+
}>[];
|
|
61
63
|
/**
|
|
62
64
|
* Default implementation of batch, which calls invoke N times.
|
|
63
65
|
* Subclasses should override this method if they can batch more efficiently.
|
|
@@ -287,6 +289,7 @@ export declare class RunnableMap<RunInput> extends Runnable<RunInput, Record<str
|
|
|
287
289
|
constructor(fields: {
|
|
288
290
|
steps: Record<string, RunnableLike<RunInput>>;
|
|
289
291
|
});
|
|
292
|
+
static from<RunInput>(steps: Record<string, RunnableLike<RunInput>>): RunnableMap<RunInput>;
|
|
290
293
|
invoke(input: RunInput, options?: Partial<BaseCallbackConfig>): Promise<Record<string, any>>;
|
|
291
294
|
}
|
|
292
295
|
/**
|
|
@@ -299,6 +302,7 @@ export declare class RunnableLambda<RunInput, RunOutput> extends Runnable<RunInp
|
|
|
299
302
|
constructor(fields: {
|
|
300
303
|
func: RunnableFunc<RunInput, RunOutput>;
|
|
301
304
|
});
|
|
305
|
+
static from<RunInput, RunOutput>(func: RunnableFunc<RunInput, RunOutput>): RunnableLambda<RunInput, RunOutput>;
|
|
302
306
|
_invoke(input: RunInput, config?: Partial<BaseCallbackConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
|
|
303
307
|
invoke(input: RunInput, options?: Partial<BaseCallbackConfig>): Promise<RunOutput>;
|
|
304
308
|
}
|
|
@@ -137,16 +137,18 @@ export class Runnable extends Serializable {
|
|
|
137
137
|
callbacks: options.callbacks,
|
|
138
138
|
tags: options.tags,
|
|
139
139
|
metadata: options.metadata,
|
|
140
|
+
runName: options.runName,
|
|
140
141
|
};
|
|
141
142
|
const callOptions = { ...options };
|
|
142
143
|
delete callOptions.callbacks;
|
|
143
144
|
delete callOptions.tags;
|
|
144
145
|
delete callOptions.metadata;
|
|
146
|
+
delete callOptions.runName;
|
|
145
147
|
return [runnableConfig, callOptions];
|
|
146
148
|
}
|
|
147
149
|
async _callWithConfig(func, input, options) {
|
|
148
150
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
149
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
|
|
151
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
|
|
150
152
|
let output;
|
|
151
153
|
try {
|
|
152
154
|
output = await func.bind(this)(input, options, runManager);
|
|
@@ -170,7 +172,7 @@ export class Runnable extends Serializable {
|
|
|
170
172
|
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
171
173
|
const configs = this._getOptionsList((options ?? {}), inputs.length);
|
|
172
174
|
const callbackManagers = await Promise.all(configs.map(getCallbackMangerForConfig));
|
|
173
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
175
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, configs[i].runType, undefined, undefined, configs[i].runName)));
|
|
174
176
|
let outputs;
|
|
175
177
|
try {
|
|
176
178
|
outputs = await func(inputs, configs, runManagers, batchOptions);
|
|
@@ -200,7 +202,7 @@ export class Runnable extends Serializable {
|
|
|
200
202
|
if (!runManager) {
|
|
201
203
|
// Start the run manager AFTER the iterator starts to preserve
|
|
202
204
|
// tracing order
|
|
203
|
-
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType);
|
|
205
|
+
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
|
|
204
206
|
}
|
|
205
207
|
if (finalInputSupported) {
|
|
206
208
|
if (finalInput === undefined) {
|
|
@@ -251,7 +253,16 @@ export class Runnable extends Serializable {
|
|
|
251
253
|
await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
|
|
252
254
|
}
|
|
253
255
|
_patchConfig(config = {}, callbackManager = undefined) {
|
|
254
|
-
|
|
256
|
+
const newConfig = { ...config };
|
|
257
|
+
if (callbackManager !== undefined) {
|
|
258
|
+
/**
|
|
259
|
+
* If we're replacing callbacks we need to unset runName
|
|
260
|
+
* since that should apply only to the same run as the original callbacks
|
|
261
|
+
*/
|
|
262
|
+
delete newConfig.runName;
|
|
263
|
+
return { ...newConfig, callbacks: callbackManager };
|
|
264
|
+
}
|
|
265
|
+
return newConfig;
|
|
255
266
|
}
|
|
256
267
|
/**
|
|
257
268
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
@@ -680,7 +691,7 @@ export class RunnableSequence extends Runnable {
|
|
|
680
691
|
}
|
|
681
692
|
async invoke(input, options) {
|
|
682
693
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
683
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
694
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
684
695
|
let nextStepInput = input;
|
|
685
696
|
let finalOutput;
|
|
686
697
|
try {
|
|
@@ -702,7 +713,7 @@ export class RunnableSequence extends Runnable {
|
|
|
702
713
|
async batch(inputs, options, batchOptions) {
|
|
703
714
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
704
715
|
const callbackManagers = await Promise.all(configList.map(getCallbackMangerForConfig));
|
|
705
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
716
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
706
717
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
707
718
|
let nextStepInputs = inputs;
|
|
708
719
|
let finalOutputs;
|
|
@@ -723,7 +734,7 @@ export class RunnableSequence extends Runnable {
|
|
|
723
734
|
}
|
|
724
735
|
async *_streamIterator(input, options) {
|
|
725
736
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
726
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
737
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
727
738
|
let nextStepInput = input;
|
|
728
739
|
const steps = [this.first, ...this.middle, this.last];
|
|
729
740
|
// Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
|
|
@@ -847,13 +858,16 @@ export class RunnableMap extends Runnable {
|
|
|
847
858
|
this.steps[key] = _coerceToRunnable(value);
|
|
848
859
|
}
|
|
849
860
|
}
|
|
861
|
+
static from(steps) {
|
|
862
|
+
return new RunnableMap({ steps });
|
|
863
|
+
}
|
|
850
864
|
async invoke(input, options
|
|
851
865
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
852
866
|
) {
|
|
853
867
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
854
868
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
|
|
855
869
|
input,
|
|
856
|
-
});
|
|
870
|
+
}, undefined, undefined, undefined, undefined, options?.runName);
|
|
857
871
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
858
872
|
const output = {};
|
|
859
873
|
try {
|
|
@@ -892,6 +906,11 @@ export class RunnableLambda extends Runnable {
|
|
|
892
906
|
});
|
|
893
907
|
this.func = fields.func;
|
|
894
908
|
}
|
|
909
|
+
static from(func) {
|
|
910
|
+
return new RunnableLambda({
|
|
911
|
+
func,
|
|
912
|
+
});
|
|
913
|
+
}
|
|
895
914
|
async _invoke(input, config, runManager) {
|
|
896
915
|
let output = await this.func(input);
|
|
897
916
|
if (output && Runnable.isRunnable(output)) {
|
|
@@ -947,7 +966,7 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
947
966
|
}
|
|
948
967
|
async invoke(input, options) {
|
|
949
968
|
const callbackManager_ = await CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
|
|
950
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
969
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
951
970
|
let firstError;
|
|
952
971
|
for (const runnable of this.runnables()) {
|
|
953
972
|
try {
|
|
@@ -973,7 +992,7 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
973
992
|
}
|
|
974
993
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
975
994
|
const callbackManagers = await Promise.all(configList.map((config) => CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
|
|
976
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
995
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
977
996
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
978
997
|
let firstError;
|
|
979
998
|
for (const runnable of this.runnables()) {
|
|
@@ -17,7 +17,7 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
17
17
|
"config.credentials.sessionToken": "AWS_SESSION_TOKEN",
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }) {
|
|
20
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key = {}, }) {
|
|
21
21
|
super();
|
|
22
22
|
Object.defineProperty(this, "lc_namespace", {
|
|
23
23
|
enumerable: true,
|
|
@@ -65,7 +65,7 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
65
65
|
enumerable: true,
|
|
66
66
|
configurable: true,
|
|
67
67
|
writable: true,
|
|
68
|
-
value:
|
|
68
|
+
value: {}
|
|
69
69
|
});
|
|
70
70
|
this.tableName = tableName;
|
|
71
71
|
this.sessionId = sessionId;
|
|
@@ -74,10 +74,13 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
74
74
|
this.sortKey = sortKey;
|
|
75
75
|
this.messageAttributeName =
|
|
76
76
|
messageAttributeName ?? this.messageAttributeName;
|
|
77
|
-
this.dynamoKey =
|
|
78
|
-
|
|
79
|
-
if (this.
|
|
80
|
-
this.dynamoKey[this.
|
|
77
|
+
this.dynamoKey = key;
|
|
78
|
+
// override dynamoKey with partition key and sort key when key not specified
|
|
79
|
+
if (Object.keys(this.dynamoKey).length === 0) {
|
|
80
|
+
this.dynamoKey[this.partitionKey] = { S: this.sessionId };
|
|
81
|
+
if (this.sortKey) {
|
|
82
|
+
this.dynamoKey[this.sortKey] = { S: this.sortKey };
|
|
83
|
+
}
|
|
81
84
|
}
|
|
82
85
|
}
|
|
83
86
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
|
|
1
|
+
import { DynamoDBClientConfig, AttributeValue } from "@aws-sdk/client-dynamodb";
|
|
2
2
|
import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface defining the fields required to create an instance of
|
|
@@ -13,6 +13,7 @@ export interface DynamoDBChatMessageHistoryFields {
|
|
|
13
13
|
sortKey?: string;
|
|
14
14
|
messageAttributeName?: string;
|
|
15
15
|
config?: DynamoDBClientConfig;
|
|
16
|
+
key?: Record<string, AttributeValue>;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Class providing methods to interact with a DynamoDB table to store and
|
|
@@ -31,7 +32,7 @@ export declare class DynamoDBChatMessageHistory extends BaseListChatMessageHisto
|
|
|
31
32
|
private sortKey?;
|
|
32
33
|
private messageAttributeName;
|
|
33
34
|
private dynamoKey;
|
|
34
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }: DynamoDBChatMessageHistoryFields);
|
|
35
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key, }: DynamoDBChatMessageHistoryFields);
|
|
35
36
|
/**
|
|
36
37
|
* Retrieves all messages from the DynamoDB table and returns them as an
|
|
37
38
|
* array of `BaseMessage` instances.
|
|
@@ -14,7 +14,7 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
14
14
|
"config.credentials.sessionToken": "AWS_SESSION_TOKEN",
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }) {
|
|
17
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key = {}, }) {
|
|
18
18
|
super();
|
|
19
19
|
Object.defineProperty(this, "lc_namespace", {
|
|
20
20
|
enumerable: true,
|
|
@@ -62,7 +62,7 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
62
62
|
enumerable: true,
|
|
63
63
|
configurable: true,
|
|
64
64
|
writable: true,
|
|
65
|
-
value:
|
|
65
|
+
value: {}
|
|
66
66
|
});
|
|
67
67
|
this.tableName = tableName;
|
|
68
68
|
this.sessionId = sessionId;
|
|
@@ -71,10 +71,13 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
71
71
|
this.sortKey = sortKey;
|
|
72
72
|
this.messageAttributeName =
|
|
73
73
|
messageAttributeName ?? this.messageAttributeName;
|
|
74
|
-
this.dynamoKey =
|
|
75
|
-
|
|
76
|
-
if (this.
|
|
77
|
-
this.dynamoKey[this.
|
|
74
|
+
this.dynamoKey = key;
|
|
75
|
+
// override dynamoKey with partition key and sort key when key not specified
|
|
76
|
+
if (Object.keys(this.dynamoKey).length === 0) {
|
|
77
|
+
this.dynamoKey[this.partitionKey] = { S: this.sessionId };
|
|
78
|
+
if (this.sortKey) {
|
|
79
|
+
this.dynamoKey[this.sortKey] = { S: this.sortKey };
|
|
80
|
+
}
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
/**
|
package/dist/text_splitter.cjs
CHANGED
|
@@ -76,21 +76,30 @@ class TextSplitter extends document_js_2.BaseDocumentTransformer {
|
|
|
76
76
|
const text = texts[i];
|
|
77
77
|
let lineCounterIndex = 1;
|
|
78
78
|
let prevChunk = null;
|
|
79
|
+
let indexPrevChunk = -1;
|
|
79
80
|
for (const chunk of await this.splitText(text)) {
|
|
80
81
|
let pageContent = chunkHeader;
|
|
81
82
|
// we need to count the \n that are in the text before getting removed by the splitting
|
|
82
|
-
|
|
83
|
-
if (prevChunk) {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
const indexChunk = text.indexOf(chunk, indexPrevChunk + 1);
|
|
84
|
+
if (prevChunk === null) {
|
|
85
|
+
const newLinesBeforeFirstChunk = this.numberOfNewLines(text, 0, indexChunk);
|
|
86
|
+
lineCounterIndex += newLinesBeforeFirstChunk;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const indexEndPrevChunk = indexPrevChunk + (await this.lengthFunction(prevChunk));
|
|
90
|
+
if (indexEndPrevChunk < indexChunk) {
|
|
91
|
+
const numberOfIntermediateNewLines = this.numberOfNewLines(text, indexEndPrevChunk, indexChunk);
|
|
92
|
+
lineCounterIndex += numberOfIntermediateNewLines;
|
|
93
|
+
}
|
|
94
|
+
else if (indexEndPrevChunk > indexChunk) {
|
|
95
|
+
const numberOfIntermediateNewLines = this.numberOfNewLines(text, indexChunk, indexEndPrevChunk);
|
|
96
|
+
lineCounterIndex -= numberOfIntermediateNewLines;
|
|
97
|
+
}
|
|
88
98
|
if (appendChunkOverlapHeader) {
|
|
89
99
|
pageContent += chunkOverlapHeader;
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
|
-
|
|
93
|
-
const newLinesCount = (chunk.match(/\n/g) || []).length;
|
|
102
|
+
const newLinesCount = this.numberOfNewLines(chunk);
|
|
94
103
|
const loc = _metadatas[i].loc && typeof _metadatas[i].loc === "object"
|
|
95
104
|
? { ..._metadatas[i].loc }
|
|
96
105
|
: {};
|
|
@@ -109,10 +118,15 @@ class TextSplitter extends document_js_2.BaseDocumentTransformer {
|
|
|
109
118
|
}));
|
|
110
119
|
lineCounterIndex += newLinesCount;
|
|
111
120
|
prevChunk = chunk;
|
|
121
|
+
indexPrevChunk = indexChunk;
|
|
112
122
|
}
|
|
113
123
|
}
|
|
114
124
|
return documents;
|
|
115
125
|
}
|
|
126
|
+
numberOfNewLines(text, start, end) {
|
|
127
|
+
const textSection = text.slice(start, end);
|
|
128
|
+
return (textSection.match(/\n/g) || []).length;
|
|
129
|
+
}
|
|
116
130
|
async splitDocuments(documents, chunkHeaderOptions = {}) {
|
|
117
131
|
const selectedDocuments = documents.filter((doc) => doc.pageContent !== undefined);
|
|
118
132
|
const texts = selectedDocuments.map((doc) => doc.pageContent);
|
package/dist/text_splitter.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export declare abstract class TextSplitter extends BaseDocumentTransformer imple
|
|
|
23
23
|
abstract splitText(text: string): Promise<string[]>;
|
|
24
24
|
protected splitOnSeparator(text: string, separator: string): string[];
|
|
25
25
|
createDocuments(texts: string[], metadatas?: Record<string, any>[], chunkHeaderOptions?: TextSplitterChunkHeaderOptions): Promise<Document[]>;
|
|
26
|
+
private numberOfNewLines;
|
|
26
27
|
splitDocuments(documents: Document[], chunkHeaderOptions?: TextSplitterChunkHeaderOptions): Promise<Document[]>;
|
|
27
28
|
private joinDocs;
|
|
28
29
|
mergeSplits(splits: string[], separator: string): Promise<string[]>;
|