langchain 0.0.213 → 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 (54) hide show
  1. package/README.md +1 -1
  2. package/dist/agents/openai_functions/index.cjs +2 -0
  3. package/dist/agents/openai_functions/index.d.ts +2 -0
  4. package/dist/agents/openai_functions/index.js +2 -0
  5. package/dist/agents/openai_tools/index.cjs +2 -0
  6. package/dist/agents/openai_tools/index.d.ts +2 -0
  7. package/dist/agents/openai_tools/index.js +2 -0
  8. package/dist/agents/react/index.cjs +2 -0
  9. package/dist/agents/react/index.d.ts +2 -0
  10. package/dist/agents/react/index.js +2 -0
  11. package/dist/agents/structured_chat/index.cjs +2 -0
  12. package/dist/agents/structured_chat/index.d.ts +2 -0
  13. package/dist/agents/structured_chat/index.js +2 -0
  14. package/dist/agents/xml/index.cjs +2 -0
  15. package/dist/agents/xml/index.d.ts +2 -0
  16. package/dist/agents/xml/index.js +2 -0
  17. package/dist/callbacks/index.cjs +1 -4
  18. package/dist/callbacks/index.d.ts +1 -2
  19. package/dist/callbacks/index.js +1 -2
  20. package/dist/chains/openai_functions/structured_output.cjs +63 -21
  21. package/dist/chains/openai_functions/structured_output.d.ts +25 -17
  22. package/dist/chains/openai_functions/structured_output.js +62 -20
  23. package/dist/experimental/autogpt/prompt.cjs +1 -1
  24. package/dist/experimental/autogpt/prompt.d.ts +1 -1
  25. package/dist/experimental/autogpt/prompt.js +1 -1
  26. package/dist/retrievers/multi_vector.cjs +11 -2
  27. package/dist/retrievers/multi_vector.d.ts +5 -3
  28. package/dist/retrievers/multi_vector.js +11 -2
  29. package/dist/retrievers/parent_document.cjs +1 -2
  30. package/dist/retrievers/parent_document.d.ts +1 -1
  31. package/dist/retrievers/parent_document.js +1 -2
  32. package/dist/retrievers/remote/chatgpt-plugin.cjs +5 -4
  33. package/dist/retrievers/remote/chatgpt-plugin.d.ts +5 -2
  34. package/dist/retrievers/remote/chatgpt-plugin.js +3 -2
  35. package/dist/retrievers/remote/index.cjs +2 -2
  36. package/dist/retrievers/remote/index.d.ts +1 -1
  37. package/dist/retrievers/remote/index.js +1 -1
  38. package/dist/retrievers/remote/remote-retriever.cjs +3 -2
  39. package/dist/retrievers/remote/remote-retriever.d.ts +3 -1
  40. package/dist/retrievers/remote/remote-retriever.js +2 -1
  41. package/dist/retrievers/vespa.cjs +15 -78
  42. package/dist/retrievers/vespa.d.ts +1 -54
  43. package/dist/retrievers/vespa.js +1 -76
  44. package/dist/schema/runnable/config.d.ts +1 -1
  45. package/dist/util/entrypoint_deprecation.cjs +18 -0
  46. package/dist/util/entrypoint_deprecation.d.ts +5 -0
  47. package/dist/util/entrypoint_deprecation.js +14 -0
  48. package/package.json +2 -2
  49. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +0 -17
  50. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +0 -1
  51. package/dist/callbacks/handlers/tracer_langchain_v1.js +0 -1
  52. package/dist/retrievers/remote/base.cjs +0 -68
  53. package/dist/retrievers/remote/base.d.ts +0 -60
  54. package/dist/retrievers/remote/base.js +0 -64
package/README.md CHANGED
@@ -63,7 +63,7 @@ This library aims to assist in the development of those types of applications. C
63
63
 
64
64
  **💬 Chatbots**
65
65
 
66
- - [Documentation](https://js.langchain.com/docs/modules/model_io/models/chat/)
66
+ - [Documentation](https://js.langchain.com/docs/modules/models/chat/)
67
67
  - End-to-end Example: [Chat-LangChain](https://github.com/langchain-ai/chat-langchain)
68
68
 
69
69
  ## 🚀 How does LangChain help?
@@ -182,6 +182,8 @@ exports.OpenAIAgent = OpenAIAgent;
182
182
  * const tools = [...];
183
183
  *
184
184
  * // Get the prompt to use - you can modify this!
185
+ * // If you want to see the prompt in full, you can at:
186
+ * // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
185
187
  * const prompt = await pull<ChatPromptTemplate>(
186
188
  * "hwchase17/openai-functions-agent"
187
189
  * );
@@ -106,6 +106,8 @@ export type CreateOpenAIFunctionsAgentParams = {
106
106
  * const tools = [...];
107
107
  *
108
108
  * // Get the prompt to use - you can modify this!
109
+ * // If you want to see the prompt in full, you can at:
110
+ * // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
109
111
  * const prompt = await pull<ChatPromptTemplate>(
110
112
  * "hwchase17/openai-functions-agent"
111
113
  * );
@@ -177,6 +177,8 @@ export class OpenAIAgent extends Agent {
177
177
  * const tools = [...];
178
178
  *
179
179
  * // Get the prompt to use - you can modify this!
180
+ * // If you want to see the prompt in full, you can at:
181
+ * // https://smith.langchain.com/hub/hwchase17/openai-functions-agent
180
182
  * const prompt = await pull<ChatPromptTemplate>(
181
183
  * "hwchase17/openai-functions-agent"
182
184
  * );
@@ -26,6 +26,8 @@ Object.defineProperty(exports, "OpenAIToolsAgentOutputParser", { enumerable: tru
26
26
  * const tools = [...];
27
27
  *
28
28
  * // Get the prompt to use - you can modify this!
29
+ * // If you want to see the prompt in full, you can at:
30
+ * // https://smith.langchain.com/hub/hwchase17/openai-tools-agent
29
31
  * const prompt = await pull<ChatPromptTemplate>(
30
32
  * "hwchase17/openai-tools-agent"
31
33
  * );
@@ -43,6 +43,8 @@ export type CreateOpenAIToolsAgentParams = {
43
43
  * const tools = [...];
44
44
  *
45
45
  * // Get the prompt to use - you can modify this!
46
+ * // If you want to see the prompt in full, you can at:
47
+ * // https://smith.langchain.com/hub/hwchase17/openai-tools-agent
46
48
  * const prompt = await pull<ChatPromptTemplate>(
47
49
  * "hwchase17/openai-tools-agent"
48
50
  * );
@@ -23,6 +23,8 @@ export { OpenAIToolsAgentOutputParser };
23
23
  * const tools = [...];
24
24
  *
25
25
  * // Get the prompt to use - you can modify this!
26
+ * // If you want to see the prompt in full, you can at:
27
+ * // https://smith.langchain.com/hub/hwchase17/openai-tools-agent
26
28
  * const prompt = await pull<ChatPromptTemplate>(
27
29
  * "hwchase17/openai-tools-agent"
28
30
  * );
@@ -24,6 +24,8 @@ const output_parser_js_1 = require("./output_parser.cjs");
24
24
  * const tools = [...];
25
25
  *
26
26
  * // Get the prompt to use - you can modify this!
27
+ * // If you want to see the prompt in full, you can at:
28
+ * // https://smith.langchain.com/hub/hwchase17/react
27
29
  * const prompt = await pull<PromptTemplate>("hwchase17/react");
28
30
  *
29
31
  * const llm = new OpenAI({
@@ -35,6 +35,8 @@ export type CreateReactAgentParams = {
35
35
  * const tools = [...];
36
36
  *
37
37
  * // Get the prompt to use - you can modify this!
38
+ * // If you want to see the prompt in full, you can at:
39
+ * // https://smith.langchain.com/hub/hwchase17/react
38
40
  * const prompt = await pull<PromptTemplate>("hwchase17/react");
39
41
  *
40
42
  * const llm = new OpenAI({
@@ -21,6 +21,8 @@ import { ReActSingleInputOutputParser } from "./output_parser.js";
21
21
  * const tools = [...];
22
22
  *
23
23
  * // Get the prompt to use - you can modify this!
24
+ * // If you want to see the prompt in full, you can at:
25
+ * // https://smith.langchain.com/hub/hwchase17/react
24
26
  * const prompt = await pull<PromptTemplate>("hwchase17/react");
25
27
  *
26
28
  * const llm = new OpenAI({
@@ -174,6 +174,8 @@ exports.StructuredChatAgent = StructuredChatAgent;
174
174
  * const tools = [...];
175
175
  *
176
176
  * // Get the prompt to use - you can modify this!
177
+ * // If you want to see the prompt in full, you can at:
178
+ * // https://smith.langchain.com/hub/hwchase17/structured-chat-agent
177
179
  * const prompt = await pull<ChatPromptTemplate>(
178
180
  * "hwchase17/structured-chat-agent"
179
181
  * );
@@ -126,6 +126,8 @@ export type CreateStructuredChatAgentParams = {
126
126
  * const tools = [...];
127
127
  *
128
128
  * // Get the prompt to use - you can modify this!
129
+ * // If you want to see the prompt in full, you can at:
130
+ * // https://smith.langchain.com/hub/hwchase17/structured-chat-agent
129
131
  * const prompt = await pull<ChatPromptTemplate>(
130
132
  * "hwchase17/structured-chat-agent"
131
133
  * );
@@ -170,6 +170,8 @@ export class StructuredChatAgent extends Agent {
170
170
  * const tools = [...];
171
171
  *
172
172
  * // Get the prompt to use - you can modify this!
173
+ * // If you want to see the prompt in full, you can at:
174
+ * // https://smith.langchain.com/hub/hwchase17/structured-chat-agent
173
175
  * const prompt = await pull<ChatPromptTemplate>(
174
176
  * "hwchase17/structured-chat-agent"
175
177
  * );
@@ -123,6 +123,8 @@ exports.XMLAgent = XMLAgent;
123
123
  * const tools = [...];
124
124
  *
125
125
  * // Get the prompt to use - you can modify this!
126
+ * // If you want to see the prompt in full, you can at:
127
+ * // https://smith.langchain.com/hub/hwchase17/xml-agent-convo
126
128
  * const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
127
129
  *
128
130
  * const llm = new ChatAnthropic({
@@ -79,6 +79,8 @@ export type CreateXmlAgentParams = {
79
79
  * const tools = [...];
80
80
  *
81
81
  * // Get the prompt to use - you can modify this!
82
+ * // If you want to see the prompt in full, you can at:
83
+ * // https://smith.langchain.com/hub/hwchase17/xml-agent-convo
82
84
  * const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
83
85
  *
84
86
  * const llm = new ChatAnthropic({
@@ -119,6 +119,8 @@ export class XMLAgent extends BaseSingleActionAgent {
119
119
  * const tools = [...];
120
120
  *
121
121
  * // Get the prompt to use - you can modify this!
122
+ * // If you want to see the prompt in full, you can at:
123
+ * // https://smith.langchain.com/hub/hwchase17/xml-agent-convo
122
124
  * const prompt = await pull<PromptTemplate>("hwchase17/xml-agent-convo");
123
125
  *
124
126
  * const llm = new ChatAnthropic({
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForRetrieverRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = exports.LangChainTracerV1 = exports.LangChainTracer = exports.RunCollectorCallbackHandler = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
3
+ exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForRetrieverRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.LangChainTracer = exports.RunCollectorCallbackHandler = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
4
4
  var base_js_1 = require("./base.cjs");
5
5
  Object.defineProperty(exports, "BaseCallbackHandler", { enumerable: true, get: function () { return base_js_1.BaseCallbackHandler; } });
6
6
  var tracer_js_1 = require("./handlers/tracer.cjs");
@@ -11,10 +11,7 @@ var run_collector_js_1 = require("./handlers/run_collector.cjs");
11
11
  Object.defineProperty(exports, "RunCollectorCallbackHandler", { enumerable: true, get: function () { return run_collector_js_1.RunCollectorCallbackHandler; } });
12
12
  var tracer_langchain_js_1 = require("./handlers/tracer_langchain.cjs");
13
13
  Object.defineProperty(exports, "LangChainTracer", { enumerable: true, get: function () { return tracer_langchain_js_1.LangChainTracer; } });
14
- var tracer_langchain_v1_js_1 = require("./handlers/tracer_langchain_v1.cjs");
15
- Object.defineProperty(exports, "LangChainTracerV1", { enumerable: true, get: function () { return tracer_langchain_v1_js_1.LangChainTracerV1; } });
16
14
  var initialize_js_1 = require("./handlers/initialize.cjs");
17
- Object.defineProperty(exports, "getTracingCallbackHandler", { enumerable: true, get: function () { return initialize_js_1.getTracingCallbackHandler; } });
18
15
  Object.defineProperty(exports, "getTracingV2CallbackHandler", { enumerable: true, get: function () { return initialize_js_1.getTracingV2CallbackHandler; } });
19
16
  var manager_js_1 = require("./manager.cjs");
20
17
  Object.defineProperty(exports, "CallbackManager", { enumerable: true, get: function () { return manager_js_1.CallbackManager; } });
@@ -3,7 +3,6 @@ export { type Run, type RunType, BaseTracer } from "./handlers/tracer.js";
3
3
  export { ConsoleCallbackHandler } from "./handlers/console.js";
4
4
  export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
5
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
6
- export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
7
- export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
6
+ export { getTracingV2CallbackHandler } from "./handlers/initialize.js";
8
7
  export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, type BaseCallbackConfig, TraceGroup, traceAsGroup, } from "./manager.js";
9
8
  export { awaitAllCallbacks, consumeCallback } from "./promises.js";
@@ -3,7 +3,6 @@ export { BaseTracer } from "./handlers/tracer.js";
3
3
  export { ConsoleCallbackHandler } from "./handlers/console.js";
4
4
  export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
5
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
6
- export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
7
- export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
6
+ export { getTracingV2CallbackHandler } from "./handlers/initialize.js";
8
7
  export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, TraceGroup, traceAsGroup, } from "./manager.js";
9
8
  export { awaitAllCallbacks, consumeCallback } from "./promises.js";
@@ -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
  }
@@ -99,7 +99,7 @@ class AutoGPTPrompt extends chat_js_1.BaseChatPromptTemplate {
99
99
  if (typeof memoryMessage.content !== "string") {
100
100
  throw new Error("Non-string message content is not supported.");
101
101
  }
102
- const usedTokensWithMemory = (await usedTokens) + (await this.tokenCounter(memoryMessage.content));
102
+ const usedTokensWithMemory = usedTokens + (await this.tokenCounter(memoryMessage.content));
103
103
  const historicalMessages = [];
104
104
  for (const message of previousMessages.slice(-10).reverse()) {
105
105
  if (typeof message.content !== "string") {
@@ -1,8 +1,8 @@
1
1
  import type { VectorStoreRetrieverInterface } from "@langchain/core/vectorstores";
2
2
  import { BaseChatPromptTemplate } from "../../prompts/chat.js";
3
+ import { SerializedBasePromptTemplate } from "../../prompts/serde.js";
3
4
  import { BaseMessage, PartialValues } from "../../schema/index.js";
4
5
  import { ObjectTool } from "./schema.js";
5
- import { SerializedBasePromptTemplate } from "../../prompts/serde.js";
6
6
  /**
7
7
  * Interface for the input parameters of the AutoGPTPrompt class.
8
8
  */
@@ -96,7 +96,7 @@ export class AutoGPTPrompt extends BaseChatPromptTemplate {
96
96
  if (typeof memoryMessage.content !== "string") {
97
97
  throw new Error("Non-string message content is not supported.");
98
98
  }
99
- const usedTokensWithMemory = (await usedTokens) + (await this.tokenCounter(memoryMessage.content));
99
+ const usedTokensWithMemory = usedTokens + (await this.tokenCounter(memoryMessage.content));
100
100
  const historicalMessages = [];
101
101
  for (const message of previousMessages.slice(-10).reverse()) {
102
102
  if (typeof message.content !== "string") {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MultiVectorRetriever = void 0;
4
4
  const retrievers_1 = require("@langchain/core/retrievers");
5
+ const encoder_backed_js_1 = require("../storage/encoder_backed.cjs");
5
6
  /**
6
7
  * A retriever that retrieves documents from a vector store and a document
7
8
  * store. It uses the vector store to find relevant documents based on a
@@ -10,7 +11,7 @@ const retrievers_1 = require("@langchain/core/retrievers");
10
11
  * ```typescript
11
12
  * const retriever = new MultiVectorRetriever({
12
13
  * vectorstore: new FaissStore(),
13
- * docstore: new InMemoryStore(),
14
+ * byteStore: new InMemoryStore<Unit8Array>(),
14
15
  * idKey: "doc_id",
15
16
  * childK: 20,
16
17
  * parentK: 5,
@@ -63,7 +64,15 @@ class MultiVectorRetriever extends retrievers_1.BaseRetriever {
63
64
  value: void 0
64
65
  });
65
66
  this.vectorstore = args.vectorstore;
66
- this.docstore = args.docstore;
67
+ if (args.byteStore) {
68
+ this.docstore = (0, encoder_backed_js_1.createDocumentStoreFromByteStore)(args.byteStore);
69
+ }
70
+ else if (args.docstore) {
71
+ this.docstore = args.docstore;
72
+ }
73
+ else {
74
+ throw new Error("byteStore and docstore are undefined. Please provide at least one.");
75
+ }
67
76
  this.idKey = args.idKey ?? "doc_id";
68
77
  this.childK = args.childK;
69
78
  this.parentK = args.parentK;