langchain 0.0.211 → 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 +28 -9
- package/dist/agents/executor.d.ts +4 -3
- package/dist/agents/executor.js +27 -8
- 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/index.cjs +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createRetrievalChain = void 0;
|
|
4
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
5
|
+
function isBaseRetriever(x) {
|
|
6
|
+
return (!!x &&
|
|
7
|
+
typeof x.getRelevantDocuments === "function");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Create a retrieval chain that retrieves documents and then passes them on.
|
|
11
|
+
* @param {CreateRetrievalChainParams} params A params object
|
|
12
|
+
* containing a retriever and a combineDocsChain.
|
|
13
|
+
* @returns An LCEL Runnable which returns a an object
|
|
14
|
+
* containing at least `context` and `answer` keys.
|
|
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 retrievalQAChatPrompt = await pull("langchain-ai/retrieval-qa-chat");
|
|
25
|
+
* const llm = new ChatOpenAI({});
|
|
26
|
+
* const retriever = ...
|
|
27
|
+
* const combineDocsChain = await createStuffDocumentsChain(...);
|
|
28
|
+
* const retrievalChain = await createRetrievalChain({
|
|
29
|
+
* retriever,
|
|
30
|
+
* combineDocsChain,
|
|
31
|
+
* });
|
|
32
|
+
* const response = await chain.invoke({ input: "..." });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
async function createRetrievalChain({ retriever, combineDocsChain, }) {
|
|
36
|
+
let retrieveDocumentsChain;
|
|
37
|
+
if (isBaseRetriever(retriever)) {
|
|
38
|
+
retrieveDocumentsChain = runnables_1.RunnableSequence.from([
|
|
39
|
+
(input) => input.input,
|
|
40
|
+
retriever,
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// TODO: Fix typing by adding withConfig to core RunnableInterface
|
|
45
|
+
retrieveDocumentsChain = retriever;
|
|
46
|
+
}
|
|
47
|
+
const retrievalChain = runnables_1.RunnableSequence.from([
|
|
48
|
+
runnables_1.RunnablePassthrough.assign({
|
|
49
|
+
context: retrieveDocumentsChain.withConfig({
|
|
50
|
+
runName: "retrieve_documents",
|
|
51
|
+
}),
|
|
52
|
+
chat_history: (input) => input.chat_history ?? [],
|
|
53
|
+
}),
|
|
54
|
+
runnables_1.RunnablePassthrough.assign({
|
|
55
|
+
answer: combineDocsChain,
|
|
56
|
+
}),
|
|
57
|
+
]).withConfig({ runName: "retrieval_chain" });
|
|
58
|
+
return retrievalChain;
|
|
59
|
+
}
|
|
60
|
+
exports.createRetrievalChain = createRetrievalChain;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { BaseRetrieverInterface } from "@langchain/core/retrievers";
|
|
2
|
+
import { type RunnableInterface } from "@langchain/core/runnables";
|
|
3
|
+
import type { BaseMessage } from "@langchain/core/messages";
|
|
4
|
+
import type { DocumentInterface } from "@langchain/core/documents";
|
|
5
|
+
/**
|
|
6
|
+
* Parameters for the createRetrievalChain method.
|
|
7
|
+
*/
|
|
8
|
+
export type CreateRetrievalChainParams<RunOutput> = {
|
|
9
|
+
/**
|
|
10
|
+
* Retriever-like object that returns list of documents. Should
|
|
11
|
+
* either be a subclass of BaseRetriever or a Runnable that returns
|
|
12
|
+
* a list of documents. If a subclass of BaseRetriever, then it
|
|
13
|
+
* is expected that an `input` key be passed in - this is what
|
|
14
|
+
* is will be used to pass into the retriever. If this is NOT a
|
|
15
|
+
* subclass of BaseRetriever, then all the inputs will be passed
|
|
16
|
+
* into this runnable, meaning that runnable should take a object
|
|
17
|
+
* as input.
|
|
18
|
+
*/
|
|
19
|
+
retriever: BaseRetrieverInterface | RunnableInterface<Record<string, any>, DocumentInterface[]>;
|
|
20
|
+
/**
|
|
21
|
+
* Runnable that takes inputs and produces a string output.
|
|
22
|
+
* The inputs to this will be any original inputs to this chain, a new
|
|
23
|
+
* context key with the retrieved documents, and chat_history (if not present
|
|
24
|
+
* in the inputs) with a value of `[]` (to easily enable conversational
|
|
25
|
+
* retrieval).
|
|
26
|
+
*/
|
|
27
|
+
combineDocsChain: RunnableInterface<Record<string, any>, RunOutput>;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Create a retrieval chain that retrieves documents and then passes them on.
|
|
31
|
+
* @param {CreateRetrievalChainParams} params A params object
|
|
32
|
+
* containing a retriever and a combineDocsChain.
|
|
33
|
+
* @returns An LCEL Runnable which returns a an object
|
|
34
|
+
* containing at least `context` and `answer` keys.
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // yarn add langchain @langchain/openai
|
|
38
|
+
*
|
|
39
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
40
|
+
* import { pull } from "langchain/hub";
|
|
41
|
+
* import { createRetrievalChain } from "langchain/chains/retrieval";
|
|
42
|
+
* import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
|
|
43
|
+
*
|
|
44
|
+
* const retrievalQAChatPrompt = await pull("langchain-ai/retrieval-qa-chat");
|
|
45
|
+
* const llm = new ChatOpenAI({});
|
|
46
|
+
* const retriever = ...
|
|
47
|
+
* const combineDocsChain = await createStuffDocumentsChain(...);
|
|
48
|
+
* const retrievalChain = await createRetrievalChain({
|
|
49
|
+
* retriever,
|
|
50
|
+
* combineDocsChain,
|
|
51
|
+
* });
|
|
52
|
+
* const response = await chain.invoke({ input: "..." });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function createRetrievalChain<RunOutput>({ retriever, combineDocsChain, }: CreateRetrievalChainParams<RunOutput>): Promise<RunnableInterface<{
|
|
56
|
+
input: string;
|
|
57
|
+
chat_history?: BaseMessage[] | string;
|
|
58
|
+
} & {
|
|
59
|
+
[key: string]: unknown;
|
|
60
|
+
}, {
|
|
61
|
+
context: string;
|
|
62
|
+
answer: RunOutput;
|
|
63
|
+
} & {
|
|
64
|
+
[key: string]: unknown;
|
|
65
|
+
}>>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { RunnableSequence, RunnablePassthrough, } from "@langchain/core/runnables";
|
|
2
|
+
function isBaseRetriever(x) {
|
|
3
|
+
return (!!x &&
|
|
4
|
+
typeof x.getRelevantDocuments === "function");
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Create a retrieval chain that retrieves documents and then passes them on.
|
|
8
|
+
* @param {CreateRetrievalChainParams} params A params object
|
|
9
|
+
* containing a retriever and a combineDocsChain.
|
|
10
|
+
* @returns An LCEL Runnable which returns a an object
|
|
11
|
+
* containing at least `context` and `answer` keys.
|
|
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 retrievalQAChatPrompt = await pull("langchain-ai/retrieval-qa-chat");
|
|
22
|
+
* const llm = new ChatOpenAI({});
|
|
23
|
+
* const retriever = ...
|
|
24
|
+
* const combineDocsChain = await createStuffDocumentsChain(...);
|
|
25
|
+
* const retrievalChain = await createRetrievalChain({
|
|
26
|
+
* retriever,
|
|
27
|
+
* combineDocsChain,
|
|
28
|
+
* });
|
|
29
|
+
* const response = await chain.invoke({ input: "..." });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export async function createRetrievalChain({ retriever, combineDocsChain, }) {
|
|
33
|
+
let retrieveDocumentsChain;
|
|
34
|
+
if (isBaseRetriever(retriever)) {
|
|
35
|
+
retrieveDocumentsChain = RunnableSequence.from([
|
|
36
|
+
(input) => input.input,
|
|
37
|
+
retriever,
|
|
38
|
+
]);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// TODO: Fix typing by adding withConfig to core RunnableInterface
|
|
42
|
+
retrieveDocumentsChain = retriever;
|
|
43
|
+
}
|
|
44
|
+
const retrievalChain = RunnableSequence.from([
|
|
45
|
+
RunnablePassthrough.assign({
|
|
46
|
+
context: retrieveDocumentsChain.withConfig({
|
|
47
|
+
runName: "retrieve_documents",
|
|
48
|
+
}),
|
|
49
|
+
chat_history: (input) => input.chat_history ?? [],
|
|
50
|
+
}),
|
|
51
|
+
RunnablePassthrough.assign({
|
|
52
|
+
answer: combineDocsChain,
|
|
53
|
+
}),
|
|
54
|
+
]).withConfig({ runName: "retrieval_chain" });
|
|
55
|
+
return retrievalChain;
|
|
56
|
+
}
|
package/dist/index.cjs
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
package/dist/load/import_map.cjs
CHANGED
|
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
return result;
|
|
25
25
|
};
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
-
exports.
|
|
28
|
-
exports.
|
|
29
|
-
exports.runnables__remote = exports.runnables = exports.evaluation = exports.experimental__masking = exports.experimental__chains__violation_of_expectations = exports.experimental__chat_models__ollama_functions = exports.experimental__chat_models__bittensor = void 0;
|
|
27
|
+
exports.document_loaders__base = exports.document = exports.memory = exports.text_splitter = exports.vectorstores__xata = exports.vectorstores__vectara = exports.vectorstores__prisma = exports.vectorstores__memory = exports.vectorstores__base = exports.prompts = exports.llms__fake = exports.llms__yandex = exports.llms__fireworks = exports.llms__ollama = exports.llms__cloudflare_workersai = exports.llms__aleph_alpha = exports.llms__ai21 = exports.llms__openai = exports.llms__base = exports.embeddings__voyage = exports.embeddings__minimax = exports.embeddings__openai = exports.embeddings__ollama = exports.embeddings__fake = exports.embeddings__cache_backed = exports.embeddings__base = exports.chains__retrieval = exports.chains__openai_functions = exports.chains__history_aware_retriever = exports.chains__combine_documents__reduce = exports.chains__combine_documents = exports.chains = exports.tools__google_places = exports.tools__retriever = exports.tools__render = exports.tools__connery = exports.tools = exports.base_language = exports.agents__openai__output_parser = exports.agents__xml__output_parser = exports.agents__react__output_parser = exports.agents__format_scratchpad__log_to_message = exports.agents__format_scratchpad__xml = exports.agents__format_scratchpad__log = exports.agents__format_scratchpad__openai_tools = exports.agents__format_scratchpad = exports.agents__toolkits__connery = exports.agents__toolkits = exports.agents = exports.load__serializable = void 0;
|
|
28
|
+
exports.experimental__openai_assistant = exports.experimental__autogpt = exports.util__time = exports.util__math = exports.util__document = exports.storage__in_memory = exports.storage__encoder_backed = exports.stores__message__in_memory = exports.stores__file__in_memory = exports.stores__doc__in_memory = exports.cache = exports.retrievers__vespa = exports.retrievers__score_threshold = exports.retrievers__hyde = exports.retrievers__document_compressors__embeddings_filter = exports.retrievers__document_compressors__chain_extract = exports.retrievers__time_weighted = exports.retrievers__tavily_search_api = exports.retrievers__parent_document = exports.retrievers__multi_vector = exports.retrievers__multi_query = exports.retrievers__document_compressors = exports.retrievers__contextual_compression = exports.retrievers__databerry = exports.retrievers__chaindesk = exports.retrievers__remote = exports.output_parsers = exports.callbacks = exports.schema__storage = exports.schema__runnable = exports.schema__retriever = exports.schema__query_constructor = exports.schema__prompt_template = exports.schema__output_parser = exports.schema__document = exports.schema = exports.chat_models__fake = exports.chat_models__yandex = exports.chat_models__minimax = exports.chat_models__ollama = exports.chat_models__baiduwenxin = exports.chat_models__fireworks = exports.chat_models__cloudflare_workersai = exports.chat_models__anthropic = exports.chat_models__openai = exports.chat_models__base = exports.document_transformers__openai_functions = exports.document_loaders__web__sort_xyz_blockchain = exports.document_loaders__web__serpapi = exports.document_loaders__web__searchapi = void 0;
|
|
29
|
+
exports.runnables__remote = exports.runnables = exports.evaluation = exports.experimental__masking = exports.experimental__chains__violation_of_expectations = exports.experimental__chat_models__ollama_functions = exports.experimental__chat_models__bittensor = exports.experimental__plan_and_execute = exports.experimental__generative_agents = exports.experimental__babyagi = exports.experimental__openai_files = void 0;
|
|
30
30
|
exports.load__serializable = __importStar(require("../load/serializable.cjs"));
|
|
31
31
|
exports.agents = __importStar(require("../agents/index.cjs"));
|
|
32
32
|
exports.agents__toolkits = __importStar(require("../agents/toolkits/index.cjs"));
|
|
@@ -43,10 +43,14 @@ exports.base_language = __importStar(require("../base_language/index.cjs"));
|
|
|
43
43
|
exports.tools = __importStar(require("../tools/index.cjs"));
|
|
44
44
|
exports.tools__connery = __importStar(require("../tools/connery.cjs"));
|
|
45
45
|
exports.tools__render = __importStar(require("../tools/render.cjs"));
|
|
46
|
+
exports.tools__retriever = __importStar(require("../tools/retriever.cjs"));
|
|
46
47
|
exports.tools__google_places = __importStar(require("../tools/google_places.cjs"));
|
|
47
48
|
exports.chains = __importStar(require("../chains/index.cjs"));
|
|
49
|
+
exports.chains__combine_documents = __importStar(require("../chains/combine_documents/index.cjs"));
|
|
48
50
|
exports.chains__combine_documents__reduce = __importStar(require("../chains/combine_documents/reduce.cjs"));
|
|
51
|
+
exports.chains__history_aware_retriever = __importStar(require("../chains/history_aware_retriever.cjs"));
|
|
49
52
|
exports.chains__openai_functions = __importStar(require("../chains/openai_functions/index.cjs"));
|
|
53
|
+
exports.chains__retrieval = __importStar(require("../chains/retrieval.cjs"));
|
|
50
54
|
exports.embeddings__base = __importStar(require("../embeddings/base.cjs"));
|
|
51
55
|
exports.embeddings__cache_backed = __importStar(require("../embeddings/cache_backed.cjs"));
|
|
52
56
|
exports.embeddings__fake = __importStar(require("../embeddings/fake.cjs"));
|
|
@@ -14,10 +14,14 @@ export * as base_language from "../base_language/index.js";
|
|
|
14
14
|
export * as tools from "../tools/index.js";
|
|
15
15
|
export * as tools__connery from "../tools/connery.js";
|
|
16
16
|
export * as tools__render from "../tools/render.js";
|
|
17
|
+
export * as tools__retriever from "../tools/retriever.js";
|
|
17
18
|
export * as tools__google_places from "../tools/google_places.js";
|
|
18
19
|
export * as chains from "../chains/index.js";
|
|
20
|
+
export * as chains__combine_documents from "../chains/combine_documents/index.js";
|
|
19
21
|
export * as chains__combine_documents__reduce from "../chains/combine_documents/reduce.js";
|
|
22
|
+
export * as chains__history_aware_retriever from "../chains/history_aware_retriever.js";
|
|
20
23
|
export * as chains__openai_functions from "../chains/openai_functions/index.js";
|
|
24
|
+
export * as chains__retrieval from "../chains/retrieval.js";
|
|
21
25
|
export * as embeddings__base from "../embeddings/base.js";
|
|
22
26
|
export * as embeddings__cache_backed from "../embeddings/cache_backed.js";
|
|
23
27
|
export * as embeddings__fake from "../embeddings/fake.js";
|
package/dist/load/import_map.js
CHANGED
|
@@ -15,10 +15,14 @@ export * as base_language from "../base_language/index.js";
|
|
|
15
15
|
export * as tools from "../tools/index.js";
|
|
16
16
|
export * as tools__connery from "../tools/connery.js";
|
|
17
17
|
export * as tools__render from "../tools/render.js";
|
|
18
|
+
export * as tools__retriever from "../tools/retriever.js";
|
|
18
19
|
export * as tools__google_places from "../tools/google_places.js";
|
|
19
20
|
export * as chains from "../chains/index.js";
|
|
21
|
+
export * as chains__combine_documents from "../chains/combine_documents/index.js";
|
|
20
22
|
export * as chains__combine_documents__reduce from "../chains/combine_documents/reduce.js";
|
|
23
|
+
export * as chains__history_aware_retriever from "../chains/history_aware_retriever.js";
|
|
21
24
|
export * as chains__openai_functions from "../chains/openai_functions/index.js";
|
|
25
|
+
export * as chains__retrieval from "../chains/retrieval.js";
|
|
22
26
|
export * as embeddings__base from "../embeddings/base.js";
|
|
23
27
|
export * as embeddings__cache_backed from "../embeddings/cache_backed.js";
|
|
24
28
|
export * as embeddings__fake from "../embeddings/fake.js";
|
|
@@ -1,81 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
|
|
3
|
-
// MIT License
|
|
4
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
3
|
exports.parsePartialJson = void 0;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (typeof s === "undefined") {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
// Attempt to parse the string as-is.
|
|
12
|
-
try {
|
|
13
|
-
return JSON.parse(s);
|
|
14
|
-
}
|
|
15
|
-
catch (error) {
|
|
16
|
-
// Pass
|
|
17
|
-
}
|
|
18
|
-
// Initialize variables.
|
|
19
|
-
let new_s = "";
|
|
20
|
-
const stack = [];
|
|
21
|
-
let isInsideString = false;
|
|
22
|
-
let escaped = false;
|
|
23
|
-
// Process each character in the string one at a time.
|
|
24
|
-
for (let char of s) {
|
|
25
|
-
if (isInsideString) {
|
|
26
|
-
if (char === '"' && !escaped) {
|
|
27
|
-
isInsideString = false;
|
|
28
|
-
}
|
|
29
|
-
else if (char === "\n" && !escaped) {
|
|
30
|
-
char = "\\n"; // Replace the newline character with the escape sequence.
|
|
31
|
-
}
|
|
32
|
-
else if (char === "\\") {
|
|
33
|
-
escaped = !escaped;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
escaped = false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
if (char === '"') {
|
|
41
|
-
isInsideString = true;
|
|
42
|
-
escaped = false;
|
|
43
|
-
}
|
|
44
|
-
else if (char === "{") {
|
|
45
|
-
stack.push("}");
|
|
46
|
-
}
|
|
47
|
-
else if (char === "[") {
|
|
48
|
-
stack.push("]");
|
|
49
|
-
}
|
|
50
|
-
else if (char === "}" || char === "]") {
|
|
51
|
-
if (stack && stack[stack.length - 1] === char) {
|
|
52
|
-
stack.pop();
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
// Mismatched closing character; the input is malformed.
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// Append the processed character to the new string.
|
|
61
|
-
new_s += char;
|
|
62
|
-
}
|
|
63
|
-
// If we're still inside a string at the end of processing,
|
|
64
|
-
// we need to close the string.
|
|
65
|
-
if (isInsideString) {
|
|
66
|
-
new_s += '"';
|
|
67
|
-
}
|
|
68
|
-
// Close any remaining open structures in the reverse order that they were opened.
|
|
69
|
-
for (let i = stack.length - 1; i >= 0; i -= 1) {
|
|
70
|
-
new_s += stack[i];
|
|
71
|
-
}
|
|
72
|
-
// Attempt to parse the modified string as JSON.
|
|
73
|
-
try {
|
|
74
|
-
return JSON.parse(new_s);
|
|
75
|
-
}
|
|
76
|
-
catch (error) {
|
|
77
|
-
// If we still can't parse the string as JSON, return null to indicate failure.
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
exports.parsePartialJson = parsePartialJson;
|
|
4
|
+
var output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
|
+
Object.defineProperty(exports, "parsePartialJson", { enumerable: true, get: function () { return output_parsers_1.parsePartialJson; } });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { parsePartialJson } from "@langchain/core/output_parsers";
|