langchain 0.0.212 → 0.0.214

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.
Files changed (138) hide show
  1. package/README.md +1 -1
  2. package/chains/combine_documents.cjs +1 -0
  3. package/chains/combine_documents.d.ts +1 -0
  4. package/chains/combine_documents.js +1 -0
  5. package/chains/history_aware_retriever.cjs +1 -0
  6. package/chains/history_aware_retriever.d.ts +1 -0
  7. package/chains/history_aware_retriever.js +1 -0
  8. package/chains/retrieval.cjs +1 -0
  9. package/chains/retrieval.d.ts +1 -0
  10. package/chains/retrieval.js +1 -0
  11. package/dist/agents/agent.cjs +1 -0
  12. package/dist/agents/agent.js +1 -0
  13. package/dist/agents/executor.cjs +21 -3
  14. package/dist/agents/executor.d.ts +1 -0
  15. package/dist/agents/executor.js +21 -3
  16. package/dist/agents/format_scratchpad/openai_functions.cjs +22 -1
  17. package/dist/agents/format_scratchpad/openai_functions.d.ts +10 -0
  18. package/dist/agents/format_scratchpad/openai_functions.js +21 -1
  19. package/dist/agents/index.cjs +11 -4
  20. package/dist/agents/index.d.ts +6 -3
  21. package/dist/agents/index.js +5 -3
  22. package/dist/agents/initialize.cjs +1 -1
  23. package/dist/agents/initialize.d.ts +1 -1
  24. package/dist/agents/initialize.js +1 -1
  25. package/dist/agents/openai/output_parser.cjs +20 -196
  26. package/dist/agents/openai/output_parser.d.ts +2 -111
  27. package/dist/agents/openai/output_parser.js +6 -193
  28. package/dist/agents/{openai → openai_functions}/index.cjs +80 -2
  29. package/dist/agents/{openai → openai_functions}/index.d.ts +77 -3
  30. package/dist/agents/{openai → openai_functions}/index.js +78 -1
  31. package/dist/agents/openai_functions/output_parser.cjs +102 -0
  32. package/dist/agents/openai_functions/output_parser.d.ts +56 -0
  33. package/dist/agents/openai_functions/output_parser.js +98 -0
  34. package/dist/agents/openai_tools/index.cjs +83 -0
  35. package/dist/agents/openai_tools/index.d.ts +82 -0
  36. package/dist/agents/openai_tools/index.js +79 -0
  37. package/dist/agents/openai_tools/output_parser.cjs +102 -0
  38. package/dist/agents/openai_tools/output_parser.d.ts +57 -0
  39. package/dist/agents/openai_tools/output_parser.js +98 -0
  40. package/dist/agents/react/index.cjs +77 -0
  41. package/dist/agents/react/index.d.ts +62 -0
  42. package/dist/agents/react/index.js +73 -0
  43. package/dist/agents/react/output_parser.cjs +0 -1
  44. package/dist/agents/react/output_parser.d.ts +0 -1
  45. package/dist/agents/react/output_parser.js +0 -1
  46. package/dist/agents/structured_chat/index.cjs +87 -1
  47. package/dist/agents/structured_chat/index.d.ts +73 -0
  48. package/dist/agents/structured_chat/index.js +85 -0
  49. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.cjs +1 -1
  50. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.js +1 -1
  51. package/dist/agents/toolkits/conversational_retrieval/tool.cjs +1 -0
  52. package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +1 -0
  53. package/dist/agents/toolkits/conversational_retrieval/tool.js +1 -0
  54. package/dist/agents/toolkits/json/json.cjs +2 -0
  55. package/dist/agents/toolkits/json/json.d.ts +2 -0
  56. package/dist/agents/toolkits/json/json.js +2 -0
  57. package/dist/agents/toolkits/openapi/openapi.cjs +2 -0
  58. package/dist/agents/toolkits/openapi/openapi.d.ts +2 -0
  59. package/dist/agents/toolkits/openapi/openapi.js +2 -0
  60. package/dist/agents/toolkits/vectorstore/vectorstore.cjs +2 -0
  61. package/dist/agents/toolkits/vectorstore/vectorstore.d.ts +2 -0
  62. package/dist/agents/toolkits/vectorstore/vectorstore.js +2 -0
  63. package/dist/agents/xml/index.cjs +77 -1
  64. package/dist/agents/xml/index.d.ts +67 -0
  65. package/dist/agents/xml/index.js +75 -0
  66. package/dist/callbacks/index.cjs +1 -4
  67. package/dist/callbacks/index.d.ts +1 -2
  68. package/dist/callbacks/index.js +1 -2
  69. package/dist/chains/combine_documents/base.cjs +16 -0
  70. package/dist/chains/combine_documents/base.d.ts +13 -0
  71. package/dist/chains/combine_documents/base.js +12 -0
  72. package/dist/chains/combine_documents/index.cjs +5 -0
  73. package/dist/chains/combine_documents/index.d.ts +1 -0
  74. package/dist/chains/combine_documents/index.js +1 -0
  75. package/dist/chains/combine_documents/reduce.cjs +5 -2
  76. package/dist/chains/combine_documents/reduce.js +4 -1
  77. package/dist/chains/combine_documents/stuff.cjs +42 -0
  78. package/dist/chains/combine_documents/stuff.d.ts +28 -0
  79. package/dist/chains/combine_documents/stuff.js +38 -0
  80. package/dist/chains/conversational_retrieval_chain.cjs +3 -3
  81. package/dist/chains/conversational_retrieval_chain.js +1 -1
  82. package/dist/chains/history_aware_retriever.cjs +55 -0
  83. package/dist/chains/history_aware_retriever.d.ts +55 -0
  84. package/dist/chains/history_aware_retriever.js +51 -0
  85. package/dist/chains/openai_functions/structured_output.cjs +63 -21
  86. package/dist/chains/openai_functions/structured_output.d.ts +25 -17
  87. package/dist/chains/openai_functions/structured_output.js +62 -20
  88. package/dist/chains/retrieval.cjs +60 -0
  89. package/dist/chains/retrieval.d.ts +65 -0
  90. package/dist/chains/retrieval.js +56 -0
  91. package/dist/experimental/autogpt/prompt.cjs +1 -1
  92. package/dist/experimental/autogpt/prompt.d.ts +1 -1
  93. package/dist/experimental/autogpt/prompt.js +1 -1
  94. package/dist/load/import_map.cjs +7 -3
  95. package/dist/load/import_map.d.ts +4 -0
  96. package/dist/load/import_map.js +4 -0
  97. package/dist/output_parsers/json.cjs +2 -78
  98. package/dist/output_parsers/json.d.ts +1 -1
  99. package/dist/output_parsers/json.js +1 -77
  100. package/dist/output_parsers/openai_functions.d.ts +1 -1
  101. package/dist/retrievers/multi_vector.cjs +11 -2
  102. package/dist/retrievers/multi_vector.d.ts +5 -3
  103. package/dist/retrievers/multi_vector.js +11 -2
  104. package/dist/retrievers/parent_document.cjs +1 -2
  105. package/dist/retrievers/parent_document.d.ts +1 -1
  106. package/dist/retrievers/parent_document.js +1 -2
  107. package/dist/retrievers/remote/chatgpt-plugin.cjs +5 -4
  108. package/dist/retrievers/remote/chatgpt-plugin.d.ts +5 -2
  109. package/dist/retrievers/remote/chatgpt-plugin.js +3 -2
  110. package/dist/retrievers/remote/index.cjs +2 -2
  111. package/dist/retrievers/remote/index.d.ts +1 -1
  112. package/dist/retrievers/remote/index.js +1 -1
  113. package/dist/retrievers/remote/remote-retriever.cjs +3 -2
  114. package/dist/retrievers/remote/remote-retriever.d.ts +3 -1
  115. package/dist/retrievers/remote/remote-retriever.js +2 -1
  116. package/dist/retrievers/vespa.cjs +15 -78
  117. package/dist/retrievers/vespa.d.ts +1 -54
  118. package/dist/retrievers/vespa.js +1 -76
  119. package/dist/schema/runnable/config.d.ts +1 -1
  120. package/dist/tools/retriever.cjs +17 -0
  121. package/dist/tools/retriever.d.ts +10 -0
  122. package/dist/tools/retriever.js +13 -0
  123. package/dist/util/entrypoint_deprecation.cjs +18 -0
  124. package/dist/util/entrypoint_deprecation.d.ts +5 -0
  125. package/dist/util/entrypoint_deprecation.js +14 -0
  126. package/package.json +36 -4
  127. package/tools/retriever.cjs +1 -0
  128. package/tools/retriever.d.ts +1 -0
  129. package/tools/retriever.js +1 -0
  130. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +0 -17
  131. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +0 -1
  132. package/dist/callbacks/handlers/tracer_langchain_v1.js +0 -1
  133. package/dist/retrievers/remote/base.cjs +0 -68
  134. package/dist/retrievers/remote/base.d.ts +0 -60
  135. package/dist/retrievers/remote/base.js +0 -64
  136. /package/dist/agents/{openai → openai_functions}/prompt.cjs +0 -0
  137. /package/dist/agents/{openai → openai_functions}/prompt.d.ts +0 -0
  138. /package/dist/agents/{openai → openai_functions}/prompt.js +0 -0
@@ -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
+ }
@@ -3,23 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createStructuredOutputChainFromZod = exports.createStructuredOutputChain = exports.FunctionCallStructuredOutputParser = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
5
  const json_schema_1 = require("@langchain/core/utils/json_schema");
6
+ const openai_1 = require("@langchain/openai");
7
+ const output_parsers_1 = require("@langchain/core/output_parsers");
6
8
  const llm_chain_js_1 = require("../llm_chain.cjs");
7
- const openai_js_1 = require("../../chat_models/openai.cjs");
8
- const output_parser_js_1 = require("../../schema/output_parser.cjs");
9
9
  const openai_functions_js_1 = require("../../output_parsers/openai_functions.cjs");
10
+ function isJsonSchema7Type(x) {
11
+ return (x.jsonSchema === undefined &&
12
+ x.zodSchema === undefined);
13
+ }
10
14
  /**
11
15
  * Class that extends the BaseLLMOutputParser class. It provides
12
16
  * functionality for parsing the structured output based on a JSON schema.
13
17
  */
14
- class FunctionCallStructuredOutputParser extends output_parser_js_1.BaseLLMOutputParser {
15
- constructor(schema) {
16
- super();
17
- Object.defineProperty(this, "schema", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: schema
22
- });
18
+ class FunctionCallStructuredOutputParser extends output_parsers_1.BaseLLMOutputParser {
19
+ constructor(fieldsOrSchema) {
20
+ let fields;
21
+ if (isJsonSchema7Type(fieldsOrSchema)) {
22
+ fields = { jsonSchema: fieldsOrSchema };
23
+ }
24
+ else {
25
+ fields = fieldsOrSchema;
26
+ }
27
+ if (fields.jsonSchema === undefined && fields.zodSchema === undefined) {
28
+ throw new Error(`Must provide one of "jsonSchema" or "zodSchema".`);
29
+ }
30
+ super(fields);
23
31
  Object.defineProperty(this, "lc_namespace", {
24
32
  enumerable: true,
25
33
  configurable: true,
@@ -38,13 +46,26 @@ class FunctionCallStructuredOutputParser extends output_parser_js_1.BaseLLMOutpu
38
46
  writable: true,
39
47
  value: void 0
40
48
  });
41
- this.jsonSchemaValidator = new json_schema_1.Validator(schema, "7");
49
+ Object.defineProperty(this, "zodSchema", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ if (fields.jsonSchema !== undefined) {
56
+ this.jsonSchemaValidator = new json_schema_1.Validator(fields.jsonSchema, "7");
57
+ }
58
+ else {
59
+ this.zodSchema = fields.zodSchema;
60
+ }
42
61
  }
43
62
  /**
44
63
  * Method to parse the result of chat generations. It first parses the
45
- * result using the functionOutputParser, then validates the parsed result
46
- * against the JSON schema. If the result is valid, it returns the parsed
47
- * result. Otherwise, it throws an OutputParserException.
64
+ * result using the functionOutputParser, then parses the result against a
65
+ * zod schema if the zod schema is available which allows the result to undergo
66
+ * Zod preprocessing, then it parses that result against the JSON schema.
67
+ * If the result is valid, it returns the parsed result. Otherwise, it throws
68
+ * an OutputParserException.
48
69
  * @param generations Array of ChatGeneration instances to be parsed.
49
70
  * @returns The parsed result if it is valid according to the JSON schema.
50
71
  */
@@ -56,12 +77,26 @@ class FunctionCallStructuredOutputParser extends output_parser_js_1.BaseLLMOutpu
56
77
  }
57
78
  return value;
58
79
  });
59
- const result = this.jsonSchemaValidator.validate(parsedResult);
60
- if (result.valid) {
61
- return parsedResult;
80
+ if (this.zodSchema) {
81
+ const zodParsedResult = this.zodSchema.safeParse(parsedResult);
82
+ if (zodParsedResult.success) {
83
+ return zodParsedResult.data;
84
+ }
85
+ else {
86
+ throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(zodParsedResult.error.errors)}`, initialResult);
87
+ }
88
+ }
89
+ else if (this.jsonSchemaValidator !== undefined) {
90
+ const result = this.jsonSchemaValidator.validate(parsedResult);
91
+ if (result.valid) {
92
+ return parsedResult;
93
+ }
94
+ else {
95
+ throw new output_parsers_1.OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(result.errors)}`, initialResult);
96
+ }
62
97
  }
63
98
  else {
64
- throw new output_parser_js_1.OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(result.errors)}`, initialResult);
99
+ throw new Error("This parser requires an input JSON Schema or an input Zod schema.");
65
100
  }
66
101
  }
67
102
  }
@@ -73,7 +108,10 @@ exports.FunctionCallStructuredOutputParser = FunctionCallStructuredOutputParser;
73
108
  * @returns OpenAPIChain
74
109
  */
75
110
  function createStructuredOutputChain(input) {
76
- const { outputSchema, llm = new openai_js_1.ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }), outputKey = "output", llmKwargs = {}, ...rest } = input;
111
+ const { outputSchema, llm = new openai_1.ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }), outputKey = "output", llmKwargs = {}, zodSchema, ...rest } = input;
112
+ if (outputSchema === undefined && zodSchema === undefined) {
113
+ throw new Error(`Must provide one of "outputSchema" or "zodSchema".`);
114
+ }
77
115
  const functionName = "output_formatter";
78
116
  return new llm_chain_js_1.LLMChain({
79
117
  llm,
@@ -91,7 +129,10 @@ function createStructuredOutputChain(input) {
91
129
  },
92
130
  },
93
131
  outputKey,
94
- outputParser: new FunctionCallStructuredOutputParser(outputSchema),
132
+ outputParser: new FunctionCallStructuredOutputParser({
133
+ jsonSchema: outputSchema,
134
+ zodSchema,
135
+ }),
95
136
  ...rest,
96
137
  });
97
138
  }
@@ -100,6 +141,7 @@ function createStructuredOutputChainFromZod(zodSchema, input) {
100
141
  return createStructuredOutputChain({
101
142
  ...input,
102
143
  outputSchema: (0, zod_to_json_schema_1.zodToJsonSchema)(zodSchema),
144
+ zodSchema,
103
145
  });
104
146
  }
105
147
  exports.createStructuredOutputChainFromZod = createStructuredOutputChainFromZod;
@@ -1,40 +1,48 @@
1
1
  import { z } from "zod";
2
2
  import { JsonSchema7Type } from "zod-to-json-schema/src/parseDef.js";
3
3
  import { Validator } from "@langchain/core/utils/json_schema";
4
- import { LLMChain, LLMChainInput } from "../llm_chain.js";
5
- import { ChatOpenAI } from "../../chat_models/openai.js";
6
- import { BasePromptTemplate } from "../../prompts/index.js";
7
- import { BaseLLMOutputParser } from "../../schema/output_parser.js";
4
+ import { ChatOpenAI } from "@langchain/openai";
5
+ import { BasePromptTemplate } from "@langchain/core/prompts";
6
+ import { BaseLLMOutputParser } from "@langchain/core/output_parsers";
7
+ import { ChatGeneration } from "@langchain/core/outputs";
8
+ import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
9
+ import type { BaseFunctionCallOptions } from "@langchain/core/language_models/base";
10
+ import { LLMChain, type LLMChainInput } from "../llm_chain.js";
8
11
  import { OutputFunctionsParser } from "../../output_parsers/openai_functions.js";
9
- import { ChatGeneration } from "../../schema/index.js";
10
- import { BaseChatModel } from "../../chat_models/base.js";
11
- import { BaseFunctionCallOptions } from "../../base_language/index.js";
12
12
  /**
13
13
  * Type representing the input for creating a structured output chain. It
14
14
  * extends the LLMChainInput type and includes an additional
15
15
  * 'outputSchema' field representing the JSON schema for the expected
16
16
  * output.
17
17
  */
18
- export type StructuredOutputChainInput = Omit<LLMChainInput, "outputParser" | "llm"> & {
19
- outputSchema: JsonSchema7Type;
18
+ export type StructuredOutputChainInput<T extends z.AnyZodObject = z.AnyZodObject> = Omit<LLMChainInput, "outputParser" | "llm"> & {
19
+ outputSchema?: JsonSchema7Type;
20
20
  prompt: BasePromptTemplate;
21
21
  llm?: BaseChatModel<BaseFunctionCallOptions>;
22
+ zodSchema?: T;
23
+ };
24
+ export type FunctionCallStructuredOutputParserFields<T extends z.AnyZodObject = z.AnyZodObject> = {
25
+ jsonSchema?: JsonSchema7Type;
26
+ zodSchema?: T;
22
27
  };
23
28
  /**
24
29
  * Class that extends the BaseLLMOutputParser class. It provides
25
30
  * functionality for parsing the structured output based on a JSON schema.
26
31
  */
27
32
  export declare class FunctionCallStructuredOutputParser<T extends z.AnyZodObject> extends BaseLLMOutputParser<z.infer<T>> {
28
- schema: JsonSchema7Type;
29
33
  lc_namespace: string[];
30
34
  protected functionOutputParser: OutputFunctionsParser;
31
- protected jsonSchemaValidator: Validator;
32
- constructor(schema: JsonSchema7Type);
35
+ protected jsonSchemaValidator?: Validator;
36
+ protected zodSchema?: T;
37
+ constructor(fieldsOrSchema: JsonSchema7Type);
38
+ constructor(fieldsOrSchema: FunctionCallStructuredOutputParserFields<T>);
33
39
  /**
34
40
  * Method to parse the result of chat generations. It first parses the
35
- * result using the functionOutputParser, then validates the parsed result
36
- * against the JSON schema. If the result is valid, it returns the parsed
37
- * result. Otherwise, it throws an OutputParserException.
41
+ * result using the functionOutputParser, then parses the result against a
42
+ * zod schema if the zod schema is available which allows the result to undergo
43
+ * Zod preprocessing, then it parses that result against the JSON schema.
44
+ * If the result is valid, it returns the parsed result. Otherwise, it throws
45
+ * an OutputParserException.
38
46
  * @param generations Array of ChatGeneration instances to be parsed.
39
47
  * @returns The parsed result if it is valid according to the JSON schema.
40
48
  */
@@ -46,5 +54,5 @@ export declare class FunctionCallStructuredOutputParser<T extends z.AnyZodObject
46
54
  * as well as an additional required "outputSchema" JSON Schema object.
47
55
  * @returns OpenAPIChain
48
56
  */
49
- export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
50
- export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput, "outputSchema">): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
57
+ export declare function createStructuredOutputChain<T extends z.AnyZodObject = z.AnyZodObject>(input: StructuredOutputChainInput<T>): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
58
+ export declare function createStructuredOutputChainFromZod<T extends z.AnyZodObject>(zodSchema: T, input: Omit<StructuredOutputChainInput<T>, "outputSchema">): LLMChain<any, BaseChatModel<BaseFunctionCallOptions> | ChatOpenAI<BaseFunctionCallOptions>>;
@@ -1,22 +1,30 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
2
  import { Validator } from "@langchain/core/utils/json_schema";
3
+ import { ChatOpenAI } from "@langchain/openai";
4
+ import { BaseLLMOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
3
5
  import { LLMChain } from "../llm_chain.js";
4
- import { ChatOpenAI } from "../../chat_models/openai.js";
5
- import { BaseLLMOutputParser, OutputParserException, } from "../../schema/output_parser.js";
6
6
  import { OutputFunctionsParser } from "../../output_parsers/openai_functions.js";
7
+ function isJsonSchema7Type(x) {
8
+ return (x.jsonSchema === undefined &&
9
+ x.zodSchema === undefined);
10
+ }
7
11
  /**
8
12
  * Class that extends the BaseLLMOutputParser class. It provides
9
13
  * functionality for parsing the structured output based on a JSON schema.
10
14
  */
11
15
  export class FunctionCallStructuredOutputParser extends BaseLLMOutputParser {
12
- constructor(schema) {
13
- super();
14
- Object.defineProperty(this, "schema", {
15
- enumerable: true,
16
- configurable: true,
17
- writable: true,
18
- value: schema
19
- });
16
+ constructor(fieldsOrSchema) {
17
+ let fields;
18
+ if (isJsonSchema7Type(fieldsOrSchema)) {
19
+ fields = { jsonSchema: fieldsOrSchema };
20
+ }
21
+ else {
22
+ fields = fieldsOrSchema;
23
+ }
24
+ if (fields.jsonSchema === undefined && fields.zodSchema === undefined) {
25
+ throw new Error(`Must provide one of "jsonSchema" or "zodSchema".`);
26
+ }
27
+ super(fields);
20
28
  Object.defineProperty(this, "lc_namespace", {
21
29
  enumerable: true,
22
30
  configurable: true,
@@ -35,13 +43,26 @@ export class FunctionCallStructuredOutputParser extends BaseLLMOutputParser {
35
43
  writable: true,
36
44
  value: void 0
37
45
  });
38
- this.jsonSchemaValidator = new Validator(schema, "7");
46
+ Object.defineProperty(this, "zodSchema", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ if (fields.jsonSchema !== undefined) {
53
+ this.jsonSchemaValidator = new Validator(fields.jsonSchema, "7");
54
+ }
55
+ else {
56
+ this.zodSchema = fields.zodSchema;
57
+ }
39
58
  }
40
59
  /**
41
60
  * Method to parse the result of chat generations. It first parses the
42
- * result using the functionOutputParser, then validates the parsed result
43
- * against the JSON schema. If the result is valid, it returns the parsed
44
- * result. Otherwise, it throws an OutputParserException.
61
+ * result using the functionOutputParser, then parses the result against a
62
+ * zod schema if the zod schema is available which allows the result to undergo
63
+ * Zod preprocessing, then it parses that result against the JSON schema.
64
+ * If the result is valid, it returns the parsed result. Otherwise, it throws
65
+ * an OutputParserException.
45
66
  * @param generations Array of ChatGeneration instances to be parsed.
46
67
  * @returns The parsed result if it is valid according to the JSON schema.
47
68
  */
@@ -53,12 +74,26 @@ export class FunctionCallStructuredOutputParser extends BaseLLMOutputParser {
53
74
  }
54
75
  return value;
55
76
  });
56
- const result = this.jsonSchemaValidator.validate(parsedResult);
57
- if (result.valid) {
58
- return parsedResult;
77
+ if (this.zodSchema) {
78
+ const zodParsedResult = this.zodSchema.safeParse(parsedResult);
79
+ if (zodParsedResult.success) {
80
+ return zodParsedResult.data;
81
+ }
82
+ else {
83
+ throw new OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(zodParsedResult.error.errors)}`, initialResult);
84
+ }
85
+ }
86
+ else if (this.jsonSchemaValidator !== undefined) {
87
+ const result = this.jsonSchemaValidator.validate(parsedResult);
88
+ if (result.valid) {
89
+ return parsedResult;
90
+ }
91
+ else {
92
+ throw new OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(result.errors)}`, initialResult);
93
+ }
59
94
  }
60
95
  else {
61
- throw new OutputParserException(`Failed to parse. Text: "${initialResult}". Error: ${JSON.stringify(result.errors)}`, initialResult);
96
+ throw new Error("This parser requires an input JSON Schema or an input Zod schema.");
62
97
  }
63
98
  }
64
99
  }
@@ -69,7 +104,10 @@ export class FunctionCallStructuredOutputParser extends BaseLLMOutputParser {
69
104
  * @returns OpenAPIChain
70
105
  */
71
106
  export function createStructuredOutputChain(input) {
72
- const { outputSchema, llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }), outputKey = "output", llmKwargs = {}, ...rest } = input;
107
+ const { outputSchema, llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613", temperature: 0 }), outputKey = "output", llmKwargs = {}, zodSchema, ...rest } = input;
108
+ if (outputSchema === undefined && zodSchema === undefined) {
109
+ throw new Error(`Must provide one of "outputSchema" or "zodSchema".`);
110
+ }
73
111
  const functionName = "output_formatter";
74
112
  return new LLMChain({
75
113
  llm,
@@ -87,7 +125,10 @@ export function createStructuredOutputChain(input) {
87
125
  },
88
126
  },
89
127
  outputKey,
90
- outputParser: new FunctionCallStructuredOutputParser(outputSchema),
128
+ outputParser: new FunctionCallStructuredOutputParser({
129
+ jsonSchema: outputSchema,
130
+ zodSchema,
131
+ }),
91
132
  ...rest,
92
133
  });
93
134
  }
@@ -95,5 +136,6 @@ export function createStructuredOutputChainFromZod(zodSchema, input) {
95
136
  return createStructuredOutputChain({
96
137
  ...input,
97
138
  outputSchema: zodToJsonSchema(zodSchema),
139
+ zodSchema,
98
140
  });
99
141
  }
@@ -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
+ }>>;