langchain 0.0.165 → 0.0.167
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/cache/momento.cjs +7 -7
- package/dist/cache/momento.d.ts +1 -1
- package/dist/cache/momento.js +1 -1
- package/dist/document_loaders/fs/unstructured.cjs +40 -0
- package/dist/document_loaders/fs/unstructured.d.ts +27 -0
- package/dist/document_loaders/fs/unstructured.js +40 -0
- package/dist/embeddings/bedrock.cjs +43 -22
- package/dist/embeddings/bedrock.d.ts +11 -4
- package/dist/embeddings/bedrock.js +43 -22
- package/dist/experimental/chat_models/anthropic_functions.cjs +34 -2
- package/dist/experimental/chat_models/anthropic_functions.d.ts +15 -5
- package/dist/experimental/chat_models/anthropic_functions.js +34 -2
- package/dist/llms/yandex.cjs +100 -0
- package/dist/llms/yandex.d.ts +40 -0
- package/dist/llms/yandex.js +96 -0
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/load/import_map.cjs +6 -2
- package/dist/load/import_map.d.ts +4 -0
- package/dist/load/import_map.js +4 -0
- package/dist/prompts/chat.cjs +19 -2
- package/dist/prompts/chat.d.ts +1 -0
- package/dist/prompts/chat.js +19 -2
- package/dist/retrievers/multi_vector.d.ts +3 -3
- package/dist/retrievers/parent_document.cjs +6 -16
- package/dist/retrievers/parent_document.d.ts +5 -12
- package/dist/retrievers/parent_document.js +6 -16
- package/dist/retrievers/self_query/base.cjs +4 -1
- package/dist/retrievers/self_query/base.d.ts +3 -2
- package/dist/retrievers/self_query/base.js +4 -1
- package/dist/retrievers/self_query/index.cjs +2 -2
- package/dist/retrievers/self_query/index.d.ts +2 -0
- package/dist/retrievers/self_query/index.js +2 -2
- package/dist/runnables/remote.cjs +225 -0
- package/dist/runnables/remote.d.ts +28 -0
- package/dist/runnables/remote.js +221 -0
- package/dist/schema/index.cjs +1 -1
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/index.js +1 -1
- package/dist/schema/runnable/base.cjs +4 -4
- package/dist/schema/runnable/base.d.ts +9 -7
- package/dist/schema/runnable/base.js +4 -4
- package/dist/schema/runnable/remote.cjs +225 -0
- package/dist/schema/runnable/remote.d.ts +28 -0
- package/dist/schema/runnable/remote.js +221 -0
- package/dist/schema/storage.d.ts +28 -1
- package/dist/storage/encoder_backed.cjs +14 -2
- package/dist/storage/encoder_backed.d.ts +2 -0
- package/dist/storage/encoder_backed.js +12 -1
- package/dist/storage/in_memory.cjs +1 -1
- package/dist/storage/in_memory.js +1 -1
- package/dist/storage/ioredis.cjs +4 -4
- package/dist/storage/ioredis.js +4 -4
- package/dist/storage/vercel_kv.cjs +146 -0
- package/dist/storage/vercel_kv.d.ts +46 -0
- package/dist/storage/vercel_kv.js +142 -0
- package/dist/stores/doc/in_memory.cjs +13 -0
- package/dist/stores/doc/in_memory.d.ts +6 -1
- package/dist/stores/doc/in_memory.js +13 -0
- package/dist/stores/message/momento.cjs +11 -11
- package/dist/stores/message/momento.d.ts +1 -1
- package/dist/stores/message/momento.js +1 -1
- package/dist/util/time.cjs +14 -0
- package/dist/util/time.d.ts +6 -0
- package/dist/util/time.js +10 -0
- package/dist/vectorstores/cassandra.cjs +4 -2
- package/dist/vectorstores/cassandra.js +4 -2
- package/dist/vectorstores/elasticsearch.cjs +3 -1
- package/dist/vectorstores/elasticsearch.js +3 -1
- package/dist/vectorstores/momento_vector_index.cjs +292 -0
- package/dist/vectorstores/momento_vector_index.d.ts +135 -0
- package/dist/vectorstores/momento_vector_index.js +265 -0
- package/dist/vectorstores/neo4j_vector.cjs +578 -0
- package/dist/vectorstores/neo4j_vector.d.ts +61 -0
- package/dist/vectorstores/neo4j_vector.js +548 -0
- package/dist/vectorstores/supabase.cjs +37 -8
- package/dist/vectorstores/supabase.d.ts +28 -1
- package/dist/vectorstores/supabase.js +37 -8
- package/llms/yandex.cjs +1 -0
- package/llms/yandex.d.ts +1 -0
- package/llms/yandex.js +1 -0
- package/package.json +73 -3
- package/runnables/remote.cjs +1 -0
- package/runnables/remote.d.ts +1 -0
- package/runnables/remote.js +1 -0
- package/storage/encoder_backed.cjs +1 -0
- package/storage/encoder_backed.d.ts +1 -0
- package/storage/encoder_backed.js +1 -0
- package/storage/vercel_kv.cjs +1 -0
- package/storage/vercel_kv.d.ts +1 -0
- package/storage/vercel_kv.js +1 -0
- package/util/time.cjs +1 -0
- package/util/time.d.ts +1 -0
- package/util/time.js +1 -0
- package/vectorstores/momento_vector_index.cjs +1 -0
- package/vectorstores/momento_vector_index.d.ts +1 -0
- package/vectorstores/momento_vector_index.js +1 -0
- package/vectorstores/neo4j_vector.cjs +1 -0
- package/vectorstores/neo4j_vector.d.ts +1 -0
- package/vectorstores/neo4j_vector.js +1 -0
|
@@ -26,9 +26,10 @@ export declare abstract class BaseTranslator<T extends VectorStore = VectorStore
|
|
|
26
26
|
* @param defaultFilter The default filter.
|
|
27
27
|
* @param generatedFilter The generated filter.
|
|
28
28
|
* @param mergeType The type of merge to perform. Can be 'and', 'or', or 'replace'.
|
|
29
|
+
* @param forceDefaultFilter If true, the default filter will be used even if the generated filter is not empty.
|
|
29
30
|
* @returns The merged filter, or undefined if both filters are empty.
|
|
30
31
|
*/
|
|
31
|
-
abstract mergeFilters(defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, mergeType?: "and" | "or" | "replace"): this["VisitStructuredQueryOutput"]["filter"] | undefined;
|
|
32
|
+
abstract mergeFilters(defaultFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, generatedFilter: this["VisitStructuredQueryOutput"]["filter"] | undefined, mergeType?: "and" | "or" | "replace", forceDefaultFilter?: boolean): this["VisitStructuredQueryOutput"]["filter"] | undefined;
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Class that extends the BaseTranslator class and provides concrete
|
|
@@ -64,5 +65,5 @@ export declare class BasicTranslator<T extends VectorStore = VectorStore> extend
|
|
|
64
65
|
* @returns The result of visiting the structured query.
|
|
65
66
|
*/
|
|
66
67
|
visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
|
|
67
|
-
mergeFilters(defaultFilter: VisitorStructuredQueryResult["filter"] | undefined, generatedFilter: VisitorStructuredQueryResult["filter"] | undefined, mergeType?: string): VisitorStructuredQueryResult["filter"] | undefined;
|
|
68
|
+
mergeFilters(defaultFilter: VisitorStructuredQueryResult["filter"] | undefined, generatedFilter: VisitorStructuredQueryResult["filter"] | undefined, mergeType?: string, forceDefaultFilter?: boolean): VisitorStructuredQueryResult["filter"] | undefined;
|
|
68
69
|
}
|
|
@@ -98,7 +98,7 @@ export class BasicTranslator extends BaseTranslator {
|
|
|
98
98
|
}
|
|
99
99
|
return nextArg;
|
|
100
100
|
}
|
|
101
|
-
mergeFilters(defaultFilter, generatedFilter, mergeType = "and") {
|
|
101
|
+
mergeFilters(defaultFilter, generatedFilter, mergeType = "and", forceDefaultFilter = false) {
|
|
102
102
|
if (isFilterEmpty(defaultFilter) && isFilterEmpty(generatedFilter)) {
|
|
103
103
|
return undefined;
|
|
104
104
|
}
|
|
@@ -109,6 +109,9 @@ export class BasicTranslator extends BaseTranslator {
|
|
|
109
109
|
return generatedFilter;
|
|
110
110
|
}
|
|
111
111
|
if (isFilterEmpty(generatedFilter)) {
|
|
112
|
+
if (forceDefaultFilter) {
|
|
113
|
+
return defaultFilter;
|
|
114
|
+
}
|
|
112
115
|
if (mergeType === "and") {
|
|
113
116
|
return undefined;
|
|
114
117
|
}
|
|
@@ -56,7 +56,7 @@ class SelfQueryRetriever extends retriever_js_1.BaseRetriever {
|
|
|
56
56
|
enumerable: true,
|
|
57
57
|
configurable: true,
|
|
58
58
|
writable: true,
|
|
59
|
-
value: { k: 4 }
|
|
59
|
+
value: { k: 4, forceDefaultFilter: false }
|
|
60
60
|
});
|
|
61
61
|
this.vectorStore = options.vectorStore;
|
|
62
62
|
this.llmChain = options.llmChain;
|
|
@@ -71,7 +71,7 @@ class SelfQueryRetriever extends retriever_js_1.BaseRetriever {
|
|
|
71
71
|
}, runManager?.getChild("llm_chain"));
|
|
72
72
|
const generatedStructuredQuery = output;
|
|
73
73
|
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(generatedStructuredQuery);
|
|
74
|
-
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator);
|
|
74
|
+
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator, this.searchParams?.forceDefaultFilter);
|
|
75
75
|
const generatedQuery = generatedStructuredQuery.query;
|
|
76
76
|
let myQuery = query;
|
|
77
77
|
if (!this.useOriginalQuery && generatedQuery && generatedQuery.length > 0) {
|
|
@@ -21,6 +21,7 @@ export interface SelfQueryRetrieverArgs<T extends VectorStore> extends BaseRetri
|
|
|
21
21
|
k?: number;
|
|
22
22
|
filter?: T["FilterType"];
|
|
23
23
|
mergeFiltersOperator?: "or" | "and" | "replace";
|
|
24
|
+
forceDefaultFilter?: boolean;
|
|
24
25
|
};
|
|
25
26
|
}
|
|
26
27
|
/**
|
|
@@ -40,6 +41,7 @@ export declare class SelfQueryRetriever<T extends VectorStore> extends BaseRetri
|
|
|
40
41
|
k?: number;
|
|
41
42
|
filter?: T["FilterType"];
|
|
42
43
|
mergeFiltersOperator?: "or" | "and" | "replace";
|
|
44
|
+
forceDefaultFilter?: boolean;
|
|
43
45
|
};
|
|
44
46
|
constructor(options: SelfQueryRetrieverArgs<T>);
|
|
45
47
|
_getRelevantDocuments(query: string, runManager?: CallbackManagerForRetrieverRun): Promise<Document<Record<string, unknown>>[]>;
|
|
@@ -51,7 +51,7 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
51
51
|
enumerable: true,
|
|
52
52
|
configurable: true,
|
|
53
53
|
writable: true,
|
|
54
|
-
value: { k: 4 }
|
|
54
|
+
value: { k: 4, forceDefaultFilter: false }
|
|
55
55
|
});
|
|
56
56
|
this.vectorStore = options.vectorStore;
|
|
57
57
|
this.llmChain = options.llmChain;
|
|
@@ -66,7 +66,7 @@ export class SelfQueryRetriever extends BaseRetriever {
|
|
|
66
66
|
}, runManager?.getChild("llm_chain"));
|
|
67
67
|
const generatedStructuredQuery = output;
|
|
68
68
|
const nextArg = this.structuredQueryTranslator.visitStructuredQuery(generatedStructuredQuery);
|
|
69
|
-
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator);
|
|
69
|
+
const filter = this.structuredQueryTranslator.mergeFilters(this.searchParams?.filter, nextArg.filter, this.searchParams?.mergeFiltersOperator, this.searchParams?.forceDefaultFilter);
|
|
70
70
|
const generatedQuery = generatedStructuredQuery.query;
|
|
71
71
|
let myQuery = query;
|
|
72
72
|
if (!this.useOriginalQuery && generatedQuery && generatedQuery.length > 0) {
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RemoteRunnable = void 0;
|
|
4
|
+
const index_js_1 = require("../schema/runnable/index.cjs");
|
|
5
|
+
const event_source_parse_js_1 = require("../util/event-source-parse.cjs");
|
|
6
|
+
const document_js_1 = require("../document.cjs");
|
|
7
|
+
const index_js_2 = require("../schema/index.cjs");
|
|
8
|
+
const base_js_1 = require("../prompts/base.cjs");
|
|
9
|
+
const chat_js_1 = require("../prompts/chat.cjs");
|
|
10
|
+
const stream_js_1 = require("../util/stream.cjs");
|
|
11
|
+
function isSuperset(set, subset) {
|
|
12
|
+
for (const elem of subset) {
|
|
13
|
+
if (!set.has(elem)) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
function revive(obj) {
|
|
21
|
+
if (Array.isArray(obj))
|
|
22
|
+
return obj.map(revive);
|
|
23
|
+
if (typeof obj === "object") {
|
|
24
|
+
const keysArr = Object.keys(obj);
|
|
25
|
+
const keys = new Set(keysArr);
|
|
26
|
+
if (isSuperset(keys, new Set(["page_content", "metadata"])))
|
|
27
|
+
return new document_js_1.Document({
|
|
28
|
+
pageContent: obj.page_content,
|
|
29
|
+
metadata: obj.metadata,
|
|
30
|
+
});
|
|
31
|
+
if (isSuperset(keys, new Set(["content", "type", "is_chunk"]))) {
|
|
32
|
+
if (!obj.is_chunk) {
|
|
33
|
+
if (obj.type === "human") {
|
|
34
|
+
return new index_js_2.HumanMessage({
|
|
35
|
+
content: obj.content,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (obj.type === "system") {
|
|
39
|
+
return new index_js_2.SystemMessage({
|
|
40
|
+
content: obj.content,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (obj.type === "chat") {
|
|
44
|
+
return new index_js_2.ChatMessage({
|
|
45
|
+
content: obj.content,
|
|
46
|
+
role: obj.role,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (obj.type === "function") {
|
|
50
|
+
return new index_js_2.FunctionMessage({
|
|
51
|
+
content: obj.content,
|
|
52
|
+
name: obj.name,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (obj.type === "ai") {
|
|
56
|
+
return new index_js_2.AIMessage({
|
|
57
|
+
content: obj.content,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
if (obj.type === "human") {
|
|
63
|
+
return new index_js_2.HumanMessageChunk({
|
|
64
|
+
content: obj.content,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (obj.type === "system") {
|
|
68
|
+
return new index_js_2.SystemMessageChunk({
|
|
69
|
+
content: obj.content,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (obj.type === "chat") {
|
|
73
|
+
return new index_js_2.ChatMessageChunk({
|
|
74
|
+
content: obj.content,
|
|
75
|
+
role: obj.role,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (obj.type === "function") {
|
|
79
|
+
return new index_js_2.FunctionMessageChunk({
|
|
80
|
+
content: obj.content,
|
|
81
|
+
name: obj.name,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (obj.type === "ai") {
|
|
85
|
+
return new index_js_2.AIMessageChunk({
|
|
86
|
+
content: obj.content,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (isSuperset(keys, new Set(["text"]))) {
|
|
92
|
+
return new base_js_1.StringPromptValue(obj.text);
|
|
93
|
+
}
|
|
94
|
+
if (isSuperset(keys, new Set(["messages"]))) {
|
|
95
|
+
return new chat_js_1.ChatPromptValue({
|
|
96
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
97
|
+
messages: obj.messages.map((msg) => revive(msg)),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
const innerRevive = (key) => [
|
|
102
|
+
key,
|
|
103
|
+
revive(obj[key]),
|
|
104
|
+
];
|
|
105
|
+
const rtn = Object.fromEntries(keysArr.map(innerRevive));
|
|
106
|
+
return rtn;
|
|
107
|
+
}
|
|
108
|
+
return obj;
|
|
109
|
+
}
|
|
110
|
+
function deserialize(str) {
|
|
111
|
+
const obj = JSON.parse(str);
|
|
112
|
+
return revive(obj);
|
|
113
|
+
}
|
|
114
|
+
function removeCallbacks(options) {
|
|
115
|
+
const rest = { ...options };
|
|
116
|
+
delete rest.callbacks;
|
|
117
|
+
return rest;
|
|
118
|
+
}
|
|
119
|
+
class RemoteRunnable extends index_js_1.Runnable {
|
|
120
|
+
constructor(fields) {
|
|
121
|
+
super(fields);
|
|
122
|
+
Object.defineProperty(this, "url", {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
configurable: true,
|
|
125
|
+
writable: true,
|
|
126
|
+
value: void 0
|
|
127
|
+
});
|
|
128
|
+
Object.defineProperty(this, "options", {
|
|
129
|
+
enumerable: true,
|
|
130
|
+
configurable: true,
|
|
131
|
+
writable: true,
|
|
132
|
+
value: void 0
|
|
133
|
+
});
|
|
134
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
135
|
+
enumerable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
writable: true,
|
|
138
|
+
value: ["langchain", "schema", "runnable", "remote"]
|
|
139
|
+
});
|
|
140
|
+
const { url, options } = fields;
|
|
141
|
+
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
142
|
+
this.options = options;
|
|
143
|
+
}
|
|
144
|
+
async post(path, body) {
|
|
145
|
+
return await fetch(`${this.url}${path}`, {
|
|
146
|
+
method: "POST",
|
|
147
|
+
body: JSON.stringify(body),
|
|
148
|
+
headers: {
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
},
|
|
151
|
+
signal: AbortSignal.timeout(this.options?.timeout ?? 5000),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
async invoke(input, options) {
|
|
155
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
156
|
+
const response = await this.post("/invoke", {
|
|
157
|
+
input,
|
|
158
|
+
config: removeCallbacks(config),
|
|
159
|
+
kwargs: kwargs ?? {},
|
|
160
|
+
});
|
|
161
|
+
return revive((await response.json()).output);
|
|
162
|
+
}
|
|
163
|
+
async _batch(inputs, options, _, batchOptions) {
|
|
164
|
+
if (batchOptions?.returnExceptions) {
|
|
165
|
+
throw new Error("returnExceptions is not supported for remote clients");
|
|
166
|
+
}
|
|
167
|
+
const configsAndKwargsArray = options?.map((opts) => this._separateRunnableConfigFromCallOptions(opts));
|
|
168
|
+
const [configs, kwargs] = configsAndKwargsArray?.reduce(([pc, pk], [c, k]) => [
|
|
169
|
+
[...pc, c],
|
|
170
|
+
[...pk, k],
|
|
171
|
+
], [[], []]) ?? [undefined, undefined];
|
|
172
|
+
const response = await this.post("/batch", {
|
|
173
|
+
inputs,
|
|
174
|
+
config: (configs ?? [])
|
|
175
|
+
.map(removeCallbacks)
|
|
176
|
+
.map((config) => ({ ...config, ...batchOptions })),
|
|
177
|
+
kwargs,
|
|
178
|
+
});
|
|
179
|
+
const body = await response.json();
|
|
180
|
+
if (!body.output)
|
|
181
|
+
throw new Error("Invalid response from remote runnable");
|
|
182
|
+
return revive(body.output);
|
|
183
|
+
}
|
|
184
|
+
async batch(inputs, options, batchOptions) {
|
|
185
|
+
if (batchOptions?.returnExceptions) {
|
|
186
|
+
throw Error("returnExceptions is not supported for remote clients");
|
|
187
|
+
}
|
|
188
|
+
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
189
|
+
}
|
|
190
|
+
async stream(input, options) {
|
|
191
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
192
|
+
const response = await this.post("/stream", {
|
|
193
|
+
input,
|
|
194
|
+
config,
|
|
195
|
+
kwargs,
|
|
196
|
+
});
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
const json = await response.json();
|
|
199
|
+
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
200
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
201
|
+
error.response = response;
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
const { body } = response;
|
|
205
|
+
if (!body) {
|
|
206
|
+
throw new Error("Could not begin remote stream. Please check the given URL and try again.");
|
|
207
|
+
}
|
|
208
|
+
const stream = new ReadableStream({
|
|
209
|
+
async start(controller) {
|
|
210
|
+
const enqueueLine = (0, event_source_parse_js_1.getMessages)((msg) => {
|
|
211
|
+
if (msg.data)
|
|
212
|
+
controller.enqueue(deserialize(msg.data));
|
|
213
|
+
});
|
|
214
|
+
const onLine = (line, fieldLength, flush) => {
|
|
215
|
+
enqueueLine(line, fieldLength, flush);
|
|
216
|
+
if (flush)
|
|
217
|
+
controller.close();
|
|
218
|
+
};
|
|
219
|
+
await (0, event_source_parse_js_1.getBytes)(body, (0, event_source_parse_js_1.getLines)(onLine));
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
return stream_js_1.IterableReadableStream.fromReadableStream(stream);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.RemoteRunnable = RemoteRunnable;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Runnable, RunnableBatchOptions } from "../schema/runnable/index.js";
|
|
2
|
+
import { RunnableConfig } from "../schema/runnable/config.js";
|
|
3
|
+
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
4
|
+
import { IterableReadableStream } from "../util/stream.js";
|
|
5
|
+
type RemoteRunnableOptions = {
|
|
6
|
+
timeout?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare class RemoteRunnable<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
9
|
+
private url;
|
|
10
|
+
private options?;
|
|
11
|
+
lc_namespace: string[];
|
|
12
|
+
constructor(fields: {
|
|
13
|
+
url: string;
|
|
14
|
+
options?: RemoteRunnableOptions;
|
|
15
|
+
});
|
|
16
|
+
private post;
|
|
17
|
+
invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
|
|
18
|
+
_batch(inputs: RunInput[], options?: Partial<CallOptions>[], _?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
19
|
+
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
|
|
20
|
+
returnExceptions?: false;
|
|
21
|
+
}): Promise<RunOutput[]>;
|
|
22
|
+
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
|
|
23
|
+
returnExceptions: true;
|
|
24
|
+
}): Promise<(RunOutput | Error)[]>;
|
|
25
|
+
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
26
|
+
stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Runnable } from "../schema/runnable/index.js";
|
|
2
|
+
import { getBytes, getLines, getMessages } from "../util/event-source-parse.js";
|
|
3
|
+
import { Document } from "../document.js";
|
|
4
|
+
import { AIMessage, AIMessageChunk, ChatMessage, ChatMessageChunk, FunctionMessage, FunctionMessageChunk, HumanMessage, HumanMessageChunk, SystemMessage, SystemMessageChunk, } from "../schema/index.js";
|
|
5
|
+
import { StringPromptValue } from "../prompts/base.js";
|
|
6
|
+
import { ChatPromptValue } from "../prompts/chat.js";
|
|
7
|
+
import { IterableReadableStream } from "../util/stream.js";
|
|
8
|
+
function isSuperset(set, subset) {
|
|
9
|
+
for (const elem of subset) {
|
|
10
|
+
if (!set.has(elem)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
function revive(obj) {
|
|
18
|
+
if (Array.isArray(obj))
|
|
19
|
+
return obj.map(revive);
|
|
20
|
+
if (typeof obj === "object") {
|
|
21
|
+
const keysArr = Object.keys(obj);
|
|
22
|
+
const keys = new Set(keysArr);
|
|
23
|
+
if (isSuperset(keys, new Set(["page_content", "metadata"])))
|
|
24
|
+
return new Document({
|
|
25
|
+
pageContent: obj.page_content,
|
|
26
|
+
metadata: obj.metadata,
|
|
27
|
+
});
|
|
28
|
+
if (isSuperset(keys, new Set(["content", "type", "is_chunk"]))) {
|
|
29
|
+
if (!obj.is_chunk) {
|
|
30
|
+
if (obj.type === "human") {
|
|
31
|
+
return new HumanMessage({
|
|
32
|
+
content: obj.content,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
if (obj.type === "system") {
|
|
36
|
+
return new SystemMessage({
|
|
37
|
+
content: obj.content,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (obj.type === "chat") {
|
|
41
|
+
return new ChatMessage({
|
|
42
|
+
content: obj.content,
|
|
43
|
+
role: obj.role,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (obj.type === "function") {
|
|
47
|
+
return new FunctionMessage({
|
|
48
|
+
content: obj.content,
|
|
49
|
+
name: obj.name,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (obj.type === "ai") {
|
|
53
|
+
return new AIMessage({
|
|
54
|
+
content: obj.content,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (obj.type === "human") {
|
|
60
|
+
return new HumanMessageChunk({
|
|
61
|
+
content: obj.content,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (obj.type === "system") {
|
|
65
|
+
return new SystemMessageChunk({
|
|
66
|
+
content: obj.content,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (obj.type === "chat") {
|
|
70
|
+
return new ChatMessageChunk({
|
|
71
|
+
content: obj.content,
|
|
72
|
+
role: obj.role,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (obj.type === "function") {
|
|
76
|
+
return new FunctionMessageChunk({
|
|
77
|
+
content: obj.content,
|
|
78
|
+
name: obj.name,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (obj.type === "ai") {
|
|
82
|
+
return new AIMessageChunk({
|
|
83
|
+
content: obj.content,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (isSuperset(keys, new Set(["text"]))) {
|
|
89
|
+
return new StringPromptValue(obj.text);
|
|
90
|
+
}
|
|
91
|
+
if (isSuperset(keys, new Set(["messages"]))) {
|
|
92
|
+
return new ChatPromptValue({
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
messages: obj.messages.map((msg) => revive(msg)),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
+
const innerRevive = (key) => [
|
|
99
|
+
key,
|
|
100
|
+
revive(obj[key]),
|
|
101
|
+
];
|
|
102
|
+
const rtn = Object.fromEntries(keysArr.map(innerRevive));
|
|
103
|
+
return rtn;
|
|
104
|
+
}
|
|
105
|
+
return obj;
|
|
106
|
+
}
|
|
107
|
+
function deserialize(str) {
|
|
108
|
+
const obj = JSON.parse(str);
|
|
109
|
+
return revive(obj);
|
|
110
|
+
}
|
|
111
|
+
function removeCallbacks(options) {
|
|
112
|
+
const rest = { ...options };
|
|
113
|
+
delete rest.callbacks;
|
|
114
|
+
return rest;
|
|
115
|
+
}
|
|
116
|
+
export class RemoteRunnable extends Runnable {
|
|
117
|
+
constructor(fields) {
|
|
118
|
+
super(fields);
|
|
119
|
+
Object.defineProperty(this, "url", {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
value: void 0
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(this, "options", {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: void 0
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: ["langchain", "schema", "runnable", "remote"]
|
|
136
|
+
});
|
|
137
|
+
const { url, options } = fields;
|
|
138
|
+
this.url = url.replace(/\/$/, ""); // remove trailing slash
|
|
139
|
+
this.options = options;
|
|
140
|
+
}
|
|
141
|
+
async post(path, body) {
|
|
142
|
+
return await fetch(`${this.url}${path}`, {
|
|
143
|
+
method: "POST",
|
|
144
|
+
body: JSON.stringify(body),
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
},
|
|
148
|
+
signal: AbortSignal.timeout(this.options?.timeout ?? 5000),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
async invoke(input, options) {
|
|
152
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
153
|
+
const response = await this.post("/invoke", {
|
|
154
|
+
input,
|
|
155
|
+
config: removeCallbacks(config),
|
|
156
|
+
kwargs: kwargs ?? {},
|
|
157
|
+
});
|
|
158
|
+
return revive((await response.json()).output);
|
|
159
|
+
}
|
|
160
|
+
async _batch(inputs, options, _, batchOptions) {
|
|
161
|
+
if (batchOptions?.returnExceptions) {
|
|
162
|
+
throw new Error("returnExceptions is not supported for remote clients");
|
|
163
|
+
}
|
|
164
|
+
const configsAndKwargsArray = options?.map((opts) => this._separateRunnableConfigFromCallOptions(opts));
|
|
165
|
+
const [configs, kwargs] = configsAndKwargsArray?.reduce(([pc, pk], [c, k]) => [
|
|
166
|
+
[...pc, c],
|
|
167
|
+
[...pk, k],
|
|
168
|
+
], [[], []]) ?? [undefined, undefined];
|
|
169
|
+
const response = await this.post("/batch", {
|
|
170
|
+
inputs,
|
|
171
|
+
config: (configs ?? [])
|
|
172
|
+
.map(removeCallbacks)
|
|
173
|
+
.map((config) => ({ ...config, ...batchOptions })),
|
|
174
|
+
kwargs,
|
|
175
|
+
});
|
|
176
|
+
const body = await response.json();
|
|
177
|
+
if (!body.output)
|
|
178
|
+
throw new Error("Invalid response from remote runnable");
|
|
179
|
+
return revive(body.output);
|
|
180
|
+
}
|
|
181
|
+
async batch(inputs, options, batchOptions) {
|
|
182
|
+
if (batchOptions?.returnExceptions) {
|
|
183
|
+
throw Error("returnExceptions is not supported for remote clients");
|
|
184
|
+
}
|
|
185
|
+
return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
|
|
186
|
+
}
|
|
187
|
+
async stream(input, options) {
|
|
188
|
+
const [config, kwargs] = this._separateRunnableConfigFromCallOptions(options);
|
|
189
|
+
const response = await this.post("/stream", {
|
|
190
|
+
input,
|
|
191
|
+
config,
|
|
192
|
+
kwargs,
|
|
193
|
+
});
|
|
194
|
+
if (!response.ok) {
|
|
195
|
+
const json = await response.json();
|
|
196
|
+
const error = new Error(`RemoteRunnable call failed with status code ${response.status}: ${json.message}`);
|
|
197
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
198
|
+
error.response = response;
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
const { body } = response;
|
|
202
|
+
if (!body) {
|
|
203
|
+
throw new Error("Could not begin remote stream. Please check the given URL and try again.");
|
|
204
|
+
}
|
|
205
|
+
const stream = new ReadableStream({
|
|
206
|
+
async start(controller) {
|
|
207
|
+
const enqueueLine = getMessages((msg) => {
|
|
208
|
+
if (msg.data)
|
|
209
|
+
controller.enqueue(deserialize(msg.data));
|
|
210
|
+
});
|
|
211
|
+
const onLine = (line, fieldLength, flush) => {
|
|
212
|
+
enqueueLine(line, fieldLength, flush);
|
|
213
|
+
if (flush)
|
|
214
|
+
controller.close();
|
|
215
|
+
};
|
|
216
|
+
await getBytes(body, getLines(onLine));
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
return IterableReadableStream.fromReadableStream(stream);
|
|
220
|
+
}
|
|
221
|
+
}
|
package/dist/schema/index.cjs
CHANGED
|
@@ -323,7 +323,7 @@ class ChatMessage extends BaseMessage {
|
|
|
323
323
|
}
|
|
324
324
|
exports.ChatMessage = ChatMessage;
|
|
325
325
|
function isBaseMessage(messageLike) {
|
|
326
|
-
return typeof messageLike
|
|
326
|
+
return typeof messageLike?._getType === "function";
|
|
327
327
|
}
|
|
328
328
|
exports.isBaseMessage = isBaseMessage;
|
|
329
329
|
function coerceMessageLikeToMessage(messageLike) {
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -218,7 +218,7 @@ export type BaseMessageLike = BaseMessage | [
|
|
|
218
218
|
MessageType | "user" | "assistant" | (string & Record<never, never>),
|
|
219
219
|
string
|
|
220
220
|
] | string;
|
|
221
|
-
export declare function isBaseMessage(messageLike
|
|
221
|
+
export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
|
|
222
222
|
export declare function coerceMessageLikeToMessage(messageLike: BaseMessageLike): BaseMessage;
|
|
223
223
|
/**
|
|
224
224
|
* Represents a chunk of a chat message, which can be concatenated with
|
package/dist/schema/index.js
CHANGED
|
@@ -308,7 +308,7 @@ export class ChatMessage extends BaseMessage {
|
|
|
308
308
|
}
|
|
309
309
|
}
|
|
310
310
|
export function isBaseMessage(messageLike) {
|
|
311
|
-
return typeof messageLike
|
|
311
|
+
return typeof messageLike?._getType === "function";
|
|
312
312
|
}
|
|
313
313
|
export function coerceMessageLikeToMessage(messageLike) {
|
|
314
314
|
if (typeof messageLike === "string") {
|
|
@@ -176,12 +176,12 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
176
176
|
* @returns A promise that resolves to the output values.
|
|
177
177
|
*/
|
|
178
178
|
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
179
|
-
const
|
|
180
|
-
const callbackManagers = await Promise.all(
|
|
181
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined,
|
|
179
|
+
const optionsList = this._getOptionsList(options ?? {}, inputs.length);
|
|
180
|
+
const callbackManagers = await Promise.all(optionsList.map(config_js_1.getCallbackMangerForConfig));
|
|
181
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, optionsList[i].runType, undefined, undefined, optionsList[i].runName)));
|
|
182
182
|
let outputs;
|
|
183
183
|
try {
|
|
184
|
-
outputs = await func(inputs,
|
|
184
|
+
outputs = await func(inputs, optionsList, runManagers, batchOptions);
|
|
185
185
|
}
|
|
186
186
|
catch (e) {
|
|
187
187
|
await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
|
|
@@ -12,9 +12,6 @@ export type RunnableBatchOptions = {
|
|
|
12
12
|
returnExceptions?: boolean;
|
|
13
13
|
};
|
|
14
14
|
export type RunnableRetryFailedAttemptHandler = (error: any) => any;
|
|
15
|
-
type RunnableConfigAndOptions = RunnableConfig & {
|
|
16
|
-
runType?: string;
|
|
17
|
-
};
|
|
18
15
|
/**
|
|
19
16
|
* A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
|
|
20
17
|
* transformed.
|
|
@@ -91,7 +88,9 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
91
88
|
*/
|
|
92
89
|
stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
|
|
93
90
|
protected _separateRunnableConfigFromCallOptions(options?: Partial<CallOptions>): [RunnableConfig, Omit<Partial<CallOptions>, keyof RunnableConfig>];
|
|
94
|
-
protected _callWithConfig<T extends RunInput>(func: ((input: T) => Promise<RunOutput>) | ((input: T, config?:
|
|
91
|
+
protected _callWithConfig<T extends RunInput>(func: ((input: T) => Promise<RunOutput>) | ((input: T, config?: Partial<CallOptions>, runManager?: CallbackManagerForChainRun) => Promise<RunOutput>), input: T, options?: Partial<CallOptions> & {
|
|
92
|
+
runType?: string;
|
|
93
|
+
}): Promise<RunOutput>;
|
|
95
94
|
/**
|
|
96
95
|
* Internal method that handles batching and configuration for a runnable
|
|
97
96
|
* It takes a function, input values, and optional configuration, and
|
|
@@ -101,13 +100,17 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
101
100
|
* @param config Optional configuration for the function execution.
|
|
102
101
|
* @returns A promise that resolves to the output values.
|
|
103
102
|
*/
|
|
104
|
-
_batchWithConfig<T extends RunInput>(func: (inputs: T[],
|
|
103
|
+
_batchWithConfig<T extends RunInput>(func: (inputs: T[], options?: Partial<CallOptions>[], runManagers?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions) => Promise<(RunOutput | Error)[]>, inputs: T[], options?: Partial<CallOptions & {
|
|
104
|
+
runType?: string;
|
|
105
|
+
}> | Partial<CallOptions & {
|
|
106
|
+
runType?: string;
|
|
107
|
+
}>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
105
108
|
/**
|
|
106
109
|
* Helper method to transform an Iterator of Input values into an Iterator of
|
|
107
110
|
* Output values, with callbacks.
|
|
108
111
|
* Use this to implement `stream()` or `transform()` in Runnable subclasses.
|
|
109
112
|
*/
|
|
110
|
-
protected _transformStreamWithConfig<I extends RunInput, O extends RunOutput>(inputGenerator: AsyncGenerator<I>, transformer: (generator: AsyncGenerator<I>, runManager?: CallbackManagerForChainRun, options?: Partial<
|
|
113
|
+
protected _transformStreamWithConfig<I extends RunInput, O extends RunOutput>(inputGenerator: AsyncGenerator<I>, transformer: (generator: AsyncGenerator<I>, runManager?: CallbackManagerForChainRun, options?: Partial<CallOptions>) => AsyncGenerator<O>, options?: CallOptions & {
|
|
111
114
|
runType?: string;
|
|
112
115
|
}): AsyncGenerator<O>;
|
|
113
116
|
_patchConfig(config?: Partial<CallOptions>, callbackManager?: CallbackManager | undefined): Partial<CallOptions>;
|
|
@@ -330,4 +333,3 @@ export declare class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable
|
|
|
330
333
|
batch(inputs: RunInput[], options?: Partial<BaseCallbackConfig> | Partial<BaseCallbackConfig>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
|
|
331
334
|
}
|
|
332
335
|
export declare function _coerceToRunnable<RunInput, RunOutput>(coerceable: RunnableLike<RunInput, RunOutput>): Runnable<RunInput, Exclude<RunOutput, Error>>;
|
|
333
|
-
export {};
|