langchain 0.0.212 → 0.0.213
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/chains/combine_documents.cjs +1 -0
- package/chains/combine_documents.d.ts +1 -0
- package/chains/combine_documents.js +1 -0
- package/chains/history_aware_retriever.cjs +1 -0
- package/chains/history_aware_retriever.d.ts +1 -0
- package/chains/history_aware_retriever.js +1 -0
- package/chains/retrieval.cjs +1 -0
- package/chains/retrieval.d.ts +1 -0
- package/chains/retrieval.js +1 -0
- package/dist/agents/agent.cjs +1 -0
- package/dist/agents/agent.js +1 -0
- package/dist/agents/executor.cjs +21 -3
- package/dist/agents/executor.d.ts +1 -0
- package/dist/agents/executor.js +21 -3
- package/dist/agents/format_scratchpad/openai_functions.cjs +22 -1
- package/dist/agents/format_scratchpad/openai_functions.d.ts +10 -0
- package/dist/agents/format_scratchpad/openai_functions.js +21 -1
- package/dist/agents/index.cjs +11 -4
- package/dist/agents/index.d.ts +6 -3
- package/dist/agents/index.js +5 -3
- 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/openai/output_parser.cjs +20 -196
- package/dist/agents/openai/output_parser.d.ts +2 -111
- package/dist/agents/openai/output_parser.js +6 -193
- package/dist/agents/{openai → openai_functions}/index.cjs +78 -2
- package/dist/agents/{openai → openai_functions}/index.d.ts +75 -3
- package/dist/agents/{openai → openai_functions}/index.js +76 -1
- package/dist/agents/openai_functions/output_parser.cjs +102 -0
- package/dist/agents/openai_functions/output_parser.d.ts +56 -0
- package/dist/agents/openai_functions/output_parser.js +98 -0
- package/dist/agents/openai_tools/index.cjs +81 -0
- package/dist/agents/openai_tools/index.d.ts +80 -0
- package/dist/agents/openai_tools/index.js +77 -0
- package/dist/agents/openai_tools/output_parser.cjs +102 -0
- package/dist/agents/openai_tools/output_parser.d.ts +57 -0
- package/dist/agents/openai_tools/output_parser.js +98 -0
- package/dist/agents/react/index.cjs +75 -0
- package/dist/agents/react/index.d.ts +60 -0
- package/dist/agents/react/index.js +71 -0
- package/dist/agents/react/output_parser.cjs +0 -1
- package/dist/agents/react/output_parser.d.ts +0 -1
- package/dist/agents/react/output_parser.js +0 -1
- package/dist/agents/structured_chat/index.cjs +85 -1
- package/dist/agents/structured_chat/index.d.ts +71 -0
- package/dist/agents/structured_chat/index.js +83 -0
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.cjs +1 -1
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.js +1 -1
- package/dist/agents/toolkits/conversational_retrieval/tool.cjs +1 -0
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -0
- package/dist/agents/toolkits/conversational_retrieval/tool.js +1 -0
- package/dist/agents/toolkits/json/json.cjs +2 -0
- package/dist/agents/toolkits/json/json.d.ts +2 -0
- package/dist/agents/toolkits/json/json.js +2 -0
- package/dist/agents/toolkits/openapi/openapi.cjs +2 -0
- package/dist/agents/toolkits/openapi/openapi.d.ts +2 -0
- package/dist/agents/toolkits/openapi/openapi.js +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.cjs +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +2 -0
- package/dist/agents/toolkits/vectorstore/vectorstore.js +2 -0
- package/dist/agents/xml/index.cjs +75 -1
- package/dist/agents/xml/index.d.ts +65 -0
- package/dist/agents/xml/index.js +73 -0
- package/dist/chains/combine_documents/base.cjs +16 -0
- package/dist/chains/combine_documents/base.d.ts +13 -0
- package/dist/chains/combine_documents/base.js +12 -0
- package/dist/chains/combine_documents/index.cjs +5 -0
- package/dist/chains/combine_documents/index.d.ts +1 -0
- package/dist/chains/combine_documents/index.js +1 -0
- package/dist/chains/combine_documents/reduce.cjs +5 -2
- package/dist/chains/combine_documents/reduce.js +4 -1
- package/dist/chains/combine_documents/stuff.cjs +42 -0
- package/dist/chains/combine_documents/stuff.d.ts +28 -0
- package/dist/chains/combine_documents/stuff.js +38 -0
- package/dist/chains/conversational_retrieval_chain.cjs +3 -3
- package/dist/chains/conversational_retrieval_chain.js +1 -1
- package/dist/chains/history_aware_retriever.cjs +55 -0
- package/dist/chains/history_aware_retriever.d.ts +55 -0
- package/dist/chains/history_aware_retriever.js +51 -0
- package/dist/chains/retrieval.cjs +60 -0
- package/dist/chains/retrieval.d.ts +65 -0
- package/dist/chains/retrieval.js +56 -0
- package/dist/load/import_map.cjs +7 -3
- package/dist/load/import_map.d.ts +4 -0
- package/dist/load/import_map.js +4 -0
- package/dist/output_parsers/json.cjs +2 -78
- package/dist/output_parsers/json.d.ts +1 -1
- package/dist/output_parsers/json.js +1 -77
- package/dist/output_parsers/openai_functions.d.ts +1 -1
- package/dist/tools/retriever.cjs +17 -0
- package/dist/tools/retriever.d.ts +10 -0
- package/dist/tools/retriever.js +13 -0
- package/package.json +36 -4
- package/tools/retriever.cjs +1 -0
- package/tools/retriever.d.ts +1 -0
- package/tools/retriever.js +1 -0
- /package/dist/agents/{openai → openai_functions}/prompt.cjs +0 -0
- /package/dist/agents/{openai → openai_functions}/prompt.d.ts +0 -0
- /package/dist/agents/{openai → openai_functions}/prompt.js +0 -0
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.XMLAgent = void 0;
|
|
3
|
+
exports.createXmlAgent = exports.XMLAgent = void 0;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
4
5
|
const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
|
|
5
6
|
const chat_js_1 = require("../../prompts/chat.cjs");
|
|
6
7
|
const agent_js_1 = require("../agent.cjs");
|
|
7
8
|
const prompt_js_1 = require("./prompt.cjs");
|
|
8
9
|
const output_parser_js_1 = require("./output_parser.cjs");
|
|
10
|
+
const render_js_1 = require("../../tools/render.cjs");
|
|
11
|
+
const xml_js_1 = require("../format_scratchpad/xml.cjs");
|
|
9
12
|
/**
|
|
10
13
|
* Class that represents an agent that uses XML tags.
|
|
11
14
|
*/
|
|
@@ -101,3 +104,74 @@ class XMLAgent extends agent_js_1.BaseSingleActionAgent {
|
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
exports.XMLAgent = XMLAgent;
|
|
107
|
+
/**
|
|
108
|
+
* Create an agent that uses XML to format its logic.
|
|
109
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
110
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
111
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
112
|
+
* AgentAction or AgentFinish.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* import { AgentExecutor, createXmlAgent } from "langchain/agents";
|
|
117
|
+
* import { pull } from "langchain/hub";
|
|
118
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
119
|
+
*
|
|
120
|
+
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
121
|
+
*
|
|
122
|
+
* // Define the tools the agent will have access to.
|
|
123
|
+
* const tools = [...];
|
|
124
|
+
*
|
|
125
|
+
* // Get the prompt to use - you can modify this!
|
|
126
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
|
|
127
|
+
*
|
|
128
|
+
* const llm = new ChatAnthropic({
|
|
129
|
+
* temperature: 0,
|
|
130
|
+
* });
|
|
131
|
+
*
|
|
132
|
+
* const agent = await createXmlAgent({
|
|
133
|
+
* llm,
|
|
134
|
+
* tools,
|
|
135
|
+
* prompt,
|
|
136
|
+
* });
|
|
137
|
+
*
|
|
138
|
+
* const agentExecutor = new AgentExecutor({
|
|
139
|
+
* agent,
|
|
140
|
+
* tools,
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* const result = await agentExecutor.invoke({
|
|
144
|
+
* input: "what is LangChain?",
|
|
145
|
+
* });
|
|
146
|
+
*
|
|
147
|
+
* // With chat history
|
|
148
|
+
* const result2 = await agentExecutor.invoke({
|
|
149
|
+
* input: "what's my name?",
|
|
150
|
+
* // Notice that chat_history is a string, since this prompt is aimed at LLMs, not chat models
|
|
151
|
+
* chat_history: "Human: Hi! My name is Cob\nAI: Hello Cob! Nice to meet you",
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
async function createXmlAgent({ llm, tools, prompt, }) {
|
|
156
|
+
const missingVariables = ["tools", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
157
|
+
if (missingVariables.length > 0) {
|
|
158
|
+
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
159
|
+
}
|
|
160
|
+
const partialedPrompt = await prompt.partial({
|
|
161
|
+
tools: (0, render_js_1.renderTextDescription)(tools),
|
|
162
|
+
});
|
|
163
|
+
// TODO: Add .bind to core runnable interface.
|
|
164
|
+
const llmWithStop = llm.bind({
|
|
165
|
+
stop: ["</tool_input>", "</final_answer>"],
|
|
166
|
+
});
|
|
167
|
+
const agent = runnables_1.RunnableSequence.from([
|
|
168
|
+
runnables_1.RunnablePassthrough.assign({
|
|
169
|
+
agent_scratchpad: (input) => (0, xml_js_1.formatXml)(input.steps),
|
|
170
|
+
}),
|
|
171
|
+
partialedPrompt,
|
|
172
|
+
llmWithStop,
|
|
173
|
+
new output_parser_js_1.XMLAgentOutputParser(),
|
|
174
|
+
]);
|
|
175
|
+
return agent;
|
|
176
|
+
}
|
|
177
|
+
exports.createXmlAgent = createXmlAgent;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
2
2
|
import type { ToolInterface } from "@langchain/core/tools";
|
|
3
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
4
|
+
import type { BasePromptTemplate } from "@langchain/core/prompts";
|
|
3
5
|
import { LLMChain } from "../../chains/llm_chain.js";
|
|
4
6
|
import { AgentStep, AgentAction, AgentFinish, ChainValues } from "../../schema/index.js";
|
|
5
7
|
import { ChatPromptTemplate } from "../../prompts/chat.js";
|
|
@@ -44,3 +46,66 @@ export declare class XMLAgent extends BaseSingleActionAgent implements XMLAgentI
|
|
|
44
46
|
*/
|
|
45
47
|
static fromLLMAndTools(llm: BaseLanguageModelInterface, tools: ToolInterface[], args?: XMLAgentInput & Pick<AgentArgs, "callbacks">): XMLAgent;
|
|
46
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Params used by the createXmlAgent function.
|
|
51
|
+
*/
|
|
52
|
+
export type CreateXmlAgentParams = {
|
|
53
|
+
/** LLM to use for the agent. */
|
|
54
|
+
llm: BaseLanguageModelInterface;
|
|
55
|
+
/** Tools this agent has access to. */
|
|
56
|
+
tools: ToolInterface[];
|
|
57
|
+
/**
|
|
58
|
+
* The prompt to use. Must have input keys for
|
|
59
|
+
* `tools` and `agent_scratchpad`.
|
|
60
|
+
*/
|
|
61
|
+
prompt: BasePromptTemplate;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Create an agent that uses XML to format its logic.
|
|
65
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
66
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
67
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
68
|
+
* AgentAction or AgentFinish.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```typescript
|
|
72
|
+
* import { AgentExecutor, createXmlAgent } from "langchain/agents";
|
|
73
|
+
* import { pull } from "langchain/hub";
|
|
74
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
75
|
+
*
|
|
76
|
+
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
77
|
+
*
|
|
78
|
+
* // Define the tools the agent will have access to.
|
|
79
|
+
* const tools = [...];
|
|
80
|
+
*
|
|
81
|
+
* // Get the prompt to use - you can modify this!
|
|
82
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
|
|
83
|
+
*
|
|
84
|
+
* const llm = new ChatAnthropic({
|
|
85
|
+
* temperature: 0,
|
|
86
|
+
* });
|
|
87
|
+
*
|
|
88
|
+
* const agent = await createXmlAgent({
|
|
89
|
+
* llm,
|
|
90
|
+
* tools,
|
|
91
|
+
* prompt,
|
|
92
|
+
* });
|
|
93
|
+
*
|
|
94
|
+
* const agentExecutor = new AgentExecutor({
|
|
95
|
+
* agent,
|
|
96
|
+
* tools,
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* const result = await agentExecutor.invoke({
|
|
100
|
+
* input: "what is LangChain?",
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* // With chat history
|
|
104
|
+
* const result2 = await agentExecutor.invoke({
|
|
105
|
+
* input: "what's my name?",
|
|
106
|
+
* // Notice that chat_history is a string, since this prompt is aimed at LLMs, not chat models
|
|
107
|
+
* chat_history: "Human: Hi! My name is Cob\nAI: Hello Cob! Nice to meet you",
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function createXmlAgent({ llm, tools, prompt, }: CreateXmlAgentParams): Promise<RunnableSequence<Record<string, unknown>, AgentAction | AgentFinish>>;
|
package/dist/agents/xml/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
|
|
1
2
|
import { LLMChain } from "../../chains/llm_chain.js";
|
|
2
3
|
import { AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, } from "../../prompts/chat.js";
|
|
3
4
|
import { BaseSingleActionAgent } from "../agent.js";
|
|
4
5
|
import { AGENT_INSTRUCTIONS } from "./prompt.js";
|
|
5
6
|
import { XMLAgentOutputParser } from "./output_parser.js";
|
|
7
|
+
import { renderTextDescription } from "../../tools/render.js";
|
|
8
|
+
import { formatXml } from "../format_scratchpad/xml.js";
|
|
6
9
|
/**
|
|
7
10
|
* Class that represents an agent that uses XML tags.
|
|
8
11
|
*/
|
|
@@ -97,3 +100,73 @@ export class XMLAgent extends BaseSingleActionAgent {
|
|
|
97
100
|
});
|
|
98
101
|
}
|
|
99
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Create an agent that uses XML to format its logic.
|
|
105
|
+
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
106
|
+
* @returns A runnable sequence representing an agent. It takes as input all the same input
|
|
107
|
+
* variables as the prompt passed in does. It returns as output either an
|
|
108
|
+
* AgentAction or AgentFinish.
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```typescript
|
|
112
|
+
* import { AgentExecutor, createXmlAgent } from "langchain/agents";
|
|
113
|
+
* import { pull } from "langchain/hub";
|
|
114
|
+
* import type { PromptTemplate } from "@langchain/core/prompts";
|
|
115
|
+
*
|
|
116
|
+
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
117
|
+
*
|
|
118
|
+
* // Define the tools the agent will have access to.
|
|
119
|
+
* const tools = [...];
|
|
120
|
+
*
|
|
121
|
+
* // Get the prompt to use - you can modify this!
|
|
122
|
+
* const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
|
|
123
|
+
*
|
|
124
|
+
* const llm = new ChatAnthropic({
|
|
125
|
+
* temperature: 0,
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* const agent = await createXmlAgent({
|
|
129
|
+
* llm,
|
|
130
|
+
* tools,
|
|
131
|
+
* prompt,
|
|
132
|
+
* });
|
|
133
|
+
*
|
|
134
|
+
* const agentExecutor = new AgentExecutor({
|
|
135
|
+
* agent,
|
|
136
|
+
* tools,
|
|
137
|
+
* });
|
|
138
|
+
*
|
|
139
|
+
* const result = await agentExecutor.invoke({
|
|
140
|
+
* input: "what is LangChain?",
|
|
141
|
+
* });
|
|
142
|
+
*
|
|
143
|
+
* // With chat history
|
|
144
|
+
* const result2 = await agentExecutor.invoke({
|
|
145
|
+
* input: "what's my name?",
|
|
146
|
+
* // Notice that chat_history is a string, since this prompt is aimed at LLMs, not chat models
|
|
147
|
+
* chat_history: "Human: Hi! My name is Cob\nAI: Hello Cob! Nice to meet you",
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export async function createXmlAgent({ llm, tools, prompt, }) {
|
|
152
|
+
const missingVariables = ["tools", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
153
|
+
if (missingVariables.length > 0) {
|
|
154
|
+
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
155
|
+
}
|
|
156
|
+
const partialedPrompt = await prompt.partial({
|
|
157
|
+
tools: renderTextDescription(tools),
|
|
158
|
+
});
|
|
159
|
+
// TODO: Add .bind to core runnable interface.
|
|
160
|
+
const llmWithStop = llm.bind({
|
|
161
|
+
stop: ["</tool_input>", "</final_answer>"],
|
|
162
|
+
});
|
|
163
|
+
const agent = RunnableSequence.from([
|
|
164
|
+
RunnablePassthrough.assign({
|
|
165
|
+
agent_scratchpad: (input) => formatXml(input.steps),
|
|
166
|
+
}),
|
|
167
|
+
partialedPrompt,
|
|
168
|
+
llmWithStop,
|
|
169
|
+
new XMLAgentOutputParser(),
|
|
170
|
+
]);
|
|
171
|
+
return agent;
|
|
172
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDocuments = exports.DEFAULT_DOCUMENT_PROMPT = exports.INTERMEDIATE_STEPS_KEY = exports.DOCUMENTS_KEY = exports.DEFAULT_DOCUMENT_SEPARATOR = void 0;
|
|
4
|
+
const prompts_1 = require("@langchain/core/prompts");
|
|
5
|
+
exports.DEFAULT_DOCUMENT_SEPARATOR = "\n\n";
|
|
6
|
+
exports.DOCUMENTS_KEY = "context";
|
|
7
|
+
exports.INTERMEDIATE_STEPS_KEY = "intermediate_steps";
|
|
8
|
+
exports.DEFAULT_DOCUMENT_PROMPT =
|
|
9
|
+
/* #__PURE__ */ prompts_1.PromptTemplate.fromTemplate("{page_content}");
|
|
10
|
+
async function formatDocuments({ documentPrompt, documentSeparator, documents, config, }) {
|
|
11
|
+
const formattedDocs = await Promise.all(documents.map((document) => documentPrompt
|
|
12
|
+
.withConfig({ runName: "document_formatter" })
|
|
13
|
+
.invoke({ page_content: document.pageContent }, config)));
|
|
14
|
+
return formattedDocs.join(documentSeparator);
|
|
15
|
+
}
|
|
16
|
+
exports.formatDocuments = formatDocuments;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Document } from "@langchain/core/documents";
|
|
2
|
+
import { BasePromptTemplate, PromptTemplate } from "@langchain/core/prompts";
|
|
3
|
+
import { RunnableConfig } from "@langchain/core/runnables";
|
|
4
|
+
export declare const DEFAULT_DOCUMENT_SEPARATOR = "\n\n";
|
|
5
|
+
export declare const DOCUMENTS_KEY = "context";
|
|
6
|
+
export declare const INTERMEDIATE_STEPS_KEY = "intermediate_steps";
|
|
7
|
+
export declare const DEFAULT_DOCUMENT_PROMPT: PromptTemplate<import("@langchain/core/prompts").ParamsFromFString<"{page_content}">, any>;
|
|
8
|
+
export declare function formatDocuments({ documentPrompt, documentSeparator, documents, config, }: {
|
|
9
|
+
documentPrompt: BasePromptTemplate;
|
|
10
|
+
documentSeparator: string;
|
|
11
|
+
documents: Document[];
|
|
12
|
+
config?: RunnableConfig;
|
|
13
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PromptTemplate } from "@langchain/core/prompts";
|
|
2
|
+
export const DEFAULT_DOCUMENT_SEPARATOR = "\n\n";
|
|
3
|
+
export const DOCUMENTS_KEY = "context";
|
|
4
|
+
export const INTERMEDIATE_STEPS_KEY = "intermediate_steps";
|
|
5
|
+
export const DEFAULT_DOCUMENT_PROMPT =
|
|
6
|
+
/* #__PURE__ */ PromptTemplate.fromTemplate("{page_content}");
|
|
7
|
+
export async function formatDocuments({ documentPrompt, documentSeparator, documents, config, }) {
|
|
8
|
+
const formattedDocs = await Promise.all(documents.map((document) => documentPrompt
|
|
9
|
+
.withConfig({ runName: "document_formatter" })
|
|
10
|
+
.invoke({ page_content: document.pageContent }, config)));
|
|
11
|
+
return formattedDocs.join(documentSeparator);
|
|
12
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStuffDocumentsChain = void 0;
|
|
4
|
+
var stuff_js_1 = require("./stuff.cjs");
|
|
5
|
+
Object.defineProperty(exports, "createStuffDocumentsChain", { enumerable: true, get: function () { return stuff_js_1.createStuffDocumentsChain; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createStuffDocumentsChain } from "./stuff.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createStuffDocumentsChain } from "./stuff.js";
|
|
@@ -48,6 +48,10 @@ exports.splitListOfDocs = splitListOfDocs;
|
|
|
48
48
|
*/
|
|
49
49
|
async function collapseDocs(docs, combineDocumentFunc) {
|
|
50
50
|
const result = await combineDocumentFunc(docs);
|
|
51
|
+
return { pageContent: result, metadata: collapseDocsMetadata(docs) };
|
|
52
|
+
}
|
|
53
|
+
exports.collapseDocs = collapseDocs;
|
|
54
|
+
function collapseDocsMetadata(docs) {
|
|
51
55
|
const combinedMetadata = {};
|
|
52
56
|
for (const key in docs[0].metadata) {
|
|
53
57
|
if (key in docs[0].metadata) {
|
|
@@ -64,6 +68,5 @@ async function collapseDocs(docs, combineDocumentFunc) {
|
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
|
-
return
|
|
71
|
+
return combinedMetadata;
|
|
68
72
|
}
|
|
69
|
-
exports.collapseDocs = collapseDocs;
|
|
@@ -44,6 +44,9 @@ lengthFunc, tokenMax) {
|
|
|
44
44
|
*/
|
|
45
45
|
export async function collapseDocs(docs, combineDocumentFunc) {
|
|
46
46
|
const result = await combineDocumentFunc(docs);
|
|
47
|
+
return { pageContent: result, metadata: collapseDocsMetadata(docs) };
|
|
48
|
+
}
|
|
49
|
+
function collapseDocsMetadata(docs) {
|
|
47
50
|
const combinedMetadata = {};
|
|
48
51
|
for (const key in docs[0].metadata) {
|
|
49
52
|
if (key in docs[0].metadata) {
|
|
@@ -60,5 +63,5 @@ export async function collapseDocs(docs, combineDocumentFunc) {
|
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
}
|
|
63
|
-
return
|
|
66
|
+
return combinedMetadata;
|
|
64
67
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createStuffDocumentsChain = void 0;
|
|
4
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
6
|
+
const base_js_1 = require("./base.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Create a chain that passes a list of documents to a model.
|
|
9
|
+
*
|
|
10
|
+
* @param llm Language model to use for responding.
|
|
11
|
+
* @param prompt Prompt template. Must contain input variable "context", which will be
|
|
12
|
+
used for passing in the formatted documents.
|
|
13
|
+
* @param outputParser Output parser. Defaults to `StringOutputParser`.
|
|
14
|
+
* @param documentPrompt Prompt used for formatting each document into a string. Input
|
|
15
|
+
variables can be "page_content" or any metadata keys that are in all documents.
|
|
16
|
+
"page_content" will automatically retrieve the `Document.page_content`, and all
|
|
17
|
+
other inputs variables will be automatically retrieved from the `Document.metadata` dictionary. Default to a prompt that only contains `Document.page_content`.
|
|
18
|
+
* @param documentSeparator String separator to use between formatted document strings.
|
|
19
|
+
* @returns An LCEL `Runnable` chain.
|
|
20
|
+
Expects a dictionary as input with a list of `Document`s being passed under
|
|
21
|
+
the "context" key.
|
|
22
|
+
Return type depends on the `output_parser` used.
|
|
23
|
+
*/
|
|
24
|
+
async function createStuffDocumentsChain({ llm, prompt, outputParser = new output_parsers_1.StringOutputParser(), documentPrompt = base_js_1.DEFAULT_DOCUMENT_PROMPT, documentSeparator = base_js_1.DEFAULT_DOCUMENT_SEPARATOR, }) {
|
|
25
|
+
if (!prompt.inputVariables.includes(base_js_1.DOCUMENTS_KEY)) {
|
|
26
|
+
throw new Error(`Prompt must include a "${base_js_1.DOCUMENTS_KEY}" variable`);
|
|
27
|
+
}
|
|
28
|
+
return runnables_1.RunnableSequence.from([
|
|
29
|
+
runnables_1.RunnablePassthrough.assign({
|
|
30
|
+
[base_js_1.DOCUMENTS_KEY]: new runnables_1.RunnablePick(base_js_1.DOCUMENTS_KEY).pipe((documents, metadata) => (0, base_js_1.formatDocuments)({
|
|
31
|
+
documents,
|
|
32
|
+
documentPrompt,
|
|
33
|
+
documentSeparator,
|
|
34
|
+
config: metadata?.config,
|
|
35
|
+
})),
|
|
36
|
+
}),
|
|
37
|
+
prompt,
|
|
38
|
+
llm,
|
|
39
|
+
outputParser,
|
|
40
|
+
], "stuff_documents_chain");
|
|
41
|
+
}
|
|
42
|
+
exports.createStuffDocumentsChain = createStuffDocumentsChain;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LanguageModelLike } from "@langchain/core/language_models/base";
|
|
2
|
+
import { BaseOutputParser } from "@langchain/core/output_parsers";
|
|
3
|
+
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
4
|
+
import { RunnableSequence } from "@langchain/core/runnables";
|
|
5
|
+
/**
|
|
6
|
+
* Create a chain that passes a list of documents to a model.
|
|
7
|
+
*
|
|
8
|
+
* @param llm Language model to use for responding.
|
|
9
|
+
* @param prompt Prompt template. Must contain input variable "context", which will be
|
|
10
|
+
used for passing in the formatted documents.
|
|
11
|
+
* @param outputParser Output parser. Defaults to `StringOutputParser`.
|
|
12
|
+
* @param documentPrompt Prompt used for formatting each document into a string. Input
|
|
13
|
+
variables can be "page_content" or any metadata keys that are in all documents.
|
|
14
|
+
"page_content" will automatically retrieve the `Document.page_content`, and all
|
|
15
|
+
other inputs variables will be automatically retrieved from the `Document.metadata` dictionary. Default to a prompt that only contains `Document.page_content`.
|
|
16
|
+
* @param documentSeparator String separator to use between formatted document strings.
|
|
17
|
+
* @returns An LCEL `Runnable` chain.
|
|
18
|
+
Expects a dictionary as input with a list of `Document`s being passed under
|
|
19
|
+
the "context" key.
|
|
20
|
+
Return type depends on the `output_parser` used.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createStuffDocumentsChain<RunOutput = string>({ llm, prompt, outputParser, documentPrompt, documentSeparator, }: {
|
|
23
|
+
llm: LanguageModelLike;
|
|
24
|
+
prompt: BasePromptTemplate;
|
|
25
|
+
outputParser?: BaseOutputParser<RunOutput>;
|
|
26
|
+
documentPrompt?: BasePromptTemplate;
|
|
27
|
+
documentSeparator?: string;
|
|
28
|
+
}): Promise<RunnableSequence<Record<string, unknown>, Exclude<RunOutput, Error>>>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { StringOutputParser, } from "@langchain/core/output_parsers";
|
|
2
|
+
import { RunnablePassthrough, RunnablePick, RunnableSequence, } from "@langchain/core/runnables";
|
|
3
|
+
import { DEFAULT_DOCUMENT_PROMPT, DEFAULT_DOCUMENT_SEPARATOR, DOCUMENTS_KEY, formatDocuments, } from "./base.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create a chain that passes a list of documents to a model.
|
|
6
|
+
*
|
|
7
|
+
* @param llm Language model to use for responding.
|
|
8
|
+
* @param prompt Prompt template. Must contain input variable "context", which will be
|
|
9
|
+
used for passing in the formatted documents.
|
|
10
|
+
* @param outputParser Output parser. Defaults to `StringOutputParser`.
|
|
11
|
+
* @param documentPrompt Prompt used for formatting each document into a string. Input
|
|
12
|
+
variables can be "page_content" or any metadata keys that are in all documents.
|
|
13
|
+
"page_content" will automatically retrieve the `Document.page_content`, and all
|
|
14
|
+
other inputs variables will be automatically retrieved from the `Document.metadata` dictionary. Default to a prompt that only contains `Document.page_content`.
|
|
15
|
+
* @param documentSeparator String separator to use between formatted document strings.
|
|
16
|
+
* @returns An LCEL `Runnable` chain.
|
|
17
|
+
Expects a dictionary as input with a list of `Document`s being passed under
|
|
18
|
+
the "context" key.
|
|
19
|
+
Return type depends on the `output_parser` used.
|
|
20
|
+
*/
|
|
21
|
+
export async function createStuffDocumentsChain({ llm, prompt, outputParser = new StringOutputParser(), documentPrompt = DEFAULT_DOCUMENT_PROMPT, documentSeparator = DEFAULT_DOCUMENT_SEPARATOR, }) {
|
|
22
|
+
if (!prompt.inputVariables.includes(DOCUMENTS_KEY)) {
|
|
23
|
+
throw new Error(`Prompt must include a "${DOCUMENTS_KEY}" variable`);
|
|
24
|
+
}
|
|
25
|
+
return RunnableSequence.from([
|
|
26
|
+
RunnablePassthrough.assign({
|
|
27
|
+
[DOCUMENTS_KEY]: new RunnablePick(DOCUMENTS_KEY).pipe((documents, metadata) => formatDocuments({
|
|
28
|
+
documents,
|
|
29
|
+
documentPrompt,
|
|
30
|
+
documentSeparator,
|
|
31
|
+
config: metadata?.config,
|
|
32
|
+
})),
|
|
33
|
+
}),
|
|
34
|
+
prompt,
|
|
35
|
+
llm,
|
|
36
|
+
outputParser,
|
|
37
|
+
], "stuff_documents_chain");
|
|
38
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConversationalRetrievalQAChain = void 0;
|
|
4
|
-
const
|
|
4
|
+
const prompts_1 = require("@langchain/core/prompts");
|
|
5
5
|
const index_js_1 = require("../schema/index.cjs");
|
|
6
6
|
const base_js_1 = require("./base.cjs");
|
|
7
7
|
const llm_chain_js_1 = require("./llm_chain.cjs");
|
|
@@ -218,11 +218,11 @@ class ConversationalRetrievalQAChain extends base_js_1.BaseChain {
|
|
|
218
218
|
const { questionGeneratorTemplate, qaTemplate, qaChainOptions = {
|
|
219
219
|
type: "stuff",
|
|
220
220
|
prompt: qaTemplate
|
|
221
|
-
?
|
|
221
|
+
? prompts_1.PromptTemplate.fromTemplate(qaTemplate)
|
|
222
222
|
: undefined,
|
|
223
223
|
}, questionGeneratorChainOptions, verbose, ...rest } = options;
|
|
224
224
|
const qaChain = (0, load_js_1.loadQAChain)(llm, qaChainOptions);
|
|
225
|
-
const questionGeneratorChainPrompt =
|
|
225
|
+
const questionGeneratorChainPrompt = prompts_1.PromptTemplate.fromTemplate(questionGeneratorChainOptions?.template ??
|
|
226
226
|
questionGeneratorTemplate ??
|
|
227
227
|
question_generator_template);
|
|
228
228
|
const questionGeneratorChain = new llm_chain_js_1.LLMChain({
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createHistoryAwareRetriever = void 0;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
5
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
6
|
+
/**
|
|
7
|
+
* Create a chain that takes conversation history and returns documents.
|
|
8
|
+
* If there is no `chat_history`, then the `input` is just passed directly to the
|
|
9
|
+
* retriever. If there is `chat_history`, then the prompt and LLM will be used
|
|
10
|
+
* to generate a search query. That search query is then passed to the retriever.
|
|
11
|
+
* @param {CreateHistoryAwareRetriever} params
|
|
12
|
+
* @returns An LCEL Runnable. The runnable input must take in `input`, and if there
|
|
13
|
+
* is chat history should take it in the form of `chat_history`.
|
|
14
|
+
* The Runnable output is a list of Documents
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // yarn add langchain @langchain/openai
|
|
18
|
+
*
|
|
19
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
20
|
+
* import { pull } from "langchain/hub";
|
|
21
|
+
* import { createRetrievalChain } from "langchain/chains/retrieval";
|
|
22
|
+
* import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
|
|
23
|
+
*
|
|
24
|
+
* const rephrasePrompt = await pull("langchain-ai/chat-langchain-rephrase");
|
|
25
|
+
* const llm = new ChatOpenAI({});
|
|
26
|
+
* const retriever = ...
|
|
27
|
+
* const historyAwareRetrieverChain = await createHistoryAwareRetriever({
|
|
28
|
+
* llm,
|
|
29
|
+
* retriever,
|
|
30
|
+
* rephrasePrompt,
|
|
31
|
+
* });
|
|
32
|
+
* const result = await chain.invoke({"input": "...", "chat_history": [] })
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async function createHistoryAwareRetriever({ llm, retriever, rephrasePrompt, }) {
|
|
36
|
+
if (!rephrasePrompt.inputVariables.includes("input")) {
|
|
37
|
+
throw new Error(`Expected "input" to be a prompt variable, but got ${JSON.stringify(rephrasePrompt.inputVariables)}`);
|
|
38
|
+
}
|
|
39
|
+
const retrieveDocuments = runnables_1.RunnableBranch.from([
|
|
40
|
+
[
|
|
41
|
+
(input) => !input.chat_history || input.chat_history.length === 0,
|
|
42
|
+
runnables_1.RunnableSequence.from([(input) => input.input, retriever]),
|
|
43
|
+
],
|
|
44
|
+
runnables_1.RunnableSequence.from([
|
|
45
|
+
rephrasePrompt,
|
|
46
|
+
llm,
|
|
47
|
+
new output_parsers_1.StringOutputParser(),
|
|
48
|
+
retriever,
|
|
49
|
+
]),
|
|
50
|
+
]).withConfig({
|
|
51
|
+
runName: "history_aware_retriever",
|
|
52
|
+
});
|
|
53
|
+
return retrieveDocuments;
|
|
54
|
+
}
|
|
55
|
+
exports.createHistoryAwareRetriever = createHistoryAwareRetriever;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LanguageModelLike } from "@langchain/core/language_models/base";
|
|
2
|
+
import { type RunnableInterface } from "@langchain/core/runnables";
|
|
3
|
+
import { type BasePromptTemplate } from "@langchain/core/prompts";
|
|
4
|
+
import type { DocumentInterface } from "@langchain/core/documents";
|
|
5
|
+
import type { BaseMessage } from "../schema/index.js";
|
|
6
|
+
/**
|
|
7
|
+
* Params for the createHistoryAwareRetriever method.
|
|
8
|
+
*/
|
|
9
|
+
export type CreateHistoryAwareRetriever = {
|
|
10
|
+
/**
|
|
11
|
+
* Language model to use for generating a search term given chat history.
|
|
12
|
+
*/
|
|
13
|
+
llm: LanguageModelLike;
|
|
14
|
+
/**
|
|
15
|
+
* RetrieverLike object that takes a string as input and outputs a list of Documents.
|
|
16
|
+
*/
|
|
17
|
+
retriever: RunnableInterface<string, DocumentInterface[]>;
|
|
18
|
+
/**
|
|
19
|
+
* The prompt used to generate the search query for the retriever.
|
|
20
|
+
*/
|
|
21
|
+
rephrasePrompt: BasePromptTemplate;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Create a chain that takes conversation history and returns documents.
|
|
25
|
+
* If there is no `chat_history`, then the `input` is just passed directly to the
|
|
26
|
+
* retriever. If there is `chat_history`, then the prompt and LLM will be used
|
|
27
|
+
* to generate a search query. That search query is then passed to the retriever.
|
|
28
|
+
* @param {CreateHistoryAwareRetriever} params
|
|
29
|
+
* @returns An LCEL Runnable. The runnable input must take in `input`, and if there
|
|
30
|
+
* is chat history should take it in the form of `chat_history`.
|
|
31
|
+
* The Runnable output is a list of Documents
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // yarn add langchain @langchain/openai
|
|
35
|
+
*
|
|
36
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
37
|
+
* import { pull } from "langchain/hub";
|
|
38
|
+
* import { createRetrievalChain } from "langchain/chains/retrieval";
|
|
39
|
+
* import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
|
|
40
|
+
*
|
|
41
|
+
* const rephrasePrompt = await pull("langchain-ai/chat-langchain-rephrase");
|
|
42
|
+
* const llm = new ChatOpenAI({});
|
|
43
|
+
* const retriever = ...
|
|
44
|
+
* const historyAwareRetrieverChain = await createHistoryAwareRetriever({
|
|
45
|
+
* llm,
|
|
46
|
+
* retriever,
|
|
47
|
+
* rephrasePrompt,
|
|
48
|
+
* });
|
|
49
|
+
* const result = await chain.invoke({"input": "...", "chat_history": [] })
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function createHistoryAwareRetriever({ llm, retriever, rephrasePrompt, }: CreateHistoryAwareRetriever): Promise<RunnableInterface<{
|
|
53
|
+
input: string;
|
|
54
|
+
chat_history: string | BaseMessage[];
|
|
55
|
+
}, DocumentInterface[]>>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { RunnableSequence, RunnableBranch, } from "@langchain/core/runnables";
|
|
2
|
+
import { StringOutputParser } from "@langchain/core/output_parsers";
|
|
3
|
+
/**
|
|
4
|
+
* Create a chain that takes conversation history and returns documents.
|
|
5
|
+
* If there is no `chat_history`, then the `input` is just passed directly to the
|
|
6
|
+
* retriever. If there is `chat_history`, then the prompt and LLM will be used
|
|
7
|
+
* to generate a search query. That search query is then passed to the retriever.
|
|
8
|
+
* @param {CreateHistoryAwareRetriever} params
|
|
9
|
+
* @returns An LCEL Runnable. The runnable input must take in `input`, and if there
|
|
10
|
+
* is chat history should take it in the form of `chat_history`.
|
|
11
|
+
* The Runnable output is a list of Documents
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // yarn add langchain @langchain/openai
|
|
15
|
+
*
|
|
16
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
17
|
+
* import { pull } from "langchain/hub";
|
|
18
|
+
* import { createRetrievalChain } from "langchain/chains/retrieval";
|
|
19
|
+
* import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
|
|
20
|
+
*
|
|
21
|
+
* const rephrasePrompt = await pull("langchain-ai/chat-langchain-rephrase");
|
|
22
|
+
* const llm = new ChatOpenAI({});
|
|
23
|
+
* const retriever = ...
|
|
24
|
+
* const historyAwareRetrieverChain = await createHistoryAwareRetriever({
|
|
25
|
+
* llm,
|
|
26
|
+
* retriever,
|
|
27
|
+
* rephrasePrompt,
|
|
28
|
+
* });
|
|
29
|
+
* const result = await chain.invoke({"input": "...", "chat_history": [] })
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export async function createHistoryAwareRetriever({ llm, retriever, rephrasePrompt, }) {
|
|
33
|
+
if (!rephrasePrompt.inputVariables.includes("input")) {
|
|
34
|
+
throw new Error(`Expected "input" to be a prompt variable, but got ${JSON.stringify(rephrasePrompt.inputVariables)}`);
|
|
35
|
+
}
|
|
36
|
+
const retrieveDocuments = RunnableBranch.from([
|
|
37
|
+
[
|
|
38
|
+
(input) => !input.chat_history || input.chat_history.length === 0,
|
|
39
|
+
RunnableSequence.from([(input) => input.input, retriever]),
|
|
40
|
+
],
|
|
41
|
+
RunnableSequence.from([
|
|
42
|
+
rephrasePrompt,
|
|
43
|
+
llm,
|
|
44
|
+
new StringOutputParser(),
|
|
45
|
+
retriever,
|
|
46
|
+
]),
|
|
47
|
+
]).withConfig({
|
|
48
|
+
runName: "history_aware_retriever",
|
|
49
|
+
});
|
|
50
|
+
return retrieveDocuments;
|
|
51
|
+
}
|