langchain 0.0.63 → 0.0.65
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 +3 -4
- package/dist/agents/agent.js +3 -4
- package/dist/agents/chat_convo/prompt.cjs +1 -1
- package/dist/agents/chat_convo/prompt.d.ts +1 -1
- package/dist/agents/chat_convo/prompt.js +1 -1
- package/dist/agents/initialize.cjs +1 -1
- package/dist/agents/initialize.d.ts +1 -1
- package/dist/agents/initialize.js +1 -1
- package/dist/agents/types.cjs +2 -2
- package/dist/agents/types.d.ts +2 -1
- package/dist/agents/types.js +1 -1
- package/dist/base_language/index.d.ts +4 -1
- package/dist/callbacks/manager.cjs +5 -1
- package/dist/callbacks/manager.js +5 -1
- package/dist/chains/analyze_documents_chain.cjs +3 -3
- package/dist/chains/analyze_documents_chain.d.ts +2 -1
- package/dist/chains/analyze_documents_chain.js +3 -3
- package/dist/chains/base.cjs +30 -11
- package/dist/chains/base.d.ts +7 -3
- package/dist/chains/base.js +30 -11
- package/dist/chains/chat_vector_db_chain.cjs +4 -4
- package/dist/chains/chat_vector_db_chain.d.ts +9 -15
- package/dist/chains/chat_vector_db_chain.js +4 -4
- package/dist/chains/combine_docs_chain.cjs +11 -11
- package/dist/chains/combine_docs_chain.d.ts +6 -5
- package/dist/chains/combine_docs_chain.js +11 -11
- package/dist/chains/conversational_retrieval_chain.cjs +4 -4
- package/dist/chains/conversational_retrieval_chain.d.ts +2 -1
- package/dist/chains/conversational_retrieval_chain.js +4 -4
- package/dist/chains/index.cjs +4 -3
- package/dist/chains/index.d.ts +2 -2
- package/dist/chains/index.js +1 -1
- package/dist/chains/llm_chain.cjs +4 -4
- package/dist/chains/llm_chain.d.ts +3 -2
- package/dist/chains/llm_chain.js +4 -4
- package/dist/chains/retrieval_qa.cjs +3 -3
- package/dist/chains/retrieval_qa.d.ts +2 -1
- package/dist/chains/retrieval_qa.js +3 -3
- package/dist/chains/sequential_chain.cjs +262 -0
- package/dist/chains/{simple_sequential_chain.d.ts → sequential_chain.d.ts} +31 -2
- package/dist/chains/sequential_chain.js +257 -0
- package/dist/chains/serde.d.ts +7 -1
- package/dist/chains/sql_db/sql_db_chain.cjs +7 -5
- package/dist/chains/sql_db/sql_db_chain.d.ts +2 -1
- package/dist/chains/sql_db/sql_db_chain.js +7 -5
- package/dist/chains/vector_db_qa.cjs +3 -3
- package/dist/chains/vector_db_qa.d.ts +2 -1
- package/dist/chains/vector_db_qa.js +3 -3
- package/dist/chat_models/base.d.ts +10 -8
- package/dist/chat_models/openai.cjs +9 -5
- package/dist/chat_models/openai.d.ts +14 -5
- package/dist/chat_models/openai.js +10 -6
- package/dist/document_loaders/fs/unstructured.cjs +48 -1
- package/dist/document_loaders/fs/unstructured.d.ts +9 -1
- package/dist/document_loaders/fs/unstructured.js +46 -0
- package/dist/embeddings/tensorflow.cjs +67 -0
- package/dist/embeddings/tensorflow.d.ts +12 -0
- package/dist/embeddings/tensorflow.js +40 -0
- package/dist/experimental/autogpt/output_parser.cjs +2 -2
- package/dist/experimental/autogpt/output_parser.d.ts +1 -1
- package/dist/experimental/autogpt/output_parser.js +1 -1
- package/dist/experimental/babyagi/agent.cjs +194 -0
- package/dist/experimental/babyagi/agent.d.ts +47 -0
- package/dist/experimental/babyagi/agent.js +190 -0
- package/dist/experimental/babyagi/index.cjs +11 -0
- package/dist/experimental/babyagi/index.d.ts +4 -0
- package/dist/experimental/babyagi/index.js +4 -0
- package/dist/experimental/babyagi/task_creation.cjs +29 -0
- package/dist/experimental/babyagi/task_creation.d.ts +5 -0
- package/dist/experimental/babyagi/task_creation.js +25 -0
- package/dist/experimental/babyagi/task_execution.cjs +20 -0
- package/dist/experimental/babyagi/task_execution.d.ts +5 -0
- package/dist/experimental/babyagi/task_execution.js +16 -0
- package/dist/experimental/babyagi/task_prioritization.cjs +23 -0
- package/dist/experimental/babyagi/task_prioritization.d.ts +5 -0
- package/dist/experimental/babyagi/task_prioritization.js +19 -0
- package/dist/llms/base.cjs +0 -9
- package/dist/llms/base.d.ts +11 -12
- package/dist/llms/base.js +0 -9
- package/dist/llms/openai-chat.cjs +9 -5
- package/dist/llms/openai-chat.d.ts +16 -7
- package/dist/llms/openai-chat.js +9 -5
- package/dist/llms/openai.cjs +9 -5
- package/dist/llms/openai.d.ts +15 -12
- package/dist/llms/openai.js +10 -6
- package/dist/memory/base.d.ts +1 -0
- package/dist/memory/buffer_memory.cjs +3 -0
- package/dist/memory/buffer_memory.d.ts +1 -0
- package/dist/memory/buffer_memory.js +3 -0
- package/dist/memory/buffer_window_memory.cjs +5 -0
- package/dist/memory/buffer_window_memory.d.ts +1 -0
- package/dist/memory/buffer_window_memory.js +5 -0
- package/dist/memory/motorhead_memory.cjs +3 -0
- package/dist/memory/motorhead_memory.d.ts +1 -0
- package/dist/memory/motorhead_memory.js +3 -0
- package/dist/memory/summary.cjs +3 -0
- package/dist/memory/summary.d.ts +1 -0
- package/dist/memory/summary.js +3 -0
- package/dist/output_parsers/combining.cjs +4 -4
- package/dist/output_parsers/combining.d.ts +3 -2
- package/dist/output_parsers/combining.js +3 -3
- package/dist/output_parsers/fix.cjs +6 -6
- package/dist/output_parsers/fix.d.ts +3 -2
- package/dist/output_parsers/fix.js +4 -4
- package/dist/output_parsers/list.cjs +3 -3
- package/dist/output_parsers/list.d.ts +1 -1
- package/dist/output_parsers/list.js +1 -1
- package/dist/output_parsers/regex.cjs +3 -3
- package/dist/output_parsers/regex.d.ts +1 -1
- package/dist/output_parsers/regex.js +1 -1
- package/dist/output_parsers/structured.cjs +10 -4
- package/dist/output_parsers/structured.d.ts +1 -1
- package/dist/output_parsers/structured.js +9 -3
- package/dist/prompts/base.d.ts +2 -1
- package/dist/prompts/chat.cjs +16 -2
- package/dist/prompts/chat.d.ts +2 -2
- package/dist/prompts/chat.js +16 -2
- package/dist/retrievers/supabase.cjs +2 -2
- package/dist/retrievers/supabase.js +2 -2
- package/dist/schema/index.cjs +1 -21
- package/dist/schema/index.d.ts +0 -30
- package/dist/schema/index.js +0 -18
- package/dist/schema/output_parser.cjs +23 -0
- package/dist/schema/output_parser.d.ts +32 -0
- package/dist/schema/output_parser.js +18 -0
- package/dist/tools/base.cjs +4 -4
- package/dist/tools/base.d.ts +4 -7
- package/dist/tools/base.js +4 -4
- package/dist/tools/fs.cjs +4 -4
- package/dist/tools/fs.d.ts +2 -2
- package/dist/tools/fs.js +4 -4
- package/dist/tools/serpapi.cjs +17 -10
- package/dist/tools/serpapi.d.ts +4 -1
- package/dist/tools/serpapi.js +17 -10
- package/dist/tools/webbrowser.cjs +5 -5
- package/dist/tools/webbrowser.d.ts +6 -6
- package/dist/tools/webbrowser.js +5 -5
- package/dist/util/async_caller.cjs +9 -0
- package/dist/util/async_caller.js +9 -0
- package/dist/util/axios-fetch-adapter.cjs +6 -0
- package/dist/util/axios-fetch-adapter.js +6 -0
- package/dist/util/axios-types.d.ts +3 -1
- package/dist/util/flatten.cjs +21 -0
- package/dist/util/flatten.d.ts +1 -0
- package/dist/util/flatten.js +17 -0
- package/dist/util/set.cjs +41 -0
- package/dist/util/set.d.ts +15 -0
- package/dist/util/set.js +35 -0
- package/dist/vectorstores/pinecone.cjs +4 -0
- package/dist/vectorstores/pinecone.js +4 -0
- package/dist/vectorstores/supabase.cjs +1 -1
- package/dist/vectorstores/supabase.js +1 -1
- package/dist/vectorstores/weaviate.cjs +15 -9
- package/dist/vectorstores/weaviate.js +15 -9
- package/embeddings/tensorflow.cjs +1 -0
- package/embeddings/tensorflow.d.ts +1 -0
- package/embeddings/tensorflow.js +1 -0
- package/experimental/babyagi.cjs +1 -0
- package/experimental/babyagi.d.ts +1 -0
- package/experimental/babyagi.js +1 -0
- package/package.json +36 -5
- package/dist/chains/simple_sequential_chain.cjs +0 -124
- package/dist/chains/simple_sequential_chain.js +0 -120
package/dist/agents/agent.cjs
CHANGED
|
@@ -100,9 +100,8 @@ class LLMSingleActionAgent extends BaseSingleActionAgent {
|
|
|
100
100
|
intermediate_steps: steps,
|
|
101
101
|
stop: this.stop,
|
|
102
102
|
...inputs,
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return this.outputParser.parse(output[this.llmChain.outputKey]);
|
|
103
|
+
}, callbackManager);
|
|
104
|
+
return this.outputParser.parse(output[this.llmChain.outputKey], callbackManager);
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
exports.LLMSingleActionAgent = LLMSingleActionAgent;
|
|
@@ -203,7 +202,7 @@ class Agent extends BaseSingleActionAgent {
|
|
|
203
202
|
newInputs.stop = this._stop();
|
|
204
203
|
}
|
|
205
204
|
const output = await this.llmChain.predict(newInputs, callbackManager);
|
|
206
|
-
return this.outputParser.parse(output);
|
|
205
|
+
return this.outputParser.parse(output, callbackManager);
|
|
207
206
|
}
|
|
208
207
|
/**
|
|
209
208
|
* Decide what to do given some input.
|
package/dist/agents/agent.js
CHANGED
|
@@ -94,9 +94,8 @@ export class LLMSingleActionAgent extends BaseSingleActionAgent {
|
|
|
94
94
|
intermediate_steps: steps,
|
|
95
95
|
stop: this.stop,
|
|
96
96
|
...inputs,
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return this.outputParser.parse(output[this.llmChain.outputKey]);
|
|
97
|
+
}, callbackManager);
|
|
98
|
+
return this.outputParser.parse(output[this.llmChain.outputKey], callbackManager);
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
101
|
/**
|
|
@@ -196,7 +195,7 @@ export class Agent extends BaseSingleActionAgent {
|
|
|
196
195
|
newInputs.stop = this._stop();
|
|
197
196
|
}
|
|
198
197
|
const output = await this.llmChain.predict(newInputs, callbackManager);
|
|
199
|
-
return this.outputParser.parse(output);
|
|
198
|
+
return this.outputParser.parse(output, callbackManager);
|
|
200
199
|
}
|
|
201
200
|
/**
|
|
202
201
|
* Decide what to do given some input.
|
|
@@ -31,7 +31,7 @@ Use this if you want to respond directly to the human. Markdown code snippet for
|
|
|
31
31
|
\`\`\`json
|
|
32
32
|
{{{{
|
|
33
33
|
"action": "Final Answer",
|
|
34
|
-
"action_input": string \\ You should put what you want to return to use here
|
|
34
|
+
"action_input": string \\ You should put what you want to return to use here and make sure to use valid json newline characters.
|
|
35
35
|
}}}}
|
|
36
36
|
\`\`\``;
|
|
37
37
|
exports.DEFAULT_SUFFIX = `TOOLS
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const DEFAULT_PREFIX = "Assistant is a large language model trained by OpenAI.\n\nAssistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.\n\nAssistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.\n\nOverall, Assistant is a powerful system that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.";
|
|
2
2
|
export declare const PREFIX_END = " However, above all else, all responses must adhere to the format of RESPONSE FORMAT INSTRUCTIONS.";
|
|
3
|
-
export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nWhen responding to me please, please output a response in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string \\ The action to take. Must be one of {tool_names}\n \"action_input\": string \\ The input to the action\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string \\ You should put what you want to return to use here
|
|
3
|
+
export declare const FORMAT_INSTRUCTIONS = "RESPONSE FORMAT INSTRUCTIONS\n----------------------------\n\nWhen responding to me please, please output a response in one of two formats:\n\n**Option 1:**\nUse this if you want the human to use a tool.\nMarkdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": string \\ The action to take. Must be one of {tool_names}\n \"action_input\": string \\ The input to the action\n}}}}\n```\n\n**Option #2:**\nUse this if you want to respond directly to the human. Markdown code snippet formatted in the following schema:\n\n```json\n{{{{\n \"action\": \"Final Answer\",\n \"action_input\": string \\ You should put what you want to return to use here and make sure to use valid json newline characters.\n}}}}\n```";
|
|
4
4
|
export declare const DEFAULT_SUFFIX = "TOOLS\n------\nAssistant can ask the user to use tools to look up information that may be helpful in answering the users original question. The tools the human can use are:\n\n{{tools}}\n\n{format_instructions}\n\nUSER'S INPUT\n--------------------\nHere is the user's input (remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else):\n\n{{{{input}}}}";
|
|
5
5
|
export declare const TEMPLATE_TOOL_RESPONSE = "TOOL RESPONSE:\n---------------------\n{observation}\n\nUSER'S INPUT\n--------------------\n\nOkay, so what is the response to my original question? If using information from tools, you must say it explicitly - I have forgotten all TOOL RESPONSES! Remember to respond with a markdown code snippet of a json blob with a single action, and NOTHING else.";
|
|
@@ -28,7 +28,7 @@ Use this if you want to respond directly to the human. Markdown code snippet for
|
|
|
28
28
|
\`\`\`json
|
|
29
29
|
{{{{
|
|
30
30
|
"action": "Final Answer",
|
|
31
|
-
"action_input": string \\ You should put what you want to return to use here
|
|
31
|
+
"action_input": string \\ You should put what you want to return to use here and make sure to use valid json newline characters.
|
|
32
32
|
}}}}
|
|
33
33
|
\`\`\``;
|
|
34
34
|
export const DEFAULT_SUFFIX = `TOOLS
|
|
@@ -7,7 +7,7 @@ const index_js_2 = require("./chat_convo/index.cjs");
|
|
|
7
7
|
const executor_js_1 = require("./executor.cjs");
|
|
8
8
|
const index_js_3 = require("./mrkl/index.cjs");
|
|
9
9
|
/**
|
|
10
|
-
* @deprecated use
|
|
10
|
+
* @deprecated use initializeAgentExecutorWithOptions instead
|
|
11
11
|
*/
|
|
12
12
|
const initializeAgentExecutor = async (tools, llm, _agentType, _verbose, _callbackManager) => {
|
|
13
13
|
const agentType = _agentType ?? "zero-shot-react-description";
|
|
@@ -7,7 +7,7 @@ import { AgentExecutor, AgentExecutorInput } from "./executor.js";
|
|
|
7
7
|
import { ZeroShotAgent } from "./mrkl/index.js";
|
|
8
8
|
type AgentType = "zero-shot-react-description" | "chat-zero-shot-react-description" | "chat-conversational-react-description";
|
|
9
9
|
/**
|
|
10
|
-
* @deprecated use
|
|
10
|
+
* @deprecated use initializeAgentExecutorWithOptions instead
|
|
11
11
|
*/
|
|
12
12
|
export declare const initializeAgentExecutor: (tools: Tool[], llm: BaseLanguageModel, _agentType?: AgentType, _verbose?: boolean, _callbackManager?: CallbackManager) => Promise<AgentExecutor>;
|
|
13
13
|
/**
|
|
@@ -4,7 +4,7 @@ import { ChatConversationalAgent } from "./chat_convo/index.js";
|
|
|
4
4
|
import { AgentExecutor } from "./executor.js";
|
|
5
5
|
import { ZeroShotAgent } from "./mrkl/index.js";
|
|
6
6
|
/**
|
|
7
|
-
* @deprecated use
|
|
7
|
+
* @deprecated use initializeAgentExecutorWithOptions instead
|
|
8
8
|
*/
|
|
9
9
|
export const initializeAgentExecutor = async (tools, llm, _agentType, _verbose, _callbackManager) => {
|
|
10
10
|
const agentType = _agentType ?? "zero-shot-react-description";
|
package/dist/agents/types.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AgentActionOutputParser = void 0;
|
|
4
|
-
const
|
|
5
|
-
class AgentActionOutputParser extends
|
|
4
|
+
const output_parser_js_1 = require("../schema/output_parser.cjs");
|
|
5
|
+
class AgentActionOutputParser extends output_parser_js_1.BaseOutputParser {
|
|
6
6
|
}
|
|
7
7
|
exports.AgentActionOutputParser = AgentActionOutputParser;
|
package/dist/agents/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LLMChain } from "../chains/llm_chain.js";
|
|
2
2
|
import { SerializedLLMChain } from "../chains/serde.js";
|
|
3
|
-
import { AgentAction, AgentFinish
|
|
3
|
+
import { AgentAction, AgentFinish } from "../schema/index.js";
|
|
4
|
+
import { BaseOutputParser } from "../schema/output_parser.js";
|
|
4
5
|
export interface AgentInput {
|
|
5
6
|
llmChain: LLMChain;
|
|
6
7
|
outputParser: AgentActionOutputParser;
|
package/dist/agents/types.js
CHANGED
|
@@ -31,17 +31,20 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
|
|
|
31
31
|
*/
|
|
32
32
|
callbackManager?: CallbackManager;
|
|
33
33
|
}
|
|
34
|
+
export interface BaseLanguageModelCallOptions {
|
|
35
|
+
}
|
|
34
36
|
/**
|
|
35
37
|
* Base class for language models.
|
|
36
38
|
*/
|
|
37
39
|
export declare abstract class BaseLanguageModel extends BaseLangChain implements BaseLanguageModelParams {
|
|
40
|
+
CallOptions: BaseLanguageModelCallOptions;
|
|
38
41
|
/**
|
|
39
42
|
* The async caller should be used by subclasses to make any async calls,
|
|
40
43
|
* which will thus benefit from the concurrency and retry logic.
|
|
41
44
|
*/
|
|
42
45
|
caller: AsyncCaller;
|
|
43
46
|
constructor(params: BaseLanguageModelParams);
|
|
44
|
-
abstract generatePrompt(promptValues: BasePromptValue[], stop?: string[], callbacks?: Callbacks): Promise<LLMResult>;
|
|
47
|
+
abstract generatePrompt(promptValues: BasePromptValue[], stop?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
45
48
|
abstract _modelType(): string;
|
|
46
49
|
abstract _llmType(): string;
|
|
47
50
|
private _encoding?;
|
|
@@ -319,7 +319,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
319
319
|
}
|
|
320
320
|
if (tracingEnabled &&
|
|
321
321
|
!callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
|
|
322
|
-
|
|
322
|
+
const session = typeof process !== "undefined"
|
|
323
|
+
? // eslint-disable-next-line no-process-env
|
|
324
|
+
process.env?.LANGCHAIN_SESSION
|
|
325
|
+
: undefined;
|
|
326
|
+
callbackManager.addHandler(await (0, initialize_js_1.getTracingCallbackHandler)(session), true);
|
|
323
327
|
}
|
|
324
328
|
}
|
|
325
329
|
return callbackManager;
|
|
@@ -312,7 +312,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
312
312
|
}
|
|
313
313
|
if (tracingEnabled &&
|
|
314
314
|
!callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
|
|
315
|
-
|
|
315
|
+
const session = typeof process !== "undefined"
|
|
316
|
+
? // eslint-disable-next-line no-process-env
|
|
317
|
+
process.env?.LANGCHAIN_SESSION
|
|
318
|
+
: undefined;
|
|
319
|
+
callbackManager.addHandler(await getTracingCallbackHandler(session), true);
|
|
316
320
|
}
|
|
317
321
|
}
|
|
318
322
|
return callbackManager;
|
|
@@ -10,7 +10,7 @@ const text_splitter_js_1 = require("../text_splitter.cjs");
|
|
|
10
10
|
*/
|
|
11
11
|
class AnalyzeDocumentChain extends base_js_1.BaseChain {
|
|
12
12
|
constructor(fields) {
|
|
13
|
-
super(
|
|
13
|
+
super(fields);
|
|
14
14
|
Object.defineProperty(this, "inputKey", {
|
|
15
15
|
enumerable: true,
|
|
16
16
|
configurable: true,
|
|
@@ -41,7 +41,7 @@ class AnalyzeDocumentChain extends base_js_1.BaseChain {
|
|
|
41
41
|
return this.combineDocumentsChain.outputKeys;
|
|
42
42
|
}
|
|
43
43
|
/** @ignore */
|
|
44
|
-
async _call(values) {
|
|
44
|
+
async _call(values, runManager) {
|
|
45
45
|
if (!(this.inputKey in values)) {
|
|
46
46
|
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
47
47
|
}
|
|
@@ -49,7 +49,7 @@ class AnalyzeDocumentChain extends base_js_1.BaseChain {
|
|
|
49
49
|
const currentDoc = doc;
|
|
50
50
|
const currentDocs = await this.textSplitter.createDocuments([currentDoc]);
|
|
51
51
|
const newInputs = { input_documents: currentDocs, ...rest };
|
|
52
|
-
const result = await this.combineDocumentsChain.call(newInputs);
|
|
52
|
+
const result = await this.combineDocumentsChain.call(newInputs, runManager?.getChild());
|
|
53
53
|
return result;
|
|
54
54
|
}
|
|
55
55
|
_chainType() {
|
|
@@ -2,6 +2,7 @@ import { BaseChain, ChainInputs } from "./base.js";
|
|
|
2
2
|
import { TextSplitter } from "../text_splitter.js";
|
|
3
3
|
import { ChainValues } from "../schema/index.js";
|
|
4
4
|
import { SerializedAnalyzeDocumentChain } from "./serde.js";
|
|
5
|
+
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
5
6
|
export type LoadValues = Record<string, any>;
|
|
6
7
|
export interface AnalyzeDocumentChainInput extends Omit<ChainInputs, "memory"> {
|
|
7
8
|
combineDocumentsChain: BaseChain;
|
|
@@ -21,7 +22,7 @@ export declare class AnalyzeDocumentChain extends BaseChain implements AnalyzeDo
|
|
|
21
22
|
get inputKeys(): string[];
|
|
22
23
|
get outputKeys(): string[];
|
|
23
24
|
/** @ignore */
|
|
24
|
-
_call(values: ChainValues): Promise<ChainValues>;
|
|
25
|
+
_call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
|
|
25
26
|
_chainType(): "analyze_document_chain";
|
|
26
27
|
static deserialize(data: SerializedAnalyzeDocumentChain, values: LoadValues): Promise<AnalyzeDocumentChain>;
|
|
27
28
|
serialize(): SerializedAnalyzeDocumentChain;
|
|
@@ -7,7 +7,7 @@ import { RecursiveCharacterTextSplitter, } from "../text_splitter.js";
|
|
|
7
7
|
*/
|
|
8
8
|
export class AnalyzeDocumentChain extends BaseChain {
|
|
9
9
|
constructor(fields) {
|
|
10
|
-
super(
|
|
10
|
+
super(fields);
|
|
11
11
|
Object.defineProperty(this, "inputKey", {
|
|
12
12
|
enumerable: true,
|
|
13
13
|
configurable: true,
|
|
@@ -38,7 +38,7 @@ export class AnalyzeDocumentChain extends BaseChain {
|
|
|
38
38
|
return this.combineDocumentsChain.outputKeys;
|
|
39
39
|
}
|
|
40
40
|
/** @ignore */
|
|
41
|
-
async _call(values) {
|
|
41
|
+
async _call(values, runManager) {
|
|
42
42
|
if (!(this.inputKey in values)) {
|
|
43
43
|
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
44
44
|
}
|
|
@@ -46,7 +46,7 @@ export class AnalyzeDocumentChain extends BaseChain {
|
|
|
46
46
|
const currentDoc = doc;
|
|
47
47
|
const currentDocs = await this.textSplitter.createDocuments([currentDoc]);
|
|
48
48
|
const newInputs = { input_documents: currentDocs, ...rest };
|
|
49
|
-
const result = await this.combineDocumentsChain.call(newInputs);
|
|
49
|
+
const result = await this.combineDocumentsChain.call(newInputs, runManager?.getChild());
|
|
50
50
|
return result;
|
|
51
51
|
}
|
|
52
52
|
_chainType() {
|
package/dist/chains/base.cjs
CHANGED
|
@@ -8,15 +8,30 @@ const index_js_2 = require("../base_language/index.cjs");
|
|
|
8
8
|
* Base interface that all chains must implement.
|
|
9
9
|
*/
|
|
10
10
|
class BaseChain extends index_js_2.BaseLangChain {
|
|
11
|
-
constructor(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
constructor(fields,
|
|
12
|
+
/** @deprecated */
|
|
13
|
+
verbose,
|
|
14
|
+
/** @deprecated */
|
|
15
|
+
callbacks) {
|
|
16
|
+
if (arguments.length === 1 &&
|
|
17
|
+
typeof fields === "object" &&
|
|
18
|
+
!("saveContext" in fields)) {
|
|
19
|
+
// fields is not a BaseMemory
|
|
20
|
+
const { memory, callbackManager, ...rest } = fields;
|
|
21
|
+
super({ ...rest, callbacks: callbackManager ?? rest.callbacks });
|
|
22
|
+
this.memory = memory;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
// fields is a BaseMemory
|
|
26
|
+
super({ verbose, callbacks });
|
|
27
|
+
this.memory = fields;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Return a json-like object representing this chain.
|
|
32
|
+
*/
|
|
33
|
+
serialize() {
|
|
34
|
+
throw new Error("Method not implemented.");
|
|
20
35
|
}
|
|
21
36
|
async run(
|
|
22
37
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -36,7 +51,7 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
36
51
|
/**
|
|
37
52
|
* Run the core logic of this chain and add to output if desired.
|
|
38
53
|
*
|
|
39
|
-
* Wraps
|
|
54
|
+
* Wraps _call and handles memory.
|
|
40
55
|
*/
|
|
41
56
|
async call(values, callbacks) {
|
|
42
57
|
const fullValues = { ...values };
|
|
@@ -82,8 +97,12 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
82
97
|
const { LLMChain } = await import("./llm_chain.js");
|
|
83
98
|
return LLMChain.deserialize(data);
|
|
84
99
|
}
|
|
100
|
+
case "sequential_chain": {
|
|
101
|
+
const { SequentialChain } = await import("./sequential_chain.js");
|
|
102
|
+
return SequentialChain.deserialize(data);
|
|
103
|
+
}
|
|
85
104
|
case "simple_sequential_chain": {
|
|
86
|
-
const { SimpleSequentialChain } = await import("./
|
|
105
|
+
const { SimpleSequentialChain } = await import("./sequential_chain.js");
|
|
87
106
|
return SimpleSequentialChain.deserialize(data);
|
|
88
107
|
}
|
|
89
108
|
case "stuff_documents_chain": {
|
package/dist/chains/base.d.ts
CHANGED
|
@@ -16,7 +16,11 @@ export interface ChainInputs extends BaseLangChainParams {
|
|
|
16
16
|
*/
|
|
17
17
|
export declare abstract class BaseChain extends BaseLangChain implements ChainInputs {
|
|
18
18
|
memory?: BaseMemory;
|
|
19
|
-
constructor(
|
|
19
|
+
constructor(fields?: BaseMemory | ChainInputs,
|
|
20
|
+
/** @deprecated */
|
|
21
|
+
verbose?: boolean,
|
|
22
|
+
/** @deprecated */
|
|
23
|
+
callbacks?: Callbacks);
|
|
20
24
|
/**
|
|
21
25
|
* Run the core logic of this chain and return the output
|
|
22
26
|
*/
|
|
@@ -28,14 +32,14 @@ export declare abstract class BaseChain extends BaseLangChain implements ChainIn
|
|
|
28
32
|
/**
|
|
29
33
|
* Return a json-like object representing this chain.
|
|
30
34
|
*/
|
|
31
|
-
|
|
35
|
+
serialize(): SerializedBaseChain;
|
|
32
36
|
abstract get inputKeys(): string[];
|
|
33
37
|
abstract get outputKeys(): string[];
|
|
34
38
|
run(input: any, callbacks?: Callbacks): Promise<string>;
|
|
35
39
|
/**
|
|
36
40
|
* Run the core logic of this chain and add to output if desired.
|
|
37
41
|
*
|
|
38
|
-
* Wraps
|
|
42
|
+
* Wraps _call and handles memory.
|
|
39
43
|
*/
|
|
40
44
|
call(values: ChainValues, callbacks?: Callbacks): Promise<ChainValues>;
|
|
41
45
|
/**
|
package/dist/chains/base.js
CHANGED
|
@@ -5,15 +5,30 @@ import { BaseLangChain } from "../base_language/index.js";
|
|
|
5
5
|
* Base interface that all chains must implement.
|
|
6
6
|
*/
|
|
7
7
|
export class BaseChain extends BaseLangChain {
|
|
8
|
-
constructor(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
constructor(fields,
|
|
9
|
+
/** @deprecated */
|
|
10
|
+
verbose,
|
|
11
|
+
/** @deprecated */
|
|
12
|
+
callbacks) {
|
|
13
|
+
if (arguments.length === 1 &&
|
|
14
|
+
typeof fields === "object" &&
|
|
15
|
+
!("saveContext" in fields)) {
|
|
16
|
+
// fields is not a BaseMemory
|
|
17
|
+
const { memory, callbackManager, ...rest } = fields;
|
|
18
|
+
super({ ...rest, callbacks: callbackManager ?? rest.callbacks });
|
|
19
|
+
this.memory = memory;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
// fields is a BaseMemory
|
|
23
|
+
super({ verbose, callbacks });
|
|
24
|
+
this.memory = fields;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Return a json-like object representing this chain.
|
|
29
|
+
*/
|
|
30
|
+
serialize() {
|
|
31
|
+
throw new Error("Method not implemented.");
|
|
17
32
|
}
|
|
18
33
|
async run(
|
|
19
34
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -33,7 +48,7 @@ export class BaseChain extends BaseLangChain {
|
|
|
33
48
|
/**
|
|
34
49
|
* Run the core logic of this chain and add to output if desired.
|
|
35
50
|
*
|
|
36
|
-
* Wraps
|
|
51
|
+
* Wraps _call and handles memory.
|
|
37
52
|
*/
|
|
38
53
|
async call(values, callbacks) {
|
|
39
54
|
const fullValues = { ...values };
|
|
@@ -79,8 +94,12 @@ export class BaseChain extends BaseLangChain {
|
|
|
79
94
|
const { LLMChain } = await import("./llm_chain.js");
|
|
80
95
|
return LLMChain.deserialize(data);
|
|
81
96
|
}
|
|
97
|
+
case "sequential_chain": {
|
|
98
|
+
const { SequentialChain } = await import("./sequential_chain.js");
|
|
99
|
+
return SequentialChain.deserialize(data);
|
|
100
|
+
}
|
|
82
101
|
case "simple_sequential_chain": {
|
|
83
|
-
const { SimpleSequentialChain } = await import("./
|
|
102
|
+
const { SimpleSequentialChain } = await import("./sequential_chain.js");
|
|
84
103
|
return SimpleSequentialChain.deserialize(data);
|
|
85
104
|
}
|
|
86
105
|
case "stuff_documents_chain": {
|
|
@@ -25,7 +25,7 @@ class ChatVectorDBQAChain extends base_js_1.BaseChain {
|
|
|
25
25
|
return [this.outputKey];
|
|
26
26
|
}
|
|
27
27
|
constructor(fields) {
|
|
28
|
-
super();
|
|
28
|
+
super(fields);
|
|
29
29
|
Object.defineProperty(this, "k", {
|
|
30
30
|
enumerable: true,
|
|
31
31
|
configurable: true,
|
|
@@ -84,7 +84,7 @@ class ChatVectorDBQAChain extends base_js_1.BaseChain {
|
|
|
84
84
|
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
85
85
|
}
|
|
86
86
|
/** @ignore */
|
|
87
|
-
async _call(values) {
|
|
87
|
+
async _call(values, runManager) {
|
|
88
88
|
if (!(this.inputKey in values)) {
|
|
89
89
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
90
90
|
}
|
|
@@ -98,7 +98,7 @@ class ChatVectorDBQAChain extends base_js_1.BaseChain {
|
|
|
98
98
|
const result = await this.questionGeneratorChain.call({
|
|
99
99
|
question,
|
|
100
100
|
chat_history: chatHistory,
|
|
101
|
-
});
|
|
101
|
+
}, runManager?.getChild());
|
|
102
102
|
const keys = Object.keys(result);
|
|
103
103
|
console.log("_call", values, keys);
|
|
104
104
|
if (keys.length === 1) {
|
|
@@ -114,7 +114,7 @@ class ChatVectorDBQAChain extends base_js_1.BaseChain {
|
|
|
114
114
|
input_documents: docs,
|
|
115
115
|
chat_history: chatHistory,
|
|
116
116
|
};
|
|
117
|
-
const result = await this.combineDocumentsChain.call(inputs);
|
|
117
|
+
const result = await this.combineDocumentsChain.call(inputs, runManager?.getChild());
|
|
118
118
|
if (this.returnSourceDocuments) {
|
|
119
119
|
return {
|
|
120
120
|
...result,
|
|
@@ -2,16 +2,18 @@ import { BaseLanguageModel } from "../base_language/index.js";
|
|
|
2
2
|
import { VectorStore } from "../vectorstores/base.js";
|
|
3
3
|
import { SerializedChatVectorDBQAChain } from "./serde.js";
|
|
4
4
|
import { ChainValues } from "../schema/index.js";
|
|
5
|
-
import { BaseChain } from "./base.js";
|
|
5
|
+
import { BaseChain, ChainInputs } from "./base.js";
|
|
6
6
|
import { LLMChain } from "./llm_chain.js";
|
|
7
|
+
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
7
8
|
export type LoadValues = Record<string, any>;
|
|
8
|
-
export interface ChatVectorDBQAChainInput {
|
|
9
|
+
export interface ChatVectorDBQAChainInput extends ChainInputs {
|
|
9
10
|
vectorstore: VectorStore;
|
|
10
|
-
k: number;
|
|
11
11
|
combineDocumentsChain: BaseChain;
|
|
12
12
|
questionGeneratorChain: LLMChain;
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
returnSourceDocuments?: boolean;
|
|
14
|
+
outputKey?: string;
|
|
15
|
+
inputKey?: string;
|
|
16
|
+
k?: number;
|
|
15
17
|
}
|
|
16
18
|
export declare class ChatVectorDBQAChain extends BaseChain implements ChatVectorDBQAChainInput {
|
|
17
19
|
k: number;
|
|
@@ -24,17 +26,9 @@ export declare class ChatVectorDBQAChain extends BaseChain implements ChatVector
|
|
|
24
26
|
combineDocumentsChain: BaseChain;
|
|
25
27
|
questionGeneratorChain: LLMChain;
|
|
26
28
|
returnSourceDocuments: boolean;
|
|
27
|
-
constructor(fields:
|
|
28
|
-
vectorstore: VectorStore;
|
|
29
|
-
combineDocumentsChain: BaseChain;
|
|
30
|
-
questionGeneratorChain: LLMChain;
|
|
31
|
-
inputKey?: string;
|
|
32
|
-
outputKey?: string;
|
|
33
|
-
k?: number;
|
|
34
|
-
returnSourceDocuments?: boolean;
|
|
35
|
-
});
|
|
29
|
+
constructor(fields: ChatVectorDBQAChainInput);
|
|
36
30
|
/** @ignore */
|
|
37
|
-
_call(values: ChainValues): Promise<ChainValues>;
|
|
31
|
+
_call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
|
|
38
32
|
_chainType(): "chat-vector-db";
|
|
39
33
|
static deserialize(data: SerializedChatVectorDBQAChain, values: LoadValues): Promise<ChatVectorDBQAChain>;
|
|
40
34
|
serialize(): SerializedChatVectorDBQAChain;
|
|
@@ -22,7 +22,7 @@ export class ChatVectorDBQAChain extends BaseChain {
|
|
|
22
22
|
return [this.outputKey];
|
|
23
23
|
}
|
|
24
24
|
constructor(fields) {
|
|
25
|
-
super();
|
|
25
|
+
super(fields);
|
|
26
26
|
Object.defineProperty(this, "k", {
|
|
27
27
|
enumerable: true,
|
|
28
28
|
configurable: true,
|
|
@@ -81,7 +81,7 @@ export class ChatVectorDBQAChain extends BaseChain {
|
|
|
81
81
|
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
82
82
|
}
|
|
83
83
|
/** @ignore */
|
|
84
|
-
async _call(values) {
|
|
84
|
+
async _call(values, runManager) {
|
|
85
85
|
if (!(this.inputKey in values)) {
|
|
86
86
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
87
87
|
}
|
|
@@ -95,7 +95,7 @@ export class ChatVectorDBQAChain extends BaseChain {
|
|
|
95
95
|
const result = await this.questionGeneratorChain.call({
|
|
96
96
|
question,
|
|
97
97
|
chat_history: chatHistory,
|
|
98
|
-
});
|
|
98
|
+
}, runManager?.getChild());
|
|
99
99
|
const keys = Object.keys(result);
|
|
100
100
|
console.log("_call", values, keys);
|
|
101
101
|
if (keys.length === 1) {
|
|
@@ -111,7 +111,7 @@ export class ChatVectorDBQAChain extends BaseChain {
|
|
|
111
111
|
input_documents: docs,
|
|
112
112
|
chat_history: chatHistory,
|
|
113
113
|
};
|
|
114
|
-
const result = await this.combineDocumentsChain.call(inputs);
|
|
114
|
+
const result = await this.combineDocumentsChain.call(inputs, runManager?.getChild());
|
|
115
115
|
if (this.returnSourceDocuments) {
|
|
116
116
|
return {
|
|
117
117
|
...result,
|
|
@@ -17,7 +17,7 @@ class StuffDocumentsChain extends base_js_1.BaseChain {
|
|
|
17
17
|
return this.llmChain.outputKeys;
|
|
18
18
|
}
|
|
19
19
|
constructor(fields) {
|
|
20
|
-
super();
|
|
20
|
+
super(fields);
|
|
21
21
|
Object.defineProperty(this, "llmChain", {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
configurable: true,
|
|
@@ -42,7 +42,7 @@ class StuffDocumentsChain extends base_js_1.BaseChain {
|
|
|
42
42
|
this.inputKey = fields.inputKey ?? this.inputKey;
|
|
43
43
|
}
|
|
44
44
|
/** @ignore */
|
|
45
|
-
async _call(values) {
|
|
45
|
+
async _call(values, runManager) {
|
|
46
46
|
if (!(this.inputKey in values)) {
|
|
47
47
|
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
48
48
|
}
|
|
@@ -52,7 +52,7 @@ class StuffDocumentsChain extends base_js_1.BaseChain {
|
|
|
52
52
|
const result = await this.llmChain.call({
|
|
53
53
|
...rest,
|
|
54
54
|
[this.documentVariableName]: text,
|
|
55
|
-
});
|
|
55
|
+
}, runManager?.getChild());
|
|
56
56
|
return result;
|
|
57
57
|
}
|
|
58
58
|
_chainType() {
|
|
@@ -87,7 +87,7 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
|
|
|
87
87
|
return this.combineDocumentChain.outputKeys;
|
|
88
88
|
}
|
|
89
89
|
constructor(fields) {
|
|
90
|
-
super();
|
|
90
|
+
super(fields);
|
|
91
91
|
Object.defineProperty(this, "llmChain", {
|
|
92
92
|
enumerable: true,
|
|
93
93
|
configurable: true,
|
|
@@ -140,7 +140,7 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
|
|
|
140
140
|
this.maxIterations = fields.maxIterations ?? this.maxIterations;
|
|
141
141
|
}
|
|
142
142
|
/** @ignore */
|
|
143
|
-
async _call(values) {
|
|
143
|
+
async _call(values, runManager) {
|
|
144
144
|
if (!(this.inputKey in values)) {
|
|
145
145
|
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
146
146
|
}
|
|
@@ -161,14 +161,14 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
|
|
|
161
161
|
if (canSkipMapStep && withinTokenLimit) {
|
|
162
162
|
break;
|
|
163
163
|
}
|
|
164
|
-
const results = await this.llmChain.apply(inputs);
|
|
164
|
+
const results = await this.llmChain.apply(inputs, runManager ? [runManager.getChild()] : undefined);
|
|
165
165
|
const { outputKey } = this.llmChain;
|
|
166
166
|
currentDocs = results.map((r) => ({
|
|
167
167
|
pageContent: r[outputKey],
|
|
168
168
|
}));
|
|
169
169
|
}
|
|
170
170
|
const newInputs = { input_documents: currentDocs, ...rest };
|
|
171
|
-
const result = await this.combineDocumentChain.call(newInputs);
|
|
171
|
+
const result = await this.combineDocumentChain.call(newInputs, runManager?.getChild());
|
|
172
172
|
return result;
|
|
173
173
|
}
|
|
174
174
|
_chainType() {
|
|
@@ -214,7 +214,7 @@ class RefineDocumentsChain extends base_js_1.BaseChain {
|
|
|
214
214
|
return [this.outputKey];
|
|
215
215
|
}
|
|
216
216
|
constructor(fields) {
|
|
217
|
-
super();
|
|
217
|
+
super(fields);
|
|
218
218
|
Object.defineProperty(this, "llmChain", {
|
|
219
219
|
enumerable: true,
|
|
220
220
|
configurable: true,
|
|
@@ -304,19 +304,19 @@ class RefineDocumentsChain extends base_js_1.BaseChain {
|
|
|
304
304
|
return inputs;
|
|
305
305
|
}
|
|
306
306
|
/** @ignore */
|
|
307
|
-
async _call(values) {
|
|
307
|
+
async _call(values, runManager) {
|
|
308
308
|
if (!(this.inputKey in values)) {
|
|
309
309
|
throw new Error(`Document key ${this.inputKey} not found.`);
|
|
310
310
|
}
|
|
311
311
|
const { [this.inputKey]: docs, ...rest } = values;
|
|
312
312
|
const currentDocs = docs;
|
|
313
313
|
const initialInputs = await this._constructInitialInputs(currentDocs[0], rest);
|
|
314
|
-
let res = await this.llmChain.predict({ ...initialInputs });
|
|
314
|
+
let res = await this.llmChain.predict({ ...initialInputs }, runManager?.getChild());
|
|
315
315
|
const refineSteps = [res];
|
|
316
316
|
for (let i = 1; i < currentDocs.length; i += 1) {
|
|
317
317
|
const refineInputs = await this._constructRefineInputs(currentDocs[i], res);
|
|
318
318
|
const inputs = { ...refineInputs, ...rest };
|
|
319
|
-
res = await this.refineLLMChain.predict({ ...inputs });
|
|
319
|
+
res = await this.refineLLMChain.predict({ ...inputs }, runManager?.getChild());
|
|
320
320
|
refineSteps.push(res);
|
|
321
321
|
}
|
|
322
322
|
return { [this.outputKey]: res };
|