langchain 0.0.52 → 0.0.53
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/agent.cjs +6 -0
- package/dist/agents/agent.d.ts +6 -0
- package/dist/agents/agent.js +6 -0
- package/dist/agents/agent_toolkits/zapier/zapier.cjs +1 -1
- package/dist/agents/agent_toolkits/zapier/zapier.d.ts +0 -1
- package/dist/agents/agent_toolkits/zapier/zapier.js +1 -1
- package/dist/agents/executor.cjs +1 -3
- package/dist/agents/executor.js +1 -3
- package/dist/agents/types.d.ts +0 -1
- package/dist/base_language/index.d.ts +2 -2
- package/dist/chains/combine_docs_chain.cjs +148 -2
- package/dist/chains/combine_docs_chain.d.ts +43 -2
- package/dist/chains/combine_docs_chain.js +146 -1
- package/dist/chains/index.cjs +3 -1
- package/dist/chains/index.d.ts +3 -3
- package/dist/chains/index.js +2 -2
- package/dist/chains/question_answering/load.cjs +23 -1
- package/dist/chains/question_answering/load.d.ts +9 -2
- package/dist/chains/question_answering/load.js +22 -1
- package/dist/chains/question_answering/refine_prompts.cjs +64 -0
- package/dist/chains/question_answering/refine_prompts.d.ts +10 -0
- package/dist/chains/question_answering/refine_prompts.js +61 -0
- package/dist/chains/serde.d.ts +7 -11
- package/dist/chat_models/base.cjs +2 -2
- package/dist/chat_models/base.d.ts +1 -1
- package/dist/chat_models/base.js +2 -2
- package/dist/document_loaders/web/playwright.cjs +60 -0
- package/dist/document_loaders/web/playwright.d.ts +27 -0
- package/dist/document_loaders/web/playwright.js +56 -0
- package/dist/document_loaders/web/s3.cjs +121 -0
- package/dist/document_loaders/web/s3.d.ts +20 -0
- package/dist/document_loaders/web/s3.js +94 -0
- package/dist/embeddings/base.d.ts +1 -1
- package/dist/output_parsers/combining.cjs +36 -0
- package/dist/output_parsers/combining.d.ts +12 -0
- package/dist/output_parsers/combining.js +32 -0
- package/dist/output_parsers/index.cjs +3 -1
- package/dist/output_parsers/index.d.ts +1 -0
- package/dist/output_parsers/index.js +1 -0
- package/dist/prompts/serde.d.ts +0 -4
- package/dist/retrievers/databerry.cjs +62 -0
- package/dist/retrievers/databerry.d.ts +17 -0
- package/dist/retrievers/databerry.js +58 -0
- package/dist/retrievers/remote/base.d.ts +1 -1
- package/dist/vectorstores/milvus.cjs +471 -0
- package/dist/vectorstores/milvus.d.ts +59 -0
- package/dist/vectorstores/milvus.js +467 -0
- package/dist/vectorstores/pinecone.cjs +16 -6
- package/dist/vectorstores/pinecone.js +16 -6
- package/document_loaders/web/playwright.cjs +1 -0
- package/document_loaders/web/playwright.d.ts +1 -0
- package/document_loaders/web/playwright.js +1 -0
- package/document_loaders/web/s3.cjs +1 -0
- package/document_loaders/web/s3.d.ts +1 -0
- package/document_loaders/web/s3.js +1 -0
- package/package.json +47 -1
- package/retrievers/databerry.cjs +1 -0
- package/retrievers/databerry.d.ts +1 -0
- package/retrievers/databerry.js +1 -0
- package/vectorstores/milvus.cjs +1 -0
- package/vectorstores/milvus.d.ts +1 -0
- package/vectorstores/milvus.js +1 -0
package/dist/agents/agent.cjs
CHANGED
|
@@ -47,9 +47,15 @@ class BaseAgent {
|
|
|
47
47
|
}
|
|
48
48
|
exports.BaseAgent = BaseAgent;
|
|
49
49
|
class BaseSingleActionAgent extends BaseAgent {
|
|
50
|
+
_agentActionType() {
|
|
51
|
+
return "single";
|
|
52
|
+
}
|
|
50
53
|
}
|
|
51
54
|
exports.BaseSingleActionAgent = BaseSingleActionAgent;
|
|
52
55
|
class BaseMultiActionAgent extends BaseAgent {
|
|
56
|
+
_agentActionType() {
|
|
57
|
+
return "multi";
|
|
58
|
+
}
|
|
53
59
|
}
|
|
54
60
|
exports.BaseMultiActionAgent = BaseMultiActionAgent;
|
|
55
61
|
class LLMSingleActionAgent extends BaseSingleActionAgent {
|
package/dist/agents/agent.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ export declare abstract class BaseAgent {
|
|
|
12
12
|
* Return the string type key uniquely identifying this class of agent.
|
|
13
13
|
*/
|
|
14
14
|
_agentType(): string;
|
|
15
|
+
/**
|
|
16
|
+
* Return the string type key uniquely identifying multi or single action agents.
|
|
17
|
+
*/
|
|
18
|
+
abstract _agentActionType(): string;
|
|
15
19
|
/**
|
|
16
20
|
* Return response when agent has been stopped due to max iterations
|
|
17
21
|
*/
|
|
@@ -22,6 +26,7 @@ export declare abstract class BaseAgent {
|
|
|
22
26
|
prepareForOutput(_returnValues: AgentFinish["returnValues"], _steps: AgentStep[]): Promise<AgentFinish["returnValues"]>;
|
|
23
27
|
}
|
|
24
28
|
export declare abstract class BaseSingleActionAgent extends BaseAgent {
|
|
29
|
+
_agentActionType(): string;
|
|
25
30
|
/**
|
|
26
31
|
* Decide what to do, given some input.
|
|
27
32
|
*
|
|
@@ -33,6 +38,7 @@ export declare abstract class BaseSingleActionAgent extends BaseAgent {
|
|
|
33
38
|
abstract plan(steps: AgentStep[], inputs: ChainValues): Promise<AgentAction | AgentFinish>;
|
|
34
39
|
}
|
|
35
40
|
export declare abstract class BaseMultiActionAgent extends BaseAgent {
|
|
41
|
+
_agentActionType(): string;
|
|
36
42
|
/**
|
|
37
43
|
* Decide what to do, given some input.
|
|
38
44
|
*
|
package/dist/agents/agent.js
CHANGED
|
@@ -43,8 +43,14 @@ export class BaseAgent {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
export class BaseSingleActionAgent extends BaseAgent {
|
|
46
|
+
_agentActionType() {
|
|
47
|
+
return "single";
|
|
48
|
+
}
|
|
46
49
|
}
|
|
47
50
|
export class BaseMultiActionAgent extends BaseAgent {
|
|
51
|
+
_agentActionType() {
|
|
52
|
+
return "multi";
|
|
53
|
+
}
|
|
48
54
|
}
|
|
49
55
|
export class LLMSingleActionAgent extends BaseSingleActionAgent {
|
|
50
56
|
constructor(input) {
|
|
@@ -5,7 +5,7 @@ const base_js_1 = require("../base.cjs");
|
|
|
5
5
|
const zapier_js_1 = require("../../../tools/zapier.cjs");
|
|
6
6
|
class ZapierToolKit extends base_js_1.Toolkit {
|
|
7
7
|
constructor() {
|
|
8
|
-
super();
|
|
8
|
+
super(...arguments);
|
|
9
9
|
Object.defineProperty(this, "tools", {
|
|
10
10
|
enumerable: true,
|
|
11
11
|
configurable: true,
|
|
@@ -3,6 +3,5 @@ import { Tool } from "../../../tools/base.js";
|
|
|
3
3
|
import { ZapierNLAWrapper } from "../../../tools/zapier.js";
|
|
4
4
|
export declare class ZapierToolKit extends Toolkit {
|
|
5
5
|
tools: Tool[];
|
|
6
|
-
protected constructor();
|
|
7
6
|
static fromZapierNLAWrapper(zapierNLAWrapper: ZapierNLAWrapper): Promise<ZapierToolKit>;
|
|
8
7
|
}
|
|
@@ -2,7 +2,7 @@ import { Toolkit } from "../base.js";
|
|
|
2
2
|
import { ZapierNLARunAction } from "../../../tools/zapier.js";
|
|
3
3
|
export class ZapierToolKit extends Toolkit {
|
|
4
4
|
constructor() {
|
|
5
|
-
super();
|
|
5
|
+
super(...arguments);
|
|
6
6
|
Object.defineProperty(this, "tools", {
|
|
7
7
|
enumerable: true,
|
|
8
8
|
configurable: true,
|
package/dist/agents/executor.cjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AgentExecutor = void 0;
|
|
4
4
|
const base_js_1 = require("../chains/base.cjs");
|
|
5
|
-
const agent_js_1 = require("./agent.cjs");
|
|
6
5
|
/**
|
|
7
6
|
* A chain managing an agent using tools.
|
|
8
7
|
* @augments BaseChain
|
|
@@ -45,8 +44,7 @@ class AgentExecutor extends base_js_1.BaseChain {
|
|
|
45
44
|
});
|
|
46
45
|
this.agent = input.agent;
|
|
47
46
|
this.tools = input.tools;
|
|
48
|
-
|
|
49
|
-
if (this.agent instanceof agent_js_1.BaseMultiActionAgent) {
|
|
47
|
+
if (this.agent._agentActionType() === "multi") {
|
|
50
48
|
for (const tool of this.tools) {
|
|
51
49
|
if (tool.returnDirect) {
|
|
52
50
|
throw new Error(`Tool with return direct ${tool.name} not supported for multi-action agent.`);
|
package/dist/agents/executor.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { BaseChain } from "../chains/base.js";
|
|
2
|
-
import { BaseMultiActionAgent } from "./agent.js";
|
|
3
2
|
/**
|
|
4
3
|
* A chain managing an agent using tools.
|
|
5
4
|
* @augments BaseChain
|
|
@@ -42,8 +41,7 @@ export class AgentExecutor extends BaseChain {
|
|
|
42
41
|
});
|
|
43
42
|
this.agent = input.agent;
|
|
44
43
|
this.tools = input.tools;
|
|
45
|
-
|
|
46
|
-
if (this.agent instanceof BaseMultiActionAgent) {
|
|
44
|
+
if (this.agent._agentActionType() === "multi") {
|
|
47
45
|
for (const tool of this.tools) {
|
|
48
46
|
if (tool.returnDirect) {
|
|
49
47
|
throw new Error(`Tool with return direct ${tool.name} not supported for multi-action agent.`);
|
package/dist/agents/types.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export type StoppingMethod = "force" | "generate";
|
|
|
12
12
|
export type SerializedAgentT<TType extends string = string, FromLLMInput extends Record<string, unknown> = Record<string, unknown>, ConstructorInput extends AgentInput = AgentInput> = {
|
|
13
13
|
_type: TType;
|
|
14
14
|
llm_chain?: SerializedLLMChain;
|
|
15
|
-
llm_chain_path?: string;
|
|
16
15
|
} & (({
|
|
17
16
|
load_from_llm_and_tools: true;
|
|
18
17
|
} & FromLLMInput) | ({
|
|
@@ -27,8 +27,8 @@ export declare abstract class BaseLanguageModel implements BaseLanguageModelPara
|
|
|
27
27
|
* The async caller should be used by subclasses to make any async calls,
|
|
28
28
|
* which will thus benefit from the concurrency and retry logic.
|
|
29
29
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
caller: AsyncCaller;
|
|
31
|
+
constructor(params: BaseLanguageModelParams);
|
|
32
32
|
abstract generatePrompt(promptValues: BasePromptValue[], stop?: string[]): Promise<LLMResult>;
|
|
33
33
|
abstract _modelType(): string;
|
|
34
34
|
abstract _llmType(): string;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MapReduceDocumentsChain = exports.StuffDocumentsChain = void 0;
|
|
3
|
+
exports.RefineDocumentsChain = exports.MapReduceDocumentsChain = exports.StuffDocumentsChain = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
5
|
const llm_chain_js_1 = require("./llm_chain.cjs");
|
|
6
|
+
const prompt_js_1 = require("../prompts/prompt.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* Chain that combines documents by stuffing into context.
|
|
8
9
|
* @augments BaseChain
|
|
@@ -77,7 +78,7 @@ class StuffDocumentsChain extends base_js_1.BaseChain {
|
|
|
77
78
|
}
|
|
78
79
|
exports.StuffDocumentsChain = StuffDocumentsChain;
|
|
79
80
|
/**
|
|
80
|
-
*
|
|
81
|
+
* Combine documents by mapping a chain over them, then combining results.
|
|
81
82
|
* @augments BaseChain
|
|
82
83
|
* @augments StuffDocumentsChainInput
|
|
83
84
|
*/
|
|
@@ -200,3 +201,148 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
|
|
|
200
201
|
}
|
|
201
202
|
}
|
|
202
203
|
exports.MapReduceDocumentsChain = MapReduceDocumentsChain;
|
|
204
|
+
/**
|
|
205
|
+
* Combine documents by doing a first pass and then refining on more documents.
|
|
206
|
+
* @augments BaseChain
|
|
207
|
+
* @augments RefineDocumentsChainInput
|
|
208
|
+
*/
|
|
209
|
+
class RefineDocumentsChain extends base_js_1.BaseChain {
|
|
210
|
+
get defaultDocumentPrompt() {
|
|
211
|
+
return new prompt_js_1.PromptTemplate({
|
|
212
|
+
inputVariables: ["page_content"],
|
|
213
|
+
template: "{page_content}",
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
get inputKeys() {
|
|
217
|
+
return [this.inputKey, ...this.refineLLMChain.inputKeys];
|
|
218
|
+
}
|
|
219
|
+
constructor(fields) {
|
|
220
|
+
super();
|
|
221
|
+
Object.defineProperty(this, "llmChain", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: void 0
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(this, "inputKey", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
configurable: true,
|
|
230
|
+
writable: true,
|
|
231
|
+
value: "input_documents"
|
|
232
|
+
});
|
|
233
|
+
Object.defineProperty(this, "outputKey", {
|
|
234
|
+
enumerable: true,
|
|
235
|
+
configurable: true,
|
|
236
|
+
writable: true,
|
|
237
|
+
value: "output_text"
|
|
238
|
+
});
|
|
239
|
+
Object.defineProperty(this, "documentVariableName", {
|
|
240
|
+
enumerable: true,
|
|
241
|
+
configurable: true,
|
|
242
|
+
writable: true,
|
|
243
|
+
value: "context"
|
|
244
|
+
});
|
|
245
|
+
Object.defineProperty(this, "initialResponseName", {
|
|
246
|
+
enumerable: true,
|
|
247
|
+
configurable: true,
|
|
248
|
+
writable: true,
|
|
249
|
+
value: "existing_answer"
|
|
250
|
+
});
|
|
251
|
+
Object.defineProperty(this, "refineLLMChain", {
|
|
252
|
+
enumerable: true,
|
|
253
|
+
configurable: true,
|
|
254
|
+
writable: true,
|
|
255
|
+
value: void 0
|
|
256
|
+
});
|
|
257
|
+
Object.defineProperty(this, "documentPrompt", {
|
|
258
|
+
enumerable: true,
|
|
259
|
+
configurable: true,
|
|
260
|
+
writable: true,
|
|
261
|
+
value: this.defaultDocumentPrompt
|
|
262
|
+
});
|
|
263
|
+
this.llmChain = fields.llmChain;
|
|
264
|
+
this.refineLLMChain = fields.refineLLMChain;
|
|
265
|
+
this.documentVariableName =
|
|
266
|
+
fields.documentVariableName ?? this.documentVariableName;
|
|
267
|
+
this.inputKey = fields.inputKey ?? this.inputKey;
|
|
268
|
+
this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;
|
|
269
|
+
this.initialResponseName =
|
|
270
|
+
fields.initialResponseName ?? this.initialResponseName;
|
|
271
|
+
}
|
|
272
|
+
_constructInitialInputs(doc, rest) {
|
|
273
|
+
const baseInfo = {
|
|
274
|
+
page_content: doc.pageContent,
|
|
275
|
+
...doc.metadata,
|
|
276
|
+
};
|
|
277
|
+
const documentInfo = {};
|
|
278
|
+
this.documentPrompt.inputVariables.forEach((value) => {
|
|
279
|
+
documentInfo[value] = baseInfo[value];
|
|
280
|
+
});
|
|
281
|
+
const baseInputs = {
|
|
282
|
+
[this.documentVariableName]: this.documentPrompt.format({
|
|
283
|
+
...documentInfo,
|
|
284
|
+
}),
|
|
285
|
+
};
|
|
286
|
+
const inputs = { ...baseInputs, ...rest };
|
|
287
|
+
return inputs;
|
|
288
|
+
}
|
|
289
|
+
_constructRefineInputs(doc, res) {
|
|
290
|
+
const baseInfo = {
|
|
291
|
+
page_content: doc.pageContent,
|
|
292
|
+
...doc.metadata,
|
|
293
|
+
};
|
|
294
|
+
const documentInfo = {};
|
|
295
|
+
this.documentPrompt.inputVariables.forEach((value) => {
|
|
296
|
+
documentInfo[value] = baseInfo[value];
|
|
297
|
+
});
|
|
298
|
+
const baseInputs = {
|
|
299
|
+
[this.documentVariableName]: this.documentPrompt.format({
|
|
300
|
+
...documentInfo,
|
|
301
|
+
}),
|
|
302
|
+
};
|
|
303
|
+
const inputs = { [this.initialResponseName]: res, ...baseInputs };
|
|
304
|
+
return inputs;
|
|
305
|
+
}
|
|
306
|
+
async _call(values) {
|
|
307
|
+
if (!(this.inputKey in values)) {
|
|
308
|
+
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
309
|
+
}
|
|
310
|
+
const { [this.inputKey]: docs, ...rest } = values;
|
|
311
|
+
const currentDocs = docs;
|
|
312
|
+
const initialInputs = this._constructInitialInputs(currentDocs[0], rest);
|
|
313
|
+
let res = await this.llmChain.predict({ ...initialInputs });
|
|
314
|
+
const refineSteps = [res];
|
|
315
|
+
for (let i = 1; i < currentDocs.length; i += 1) {
|
|
316
|
+
const refineInputs = this._constructRefineInputs(currentDocs[i], res);
|
|
317
|
+
const inputs = { ...refineInputs, ...rest };
|
|
318
|
+
res = await this.refineLLMChain.predict({ ...inputs });
|
|
319
|
+
refineSteps.push(res);
|
|
320
|
+
}
|
|
321
|
+
return { [this.outputKey]: res };
|
|
322
|
+
}
|
|
323
|
+
_chainType() {
|
|
324
|
+
return "refine_documents_chain";
|
|
325
|
+
}
|
|
326
|
+
static async deserialize(data) {
|
|
327
|
+
const SerializedLLMChain = data.llm_chain;
|
|
328
|
+
if (!SerializedLLMChain) {
|
|
329
|
+
throw new Error("Missing llm_chain");
|
|
330
|
+
}
|
|
331
|
+
const SerializedRefineDocumentChain = data.refine_llm_chain;
|
|
332
|
+
if (!SerializedRefineDocumentChain) {
|
|
333
|
+
throw new Error("Missing refine_llm_chain");
|
|
334
|
+
}
|
|
335
|
+
return new RefineDocumentsChain({
|
|
336
|
+
llmChain: await llm_chain_js_1.LLMChain.deserialize(SerializedLLMChain),
|
|
337
|
+
refineLLMChain: await llm_chain_js_1.LLMChain.deserialize(SerializedRefineDocumentChain),
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
serialize() {
|
|
341
|
+
return {
|
|
342
|
+
_type: this._chainType(),
|
|
343
|
+
llm_chain: this.llmChain.serialize(),
|
|
344
|
+
refine_llm_chain: this.refineLLMChain.serialize(),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
exports.RefineDocumentsChain = RefineDocumentsChain;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { SerializedStuffDocumentsChain, SerializedMapReduceDocumentsChain } from "./serde.js";
|
|
1
|
+
import type { SerializedStuffDocumentsChain, SerializedMapReduceDocumentsChain, SerializedRefineDocumentsChain } from "./serde.js";
|
|
2
2
|
import { BaseChain } from "./base.js";
|
|
3
3
|
import { LLMChain } from "./llm_chain.js";
|
|
4
|
+
import { Document } from "../document.js";
|
|
4
5
|
import { ChainValues } from "../schema/index.js";
|
|
6
|
+
import { BasePromptTemplate } from "../prompts/base.js";
|
|
5
7
|
export interface StuffDocumentsChainInput {
|
|
6
8
|
/** LLM Wrapper to use after formatting documents */
|
|
7
9
|
llmChain: LLMChain;
|
|
@@ -38,7 +40,7 @@ export interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {
|
|
|
38
40
|
combineDocumentsChain: BaseChain;
|
|
39
41
|
}
|
|
40
42
|
/**
|
|
41
|
-
*
|
|
43
|
+
* Combine documents by mapping a chain over them, then combining results.
|
|
42
44
|
* @augments BaseChain
|
|
43
45
|
* @augments StuffDocumentsChainInput
|
|
44
46
|
*/
|
|
@@ -67,3 +69,42 @@ export declare class MapReduceDocumentsChain extends BaseChain implements StuffD
|
|
|
67
69
|
static deserialize(data: SerializedMapReduceDocumentsChain): Promise<MapReduceDocumentsChain>;
|
|
68
70
|
serialize(): SerializedMapReduceDocumentsChain;
|
|
69
71
|
}
|
|
72
|
+
export interface RefineDocumentsChainInput extends StuffDocumentsChainInput {
|
|
73
|
+
refineLLMChain: LLMChain;
|
|
74
|
+
documentPrompt: BasePromptTemplate;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Combine documents by doing a first pass and then refining on more documents.
|
|
78
|
+
* @augments BaseChain
|
|
79
|
+
* @augments RefineDocumentsChainInput
|
|
80
|
+
*/
|
|
81
|
+
export declare class RefineDocumentsChain extends BaseChain implements RefineDocumentsChainInput {
|
|
82
|
+
llmChain: LLMChain;
|
|
83
|
+
inputKey: string;
|
|
84
|
+
outputKey: string;
|
|
85
|
+
documentVariableName: string;
|
|
86
|
+
initialResponseName: string;
|
|
87
|
+
refineLLMChain: LLMChain;
|
|
88
|
+
get defaultDocumentPrompt(): BasePromptTemplate;
|
|
89
|
+
documentPrompt: BasePromptTemplate;
|
|
90
|
+
get inputKeys(): string[];
|
|
91
|
+
constructor(fields: {
|
|
92
|
+
llmChain: LLMChain;
|
|
93
|
+
refineLLMChain: LLMChain;
|
|
94
|
+
inputKey?: string;
|
|
95
|
+
outputKey?: string;
|
|
96
|
+
documentVariableName?: string;
|
|
97
|
+
documentPrompt?: BasePromptTemplate;
|
|
98
|
+
initialResponseName?: string;
|
|
99
|
+
});
|
|
100
|
+
_constructInitialInputs(doc: Document, rest: Record<string, unknown>): {
|
|
101
|
+
[x: string]: unknown;
|
|
102
|
+
};
|
|
103
|
+
_constructRefineInputs(doc: Document, res: string): {
|
|
104
|
+
[x: string]: unknown;
|
|
105
|
+
};
|
|
106
|
+
_call(values: ChainValues): Promise<ChainValues>;
|
|
107
|
+
_chainType(): "refine_documents_chain";
|
|
108
|
+
static deserialize(data: SerializedRefineDocumentsChain): Promise<RefineDocumentsChain>;
|
|
109
|
+
serialize(): SerializedRefineDocumentsChain;
|
|
110
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseChain } from "./base.js";
|
|
2
2
|
import { LLMChain } from "./llm_chain.js";
|
|
3
|
+
import { PromptTemplate } from "../prompts/prompt.js";
|
|
3
4
|
/**
|
|
4
5
|
* Chain that combines documents by stuffing into context.
|
|
5
6
|
* @augments BaseChain
|
|
@@ -73,7 +74,7 @@ export class StuffDocumentsChain extends BaseChain {
|
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
/**
|
|
76
|
-
*
|
|
77
|
+
* Combine documents by mapping a chain over them, then combining results.
|
|
77
78
|
* @augments BaseChain
|
|
78
79
|
* @augments StuffDocumentsChainInput
|
|
79
80
|
*/
|
|
@@ -195,3 +196,147 @@ export class MapReduceDocumentsChain extends BaseChain {
|
|
|
195
196
|
};
|
|
196
197
|
}
|
|
197
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Combine documents by doing a first pass and then refining on more documents.
|
|
201
|
+
* @augments BaseChain
|
|
202
|
+
* @augments RefineDocumentsChainInput
|
|
203
|
+
*/
|
|
204
|
+
export class RefineDocumentsChain extends BaseChain {
|
|
205
|
+
get defaultDocumentPrompt() {
|
|
206
|
+
return new PromptTemplate({
|
|
207
|
+
inputVariables: ["page_content"],
|
|
208
|
+
template: "{page_content}",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
get inputKeys() {
|
|
212
|
+
return [this.inputKey, ...this.refineLLMChain.inputKeys];
|
|
213
|
+
}
|
|
214
|
+
constructor(fields) {
|
|
215
|
+
super();
|
|
216
|
+
Object.defineProperty(this, "llmChain", {
|
|
217
|
+
enumerable: true,
|
|
218
|
+
configurable: true,
|
|
219
|
+
writable: true,
|
|
220
|
+
value: void 0
|
|
221
|
+
});
|
|
222
|
+
Object.defineProperty(this, "inputKey", {
|
|
223
|
+
enumerable: true,
|
|
224
|
+
configurable: true,
|
|
225
|
+
writable: true,
|
|
226
|
+
value: "input_documents"
|
|
227
|
+
});
|
|
228
|
+
Object.defineProperty(this, "outputKey", {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
configurable: true,
|
|
231
|
+
writable: true,
|
|
232
|
+
value: "output_text"
|
|
233
|
+
});
|
|
234
|
+
Object.defineProperty(this, "documentVariableName", {
|
|
235
|
+
enumerable: true,
|
|
236
|
+
configurable: true,
|
|
237
|
+
writable: true,
|
|
238
|
+
value: "context"
|
|
239
|
+
});
|
|
240
|
+
Object.defineProperty(this, "initialResponseName", {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
configurable: true,
|
|
243
|
+
writable: true,
|
|
244
|
+
value: "existing_answer"
|
|
245
|
+
});
|
|
246
|
+
Object.defineProperty(this, "refineLLMChain", {
|
|
247
|
+
enumerable: true,
|
|
248
|
+
configurable: true,
|
|
249
|
+
writable: true,
|
|
250
|
+
value: void 0
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(this, "documentPrompt", {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
configurable: true,
|
|
255
|
+
writable: true,
|
|
256
|
+
value: this.defaultDocumentPrompt
|
|
257
|
+
});
|
|
258
|
+
this.llmChain = fields.llmChain;
|
|
259
|
+
this.refineLLMChain = fields.refineLLMChain;
|
|
260
|
+
this.documentVariableName =
|
|
261
|
+
fields.documentVariableName ?? this.documentVariableName;
|
|
262
|
+
this.inputKey = fields.inputKey ?? this.inputKey;
|
|
263
|
+
this.documentPrompt = fields.documentPrompt ?? this.documentPrompt;
|
|
264
|
+
this.initialResponseName =
|
|
265
|
+
fields.initialResponseName ?? this.initialResponseName;
|
|
266
|
+
}
|
|
267
|
+
_constructInitialInputs(doc, rest) {
|
|
268
|
+
const baseInfo = {
|
|
269
|
+
page_content: doc.pageContent,
|
|
270
|
+
...doc.metadata,
|
|
271
|
+
};
|
|
272
|
+
const documentInfo = {};
|
|
273
|
+
this.documentPrompt.inputVariables.forEach((value) => {
|
|
274
|
+
documentInfo[value] = baseInfo[value];
|
|
275
|
+
});
|
|
276
|
+
const baseInputs = {
|
|
277
|
+
[this.documentVariableName]: this.documentPrompt.format({
|
|
278
|
+
...documentInfo,
|
|
279
|
+
}),
|
|
280
|
+
};
|
|
281
|
+
const inputs = { ...baseInputs, ...rest };
|
|
282
|
+
return inputs;
|
|
283
|
+
}
|
|
284
|
+
_constructRefineInputs(doc, res) {
|
|
285
|
+
const baseInfo = {
|
|
286
|
+
page_content: doc.pageContent,
|
|
287
|
+
...doc.metadata,
|
|
288
|
+
};
|
|
289
|
+
const documentInfo = {};
|
|
290
|
+
this.documentPrompt.inputVariables.forEach((value) => {
|
|
291
|
+
documentInfo[value] = baseInfo[value];
|
|
292
|
+
});
|
|
293
|
+
const baseInputs = {
|
|
294
|
+
[this.documentVariableName]: this.documentPrompt.format({
|
|
295
|
+
...documentInfo,
|
|
296
|
+
}),
|
|
297
|
+
};
|
|
298
|
+
const inputs = { [this.initialResponseName]: res, ...baseInputs };
|
|
299
|
+
return inputs;
|
|
300
|
+
}
|
|
301
|
+
async _call(values) {
|
|
302
|
+
if (!(this.inputKey in values)) {
|
|
303
|
+
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
304
|
+
}
|
|
305
|
+
const { [this.inputKey]: docs, ...rest } = values;
|
|
306
|
+
const currentDocs = docs;
|
|
307
|
+
const initialInputs = this._constructInitialInputs(currentDocs[0], rest);
|
|
308
|
+
let res = await this.llmChain.predict({ ...initialInputs });
|
|
309
|
+
const refineSteps = [res];
|
|
310
|
+
for (let i = 1; i < currentDocs.length; i += 1) {
|
|
311
|
+
const refineInputs = this._constructRefineInputs(currentDocs[i], res);
|
|
312
|
+
const inputs = { ...refineInputs, ...rest };
|
|
313
|
+
res = await this.refineLLMChain.predict({ ...inputs });
|
|
314
|
+
refineSteps.push(res);
|
|
315
|
+
}
|
|
316
|
+
return { [this.outputKey]: res };
|
|
317
|
+
}
|
|
318
|
+
_chainType() {
|
|
319
|
+
return "refine_documents_chain";
|
|
320
|
+
}
|
|
321
|
+
static async deserialize(data) {
|
|
322
|
+
const SerializedLLMChain = data.llm_chain;
|
|
323
|
+
if (!SerializedLLMChain) {
|
|
324
|
+
throw new Error("Missing llm_chain");
|
|
325
|
+
}
|
|
326
|
+
const SerializedRefineDocumentChain = data.refine_llm_chain;
|
|
327
|
+
if (!SerializedRefineDocumentChain) {
|
|
328
|
+
throw new Error("Missing refine_llm_chain");
|
|
329
|
+
}
|
|
330
|
+
return new RefineDocumentsChain({
|
|
331
|
+
llmChain: await LLMChain.deserialize(SerializedLLMChain),
|
|
332
|
+
refineLLMChain: await LLMChain.deserialize(SerializedRefineDocumentChain),
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
serialize() {
|
|
336
|
+
return {
|
|
337
|
+
_type: this._chainType(),
|
|
338
|
+
llm_chain: this.llmChain.serialize(),
|
|
339
|
+
refine_llm_chain: this.refineLLMChain.serialize(),
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
}
|
package/dist/chains/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RetrievalQAChain = exports.ConversationalRetrievalQAChain = exports.SqlDatabaseChain = exports.loadSummarizationChain = exports.loadQAMapReduceChain = exports.loadQAStuffChain = exports.loadQAChain = exports.VectorDBQAChain = exports.AnalyzeDocumentChain = exports.ChatVectorDBQAChain = exports.MapReduceDocumentsChain = exports.StuffDocumentsChain = exports.ConversationChain = exports.LLMChain = exports.BaseChain = void 0;
|
|
3
|
+
exports.RetrievalQAChain = exports.ConversationalRetrievalQAChain = exports.SqlDatabaseChain = exports.loadSummarizationChain = exports.loadQARefineChain = exports.loadQAMapReduceChain = exports.loadQAStuffChain = exports.loadQAChain = exports.VectorDBQAChain = exports.AnalyzeDocumentChain = exports.ChatVectorDBQAChain = exports.RefineDocumentsChain = exports.MapReduceDocumentsChain = exports.StuffDocumentsChain = exports.ConversationChain = exports.LLMChain = exports.BaseChain = void 0;
|
|
4
4
|
var base_js_1 = require("./base.cjs");
|
|
5
5
|
Object.defineProperty(exports, "BaseChain", { enumerable: true, get: function () { return base_js_1.BaseChain; } });
|
|
6
6
|
var llm_chain_js_1 = require("./llm_chain.cjs");
|
|
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "ConversationChain", { enumerable: true, get: fun
|
|
|
9
9
|
var combine_docs_chain_js_1 = require("./combine_docs_chain.cjs");
|
|
10
10
|
Object.defineProperty(exports, "StuffDocumentsChain", { enumerable: true, get: function () { return combine_docs_chain_js_1.StuffDocumentsChain; } });
|
|
11
11
|
Object.defineProperty(exports, "MapReduceDocumentsChain", { enumerable: true, get: function () { return combine_docs_chain_js_1.MapReduceDocumentsChain; } });
|
|
12
|
+
Object.defineProperty(exports, "RefineDocumentsChain", { enumerable: true, get: function () { return combine_docs_chain_js_1.RefineDocumentsChain; } });
|
|
12
13
|
var chat_vector_db_chain_js_1 = require("./chat_vector_db_chain.cjs");
|
|
13
14
|
Object.defineProperty(exports, "ChatVectorDBQAChain", { enumerable: true, get: function () { return chat_vector_db_chain_js_1.ChatVectorDBQAChain; } });
|
|
14
15
|
var analyze_documents_chain_js_1 = require("./analyze_documents_chain.cjs");
|
|
@@ -19,6 +20,7 @@ var load_js_1 = require("./question_answering/load.cjs");
|
|
|
19
20
|
Object.defineProperty(exports, "loadQAChain", { enumerable: true, get: function () { return load_js_1.loadQAChain; } });
|
|
20
21
|
Object.defineProperty(exports, "loadQAStuffChain", { enumerable: true, get: function () { return load_js_1.loadQAStuffChain; } });
|
|
21
22
|
Object.defineProperty(exports, "loadQAMapReduceChain", { enumerable: true, get: function () { return load_js_1.loadQAMapReduceChain; } });
|
|
23
|
+
Object.defineProperty(exports, "loadQARefineChain", { enumerable: true, get: function () { return load_js_1.loadQARefineChain; } });
|
|
22
24
|
var load_js_2 = require("./summarization/load.cjs");
|
|
23
25
|
Object.defineProperty(exports, "loadSummarizationChain", { enumerable: true, get: function () { return load_js_2.loadSummarizationChain; } });
|
|
24
26
|
var sql_db_chain_js_1 = require("./sql_db/sql_db_chain.cjs");
|
package/dist/chains/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export { BaseChain, ChainInputs } from "./base.js";
|
|
2
2
|
export { LLMChain, ConversationChain } from "./llm_chain.js";
|
|
3
|
-
export { StuffDocumentsChain, MapReduceDocumentsChain, } from "./combine_docs_chain.js";
|
|
3
|
+
export { StuffDocumentsChain, MapReduceDocumentsChain, RefineDocumentsChain, } from "./combine_docs_chain.js";
|
|
4
4
|
export { ChatVectorDBQAChain } from "./chat_vector_db_chain.js";
|
|
5
5
|
export { AnalyzeDocumentChain } from "./analyze_documents_chain.js";
|
|
6
6
|
export { VectorDBQAChain } from "./vector_db_qa.js";
|
|
7
|
-
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, } from "./question_answering/load.js";
|
|
7
|
+
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, loadQARefineChain, } from "./question_answering/load.js";
|
|
8
8
|
export { loadSummarizationChain } from "./summarization/load.js";
|
|
9
9
|
export { SqlDatabaseChain } from "./sql_db/sql_db_chain.js";
|
|
10
10
|
export { ConversationalRetrievalQAChain } from "./conversational_retrieval_chain.js";
|
|
11
11
|
export { RetrievalQAChain } from "./retrieval_qa.js";
|
|
12
|
-
export { SerializedLLMChain, SerializedSqlDatabaseChain, SerializedAnalyzeDocumentChain, SerializedBaseChain, SerializedChatVectorDBQAChain, SerializedMapReduceDocumentsChain, SerializedStuffDocumentsChain, SerializedVectorDBQAChain, } from "./serde.js";
|
|
12
|
+
export { SerializedLLMChain, SerializedSqlDatabaseChain, SerializedAnalyzeDocumentChain, SerializedBaseChain, SerializedChatVectorDBQAChain, SerializedMapReduceDocumentsChain, SerializedStuffDocumentsChain, SerializedVectorDBQAChain, SerializedRefineDocumentsChain, } from "./serde.js";
|
package/dist/chains/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export { BaseChain } from "./base.js";
|
|
2
2
|
export { LLMChain, ConversationChain } from "./llm_chain.js";
|
|
3
|
-
export { StuffDocumentsChain, MapReduceDocumentsChain, } from "./combine_docs_chain.js";
|
|
3
|
+
export { StuffDocumentsChain, MapReduceDocumentsChain, RefineDocumentsChain, } from "./combine_docs_chain.js";
|
|
4
4
|
export { ChatVectorDBQAChain } from "./chat_vector_db_chain.js";
|
|
5
5
|
export { AnalyzeDocumentChain } from "./analyze_documents_chain.js";
|
|
6
6
|
export { VectorDBQAChain } from "./vector_db_qa.js";
|
|
7
|
-
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, } from "./question_answering/load.js";
|
|
7
|
+
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, loadQARefineChain, } from "./question_answering/load.js";
|
|
8
8
|
export { loadSummarizationChain } from "./summarization/load.js";
|
|
9
9
|
export { SqlDatabaseChain } from "./sql_db/sql_db_chain.js";
|
|
10
10
|
export { ConversationalRetrievalQAChain } from "./conversational_retrieval_chain.js";
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.loadQAMapReduceChain = exports.loadQAStuffChain = exports.loadQAChain = void 0;
|
|
3
|
+
exports.loadQARefineChain = exports.loadQAMapReduceChain = exports.loadQAStuffChain = exports.loadQAChain = void 0;
|
|
4
4
|
const llm_chain_js_1 = require("../llm_chain.cjs");
|
|
5
5
|
const combine_docs_chain_js_1 = require("../combine_docs_chain.cjs");
|
|
6
6
|
const stuff_prompts_js_1 = require("./stuff_prompts.cjs");
|
|
7
7
|
const map_reduce_prompts_js_1 = require("./map_reduce_prompts.cjs");
|
|
8
|
+
const refine_prompts_js_1 = require("./refine_prompts.cjs");
|
|
8
9
|
const loadQAChain = (llm, params = {}) => {
|
|
9
10
|
const { prompt = stuff_prompts_js_1.DEFAULT_QA_PROMPT, combineMapPrompt = map_reduce_prompts_js_1.DEFAULT_COMBINE_QA_PROMPT, combinePrompt = map_reduce_prompts_js_1.COMBINE_PROMPT, type = "stuff", } = params;
|
|
10
11
|
if (type === "stuff") {
|
|
@@ -25,6 +26,16 @@ const loadQAChain = (llm, params = {}) => {
|
|
|
25
26
|
});
|
|
26
27
|
return chain;
|
|
27
28
|
}
|
|
29
|
+
if (type === "refine") {
|
|
30
|
+
const { questionPrompt = refine_prompts_js_1.QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = refine_prompts_js_1.REFINE_PROMPT_SELECTOR.getPrompt(llm), } = params;
|
|
31
|
+
const llmChain = new llm_chain_js_1.LLMChain({ prompt: questionPrompt, llm });
|
|
32
|
+
const refineLLMChain = new llm_chain_js_1.LLMChain({ prompt: refinePrompt, llm });
|
|
33
|
+
const chain = new combine_docs_chain_js_1.RefineDocumentsChain({
|
|
34
|
+
llmChain,
|
|
35
|
+
refineLLMChain,
|
|
36
|
+
});
|
|
37
|
+
return chain;
|
|
38
|
+
}
|
|
28
39
|
throw new Error(`Invalid _type: ${type}`);
|
|
29
40
|
};
|
|
30
41
|
exports.loadQAChain = loadQAChain;
|
|
@@ -50,3 +61,14 @@ const loadQAMapReduceChain = (llm, params = {}) => {
|
|
|
50
61
|
return chain;
|
|
51
62
|
};
|
|
52
63
|
exports.loadQAMapReduceChain = loadQAMapReduceChain;
|
|
64
|
+
const loadQARefineChain = (llm, params = {}) => {
|
|
65
|
+
const { questionPrompt = refine_prompts_js_1.QUESTION_PROMPT_SELECTOR.getPrompt(llm), refinePrompt = refine_prompts_js_1.REFINE_PROMPT_SELECTOR.getPrompt(llm), } = params;
|
|
66
|
+
const llmChain = new llm_chain_js_1.LLMChain({ prompt: questionPrompt, llm });
|
|
67
|
+
const refineLLMChain = new llm_chain_js_1.LLMChain({ prompt: refinePrompt, llm });
|
|
68
|
+
const chain = new combine_docs_chain_js_1.RefineDocumentsChain({
|
|
69
|
+
llmChain,
|
|
70
|
+
refineLLMChain,
|
|
71
|
+
});
|
|
72
|
+
return chain;
|
|
73
|
+
};
|
|
74
|
+
exports.loadQARefineChain = loadQARefineChain;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { BasePromptTemplate } from "../../prompts/base.js";
|
|
2
|
-
import { StuffDocumentsChain, MapReduceDocumentsChain } from "../combine_docs_chain.js";
|
|
2
|
+
import { StuffDocumentsChain, MapReduceDocumentsChain, RefineDocumentsChain } from "../combine_docs_chain.js";
|
|
3
3
|
import { BaseLanguageModel } from "../../base_language/index.js";
|
|
4
4
|
interface qaChainParams {
|
|
5
5
|
prompt?: BasePromptTemplate;
|
|
6
6
|
combineMapPrompt?: BasePromptTemplate;
|
|
7
7
|
combinePrompt?: BasePromptTemplate;
|
|
8
|
+
questionPrompt?: BasePromptTemplate;
|
|
9
|
+
refinePrompt?: BasePromptTemplate;
|
|
8
10
|
type?: string;
|
|
9
11
|
}
|
|
10
|
-
export declare const loadQAChain: (llm: BaseLanguageModel, params?: qaChainParams) => StuffDocumentsChain | MapReduceDocumentsChain;
|
|
12
|
+
export declare const loadQAChain: (llm: BaseLanguageModel, params?: qaChainParams) => StuffDocumentsChain | MapReduceDocumentsChain | RefineDocumentsChain;
|
|
11
13
|
interface StuffQAChainParams {
|
|
12
14
|
prompt?: BasePromptTemplate;
|
|
13
15
|
}
|
|
@@ -17,4 +19,9 @@ interface MapReduceQAChainParams {
|
|
|
17
19
|
combinePrompt?: BasePromptTemplate;
|
|
18
20
|
}
|
|
19
21
|
export declare const loadQAMapReduceChain: (llm: BaseLanguageModel, params?: MapReduceQAChainParams) => MapReduceDocumentsChain;
|
|
22
|
+
interface RefineQAChainParams {
|
|
23
|
+
questionPrompt?: BasePromptTemplate;
|
|
24
|
+
refinePrompt?: BasePromptTemplate;
|
|
25
|
+
}
|
|
26
|
+
export declare const loadQARefineChain: (llm: BaseLanguageModel, params?: RefineQAChainParams) => RefineDocumentsChain;
|
|
20
27
|
export {};
|