langchain 0.1.29-rc.0 → 0.1.30

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Agent = exports.LLMSingleActionAgent = exports.RunnableAgent = exports.RunnableMultiActionAgent = exports.RunnableSingleActionAgent = exports.isRunnableAgent = exports.BaseMultiActionAgent = exports.BaseSingleActionAgent = exports.BaseAgent = void 0;
3
+ exports.Agent = exports.LLMSingleActionAgent = exports.RunnableAgent = exports.RunnableMultiActionAgent = exports.RunnableSingleActionAgent = exports.AgentRunnableSequence = exports.isRunnableAgent = exports.BaseMultiActionAgent = exports.BaseSingleActionAgent = exports.BaseAgent = void 0;
4
4
  const serializable_1 = require("@langchain/core/load/serializable");
5
5
  const runnables_1 = require("@langchain/core/runnables");
6
6
  /**
@@ -86,6 +86,37 @@ function isRunnableAgent(x) {
86
86
  undefined);
87
87
  }
88
88
  exports.isRunnableAgent = isRunnableAgent;
89
+ // TODO: Remove in the future. Only for backwards compatibility.
90
+ // Allows for the creation of runnables with properties that will
91
+ // be passed to the agent executor constructor.
92
+ class AgentRunnableSequence extends runnables_1.RunnableSequence {
93
+ constructor() {
94
+ super(...arguments);
95
+ Object.defineProperty(this, "streamRunnable", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "singleAction", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ }
108
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
109
+ static fromRunnables([first, ...runnables], config) {
110
+ const sequence = runnables_1.RunnableSequence.from([first, ...runnables], config.name);
111
+ sequence.singleAction = config.singleAction;
112
+ sequence.streamRunnable = config.streamRunnable;
113
+ return sequence;
114
+ }
115
+ static isAgentRunnableSequence(x) {
116
+ return typeof x.singleAction === "boolean";
117
+ }
118
+ }
119
+ exports.AgentRunnableSequence = AgentRunnableSequence;
89
120
  /**
90
121
  * Class representing a single-action agent powered by runnables.
91
122
  * Extends the BaseSingleActionAgent class and provides methods for
@@ -133,7 +164,8 @@ class RunnableSingleActionAgent extends BaseSingleActionAgent {
133
164
  value: "RunnableAgent"
134
165
  });
135
166
  this.runnable = fields.runnable;
136
- this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
167
+ this.defaultRunName =
168
+ fields.defaultRunName ?? this.runnable.name ?? this.defaultRunName;
137
169
  this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
138
170
  }
139
171
  async plan(steps, inputs, callbackManager, config) {
@@ -6,7 +6,7 @@ import { AgentAction, AgentFinish, AgentStep } from "@langchain/core/agents";
6
6
  import { BaseMessage } from "@langchain/core/messages";
7
7
  import { ChainValues } from "@langchain/core/utils/types";
8
8
  import { Serializable } from "@langchain/core/load/serializable";
9
- import { Runnable, type RunnableConfig } from "@langchain/core/runnables";
9
+ import { Runnable, type RunnableConfig, RunnableSequence, RunnableLike } from "@langchain/core/runnables";
10
10
  import { LLMChain } from "../chains/llm_chain.js";
11
11
  import type { AgentActionOutputParser, AgentInput, RunnableMultiActionAgentInput, RunnableSingleActionAgentInput, SerializedAgent, StoppingMethod } from "./types.js";
12
12
  /**
@@ -76,6 +76,20 @@ export declare abstract class BaseMultiActionAgent extends BaseAgent {
76
76
  abstract plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager, config?: RunnableConfig): Promise<AgentAction[] | AgentFinish>;
77
77
  }
78
78
  export declare function isRunnableAgent(x: BaseAgent): boolean;
79
+ export declare class AgentRunnableSequence<RunInput = any, RunOutput = any> extends RunnableSequence<RunInput, RunOutput> {
80
+ streamRunnable?: boolean;
81
+ singleAction: boolean;
82
+ static fromRunnables<RunInput = any, RunOutput = any>([first, ...runnables]: [
83
+ RunnableLike<RunInput>,
84
+ ...RunnableLike[],
85
+ RunnableLike<any, RunOutput>
86
+ ], config: {
87
+ singleAction: boolean;
88
+ streamRunnable?: boolean;
89
+ name?: string;
90
+ }): AgentRunnableSequence<RunInput, Exclude<RunOutput, Error>>;
91
+ static isAgentRunnableSequence(x: Runnable): x is AgentRunnableSequence;
92
+ }
79
93
  /**
80
94
  * Class representing a single-action agent powered by runnables.
81
95
  * Extends the BaseSingleActionAgent class and provides methods for
@@ -1,5 +1,5 @@
1
1
  import { Serializable } from "@langchain/core/load/serializable";
2
- import { patchConfig, } from "@langchain/core/runnables";
2
+ import { patchConfig, RunnableSequence, } from "@langchain/core/runnables";
3
3
  /**
4
4
  * Error class for parse errors in LangChain. Contains information about
5
5
  * the error message and the output that caused the error.
@@ -79,6 +79,36 @@ export function isRunnableAgent(x) {
79
79
  return (x.runnable !==
80
80
  undefined);
81
81
  }
82
+ // TODO: Remove in the future. Only for backwards compatibility.
83
+ // Allows for the creation of runnables with properties that will
84
+ // be passed to the agent executor constructor.
85
+ export class AgentRunnableSequence extends RunnableSequence {
86
+ constructor() {
87
+ super(...arguments);
88
+ Object.defineProperty(this, "streamRunnable", {
89
+ enumerable: true,
90
+ configurable: true,
91
+ writable: true,
92
+ value: void 0
93
+ });
94
+ Object.defineProperty(this, "singleAction", {
95
+ enumerable: true,
96
+ configurable: true,
97
+ writable: true,
98
+ value: void 0
99
+ });
100
+ }
101
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
+ static fromRunnables([first, ...runnables], config) {
103
+ const sequence = RunnableSequence.from([first, ...runnables], config.name);
104
+ sequence.singleAction = config.singleAction;
105
+ sequence.streamRunnable = config.streamRunnable;
106
+ return sequence;
107
+ }
108
+ static isAgentRunnableSequence(x) {
109
+ return typeof x.singleAction === "boolean";
110
+ }
111
+ }
82
112
  /**
83
113
  * Class representing a single-action agent powered by runnables.
84
114
  * Extends the BaseSingleActionAgent class and provides methods for
@@ -126,7 +156,8 @@ export class RunnableSingleActionAgent extends BaseSingleActionAgent {
126
156
  value: "RunnableAgent"
127
157
  });
128
158
  this.runnable = fields.runnable;
129
- this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
159
+ this.defaultRunName =
160
+ fields.defaultRunName ?? this.runnable.name ?? this.defaultRunName;
130
161
  this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
131
162
  }
132
163
  async plan(steps, inputs, callbackManager, config) {
@@ -282,7 +282,23 @@ class AgentExecutor extends base_js_1.BaseChain {
282
282
  let agent;
283
283
  let returnOnlyOutputs = true;
284
284
  if (runnables_1.Runnable.isRunnable(input.agent)) {
285
- agent = new agent_js_1.RunnableMultiActionAgent({ runnable: input.agent });
285
+ if (agent_js_1.AgentRunnableSequence.isAgentRunnableSequence(input.agent)) {
286
+ if (input.agent.singleAction) {
287
+ agent = new agent_js_1.RunnableSingleActionAgent({
288
+ runnable: input.agent,
289
+ streamRunnable: input.agent.streamRunnable,
290
+ });
291
+ }
292
+ else {
293
+ agent = new agent_js_1.RunnableMultiActionAgent({
294
+ runnable: input.agent,
295
+ streamRunnable: input.agent.streamRunnable,
296
+ });
297
+ }
298
+ }
299
+ else {
300
+ agent = new agent_js_1.RunnableMultiActionAgent({ runnable: input.agent });
301
+ }
286
302
  // TODO: Update BaseChain implementation on breaking change
287
303
  returnOnlyOutputs = false;
288
304
  }
@@ -3,7 +3,7 @@ import { Runnable, patchConfig, } from "@langchain/core/runnables";
3
3
  import { CallbackManager, } from "@langchain/core/callbacks/manager";
4
4
  import { OutputParserException } from "@langchain/core/output_parsers";
5
5
  import { Serializable } from "@langchain/core/load/serializable";
6
- import { RunnableMultiActionAgent, isRunnableAgent, } from "./agent.js";
6
+ import { AgentRunnableSequence, RunnableMultiActionAgent, RunnableSingleActionAgent, isRunnableAgent, } from "./agent.js";
7
7
  import { BaseChain } from "../chains/base.js";
8
8
  export class AgentExecutorIterator extends Serializable {
9
9
  get finalOutputs() {
@@ -277,7 +277,23 @@ export class AgentExecutor extends BaseChain {
277
277
  let agent;
278
278
  let returnOnlyOutputs = true;
279
279
  if (Runnable.isRunnable(input.agent)) {
280
- agent = new RunnableMultiActionAgent({ runnable: input.agent });
280
+ if (AgentRunnableSequence.isAgentRunnableSequence(input.agent)) {
281
+ if (input.agent.singleAction) {
282
+ agent = new RunnableSingleActionAgent({
283
+ runnable: input.agent,
284
+ streamRunnable: input.agent.streamRunnable,
285
+ });
286
+ }
287
+ else {
288
+ agent = new RunnableMultiActionAgent({
289
+ runnable: input.agent,
290
+ streamRunnable: input.agent.streamRunnable,
291
+ });
292
+ }
293
+ }
294
+ else {
295
+ agent = new RunnableMultiActionAgent({ runnable: input.agent });
296
+ }
281
297
  // TODO: Update BaseChain implementation on breaking change
282
298
  returnOnlyOutputs = false;
283
299
  }
@@ -229,18 +229,18 @@ async function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable,
229
229
  const llmWithTools = llm.bind({
230
230
  functions: tools.map(function_calling_1.convertToOpenAIFunction),
231
231
  });
232
- const agent = runnables_1.RunnableSequence.from([
232
+ const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
233
233
  runnables_1.RunnablePassthrough.assign({
234
234
  agent_scratchpad: (input) => (0, openai_functions_js_1.formatToOpenAIFunctionMessages)(input.steps),
235
235
  }),
236
236
  prompt,
237
237
  llmWithTools,
238
238
  new output_parser_js_1.OpenAIFunctionsAgentOutputParser(),
239
- ]);
240
- return new agent_js_1.RunnableSingleActionAgent({
241
- runnable: agent,
242
- defaultRunName: "OpenAIFunctionsAgent",
239
+ ], {
240
+ name: "OpenAIFunctionsAgent",
243
241
  streamRunnable,
242
+ singleAction: true,
244
243
  });
244
+ return agent;
245
245
  }
246
246
  exports.createOpenAIFunctionsAgent = createOpenAIFunctionsAgent;
@@ -6,7 +6,7 @@ import { BaseMessage, SystemMessage } from "@langchain/core/messages";
6
6
  import { ChainValues } from "@langchain/core/utils/types";
7
7
  import { ChatPromptTemplate, BasePromptTemplate } from "@langchain/core/prompts";
8
8
  import { CallbackManager } from "@langchain/core/callbacks/manager";
9
- import { Agent, AgentArgs, RunnableSingleActionAgent } from "../agent.js";
9
+ import { Agent, AgentArgs, AgentRunnableSequence } from "../agent.js";
10
10
  import { AgentInput } from "../types.js";
11
11
  import { OpenAIFunctionsAgentOutputParser } from "../openai/output_parser.js";
12
12
  export declare function _formatIntermediateSteps(intermediateSteps: AgentStep[]): BaseMessage[];
@@ -148,4 +148,6 @@ export type CreateOpenAIFunctionsAgentParams = {
148
148
  * });
149
149
  * ```
150
150
  */
151
- export declare function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIFunctionsAgentParams): Promise<RunnableSingleActionAgent>;
151
+ export declare function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIFunctionsAgentParams): Promise<AgentRunnableSequence<{
152
+ steps: AgentStep[];
153
+ }, AgentAction | AgentFinish>>;
@@ -1,8 +1,8 @@
1
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
1
+ import { RunnablePassthrough } from "@langchain/core/runnables";
2
2
  import { convertToOpenAIFunction } from "@langchain/core/utils/function_calling";
3
3
  import { AIMessage, FunctionMessage, } from "@langchain/core/messages";
4
4
  import { ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, } from "@langchain/core/prompts";
5
- import { Agent, RunnableSingleActionAgent } from "../agent.js";
5
+ import { Agent, AgentRunnableSequence } from "../agent.js";
6
6
  import { PREFIX } from "./prompt.js";
7
7
  import { LLMChain } from "../../chains/llm_chain.js";
8
8
  import { OpenAIFunctionsAgentOutputParser, } from "../openai/output_parser.js";
@@ -224,17 +224,17 @@ export async function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRun
224
224
  const llmWithTools = llm.bind({
225
225
  functions: tools.map(convertToOpenAIFunction),
226
226
  });
227
- const agent = RunnableSequence.from([
227
+ const agent = AgentRunnableSequence.fromRunnables([
228
228
  RunnablePassthrough.assign({
229
229
  agent_scratchpad: (input) => formatToOpenAIFunctionMessages(input.steps),
230
230
  }),
231
231
  prompt,
232
232
  llmWithTools,
233
233
  new OpenAIFunctionsAgentOutputParser(),
234
- ]);
235
- return new RunnableSingleActionAgent({
236
- runnable: agent,
237
- defaultRunName: "OpenAIFunctionsAgent",
234
+ ], {
235
+ name: "OpenAIFunctionsAgent",
238
236
  streamRunnable,
237
+ singleAction: true,
239
238
  });
239
+ return agent;
240
240
  }
@@ -71,18 +71,18 @@ async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }) {
71
71
  ].join("\n"));
72
72
  }
73
73
  const modelWithTools = llm.bind({ tools: tools.map(function_calling_1.convertToOpenAITool) });
74
- const agent = runnables_1.RunnableSequence.from([
74
+ const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
75
75
  runnables_1.RunnablePassthrough.assign({
76
76
  agent_scratchpad: (input) => (0, openai_tools_js_1.formatToOpenAIToolMessages)(input.steps),
77
77
  }),
78
78
  prompt,
79
79
  modelWithTools,
80
80
  new output_parser_js_1.OpenAIToolsAgentOutputParser(),
81
- ]);
82
- return new agent_js_1.RunnableMultiActionAgent({
83
- runnable: agent,
84
- defaultRunName: "OpenAIToolsAgent",
81
+ ], {
82
+ name: "OpenAIToolsAgent",
85
83
  streamRunnable,
84
+ singleAction: false,
86
85
  });
86
+ return agent;
87
87
  }
88
88
  exports.createOpenAIToolsAgent = createOpenAIToolsAgent;
@@ -3,7 +3,7 @@ import type { BaseChatModel, BaseChatModelCallOptions } from "@langchain/core/la
3
3
  import { ChatPromptTemplate } from "@langchain/core/prompts";
4
4
  import { OpenAIClient } from "@langchain/openai";
5
5
  import { OpenAIToolsAgentOutputParser, type ToolsAgentStep } from "../openai/output_parser.js";
6
- import { RunnableMultiActionAgent } from "../agent.js";
6
+ import { AgentRunnableSequence } from "../agent.js";
7
7
  export { OpenAIToolsAgentOutputParser, type ToolsAgentStep };
8
8
  /**
9
9
  * Params used by the createOpenAIToolsAgent function.
@@ -15,8 +15,7 @@ export type CreateOpenAIToolsAgentParams = {
15
15
  * a different model that adds in equivalent support.
16
16
  */
17
17
  llm: BaseChatModel<BaseChatModelCallOptions & {
18
- tools?: StructuredToolInterface[] | OpenAIClient.ChatCompletionTool[];
19
- tool_choice?: OpenAIClient.ChatCompletionToolChoiceOption;
18
+ tools?: StructuredToolInterface[] | OpenAIClient.ChatCompletionTool[] | any[];
20
19
  }>;
21
20
  /** Tools this agent has access to. */
22
21
  tools: StructuredToolInterface[];
@@ -84,4 +83,6 @@ export type CreateOpenAIToolsAgentParams = {
84
83
  * });
85
84
  * ```
86
85
  */
87
- export declare function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIToolsAgentParams): Promise<RunnableMultiActionAgent>;
86
+ export declare function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIToolsAgentParams): Promise<AgentRunnableSequence<{
87
+ steps: ToolsAgentStep[];
88
+ }, import("@langchain/core/agents").AgentFinish | import("@langchain/core/agents").AgentAction[]>>;
@@ -1,8 +1,8 @@
1
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
1
+ import { RunnablePassthrough } from "@langchain/core/runnables";
2
2
  import { convertToOpenAITool } from "@langchain/core/utils/function_calling";
3
3
  import { formatToOpenAIToolMessages } from "../format_scratchpad/openai_tools.js";
4
4
  import { OpenAIToolsAgentOutputParser, } from "../openai/output_parser.js";
5
- import { RunnableMultiActionAgent } from "../agent.js";
5
+ import { AgentRunnableSequence } from "../agent.js";
6
6
  export { OpenAIToolsAgentOutputParser };
7
7
  /**
8
8
  * Create an agent that uses OpenAI-style tool calling.
@@ -68,17 +68,17 @@ export async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnabl
68
68
  ].join("\n"));
69
69
  }
70
70
  const modelWithTools = llm.bind({ tools: tools.map(convertToOpenAITool) });
71
- const agent = RunnableSequence.from([
71
+ const agent = AgentRunnableSequence.fromRunnables([
72
72
  RunnablePassthrough.assign({
73
73
  agent_scratchpad: (input) => formatToOpenAIToolMessages(input.steps),
74
74
  }),
75
75
  prompt,
76
76
  modelWithTools,
77
77
  new OpenAIToolsAgentOutputParser(),
78
- ]);
79
- return new RunnableMultiActionAgent({
80
- runnable: agent,
81
- defaultRunName: "OpenAIToolsAgent",
78
+ ], {
79
+ name: "OpenAIToolsAgent",
82
80
  streamRunnable,
81
+ singleAction: false,
83
82
  });
83
+ return agent;
84
84
  }
@@ -63,7 +63,7 @@ async function createReactAgent({ llm, tools, prompt, streamRunnable, }) {
63
63
  const llmWithStop = llm.bind({
64
64
  stop: ["\nObservation:"],
65
65
  });
66
- const agent = runnables_1.RunnableSequence.from([
66
+ const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
67
67
  runnables_1.RunnablePassthrough.assign({
68
68
  agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
69
69
  }),
@@ -72,11 +72,11 @@ async function createReactAgent({ llm, tools, prompt, streamRunnable, }) {
72
72
  new output_parser_js_1.ReActSingleInputOutputParser({
73
73
  toolNames,
74
74
  }),
75
- ]);
76
- return new agent_js_1.RunnableSingleActionAgent({
77
- runnable: agent,
78
- defaultRunName: "ReactAgent",
75
+ ], {
76
+ name: "ReactAgent",
79
77
  streamRunnable,
78
+ singleAction: true,
80
79
  });
80
+ return agent;
81
81
  }
82
82
  exports.createReactAgent = createReactAgent;
@@ -1,7 +1,8 @@
1
1
  import type { ToolInterface } from "@langchain/core/tools";
2
2
  import { BasePromptTemplate } from "@langchain/core/prompts";
3
3
  import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
4
- import { RunnableSingleActionAgent } from "../agent.js";
4
+ import { AgentStep } from "@langchain/core/agents";
5
+ import { AgentRunnableSequence } from "../agent.js";
5
6
  /**
6
7
  * Params used by the createXmlAgent function.
7
8
  */
@@ -64,4 +65,6 @@ export type CreateReactAgentParams = {
64
65
  * });
65
66
  * ```
66
67
  */
67
- export declare function createReactAgent({ llm, tools, prompt, streamRunnable, }: CreateReactAgentParams): Promise<RunnableSingleActionAgent>;
68
+ export declare function createReactAgent({ llm, tools, prompt, streamRunnable, }: CreateReactAgentParams): Promise<AgentRunnableSequence<{
69
+ steps: AgentStep[];
70
+ }, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
@@ -1,8 +1,8 @@
1
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
1
+ import { RunnablePassthrough } from "@langchain/core/runnables";
2
2
  import { renderTextDescription } from "../../tools/render.js";
3
3
  import { formatLogToString } from "../format_scratchpad/log.js";
4
4
  import { ReActSingleInputOutputParser } from "./output_parser.js";
5
- import { RunnableSingleActionAgent } from "../agent.js";
5
+ import { AgentRunnableSequence } from "../agent.js";
6
6
  /**
7
7
  * Create an agent that uses ReAct prompting.
8
8
  * @param params Params required to create the agent. Includes an LLM, tools, and prompt.
@@ -60,7 +60,7 @@ export async function createReactAgent({ llm, tools, prompt, streamRunnable, })
60
60
  const llmWithStop = llm.bind({
61
61
  stop: ["\nObservation:"],
62
62
  });
63
- const agent = RunnableSequence.from([
63
+ const agent = AgentRunnableSequence.fromRunnables([
64
64
  RunnablePassthrough.assign({
65
65
  agent_scratchpad: (input) => formatLogToString(input.steps),
66
66
  }),
@@ -69,10 +69,10 @@ export async function createReactAgent({ llm, tools, prompt, streamRunnable, })
69
69
  new ReActSingleInputOutputParser({
70
70
  toolNames,
71
71
  }),
72
- ]);
73
- return new RunnableSingleActionAgent({
74
- runnable: agent,
75
- defaultRunName: "ReactAgent",
72
+ ], {
73
+ name: "ReactAgent",
76
74
  streamRunnable,
75
+ singleAction: true,
77
76
  });
77
+ return agent;
78
78
  }
@@ -224,7 +224,7 @@ async function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }
224
224
  const llmWithStop = llm.bind({
225
225
  stop: ["Observation"],
226
226
  });
227
- const agent = runnables_1.RunnableSequence.from([
227
+ const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
228
228
  runnables_1.RunnablePassthrough.assign({
229
229
  agent_scratchpad: (input) => (0, log_js_1.formatLogToString)(input.steps),
230
230
  }),
@@ -233,11 +233,11 @@ async function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }
233
233
  outputParser_js_1.StructuredChatOutputParserWithRetries.fromLLM(llm, {
234
234
  toolNames,
235
235
  }),
236
- ]);
237
- return new agent_js_1.RunnableSingleActionAgent({
238
- runnable: agent,
239
- defaultRunName: "StructuredChatAgent",
236
+ ], {
237
+ name: "StructuredChatAgent",
240
238
  streamRunnable,
239
+ singleAction: true,
241
240
  });
241
+ return agent;
242
242
  }
243
243
  exports.createStructuredChatAgent = createStructuredChatAgent;
@@ -4,7 +4,7 @@ import type { BasePromptTemplate } from "@langchain/core/prompts";
4
4
  import { BaseMessagePromptTemplate, ChatPromptTemplate } from "@langchain/core/prompts";
5
5
  import { AgentStep } from "@langchain/core/agents";
6
6
  import { Optional } from "../../types/type-utils.js";
7
- import { Agent, AgentArgs, OutputParserArgs, RunnableSingleActionAgent } from "../agent.js";
7
+ import { Agent, AgentArgs, AgentRunnableSequence, OutputParserArgs } from "../agent.js";
8
8
  import { AgentInput } from "../types.js";
9
9
  import { StructuredChatOutputParserWithRetries } from "./outputParser.js";
10
10
  /**
@@ -167,4 +167,6 @@ export type CreateStructuredChatAgentParams = {
167
167
  * });
168
168
  * ```
169
169
  */
170
- export declare function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }: CreateStructuredChatAgentParams): Promise<RunnableSingleActionAgent>;
170
+ export declare function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }: CreateStructuredChatAgentParams): Promise<AgentRunnableSequence<{
171
+ steps: AgentStep[];
172
+ }, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
@@ -1,8 +1,8 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
2
+ import { RunnablePassthrough } from "@langchain/core/runnables";
3
3
  import { ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, PromptTemplate, } from "@langchain/core/prompts";
4
4
  import { LLMChain } from "../../chains/llm_chain.js";
5
- import { Agent, RunnableSingleActionAgent, } from "../agent.js";
5
+ import { Agent, AgentRunnableSequence, } from "../agent.js";
6
6
  import { StructuredChatOutputParserWithRetries } from "./outputParser.js";
7
7
  import { FORMAT_INSTRUCTIONS, PREFIX, SUFFIX } from "./prompt.js";
8
8
  import { renderTextDescriptionAndArgs } from "../../tools/render.js";
@@ -220,7 +220,7 @@ export async function createStructuredChatAgent({ llm, tools, prompt, streamRunn
220
220
  const llmWithStop = llm.bind({
221
221
  stop: ["Observation"],
222
222
  });
223
- const agent = RunnableSequence.from([
223
+ const agent = AgentRunnableSequence.fromRunnables([
224
224
  RunnablePassthrough.assign({
225
225
  agent_scratchpad: (input) => formatLogToString(input.steps),
226
226
  }),
@@ -229,10 +229,10 @@ export async function createStructuredChatAgent({ llm, tools, prompt, streamRunn
229
229
  StructuredChatOutputParserWithRetries.fromLLM(llm, {
230
230
  toolNames,
231
231
  }),
232
- ]);
233
- return new RunnableSingleActionAgent({
234
- runnable: agent,
235
- defaultRunName: "StructuredChatAgent",
232
+ ], {
233
+ name: "StructuredChatAgent",
236
234
  streamRunnable,
235
+ singleAction: true,
237
236
  });
237
+ return agent;
238
238
  }
@@ -168,18 +168,18 @@ async function createXmlAgent({ llm, tools, prompt, streamRunnable, }) {
168
168
  const llmWithStop = llm.bind({
169
169
  stop: ["</tool_input>", "</final_answer>"],
170
170
  });
171
- const agent = runnables_1.RunnableSequence.from([
171
+ const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
172
172
  runnables_1.RunnablePassthrough.assign({
173
173
  agent_scratchpad: (input) => (0, xml_js_1.formatXml)(input.steps),
174
174
  }),
175
175
  partialedPrompt,
176
176
  llmWithStop,
177
177
  new output_parser_js_1.XMLAgentOutputParser(),
178
- ]);
179
- return new agent_js_1.RunnableSingleActionAgent({
180
- runnable: agent,
181
- defaultRunName: "XMLAgent",
178
+ ], {
179
+ name: "XMLAgent",
182
180
  streamRunnable,
181
+ singleAction: true,
183
182
  });
183
+ return agent;
184
184
  }
185
185
  exports.createXmlAgent = createXmlAgent;
@@ -6,7 +6,7 @@ import { ChainValues } from "@langchain/core/utils/types";
6
6
  import { ChatPromptTemplate } from "@langchain/core/prompts";
7
7
  import { CallbackManager } from "@langchain/core/callbacks/manager";
8
8
  import { LLMChain } from "../../chains/llm_chain.js";
9
- import { AgentArgs, BaseSingleActionAgent, RunnableSingleActionAgent } from "../agent.js";
9
+ import { AgentArgs, AgentRunnableSequence, BaseSingleActionAgent } from "../agent.js";
10
10
  import { XMLAgentOutputParser } from "./output_parser.js";
11
11
  /**
12
12
  * Interface for the input to the XMLAgent class.
@@ -117,4 +117,6 @@ export type CreateXmlAgentParams = {
117
117
  * });
118
118
  * ```
119
119
  */
120
- export declare function createXmlAgent({ llm, tools, prompt, streamRunnable, }: CreateXmlAgentParams): Promise<RunnableSingleActionAgent>;
120
+ export declare function createXmlAgent({ llm, tools, prompt, streamRunnable, }: CreateXmlAgentParams): Promise<AgentRunnableSequence<{
121
+ steps: AgentStep[];
122
+ }, AgentAction | AgentFinish>>;
@@ -1,7 +1,7 @@
1
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
1
+ import { RunnablePassthrough } from "@langchain/core/runnables";
2
2
  import { AIMessagePromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, } from "@langchain/core/prompts";
3
3
  import { LLMChain } from "../../chains/llm_chain.js";
4
- import { BaseSingleActionAgent, RunnableSingleActionAgent, } from "../agent.js";
4
+ import { AgentRunnableSequence, BaseSingleActionAgent, } from "../agent.js";
5
5
  import { AGENT_INSTRUCTIONS } from "./prompt.js";
6
6
  import { XMLAgentOutputParser } from "./output_parser.js";
7
7
  import { renderTextDescription } from "../../tools/render.js";
@@ -164,17 +164,17 @@ export async function createXmlAgent({ llm, tools, prompt, streamRunnable, }) {
164
164
  const llmWithStop = llm.bind({
165
165
  stop: ["</tool_input>", "</final_answer>"],
166
166
  });
167
- const agent = RunnableSequence.from([
167
+ const agent = AgentRunnableSequence.fromRunnables([
168
168
  RunnablePassthrough.assign({
169
169
  agent_scratchpad: (input) => formatXml(input.steps),
170
170
  }),
171
171
  partialedPrompt,
172
172
  llmWithStop,
173
173
  new XMLAgentOutputParser(),
174
- ]);
175
- return new RunnableSingleActionAgent({
176
- runnable: agent,
177
- defaultRunName: "XMLAgent",
174
+ ], {
175
+ name: "XMLAgent",
178
176
  streamRunnable,
177
+ singleAction: true,
179
178
  });
179
+ return agent;
180
180
  }
@@ -145,7 +145,7 @@ class TextSplitter extends documents_1.BaseDocumentTransformer {
145
145
  let total = 0;
146
146
  for (const d of splits) {
147
147
  const _len = await this.lengthFunction(d);
148
- if (total + _len + (currentDoc.length > 0 ? separator.length : 0) >
148
+ if (total + _len + currentDoc.length * separator.length >
149
149
  this.chunkSize) {
150
150
  if (total > this.chunkSize) {
151
151
  console.warn(`Created a chunk of size ${total}, +
@@ -160,7 +160,9 @@ which is longer than the specified ${this.chunkSize}`);
160
160
  // - we have a larger chunk than in the chunk overlap
161
161
  // - or if we still have any chunks and the length is long
162
162
  while (total > this.chunkOverlap ||
163
- (total + _len > this.chunkSize && total > 0)) {
163
+ (total + _len + currentDoc.length * separator.length >
164
+ this.chunkSize &&
165
+ total > 0)) {
164
166
  total -= await this.lengthFunction(currentDoc[0]);
165
167
  currentDoc.shift();
166
168
  }
@@ -142,7 +142,7 @@ export class TextSplitter extends BaseDocumentTransformer {
142
142
  let total = 0;
143
143
  for (const d of splits) {
144
144
  const _len = await this.lengthFunction(d);
145
- if (total + _len + (currentDoc.length > 0 ? separator.length : 0) >
145
+ if (total + _len + currentDoc.length * separator.length >
146
146
  this.chunkSize) {
147
147
  if (total > this.chunkSize) {
148
148
  console.warn(`Created a chunk of size ${total}, +
@@ -157,7 +157,9 @@ which is longer than the specified ${this.chunkSize}`);
157
157
  // - we have a larger chunk than in the chunk overlap
158
158
  // - or if we still have any chunks and the length is long
159
159
  while (total > this.chunkOverlap ||
160
- (total + _len > this.chunkSize && total > 0)) {
160
+ (total + _len + currentDoc.length * separator.length >
161
+ this.chunkSize &&
162
+ total > 0)) {
161
163
  total -= await this.lengthFunction(currentDoc[0]);
162
164
  currentDoc.shift();
163
165
  }
@@ -1,48 +1,21 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Calculator = void 0;
4
- const expr_eval_1 = require("expr-eval");
5
- const tools_1 = require("@langchain/core/tools");
6
- /**
7
- * The Calculator class is a tool used to evaluate mathematical
8
- * expressions. It extends the base Tool class.
9
- * @example
10
- * ```typescript
11
- * const calculator = new Calculator();
12
- * const sum = calculator.add(99, 99);
13
- * console.log("The sum of 99 and 99 is:", sum);
14
- * ```
15
- */
16
- class Calculator extends tools_1.Tool {
17
- constructor() {
18
- super(...arguments);
19
- Object.defineProperty(this, "name", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: "calculator"
24
- });
25
- Object.defineProperty(this, "description", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: `Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.`
30
- });
31
- }
32
- static lc_name() {
33
- return "Calculator";
34
- }
35
- get lc_namespace() {
36
- return [...super.lc_namespace, "calculator"];
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
37
7
  }
38
- /** @ignore */
39
- async _call(input) {
40
- try {
41
- return expr_eval_1.Parser.evaluate(input).toString();
42
- }
43
- catch (error) {
44
- return "I don't know how to do that.";
45
- }
46
- }
47
- }
48
- exports.Calculator = Calculator;
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ const entrypoint_deprecation_js_1 = require("../util/entrypoint_deprecation.cjs");
18
+ /* #__PURE__ */ (0, entrypoint_deprecation_js_1.logVersion010MigrationWarning)({
19
+ oldEntrypointName: "tools/calculator",
20
+ });
21
+ __exportStar(require("@langchain/community/tools/calculator"), exports);
@@ -1,19 +1 @@
1
- import { Tool } from "@langchain/core/tools";
2
- /**
3
- * The Calculator class is a tool used to evaluate mathematical
4
- * expressions. It extends the base Tool class.
5
- * @example
6
- * ```typescript
7
- * const calculator = new Calculator();
8
- * const sum = calculator.add(99, 99);
9
- * console.log("The sum of 99 and 99 is:", sum);
10
- * ```
11
- */
12
- export declare class Calculator extends Tool {
13
- static lc_name(): string;
14
- get lc_namespace(): string[];
15
- name: string;
16
- /** @ignore */
17
- _call(input: string): Promise<string>;
18
- description: string;
19
- }
1
+ export * from "@langchain/community/tools/calculator";
@@ -1,44 +1,5 @@
1
- import { Parser } from "expr-eval";
2
- import { Tool } from "@langchain/core/tools";
3
- /**
4
- * The Calculator class is a tool used to evaluate mathematical
5
- * expressions. It extends the base Tool class.
6
- * @example
7
- * ```typescript
8
- * const calculator = new Calculator();
9
- * const sum = calculator.add(99, 99);
10
- * console.log("The sum of 99 and 99 is:", sum);
11
- * ```
12
- */
13
- export class Calculator extends Tool {
14
- constructor() {
15
- super(...arguments);
16
- Object.defineProperty(this, "name", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: "calculator"
21
- });
22
- Object.defineProperty(this, "description", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: `Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.`
27
- });
28
- }
29
- static lc_name() {
30
- return "Calculator";
31
- }
32
- get lc_namespace() {
33
- return [...super.lc_namespace, "calculator"];
34
- }
35
- /** @ignore */
36
- async _call(input) {
37
- try {
38
- return Parser.evaluate(input).toString();
39
- }
40
- catch (error) {
41
- return "I don't know how to do that.";
42
- }
43
- }
44
- }
1
+ import { logVersion010MigrationWarning } from "../util/entrypoint_deprecation.js";
2
+ /* #__PURE__ */ logVersion010MigrationWarning({
3
+ oldEntrypointName: "tools/calculator",
4
+ });
5
+ export * from "@langchain/community/tools/calculator";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.1.29-rc.0",
3
+ "version": "0.1.30",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -1190,7 +1190,7 @@
1190
1190
  "homepage": "https://github.com/langchain-ai/langchainjs/tree/main/langchain/",
1191
1191
  "scripts": {
1192
1192
  "build": "yarn run build:deps && yarn clean && yarn build:esm && yarn build:cjs && yarn build:scripts",
1193
- "build:deps": "yarn run turbo:command build --filter=@langchain/core --filter=@langchain/anthropic --filter=@langchain/openai --filter=@langchain/community --concurrency=1",
1193
+ "build:deps": "yarn run turbo:command build --filter=@langchain/anthropic --filter=@langchain/openai --filter=@langchain/community --concurrency=1",
1194
1194
  "build:esm": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist/ && rimraf dist/tests dist/**/tests",
1195
1195
  "build:cjs": "NODE_OPTIONS=--max-old-space-size=4096 tsc --outDir dist-cjs/ -p tsconfig.cjs.json && yarn move-cjs-to-dist && rimraf dist-cjs",
1196
1196
  "build:watch": "yarn create-entrypoints && tsc --outDir dist/ --watch",
@@ -1255,7 +1255,7 @@
1255
1255
  "cheerio": "^1.0.0-rc.12",
1256
1256
  "chromadb": "^1.5.3",
1257
1257
  "convex": "^1.3.1",
1258
- "couchbase": "^4.2.10",
1258
+ "couchbase": "^4.3.0",
1259
1259
  "d3-dsv": "^2.0.0",
1260
1260
  "dotenv": "^16.0.3",
1261
1261
  "dpdm": "^3.12.0",
@@ -1325,7 +1325,7 @@
1325
1325
  "cheerio": "^1.0.0-rc.12",
1326
1326
  "chromadb": "*",
1327
1327
  "convex": "^1.3.1",
1328
- "couchbase": "^4.2.10",
1328
+ "couchbase": "^4.3.0",
1329
1329
  "d3-dsv": "^2.0.0",
1330
1330
  "epub2": "^3.0.1",
1331
1331
  "fast-xml-parser": "*",
@@ -1512,11 +1512,10 @@
1512
1512
  },
1513
1513
  "dependencies": {
1514
1514
  "@anthropic-ai/sdk": "^0.9.1",
1515
- "@langchain/community": "~0.0.36",
1515
+ "@langchain/community": "~0.0.41",
1516
1516
  "@langchain/core": "~0.1.44",
1517
1517
  "@langchain/openai": "~0.0.19",
1518
1518
  "binary-extensions": "^2.2.0",
1519
- "expr-eval": "^2.0.2",
1520
1519
  "js-tiktoken": "^1.0.7",
1521
1520
  "js-yaml": "^4.1.0",
1522
1521
  "jsonpointer": "^5.0.1",