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,98 @@
1
+ import { isBaseMessage, } from "../../schema/index.js";
2
+ import { AgentMultiActionOutputParser } from "../types.js";
3
+ import { OutputParserException } from "../../schema/output_parser.js";
4
+ /**
5
+ * @example
6
+ * ```typescript
7
+ *
8
+ * const prompt = ChatPromptTemplate.fromMessages([
9
+ * ["ai", "You are a helpful assistant"],
10
+ * ["human", "{input}"],
11
+ * new MessagesPlaceholder("agent_scratchpad"),
12
+ * ]);
13
+ *
14
+ * const runnableAgent = RunnableSequence.from([
15
+ * {
16
+ * input: (i: { input: string; steps: ToolsAgentStep[] }) => i.input,
17
+ * agent_scratchpad: (i: { input: string; steps: ToolsAgentStep[] }) =>
18
+ * formatToOpenAIToolMessages(i.steps),
19
+ * },
20
+ * prompt,
21
+ * new ChatOpenAI({
22
+ * modelName: "gpt-3.5-turbo-1106",
23
+ * temperature: 0,
24
+ * }).bind({ tools: tools.map(formatToOpenAITool) }),
25
+ * new OpenAIToolsAgentOutputParser(),
26
+ * ]).withConfig({ runName: "OpenAIToolsAgent" });
27
+ *
28
+ * const result = await runnableAgent.invoke({
29
+ * input:
30
+ * "What is the sum of the current temperature in San Francisco, New York, and Tokyo?",
31
+ * });
32
+ *
33
+ * ```
34
+ */
35
+ export class OpenAIToolsAgentOutputParser extends AgentMultiActionOutputParser {
36
+ constructor() {
37
+ super(...arguments);
38
+ Object.defineProperty(this, "lc_namespace", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: ["langchain", "agents", "openai"]
43
+ });
44
+ }
45
+ static lc_name() {
46
+ return "OpenAIToolsAgentOutputParser";
47
+ }
48
+ async parse(text) {
49
+ throw new Error(`OpenAIFunctionsAgentOutputParser can only parse messages.\nPassed input: ${text}`);
50
+ }
51
+ async parseResult(generations) {
52
+ if ("message" in generations[0] && isBaseMessage(generations[0].message)) {
53
+ return this.parseAIMessage(generations[0].message);
54
+ }
55
+ throw new Error("parseResult on OpenAIFunctionsAgentOutputParser only works on ChatGeneration output");
56
+ }
57
+ /**
58
+ * Parses the output message into a ToolsAgentAction[] or AgentFinish
59
+ * object.
60
+ * @param message The BaseMessage to parse.
61
+ * @returns A ToolsAgentAction[] or AgentFinish object.
62
+ */
63
+ parseAIMessage(message) {
64
+ if (message.content && typeof message.content !== "string") {
65
+ throw new Error("This agent cannot parse non-string model responses.");
66
+ }
67
+ if (message.additional_kwargs.tool_calls) {
68
+ const toolCalls = message.additional_kwargs.tool_calls;
69
+ try {
70
+ return toolCalls.map((toolCall, i) => {
71
+ const toolInput = toolCall.function.arguments
72
+ ? JSON.parse(toolCall.function.arguments)
73
+ : {};
74
+ const messageLog = i === 0 ? [message] : [];
75
+ return {
76
+ tool: toolCall.function.name,
77
+ toolInput,
78
+ toolCallId: toolCall.id,
79
+ log: `Invoking "${toolCall.function.name}" with ${toolCall.function.arguments ?? "{}"}\n${message.content}`,
80
+ messageLog,
81
+ };
82
+ });
83
+ }
84
+ catch (error) {
85
+ throw new OutputParserException(`Failed to parse tool arguments from chat model response. Text: "${JSON.stringify(toolCalls)}". ${error}`);
86
+ }
87
+ }
88
+ else {
89
+ return {
90
+ returnValues: { output: message.content },
91
+ log: message.content,
92
+ };
93
+ }
94
+ }
95
+ getFormatInstructions() {
96
+ throw new Error("getFormatInstructions not implemented inside OpenAIToolsAgentOutputParser.");
97
+ }
98
+ }
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createReactAgent = void 0;
4
+ const runnables_1 = require("@langchain/core/runnables");
5
+ const render_js_1 = require("../../tools/render.cjs");
6
+ const log_js_1 = require("../format_scratchpad/log.cjs");
7
+ const output_parser_js_1 = require("./output_parser.cjs");
8
+ /**
9
+ * Create an agent that uses ReAct prompting.
10
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
11
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
12
+ * variables as the prompt passed in does. It returns as output either an
13
+ * AgentAction or AgentFinish.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * import { AgentExecutor, createReactAgent } from "langchain/agents";
18
+ * import { pull } from "langchain/hub";
19
+ * import type { PromptTemplate } from "@langchain/core/prompts";
20
+ *
21
+ * import { OpenAI } from "@langchain/openai";
22
+ *
23
+ * // Define the tools the agent will have access to.
24
+ * const tools = [...];
25
+ *
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
29
+ * const prompt = await pull<PromptTemplate>("hwchase17/react");
30
+ *
31
+ * const llm = new OpenAI({
32
+ * temperature: 0,
33
+ * });
34
+ *
35
+ * const agent = await createReactAgent({
36
+ * llm,
37
+ * tools,
38
+ * prompt,
39
+ * });
40
+ *
41
+ * const agentExecutor = new AgentExecutor({
42
+ * agent,
43
+ * tools,
44
+ * });
45
+ *
46
+ * const result = await agentExecutor.invoke({
47
+ * input: "what is LangChain?",
48
+ * });
49
+ * ```
50
+ */
51
+ async function createReactAgent({ llm, tools, prompt, }) {
52
+ const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
53
+ if (missingVariables.length > 0) {
54
+ throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
55
+ }
56
+ const toolNames = tools.map((tool) => tool.name);
57
+ const partialedPrompt = await prompt.partial({
58
+ tools: (0, render_js_1.renderTextDescription)(tools),
59
+ tool_names: toolNames.join(", "),
60
+ });
61
+ // TODO: Add .bind to core runnable interface.
62
+ const llmWithStop = llm.bind({
63
+ stop: ["\nObservation:"],
64
+ });
65
+ const agent = runnables_1.RunnableSequence.from([
66
+ runnables_1.RunnablePassthrough.assign({
67
+ agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
68
+ }),
69
+ partialedPrompt,
70
+ llmWithStop,
71
+ new output_parser_js_1.ReActSingleInputOutputParser({
72
+ toolNames,
73
+ }),
74
+ ]);
75
+ return agent;
76
+ }
77
+ exports.createReactAgent = createReactAgent;
@@ -0,0 +1,62 @@
1
+ import type { ToolInterface } from "@langchain/core/tools";
2
+ import { BasePromptTemplate } from "@langchain/core/prompts";
3
+ import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
4
+ import { RunnableSequence } from "@langchain/core/runnables";
5
+ /**
6
+ * Params used by the createXmlAgent function.
7
+ */
8
+ export type CreateReactAgentParams = {
9
+ /** LLM to use for the agent. */
10
+ llm: BaseLanguageModelInterface;
11
+ /** Tools this agent has access to. */
12
+ tools: ToolInterface[];
13
+ /**
14
+ * The prompt to use. Must have input keys for
15
+ * `tools`, `tool_names`, and `agent_scratchpad`.
16
+ */
17
+ prompt: BasePromptTemplate;
18
+ };
19
+ /**
20
+ * Create an agent that uses ReAct prompting.
21
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
22
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
23
+ * variables as the prompt passed in does. It returns as output either an
24
+ * AgentAction or AgentFinish.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * import { AgentExecutor, createReactAgent } from "langchain/agents";
29
+ * import { pull } from "langchain/hub";
30
+ * import type { PromptTemplate } from "@langchain/core/prompts";
31
+ *
32
+ * import { OpenAI } from "@langchain/openai";
33
+ *
34
+ * // Define the tools the agent will have access to.
35
+ * const tools = [...];
36
+ *
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
40
+ * const prompt = await pull<PromptTemplate>("hwchase17/react");
41
+ *
42
+ * const llm = new OpenAI({
43
+ * temperature: 0,
44
+ * });
45
+ *
46
+ * const agent = await createReactAgent({
47
+ * llm,
48
+ * tools,
49
+ * prompt,
50
+ * });
51
+ *
52
+ * const agentExecutor = new AgentExecutor({
53
+ * agent,
54
+ * tools,
55
+ * });
56
+ *
57
+ * const result = await agentExecutor.invoke({
58
+ * input: "what is LangChain?",
59
+ * });
60
+ * ```
61
+ */
62
+ export declare function createReactAgent({ llm, tools, prompt, }: CreateReactAgentParams): Promise<RunnableSequence<Record<string, unknown>, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
@@ -0,0 +1,73 @@
1
+ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
2
+ import { renderTextDescription } from "../../tools/render.js";
3
+ import { formatLogToString } from "../format_scratchpad/log.js";
4
+ import { ReActSingleInputOutputParser } from "./output_parser.js";
5
+ /**
6
+ * Create an agent that uses ReAct prompting.
7
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
8
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
9
+ * variables as the prompt passed in does. It returns as output either an
10
+ * AgentAction or AgentFinish.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { AgentExecutor, createReactAgent } from "langchain/agents";
15
+ * import { pull } from "langchain/hub";
16
+ * import type { PromptTemplate } from "@langchain/core/prompts";
17
+ *
18
+ * import { OpenAI } from "@langchain/openai";
19
+ *
20
+ * // Define the tools the agent will have access to.
21
+ * const tools = [...];
22
+ *
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
26
+ * const prompt = await pull<PromptTemplate>("hwchase17/react");
27
+ *
28
+ * const llm = new OpenAI({
29
+ * temperature: 0,
30
+ * });
31
+ *
32
+ * const agent = await createReactAgent({
33
+ * llm,
34
+ * tools,
35
+ * prompt,
36
+ * });
37
+ *
38
+ * const agentExecutor = new AgentExecutor({
39
+ * agent,
40
+ * tools,
41
+ * });
42
+ *
43
+ * const result = await agentExecutor.invoke({
44
+ * input: "what is LangChain?",
45
+ * });
46
+ * ```
47
+ */
48
+ export async function createReactAgent({ llm, tools, prompt, }) {
49
+ const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
50
+ if (missingVariables.length > 0) {
51
+ throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
52
+ }
53
+ const toolNames = tools.map((tool) => tool.name);
54
+ const partialedPrompt = await prompt.partial({
55
+ tools: renderTextDescription(tools),
56
+ tool_names: toolNames.join(", "),
57
+ });
58
+ // TODO: Add .bind to core runnable interface.
59
+ const llmWithStop = llm.bind({
60
+ stop: ["\nObservation:"],
61
+ });
62
+ const agent = RunnableSequence.from([
63
+ RunnablePassthrough.assign({
64
+ agent_scratchpad: (input) => formatLogToString(input.steps),
65
+ }),
66
+ partialedPrompt,
67
+ llmWithStop,
68
+ new ReActSingleInputOutputParser({
69
+ toolNames,
70
+ }),
71
+ ]);
72
+ return agent;
73
+ }
@@ -43,7 +43,6 @@ const FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Parsing LLM output produ
43
43
  * const result = await agent.invoke({
44
44
  * input: "whats the weather in pomfret?",
45
45
  * });
46
- *
47
46
  * ```
48
47
  */
49
48
  class ReActSingleInputOutputParser extends types_js_1.AgentActionOutputParser {
@@ -37,7 +37,6 @@ import { AgentAction, AgentFinish } from "../../schema/index.js";
37
37
  * const result = await agent.invoke({
38
38
  * input: "whats the weather in pomfret?",
39
39
  * });
40
- *
41
40
  * ```
42
41
  */
43
42
  export declare class ReActSingleInputOutputParser extends AgentActionOutputParser {
@@ -40,7 +40,6 @@ const FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = "Parsing LLM output produ
40
40
  * const result = await agent.invoke({
41
41
  * input: "whats the weather in pomfret?",
42
42
  * });
43
- *
44
43
  * ```
45
44
  */
46
45
  export class ReActSingleInputOutputParser extends AgentActionOutputParser {
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StructuredChatAgent = void 0;
3
+ exports.createStructuredChatAgent = exports.StructuredChatAgent = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
+ const runnables_1 = require("@langchain/core/runnables");
5
6
  const llm_chain_js_1 = require("../../chains/llm_chain.cjs");
6
7
  const prompt_js_1 = require("../../prompts/prompt.cjs");
7
8
  const chat_js_1 = require("../../prompts/chat.cjs");
8
9
  const agent_js_1 = require("../agent.cjs");
9
10
  const outputParser_js_1 = require("./outputParser.cjs");
10
11
  const prompt_js_2 = require("./prompt.cjs");
12
+ const render_js_1 = require("../../tools/render.cjs");
13
+ const log_js_1 = require("../format_scratchpad/log.cjs");
11
14
  /**
12
15
  * Agent that interoperates with Structured Tools using React logic.
13
16
  * @augments Agent
@@ -151,3 +154,86 @@ class StructuredChatAgent extends agent_js_1.Agent {
151
154
  }
152
155
  }
153
156
  exports.StructuredChatAgent = StructuredChatAgent;
157
+ /**
158
+ * Create an agent aimed at supporting tools with multiple inputs.
159
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
160
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
161
+ * variables as the prompt passed in does. It returns as output either an
162
+ * AgentAction or AgentFinish.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * import { AgentExecutor, createStructuredChatAgent } from "langchain/agents";
167
+ * import { pull } from "langchain/hub";
168
+ * import type { ChatPromptTemplate } from "@langchain/core/prompts";
169
+ * import { AIMessage, HumanMessage } from "@langchain/core/messages";
170
+ *
171
+ * import { ChatOpenAI } from "@langchain/openai";
172
+ *
173
+ * // Define the tools the agent will have access to.
174
+ * const tools = [...];
175
+ *
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
179
+ * const prompt = await pull<ChatPromptTemplate>(
180
+ * "hwchase17/structured-chat-agent"
181
+ * );
182
+ *
183
+ * const llm = new ChatOpenAI({
184
+ * temperature: 0,
185
+ * modelName: "gpt-3.5-turbo-1106",
186
+ * });
187
+ *
188
+ * const agent = await createStructuredChatAgent({
189
+ * llm,
190
+ * tools,
191
+ * prompt,
192
+ * });
193
+ *
194
+ * const agentExecutor = new AgentExecutor({
195
+ * agent,
196
+ * tools,
197
+ * });
198
+ *
199
+ * const result = await agentExecutor.invoke({
200
+ * input: "what is LangChain?",
201
+ * });
202
+ *
203
+ * // With chat history
204
+ * const result2 = await agentExecutor.invoke({
205
+ * input: "what's my name?",
206
+ * chat_history: [
207
+ * new HumanMessage("hi! my name is cob"),
208
+ * new AIMessage("Hello Cob! How can I assist you today?"),
209
+ * ],
210
+ * });
211
+ * ```
212
+ */
213
+ async function createStructuredChatAgent({ llm, tools, prompt, }) {
214
+ const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
215
+ if (missingVariables.length > 0) {
216
+ throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
217
+ }
218
+ const toolNames = tools.map((tool) => tool.name);
219
+ const partialedPrompt = await prompt.partial({
220
+ tools: (0, render_js_1.renderTextDescriptionAndArgs)(tools),
221
+ tool_names: toolNames.join(", "),
222
+ });
223
+ // TODO: Add .bind to core runnable interface.
224
+ const llmWithStop = llm.bind({
225
+ stop: ["Observation"],
226
+ });
227
+ const agent = runnables_1.RunnableSequence.from([
228
+ runnables_1.RunnablePassthrough.assign({
229
+ agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
230
+ }),
231
+ partialedPrompt,
232
+ llmWithStop,
233
+ outputParser_js_1.StructuredChatOutputParserWithRetries.fromLLM(llm, {
234
+ toolNames,
235
+ }),
236
+ ]);
237
+ return agent;
238
+ }
239
+ exports.createStructuredChatAgent = createStructuredChatAgent;
@@ -1,5 +1,7 @@
1
1
  import type { StructuredToolInterface } from "@langchain/core/tools";
2
2
  import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
3
+ import { RunnableSequence } from "@langchain/core/runnables";
4
+ import type { BasePromptTemplate } from "@langchain/core/prompts";
3
5
  import { BaseMessagePromptTemplate, ChatPromptTemplate } from "../../prompts/chat.js";
4
6
  import { AgentStep } from "../../schema/index.js";
5
7
  import { Optional } from "../../types/type-utils.js";
@@ -90,3 +92,74 @@ export declare class StructuredChatAgent extends Agent {
90
92
  */
91
93
  static fromLLMAndTools(llm: BaseLanguageModelInterface, tools: StructuredToolInterface[], args?: StructuredChatCreatePromptArgs & AgentArgs): StructuredChatAgent;
92
94
  }
95
+ /**
96
+ * Params used by the createStructuredChatAgent function.
97
+ */
98
+ export type CreateStructuredChatAgentParams = {
99
+ /** LLM to use as the agent. */
100
+ llm: BaseLanguageModelInterface;
101
+ /** Tools this agent has access to. */
102
+ tools: StructuredToolInterface[];
103
+ /**
104
+ * The prompt to use. Must have input keys for
105
+ * `tools`, `tool_names`, and `agent_scratchpad`.
106
+ */
107
+ prompt: BasePromptTemplate;
108
+ };
109
+ /**
110
+ * Create an agent aimed at supporting tools with multiple inputs.
111
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
112
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
113
+ * variables as the prompt passed in does. It returns as output either an
114
+ * AgentAction or AgentFinish.
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * import { AgentExecutor, createStructuredChatAgent } from "langchain/agents";
119
+ * import { pull } from "langchain/hub";
120
+ * import type { ChatPromptTemplate } from "@langchain/core/prompts";
121
+ * import { AIMessage, HumanMessage } from "@langchain/core/messages";
122
+ *
123
+ * import { ChatOpenAI } from "@langchain/openai";
124
+ *
125
+ * // Define the tools the agent will have access to.
126
+ * const tools = [...];
127
+ *
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
131
+ * const prompt = await pull<ChatPromptTemplate>(
132
+ * "hwchase17/structured-chat-agent"
133
+ * );
134
+ *
135
+ * const llm = new ChatOpenAI({
136
+ * temperature: 0,
137
+ * modelName: "gpt-3.5-turbo-1106",
138
+ * });
139
+ *
140
+ * const agent = await createStructuredChatAgent({
141
+ * llm,
142
+ * tools,
143
+ * prompt,
144
+ * });
145
+ *
146
+ * const agentExecutor = new AgentExecutor({
147
+ * agent,
148
+ * tools,
149
+ * });
150
+ *
151
+ * const result = await agentExecutor.invoke({
152
+ * input: "what is LangChain?",
153
+ * });
154
+ *
155
+ * // With chat history
156
+ * const result2 = await agentExecutor.invoke({
157
+ * input: "what's my name?",
158
+ * chat_history: [
159
+ * new HumanMessage("hi! my name is cob"),
160
+ * new AIMessage("Hello Cob! How can I assist you today?"),
161
+ * ],
162
+ * });
163
+ * ```
164
+ */
165
+ export declare function createStructuredChatAgent({ llm, tools, prompt, }: CreateStructuredChatAgentParams): Promise<RunnableSequence<Record<string, unknown>, import("../../schema/index.js").AgentAction | import("../../schema/index.js").AgentFinish>>;
@@ -1,10 +1,13 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
+ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
2
3
  import { LLMChain } from "../../chains/llm_chain.js";
3
4
  import { PromptTemplate } from "../../prompts/prompt.js";
4
5
  import { ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, } from "../../prompts/chat.js";
5
6
  import { Agent } from "../agent.js";
6
7
  import { StructuredChatOutputParserWithRetries } from "./outputParser.js";
7
8
  import { FORMAT_INSTRUCTIONS, PREFIX, SUFFIX } from "./prompt.js";
9
+ import { renderTextDescriptionAndArgs } from "../../tools/render.js";
10
+ import { formatLogToString } from "../format_scratchpad/log.js";
8
11
  /**
9
12
  * Agent that interoperates with Structured Tools using React logic.
10
13
  * @augments Agent
@@ -147,3 +150,85 @@ export class StructuredChatAgent extends Agent {
147
150
  });
148
151
  }
149
152
  }
153
+ /**
154
+ * Create an agent aimed at supporting tools with multiple inputs.
155
+ * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
156
+ * @returns A runnable sequence representing an agent. It takes as input all the same input
157
+ * variables as the prompt passed in does. It returns as output either an
158
+ * AgentAction or AgentFinish.
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * import { AgentExecutor, createStructuredChatAgent } from "langchain/agents";
163
+ * import { pull } from "langchain/hub";
164
+ * import type { ChatPromptTemplate } from "@langchain/core/prompts";
165
+ * import { AIMessage, HumanMessage } from "@langchain/core/messages";
166
+ *
167
+ * import { ChatOpenAI } from "@langchain/openai";
168
+ *
169
+ * // Define the tools the agent will have access to.
170
+ * const tools = [...];
171
+ *
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
175
+ * const prompt = await pull<ChatPromptTemplate>(
176
+ * "hwchase17/structured-chat-agent"
177
+ * );
178
+ *
179
+ * const llm = new ChatOpenAI({
180
+ * temperature: 0,
181
+ * modelName: "gpt-3.5-turbo-1106",
182
+ * });
183
+ *
184
+ * const agent = await createStructuredChatAgent({
185
+ * llm,
186
+ * tools,
187
+ * prompt,
188
+ * });
189
+ *
190
+ * const agentExecutor = new AgentExecutor({
191
+ * agent,
192
+ * tools,
193
+ * });
194
+ *
195
+ * const result = await agentExecutor.invoke({
196
+ * input: "what is LangChain?",
197
+ * });
198
+ *
199
+ * // With chat history
200
+ * const result2 = await agentExecutor.invoke({
201
+ * input: "what's my name?",
202
+ * chat_history: [
203
+ * new HumanMessage("hi! my name is cob"),
204
+ * new AIMessage("Hello Cob! How can I assist you today?"),
205
+ * ],
206
+ * });
207
+ * ```
208
+ */
209
+ export async function createStructuredChatAgent({ llm, tools, prompt, }) {
210
+ const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
211
+ if (missingVariables.length > 0) {
212
+ throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
213
+ }
214
+ const toolNames = tools.map((tool) => tool.name);
215
+ const partialedPrompt = await prompt.partial({
216
+ tools: renderTextDescriptionAndArgs(tools),
217
+ tool_names: toolNames.join(", "),
218
+ });
219
+ // TODO: Add .bind to core runnable interface.
220
+ const llmWithStop = llm.bind({
221
+ stop: ["Observation"],
222
+ });
223
+ const agent = RunnableSequence.from([
224
+ RunnablePassthrough.assign({
225
+ agent_scratchpad: (input) => formatLogToString(input.steps),
226
+ }),
227
+ partialedPrompt,
228
+ llmWithStop,
229
+ StructuredChatOutputParserWithRetries.fromLLM(llm, {
230
+ toolNames,
231
+ }),
232
+ ]);
233
+ return agent;
234
+ }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OpenAIAgentTokenBufferMemory = void 0;
4
4
  const base_js_1 = require("../../../memory/base.cjs");
5
5
  const chat_memory_js_1 = require("../../../memory/chat_memory.cjs");
6
- const index_js_1 = require("../../openai/index.cjs");
6
+ const index_js_1 = require("../../openai_functions/index.cjs");
7
7
  /**
8
8
  * Memory used to save agent output and intermediate steps.
9
9
  */
@@ -1,6 +1,6 @@
1
1
  import { getBufferString, getInputValue, getOutputValue, } from "../../../memory/base.js";
2
2
  import { BaseChatMemory, } from "../../../memory/chat_memory.js";
3
- import { _formatIntermediateSteps } from "../../openai/index.js";
3
+ import { _formatIntermediateSteps } from "../../openai_functions/index.js";
4
4
  /**
5
5
  * Memory used to save agent output and intermediate steps.
6
6
  */
@@ -4,6 +4,7 @@ exports.createRetrieverTool = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const dynamic_js_1 = require("../../../tools/dynamic.cjs");
6
6
  const document_js_1 = require("../../../util/document.cjs");
7
+ /** @deprecated Use "langchain/tools/retriever" instead. */
7
8
  function createRetrieverTool(retriever, input) {
8
9
  const func = async ({ input }, runManager) => {
9
10
  const docs = await retriever.getRelevantDocuments(input, runManager?.getChild("retriever"));
@@ -1,6 +1,7 @@
1
1
  import type { BaseRetrieverInterface } from "@langchain/core/retrievers";
2
2
  import { z } from "zod";
3
3
  import { DynamicStructuredTool, DynamicStructuredToolInput } from "../../../tools/dynamic.js";
4
+ /** @deprecated Use "langchain/tools/retriever" instead. */
4
5
  export declare function createRetrieverTool(retriever: BaseRetrieverInterface, input: Omit<DynamicStructuredToolInput, "func" | "schema">): DynamicStructuredTool<z.ZodObject<{
5
6
  input: z.ZodString;
6
7
  }, "strip", z.ZodTypeAny, {
@@ -1,6 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { DynamicStructuredTool, } from "../../../tools/dynamic.js";
3
3
  import { formatDocumentsAsString } from "../../../util/document.js";
4
+ /** @deprecated Use "langchain/tools/retriever" instead. */
4
5
  export function createRetrieverTool(retriever, input) {
5
6
  const func = async ({ input }, runManager) => {
6
7
  const docs = await retriever.getRelevantDocuments(input, runManager?.getChild("retriever"));