langchain 0.1.27 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/agent.cjs +137 -10
- package/dist/agents/agent.d.ts +37 -5
- package/dist/agents/agent.js +133 -9
- package/dist/agents/executor.cjs +4 -1
- package/dist/agents/executor.js +5 -2
- package/dist/agents/openai_functions/index.cjs +6 -2
- package/dist/agents/openai_functions/index.d.ts +7 -5
- package/dist/agents/openai_functions/index.js +7 -3
- package/dist/agents/openai_tools/index.cjs +7 -2
- package/dist/agents/openai_tools/index.d.ts +7 -4
- package/dist/agents/openai_tools/index.js +7 -2
- package/dist/agents/react/index.cjs +7 -2
- package/dist/agents/react/index.d.ts +7 -5
- package/dist/agents/react/index.js +7 -2
- package/dist/agents/structured_chat/index.cjs +6 -2
- package/dist/agents/structured_chat/index.d.ts +7 -5
- package/dist/agents/structured_chat/index.js +7 -3
- package/dist/agents/types.d.ts +21 -3
- package/dist/agents/xml/index.cjs +6 -2
- package/dist/agents/xml/index.d.ts +7 -5
- package/dist/agents/xml/index.js +7 -3
- package/dist/document_loaders/fs/unstructured.cjs +40 -0
- package/dist/document_loaders/fs/unstructured.d.ts +8 -0
- package/dist/document_loaders/fs/unstructured.js +40 -0
- package/dist/output_parsers/fix.cjs +16 -4
- package/dist/output_parsers/fix.d.ts +10 -3
- package/dist/output_parsers/fix.js +16 -4
- package/package.json +1 -1
package/dist/agents/agent.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Agent = exports.LLMSingleActionAgent = exports.RunnableAgent = exports.BaseMultiActionAgent = exports.BaseSingleActionAgent = exports.BaseAgent = void 0;
|
|
3
|
+
exports.Agent = exports.LLMSingleActionAgent = exports.RunnableAgent = exports.RunnableMultiActionAgent = exports.RunnableSingleActionAgent = 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
|
/**
|
|
@@ -81,23 +81,106 @@ exports.BaseMultiActionAgent = BaseMultiActionAgent;
|
|
|
81
81
|
function isAgentAction(input) {
|
|
82
82
|
return !Array.isArray(input) && input?.tool !== undefined;
|
|
83
83
|
}
|
|
84
|
+
function isRunnableAgent(x) {
|
|
85
|
+
return (x.runnable !==
|
|
86
|
+
undefined);
|
|
87
|
+
}
|
|
88
|
+
exports.isRunnableAgent = isRunnableAgent;
|
|
84
89
|
/**
|
|
85
|
-
* Class representing a single
|
|
86
|
-
* Extends the
|
|
90
|
+
* Class representing a single-action agent powered by runnables.
|
|
91
|
+
* Extends the BaseSingleActionAgent class and provides methods for
|
|
87
92
|
* planning agent actions with runnables.
|
|
88
93
|
*/
|
|
89
|
-
class
|
|
94
|
+
class RunnableSingleActionAgent extends BaseSingleActionAgent {
|
|
90
95
|
get inputKeys() {
|
|
91
96
|
return [];
|
|
92
97
|
}
|
|
93
98
|
constructor(fields) {
|
|
94
|
-
super();
|
|
95
|
-
Object.defineProperty(this, "
|
|
99
|
+
super(fields);
|
|
100
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
101
|
+
enumerable: true,
|
|
102
|
+
configurable: true,
|
|
103
|
+
writable: true,
|
|
104
|
+
value: ["langchain", "agents", "runnable"]
|
|
105
|
+
});
|
|
106
|
+
Object.defineProperty(this, "runnable", {
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true,
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
112
|
+
/**
|
|
113
|
+
* Whether to stream from the runnable or not.
|
|
114
|
+
* If true, the underlying LLM is invoked in a streaming fashion to make it
|
|
115
|
+
* possible to get access to the individual LLM tokens when using
|
|
116
|
+
* `streamLog` with the Agent Executor. If false then LLM is invoked in a
|
|
117
|
+
* non-streaming fashion and individual LLM tokens will not be available
|
|
118
|
+
* in `streamLog`.
|
|
119
|
+
*
|
|
120
|
+
* Note that the runnable should still only stream a single action or
|
|
121
|
+
* finish chunk.
|
|
122
|
+
*/
|
|
123
|
+
Object.defineProperty(this, "streamRunnable", {
|
|
96
124
|
enumerable: true,
|
|
97
125
|
configurable: true,
|
|
98
126
|
writable: true,
|
|
99
127
|
value: true
|
|
100
128
|
});
|
|
129
|
+
Object.defineProperty(this, "defaultRunName", {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
configurable: true,
|
|
132
|
+
writable: true,
|
|
133
|
+
value: "RunnableAgent"
|
|
134
|
+
});
|
|
135
|
+
this.runnable = fields.runnable;
|
|
136
|
+
this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
|
|
137
|
+
this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
|
|
138
|
+
}
|
|
139
|
+
async plan(steps, inputs, callbackManager, config) {
|
|
140
|
+
const combinedInput = { ...inputs, steps };
|
|
141
|
+
const combinedConfig = (0, runnables_1.patchConfig)(config, {
|
|
142
|
+
callbacks: callbackManager,
|
|
143
|
+
runName: this.defaultRunName,
|
|
144
|
+
});
|
|
145
|
+
if (this.streamRunnable) {
|
|
146
|
+
const stream = await this.runnable.stream(combinedInput, combinedConfig);
|
|
147
|
+
let finalOutput;
|
|
148
|
+
for await (const chunk of stream) {
|
|
149
|
+
if (finalOutput === undefined) {
|
|
150
|
+
finalOutput = chunk;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
throw new Error([
|
|
154
|
+
`Multiple agent actions/finishes received in streamed agent output.`,
|
|
155
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
156
|
+
].join("\n"));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (finalOutput === undefined) {
|
|
160
|
+
throw new Error([
|
|
161
|
+
"No streaming output received from underlying runnable.",
|
|
162
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
163
|
+
].join("\n"));
|
|
164
|
+
}
|
|
165
|
+
return finalOutput;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
return this.runnable.invoke(combinedInput, combinedConfig);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.RunnableSingleActionAgent = RunnableSingleActionAgent;
|
|
173
|
+
/**
|
|
174
|
+
* Class representing a multi-action agent powered by runnables.
|
|
175
|
+
* Extends the BaseMultiActionAgent class and provides methods for
|
|
176
|
+
* planning agent actions with runnables.
|
|
177
|
+
*/
|
|
178
|
+
class RunnableMultiActionAgent extends BaseMultiActionAgent {
|
|
179
|
+
get inputKeys() {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
constructor(fields) {
|
|
183
|
+
super(fields);
|
|
101
184
|
Object.defineProperty(this, "lc_namespace", {
|
|
102
185
|
enumerable: true,
|
|
103
186
|
configurable: true,
|
|
@@ -111,27 +194,71 @@ class RunnableAgent extends BaseMultiActionAgent {
|
|
|
111
194
|
writable: true,
|
|
112
195
|
value: void 0
|
|
113
196
|
});
|
|
197
|
+
Object.defineProperty(this, "defaultRunName", {
|
|
198
|
+
enumerable: true,
|
|
199
|
+
configurable: true,
|
|
200
|
+
writable: true,
|
|
201
|
+
value: "RunnableAgent"
|
|
202
|
+
});
|
|
114
203
|
Object.defineProperty(this, "stop", {
|
|
115
204
|
enumerable: true,
|
|
116
205
|
configurable: true,
|
|
117
206
|
writable: true,
|
|
118
207
|
value: void 0
|
|
119
208
|
});
|
|
209
|
+
Object.defineProperty(this, "streamRunnable", {
|
|
210
|
+
enumerable: true,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true,
|
|
213
|
+
value: true
|
|
214
|
+
});
|
|
120
215
|
this.runnable = fields.runnable;
|
|
121
216
|
this.stop = fields.stop;
|
|
217
|
+
this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
|
|
218
|
+
this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
|
|
122
219
|
}
|
|
123
220
|
async plan(steps, inputs, callbackManager, config) {
|
|
124
|
-
const
|
|
125
|
-
const
|
|
221
|
+
const combinedInput = { ...inputs, steps };
|
|
222
|
+
const combinedConfig = (0, runnables_1.patchConfig)(config, {
|
|
126
223
|
callbacks: callbackManager,
|
|
127
|
-
runName:
|
|
128
|
-
})
|
|
224
|
+
runName: this.defaultRunName,
|
|
225
|
+
});
|
|
226
|
+
let output;
|
|
227
|
+
if (this.streamRunnable) {
|
|
228
|
+
const stream = await this.runnable.stream(combinedInput, combinedConfig);
|
|
229
|
+
let finalOutput;
|
|
230
|
+
for await (const chunk of stream) {
|
|
231
|
+
if (finalOutput === undefined) {
|
|
232
|
+
finalOutput = chunk;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
throw new Error([
|
|
236
|
+
`Multiple agent actions/finishes received in streamed agent output.`,
|
|
237
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
238
|
+
].join("\n"));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (finalOutput === undefined) {
|
|
242
|
+
throw new Error([
|
|
243
|
+
"No streaming output received from underlying runnable.",
|
|
244
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
245
|
+
].join("\n"));
|
|
246
|
+
}
|
|
247
|
+
output = finalOutput;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
output = await this.runnable.invoke(combinedInput, combinedConfig);
|
|
251
|
+
}
|
|
129
252
|
if (isAgentAction(output)) {
|
|
130
253
|
return [output];
|
|
131
254
|
}
|
|
132
255
|
return output;
|
|
133
256
|
}
|
|
134
257
|
}
|
|
258
|
+
exports.RunnableMultiActionAgent = RunnableMultiActionAgent;
|
|
259
|
+
/** @deprecated Renamed to RunnableMultiActionAgent. */
|
|
260
|
+
class RunnableAgent extends RunnableMultiActionAgent {
|
|
261
|
+
}
|
|
135
262
|
exports.RunnableAgent = RunnableAgent;
|
|
136
263
|
/**
|
|
137
264
|
* Class representing a single action agent using a LLMChain in LangChain.
|
package/dist/agents/agent.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { ChainValues } from "@langchain/core/utils/types";
|
|
|
8
8
|
import { Serializable } from "@langchain/core/load/serializable";
|
|
9
9
|
import { Runnable, type RunnableConfig } from "@langchain/core/runnables";
|
|
10
10
|
import { LLMChain } from "../chains/llm_chain.js";
|
|
11
|
-
import { AgentActionOutputParser, AgentInput,
|
|
11
|
+
import type { AgentActionOutputParser, AgentInput, RunnableMultiActionAgentInput, RunnableSingleActionAgentInput, SerializedAgent, StoppingMethod } from "./types.js";
|
|
12
12
|
/**
|
|
13
13
|
* Record type for arguments passed to output parsers.
|
|
14
14
|
*/
|
|
@@ -75,22 +75,54 @@ export declare abstract class BaseMultiActionAgent extends BaseAgent {
|
|
|
75
75
|
*/
|
|
76
76
|
abstract plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager, config?: RunnableConfig): Promise<AgentAction[] | AgentFinish>;
|
|
77
77
|
}
|
|
78
|
+
export declare function isRunnableAgent(x: BaseAgent): boolean;
|
|
78
79
|
/**
|
|
79
|
-
* Class representing a single
|
|
80
|
+
* Class representing a single-action agent powered by runnables.
|
|
81
|
+
* Extends the BaseSingleActionAgent class and provides methods for
|
|
82
|
+
* planning agent actions with runnables.
|
|
83
|
+
*/
|
|
84
|
+
export declare class RunnableSingleActionAgent extends BaseSingleActionAgent {
|
|
85
|
+
lc_namespace: string[];
|
|
86
|
+
runnable: Runnable<ChainValues & {
|
|
87
|
+
steps: AgentStep[];
|
|
88
|
+
}, AgentAction | AgentFinish>;
|
|
89
|
+
get inputKeys(): string[];
|
|
90
|
+
/**
|
|
91
|
+
* Whether to stream from the runnable or not.
|
|
92
|
+
* If true, the underlying LLM is invoked in a streaming fashion to make it
|
|
93
|
+
* possible to get access to the individual LLM tokens when using
|
|
94
|
+
* `streamLog` with the Agent Executor. If false then LLM is invoked in a
|
|
95
|
+
* non-streaming fashion and individual LLM tokens will not be available
|
|
96
|
+
* in `streamLog`.
|
|
97
|
+
*
|
|
98
|
+
* Note that the runnable should still only stream a single action or
|
|
99
|
+
* finish chunk.
|
|
100
|
+
*/
|
|
101
|
+
streamRunnable: boolean;
|
|
102
|
+
defaultRunName: string;
|
|
103
|
+
constructor(fields: RunnableSingleActionAgentInput);
|
|
104
|
+
plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager, config?: RunnableConfig): Promise<AgentAction | AgentFinish>;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Class representing a multi-action agent powered by runnables.
|
|
80
108
|
* Extends the BaseMultiActionAgent class and provides methods for
|
|
81
109
|
* planning agent actions with runnables.
|
|
82
110
|
*/
|
|
83
|
-
export declare class
|
|
84
|
-
protected lc_runnable: boolean;
|
|
111
|
+
export declare class RunnableMultiActionAgent extends BaseMultiActionAgent {
|
|
85
112
|
lc_namespace: string[];
|
|
86
113
|
runnable: Runnable<ChainValues & {
|
|
87
114
|
steps: AgentStep[];
|
|
88
115
|
}, AgentAction[] | AgentAction | AgentFinish>;
|
|
116
|
+
defaultRunName: string;
|
|
89
117
|
stop?: string[];
|
|
118
|
+
streamRunnable: boolean;
|
|
90
119
|
get inputKeys(): string[];
|
|
91
|
-
constructor(fields:
|
|
120
|
+
constructor(fields: RunnableMultiActionAgentInput);
|
|
92
121
|
plan(steps: AgentStep[], inputs: ChainValues, callbackManager?: CallbackManager, config?: RunnableConfig): Promise<AgentAction[] | AgentFinish>;
|
|
93
122
|
}
|
|
123
|
+
/** @deprecated Renamed to RunnableMultiActionAgent. */
|
|
124
|
+
export declare class RunnableAgent extends RunnableMultiActionAgent {
|
|
125
|
+
}
|
|
94
126
|
/**
|
|
95
127
|
* Interface for input data for creating a LLMSingleActionAgent.
|
|
96
128
|
*/
|
package/dist/agents/agent.js
CHANGED
|
@@ -75,23 +75,104 @@ export class BaseMultiActionAgent extends BaseAgent {
|
|
|
75
75
|
function isAgentAction(input) {
|
|
76
76
|
return !Array.isArray(input) && input?.tool !== undefined;
|
|
77
77
|
}
|
|
78
|
+
export function isRunnableAgent(x) {
|
|
79
|
+
return (x.runnable !==
|
|
80
|
+
undefined);
|
|
81
|
+
}
|
|
78
82
|
/**
|
|
79
|
-
* Class representing a single
|
|
80
|
-
* Extends the
|
|
83
|
+
* Class representing a single-action agent powered by runnables.
|
|
84
|
+
* Extends the BaseSingleActionAgent class and provides methods for
|
|
81
85
|
* planning agent actions with runnables.
|
|
82
86
|
*/
|
|
83
|
-
export class
|
|
87
|
+
export class RunnableSingleActionAgent extends BaseSingleActionAgent {
|
|
84
88
|
get inputKeys() {
|
|
85
89
|
return [];
|
|
86
90
|
}
|
|
87
91
|
constructor(fields) {
|
|
88
|
-
super();
|
|
89
|
-
Object.defineProperty(this, "
|
|
92
|
+
super(fields);
|
|
93
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: ["langchain", "agents", "runnable"]
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperty(this, "runnable", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: void 0
|
|
104
|
+
});
|
|
105
|
+
/**
|
|
106
|
+
* Whether to stream from the runnable or not.
|
|
107
|
+
* If true, the underlying LLM is invoked in a streaming fashion to make it
|
|
108
|
+
* possible to get access to the individual LLM tokens when using
|
|
109
|
+
* `streamLog` with the Agent Executor. If false then LLM is invoked in a
|
|
110
|
+
* non-streaming fashion and individual LLM tokens will not be available
|
|
111
|
+
* in `streamLog`.
|
|
112
|
+
*
|
|
113
|
+
* Note that the runnable should still only stream a single action or
|
|
114
|
+
* finish chunk.
|
|
115
|
+
*/
|
|
116
|
+
Object.defineProperty(this, "streamRunnable", {
|
|
90
117
|
enumerable: true,
|
|
91
118
|
configurable: true,
|
|
92
119
|
writable: true,
|
|
93
120
|
value: true
|
|
94
121
|
});
|
|
122
|
+
Object.defineProperty(this, "defaultRunName", {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
configurable: true,
|
|
125
|
+
writable: true,
|
|
126
|
+
value: "RunnableAgent"
|
|
127
|
+
});
|
|
128
|
+
this.runnable = fields.runnable;
|
|
129
|
+
this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
|
|
130
|
+
this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
|
|
131
|
+
}
|
|
132
|
+
async plan(steps, inputs, callbackManager, config) {
|
|
133
|
+
const combinedInput = { ...inputs, steps };
|
|
134
|
+
const combinedConfig = patchConfig(config, {
|
|
135
|
+
callbacks: callbackManager,
|
|
136
|
+
runName: this.defaultRunName,
|
|
137
|
+
});
|
|
138
|
+
if (this.streamRunnable) {
|
|
139
|
+
const stream = await this.runnable.stream(combinedInput, combinedConfig);
|
|
140
|
+
let finalOutput;
|
|
141
|
+
for await (const chunk of stream) {
|
|
142
|
+
if (finalOutput === undefined) {
|
|
143
|
+
finalOutput = chunk;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
throw new Error([
|
|
147
|
+
`Multiple agent actions/finishes received in streamed agent output.`,
|
|
148
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
149
|
+
].join("\n"));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (finalOutput === undefined) {
|
|
153
|
+
throw new Error([
|
|
154
|
+
"No streaming output received from underlying runnable.",
|
|
155
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
156
|
+
].join("\n"));
|
|
157
|
+
}
|
|
158
|
+
return finalOutput;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return this.runnable.invoke(combinedInput, combinedConfig);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Class representing a multi-action agent powered by runnables.
|
|
167
|
+
* Extends the BaseMultiActionAgent class and provides methods for
|
|
168
|
+
* planning agent actions with runnables.
|
|
169
|
+
*/
|
|
170
|
+
export class RunnableMultiActionAgent extends BaseMultiActionAgent {
|
|
171
|
+
get inputKeys() {
|
|
172
|
+
return [];
|
|
173
|
+
}
|
|
174
|
+
constructor(fields) {
|
|
175
|
+
super(fields);
|
|
95
176
|
Object.defineProperty(this, "lc_namespace", {
|
|
96
177
|
enumerable: true,
|
|
97
178
|
configurable: true,
|
|
@@ -105,27 +186,70 @@ export class RunnableAgent extends BaseMultiActionAgent {
|
|
|
105
186
|
writable: true,
|
|
106
187
|
value: void 0
|
|
107
188
|
});
|
|
189
|
+
Object.defineProperty(this, "defaultRunName", {
|
|
190
|
+
enumerable: true,
|
|
191
|
+
configurable: true,
|
|
192
|
+
writable: true,
|
|
193
|
+
value: "RunnableAgent"
|
|
194
|
+
});
|
|
108
195
|
Object.defineProperty(this, "stop", {
|
|
109
196
|
enumerable: true,
|
|
110
197
|
configurable: true,
|
|
111
198
|
writable: true,
|
|
112
199
|
value: void 0
|
|
113
200
|
});
|
|
201
|
+
Object.defineProperty(this, "streamRunnable", {
|
|
202
|
+
enumerable: true,
|
|
203
|
+
configurable: true,
|
|
204
|
+
writable: true,
|
|
205
|
+
value: true
|
|
206
|
+
});
|
|
114
207
|
this.runnable = fields.runnable;
|
|
115
208
|
this.stop = fields.stop;
|
|
209
|
+
this.defaultRunName = fields.defaultRunName ?? this.defaultRunName;
|
|
210
|
+
this.streamRunnable = fields.streamRunnable ?? this.streamRunnable;
|
|
116
211
|
}
|
|
117
212
|
async plan(steps, inputs, callbackManager, config) {
|
|
118
|
-
const
|
|
119
|
-
const
|
|
213
|
+
const combinedInput = { ...inputs, steps };
|
|
214
|
+
const combinedConfig = patchConfig(config, {
|
|
120
215
|
callbacks: callbackManager,
|
|
121
|
-
runName:
|
|
122
|
-
})
|
|
216
|
+
runName: this.defaultRunName,
|
|
217
|
+
});
|
|
218
|
+
let output;
|
|
219
|
+
if (this.streamRunnable) {
|
|
220
|
+
const stream = await this.runnable.stream(combinedInput, combinedConfig);
|
|
221
|
+
let finalOutput;
|
|
222
|
+
for await (const chunk of stream) {
|
|
223
|
+
if (finalOutput === undefined) {
|
|
224
|
+
finalOutput = chunk;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw new Error([
|
|
228
|
+
`Multiple agent actions/finishes received in streamed agent output.`,
|
|
229
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
230
|
+
].join("\n"));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (finalOutput === undefined) {
|
|
234
|
+
throw new Error([
|
|
235
|
+
"No streaming output received from underlying runnable.",
|
|
236
|
+
`Set "streamRunnable: false" when initializing the agent to invoke this agent in non-streaming mode.`,
|
|
237
|
+
].join("\n"));
|
|
238
|
+
}
|
|
239
|
+
output = finalOutput;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
output = await this.runnable.invoke(combinedInput, combinedConfig);
|
|
243
|
+
}
|
|
123
244
|
if (isAgentAction(output)) {
|
|
124
245
|
return [output];
|
|
125
246
|
}
|
|
126
247
|
return output;
|
|
127
248
|
}
|
|
128
249
|
}
|
|
250
|
+
/** @deprecated Renamed to RunnableMultiActionAgent. */
|
|
251
|
+
export class RunnableAgent extends RunnableMultiActionAgent {
|
|
252
|
+
}
|
|
129
253
|
/**
|
|
130
254
|
* Class representing a single action agent using a LLMChain in LangChain.
|
|
131
255
|
* Extends the BaseSingleActionAgent class and provides methods for
|
package/dist/agents/executor.cjs
CHANGED
|
@@ -282,11 +282,14 @@ 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.
|
|
285
|
+
agent = new agent_js_1.RunnableMultiActionAgent({ runnable: input.agent });
|
|
286
286
|
// TODO: Update BaseChain implementation on breaking change
|
|
287
287
|
returnOnlyOutputs = false;
|
|
288
288
|
}
|
|
289
289
|
else {
|
|
290
|
+
if ((0, agent_js_1.isRunnableAgent)(input.agent)) {
|
|
291
|
+
returnOnlyOutputs = false;
|
|
292
|
+
}
|
|
290
293
|
agent = input.agent;
|
|
291
294
|
}
|
|
292
295
|
super(input);
|
package/dist/agents/executor.js
CHANGED
|
@@ -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 {
|
|
6
|
+
import { RunnableMultiActionAgent, isRunnableAgent, } from "./agent.js";
|
|
7
7
|
import { BaseChain } from "../chains/base.js";
|
|
8
8
|
export class AgentExecutorIterator extends Serializable {
|
|
9
9
|
get finalOutputs() {
|
|
@@ -277,11 +277,14 @@ export class AgentExecutor extends BaseChain {
|
|
|
277
277
|
let agent;
|
|
278
278
|
let returnOnlyOutputs = true;
|
|
279
279
|
if (Runnable.isRunnable(input.agent)) {
|
|
280
|
-
agent = new
|
|
280
|
+
agent = new RunnableMultiActionAgent({ runnable: input.agent });
|
|
281
281
|
// TODO: Update BaseChain implementation on breaking change
|
|
282
282
|
returnOnlyOutputs = false;
|
|
283
283
|
}
|
|
284
284
|
else {
|
|
285
|
+
if (isRunnableAgent(input.agent)) {
|
|
286
|
+
returnOnlyOutputs = false;
|
|
287
|
+
}
|
|
285
288
|
agent = input.agent;
|
|
286
289
|
}
|
|
287
290
|
super(input);
|
|
@@ -219,7 +219,7 @@ exports.OpenAIAgent = OpenAIAgent;
|
|
|
219
219
|
* });
|
|
220
220
|
* ```
|
|
221
221
|
*/
|
|
222
|
-
async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
222
|
+
async function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
223
223
|
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
224
224
|
throw new Error([
|
|
225
225
|
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
@@ -237,6 +237,10 @@ async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
|
237
237
|
llmWithTools,
|
|
238
238
|
new output_parser_js_1.OpenAIFunctionsAgentOutputParser(),
|
|
239
239
|
]);
|
|
240
|
-
return
|
|
240
|
+
return new agent_js_1.RunnableSingleActionAgent({
|
|
241
|
+
runnable: agent,
|
|
242
|
+
defaultRunName: "OpenAIFunctionsAgent",
|
|
243
|
+
streamRunnable,
|
|
244
|
+
});
|
|
241
245
|
}
|
|
242
246
|
exports.createOpenAIFunctionsAgent = createOpenAIFunctionsAgent;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { BaseLanguageModelInterface, BaseFunctionCallOptions } from "@langchain/core/language_models/base";
|
|
2
2
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
3
3
|
import type { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
4
|
-
import { RunnableSequence } from "@langchain/core/runnables";
|
|
5
4
|
import type { AgentAction, AgentFinish, AgentStep } from "@langchain/core/agents";
|
|
6
5
|
import { BaseMessage, SystemMessage } from "@langchain/core/messages";
|
|
7
6
|
import { ChainValues } from "@langchain/core/utils/types";
|
|
8
7
|
import { ChatPromptTemplate, BasePromptTemplate } from "@langchain/core/prompts";
|
|
9
8
|
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
10
|
-
import { Agent, AgentArgs } from "../agent.js";
|
|
9
|
+
import { Agent, AgentArgs, RunnableSingleActionAgent } from "../agent.js";
|
|
11
10
|
import { AgentInput } from "../types.js";
|
|
12
11
|
import { OpenAIFunctionsAgentOutputParser } from "../openai/output_parser.js";
|
|
13
12
|
export declare function _formatIntermediateSteps(intermediateSteps: AgentStep[]): BaseMessage[];
|
|
@@ -88,6 +87,11 @@ export type CreateOpenAIFunctionsAgentParams = {
|
|
|
88
87
|
tools: StructuredToolInterface[];
|
|
89
88
|
/** The prompt to use, must have an input key for `agent_scratchpad`. */
|
|
90
89
|
prompt: ChatPromptTemplate;
|
|
90
|
+
/**
|
|
91
|
+
* Whether to invoke the underlying model in streaming mode,
|
|
92
|
+
* allowing streaming of intermediate steps. Defaults to true.
|
|
93
|
+
*/
|
|
94
|
+
streamRunnable?: boolean;
|
|
91
95
|
};
|
|
92
96
|
/**
|
|
93
97
|
* Create an agent that uses OpenAI-style function calling.
|
|
@@ -144,6 +148,4 @@ export type CreateOpenAIFunctionsAgentParams = {
|
|
|
144
148
|
* });
|
|
145
149
|
* ```
|
|
146
150
|
*/
|
|
147
|
-
export declare function createOpenAIFunctionsAgent({ llm, tools, prompt, }: CreateOpenAIFunctionsAgentParams): Promise<
|
|
148
|
-
steps: AgentStep[];
|
|
149
|
-
}, AgentAction | AgentFinish>>;
|
|
151
|
+
export declare function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIFunctionsAgentParams): Promise<RunnableSingleActionAgent>;
|
|
@@ -2,7 +2,7 @@ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnable
|
|
|
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 } from "../agent.js";
|
|
5
|
+
import { Agent, RunnableSingleActionAgent } 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";
|
|
@@ -214,7 +214,7 @@ export class OpenAIAgent extends Agent {
|
|
|
214
214
|
* });
|
|
215
215
|
* ```
|
|
216
216
|
*/
|
|
217
|
-
export async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
217
|
+
export async function createOpenAIFunctionsAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
218
218
|
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
219
219
|
throw new Error([
|
|
220
220
|
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
@@ -232,5 +232,9 @@ export async function createOpenAIFunctionsAgent({ llm, tools, prompt, }) {
|
|
|
232
232
|
llmWithTools,
|
|
233
233
|
new OpenAIFunctionsAgentOutputParser(),
|
|
234
234
|
]);
|
|
235
|
-
return
|
|
235
|
+
return new RunnableSingleActionAgent({
|
|
236
|
+
runnable: agent,
|
|
237
|
+
defaultRunName: "OpenAIFunctionsAgent",
|
|
238
|
+
streamRunnable,
|
|
239
|
+
});
|
|
236
240
|
}
|
|
@@ -6,6 +6,7 @@ const function_calling_1 = require("@langchain/core/utils/function_calling");
|
|
|
6
6
|
const openai_tools_js_1 = require("../format_scratchpad/openai_tools.cjs");
|
|
7
7
|
const output_parser_js_1 = require("../openai/output_parser.cjs");
|
|
8
8
|
Object.defineProperty(exports, "OpenAIToolsAgentOutputParser", { enumerable: true, get: function () { return output_parser_js_1.OpenAIToolsAgentOutputParser; } });
|
|
9
|
+
const agent_js_1 = require("../agent.cjs");
|
|
9
10
|
/**
|
|
10
11
|
* Create an agent that uses OpenAI-style tool calling.
|
|
11
12
|
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
@@ -62,7 +63,7 @@ Object.defineProperty(exports, "OpenAIToolsAgentOutputParser", { enumerable: tru
|
|
|
62
63
|
* });
|
|
63
64
|
* ```
|
|
64
65
|
*/
|
|
65
|
-
async function createOpenAIToolsAgent({ llm, tools, prompt, }) {
|
|
66
|
+
async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
66
67
|
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
67
68
|
throw new Error([
|
|
68
69
|
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
@@ -78,6 +79,10 @@ async function createOpenAIToolsAgent({ llm, tools, prompt, }) {
|
|
|
78
79
|
modelWithTools,
|
|
79
80
|
new output_parser_js_1.OpenAIToolsAgentOutputParser(),
|
|
80
81
|
]);
|
|
81
|
-
return
|
|
82
|
+
return new agent_js_1.RunnableMultiActionAgent({
|
|
83
|
+
runnable: agent,
|
|
84
|
+
defaultRunName: "OpenAIToolsAgent",
|
|
85
|
+
streamRunnable,
|
|
86
|
+
});
|
|
82
87
|
}
|
|
83
88
|
exports.createOpenAIToolsAgent = createOpenAIToolsAgent;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
2
|
import type { BaseChatModel, BaseChatModelCallOptions } from "@langchain/core/language_models/chat_models";
|
|
3
3
|
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
4
|
-
import { RunnableSequence } from "@langchain/core/runnables";
|
|
5
4
|
import { OpenAIClient } from "@langchain/openai";
|
|
6
5
|
import { OpenAIToolsAgentOutputParser, type ToolsAgentStep } from "../openai/output_parser.js";
|
|
6
|
+
import { RunnableMultiActionAgent } from "../agent.js";
|
|
7
7
|
export { OpenAIToolsAgentOutputParser, type ToolsAgentStep };
|
|
8
8
|
/**
|
|
9
9
|
* Params used by the createOpenAIToolsAgent function.
|
|
@@ -22,6 +22,11 @@ export type CreateOpenAIToolsAgentParams = {
|
|
|
22
22
|
tools: StructuredToolInterface[];
|
|
23
23
|
/** The prompt to use, must have an input key of `agent_scratchpad`. */
|
|
24
24
|
prompt: ChatPromptTemplate;
|
|
25
|
+
/**
|
|
26
|
+
* Whether to invoke the underlying model in streaming mode,
|
|
27
|
+
* allowing streaming of intermediate steps. Defaults to true.
|
|
28
|
+
*/
|
|
29
|
+
streamRunnable?: boolean;
|
|
25
30
|
};
|
|
26
31
|
/**
|
|
27
32
|
* Create an agent that uses OpenAI-style tool calling.
|
|
@@ -79,6 +84,4 @@ export type CreateOpenAIToolsAgentParams = {
|
|
|
79
84
|
* });
|
|
80
85
|
* ```
|
|
81
86
|
*/
|
|
82
|
-
export declare function createOpenAIToolsAgent({ llm, tools, prompt, }: CreateOpenAIToolsAgentParams): Promise<
|
|
83
|
-
steps: ToolsAgentStep[];
|
|
84
|
-
}, import("@langchain/core/agents").AgentFinish | import("@langchain/core/agents").AgentAction[]>>;
|
|
87
|
+
export declare function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }: CreateOpenAIToolsAgentParams): Promise<RunnableMultiActionAgent>;
|
|
@@ -2,6 +2,7 @@ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnable
|
|
|
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
6
|
export { OpenAIToolsAgentOutputParser };
|
|
6
7
|
/**
|
|
7
8
|
* Create an agent that uses OpenAI-style tool calling.
|
|
@@ -59,7 +60,7 @@ export { OpenAIToolsAgentOutputParser };
|
|
|
59
60
|
* });
|
|
60
61
|
* ```
|
|
61
62
|
*/
|
|
62
|
-
export async function createOpenAIToolsAgent({ llm, tools, prompt, }) {
|
|
63
|
+
export async function createOpenAIToolsAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
63
64
|
if (!prompt.inputVariables.includes("agent_scratchpad")) {
|
|
64
65
|
throw new Error([
|
|
65
66
|
`Prompt must have an input variable named "agent_scratchpad".`,
|
|
@@ -75,5 +76,9 @@ export async function createOpenAIToolsAgent({ llm, tools, prompt, }) {
|
|
|
75
76
|
modelWithTools,
|
|
76
77
|
new OpenAIToolsAgentOutputParser(),
|
|
77
78
|
]);
|
|
78
|
-
return
|
|
79
|
+
return new RunnableMultiActionAgent({
|
|
80
|
+
runnable: agent,
|
|
81
|
+
defaultRunName: "OpenAIToolsAgent",
|
|
82
|
+
streamRunnable,
|
|
83
|
+
});
|
|
79
84
|
}
|
|
@@ -5,6 +5,7 @@ const runnables_1 = require("@langchain/core/runnables");
|
|
|
5
5
|
const render_js_1 = require("../../tools/render.cjs");
|
|
6
6
|
const log_js_1 = require("../format_scratchpad/log.cjs");
|
|
7
7
|
const output_parser_js_1 = require("./output_parser.cjs");
|
|
8
|
+
const agent_js_1 = require("../agent.cjs");
|
|
8
9
|
/**
|
|
9
10
|
* Create an agent that uses ReAct prompting.
|
|
10
11
|
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
@@ -48,7 +49,7 @@ const output_parser_js_1 = require("./output_parser.cjs");
|
|
|
48
49
|
* });
|
|
49
50
|
* ```
|
|
50
51
|
*/
|
|
51
|
-
async function createReactAgent({ llm, tools, prompt, }) {
|
|
52
|
+
async function createReactAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
52
53
|
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
53
54
|
if (missingVariables.length > 0) {
|
|
54
55
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -72,6 +73,10 @@ async function createReactAgent({ llm, tools, prompt, }) {
|
|
|
72
73
|
toolNames,
|
|
73
74
|
}),
|
|
74
75
|
]);
|
|
75
|
-
return
|
|
76
|
+
return new agent_js_1.RunnableSingleActionAgent({
|
|
77
|
+
runnable: agent,
|
|
78
|
+
defaultRunName: "ReactAgent",
|
|
79
|
+
streamRunnable,
|
|
80
|
+
});
|
|
76
81
|
}
|
|
77
82
|
exports.createReactAgent = createReactAgent;
|
|
@@ -1,8 +1,7 @@
|
|
|
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 {
|
|
5
|
-
import { AgentStep } from "@langchain/core/agents";
|
|
4
|
+
import { RunnableSingleActionAgent } from "../agent.js";
|
|
6
5
|
/**
|
|
7
6
|
* Params used by the createXmlAgent function.
|
|
8
7
|
*/
|
|
@@ -16,6 +15,11 @@ export type CreateReactAgentParams = {
|
|
|
16
15
|
* `tools`, `tool_names`, and `agent_scratchpad`.
|
|
17
16
|
*/
|
|
18
17
|
prompt: BasePromptTemplate;
|
|
18
|
+
/**
|
|
19
|
+
* Whether to invoke the underlying model in streaming mode,
|
|
20
|
+
* allowing streaming of intermediate steps. Defaults to true.
|
|
21
|
+
*/
|
|
22
|
+
streamRunnable?: boolean;
|
|
19
23
|
};
|
|
20
24
|
/**
|
|
21
25
|
* Create an agent that uses ReAct prompting.
|
|
@@ -60,6 +64,4 @@ export type CreateReactAgentParams = {
|
|
|
60
64
|
* });
|
|
61
65
|
* ```
|
|
62
66
|
*/
|
|
63
|
-
export declare function createReactAgent({ llm, tools, prompt, }: CreateReactAgentParams): Promise<
|
|
64
|
-
steps: AgentStep[];
|
|
65
|
-
}, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
|
|
67
|
+
export declare function createReactAgent({ llm, tools, prompt, streamRunnable, }: CreateReactAgentParams): Promise<RunnableSingleActionAgent>;
|
|
@@ -2,6 +2,7 @@ import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnable
|
|
|
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
6
|
/**
|
|
6
7
|
* Create an agent that uses ReAct prompting.
|
|
7
8
|
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
@@ -45,7 +46,7 @@ import { ReActSingleInputOutputParser } from "./output_parser.js";
|
|
|
45
46
|
* });
|
|
46
47
|
* ```
|
|
47
48
|
*/
|
|
48
|
-
export async function createReactAgent({ llm, tools, prompt, }) {
|
|
49
|
+
export async function createReactAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
49
50
|
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
50
51
|
if (missingVariables.length > 0) {
|
|
51
52
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -69,5 +70,9 @@ export async function createReactAgent({ llm, tools, prompt, }) {
|
|
|
69
70
|
toolNames,
|
|
70
71
|
}),
|
|
71
72
|
]);
|
|
72
|
-
return
|
|
73
|
+
return new RunnableSingleActionAgent({
|
|
74
|
+
runnable: agent,
|
|
75
|
+
defaultRunName: "ReactAgent",
|
|
76
|
+
streamRunnable,
|
|
77
|
+
});
|
|
73
78
|
}
|
|
@@ -210,7 +210,7 @@ exports.StructuredChatAgent = StructuredChatAgent;
|
|
|
210
210
|
* });
|
|
211
211
|
* ```
|
|
212
212
|
*/
|
|
213
|
-
async function createStructuredChatAgent({ llm, tools, prompt, }) {
|
|
213
|
+
async function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
214
214
|
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
215
215
|
if (missingVariables.length > 0) {
|
|
216
216
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -234,6 +234,10 @@ async function createStructuredChatAgent({ llm, tools, prompt, }) {
|
|
|
234
234
|
toolNames,
|
|
235
235
|
}),
|
|
236
236
|
]);
|
|
237
|
-
return
|
|
237
|
+
return new agent_js_1.RunnableSingleActionAgent({
|
|
238
|
+
runnable: agent,
|
|
239
|
+
defaultRunName: "StructuredChatAgent",
|
|
240
|
+
streamRunnable,
|
|
241
|
+
});
|
|
238
242
|
}
|
|
239
243
|
exports.createStructuredChatAgent = createStructuredChatAgent;
|
|
@@ -1,11 +1,10 @@
|
|
|
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
3
|
import type { BasePromptTemplate } from "@langchain/core/prompts";
|
|
5
4
|
import { BaseMessagePromptTemplate, ChatPromptTemplate } from "@langchain/core/prompts";
|
|
6
5
|
import { AgentStep } from "@langchain/core/agents";
|
|
7
6
|
import { Optional } from "../../types/type-utils.js";
|
|
8
|
-
import { Agent, AgentArgs, OutputParserArgs } from "../agent.js";
|
|
7
|
+
import { Agent, AgentArgs, OutputParserArgs, RunnableSingleActionAgent } from "../agent.js";
|
|
9
8
|
import { AgentInput } from "../types.js";
|
|
10
9
|
import { StructuredChatOutputParserWithRetries } from "./outputParser.js";
|
|
11
10
|
/**
|
|
@@ -106,6 +105,11 @@ export type CreateStructuredChatAgentParams = {
|
|
|
106
105
|
* `tools`, `tool_names`, and `agent_scratchpad`.
|
|
107
106
|
*/
|
|
108
107
|
prompt: BasePromptTemplate;
|
|
108
|
+
/**
|
|
109
|
+
* Whether to invoke the underlying model in streaming mode,
|
|
110
|
+
* allowing streaming of intermediate steps. Defaults to true.
|
|
111
|
+
*/
|
|
112
|
+
streamRunnable?: boolean;
|
|
109
113
|
};
|
|
110
114
|
/**
|
|
111
115
|
* Create an agent aimed at supporting tools with multiple inputs.
|
|
@@ -163,6 +167,4 @@ export type CreateStructuredChatAgentParams = {
|
|
|
163
167
|
* });
|
|
164
168
|
* ```
|
|
165
169
|
*/
|
|
166
|
-
export declare function createStructuredChatAgent({ llm, tools, prompt, }: CreateStructuredChatAgentParams): Promise<
|
|
167
|
-
steps: AgentStep[];
|
|
168
|
-
}, import("@langchain/core/agents").AgentAction | import("@langchain/core/agents").AgentFinish>>;
|
|
170
|
+
export declare function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }: CreateStructuredChatAgentParams): Promise<RunnableSingleActionAgent>;
|
|
@@ -2,7 +2,7 @@ import { zodToJsonSchema } from "zod-to-json-schema";
|
|
|
2
2
|
import { RunnablePassthrough, RunnableSequence, } 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 } from "../agent.js";
|
|
5
|
+
import { Agent, RunnableSingleActionAgent, } 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";
|
|
@@ -206,7 +206,7 @@ export class StructuredChatAgent extends Agent {
|
|
|
206
206
|
* });
|
|
207
207
|
* ```
|
|
208
208
|
*/
|
|
209
|
-
export async function createStructuredChatAgent({ llm, tools, prompt, }) {
|
|
209
|
+
export async function createStructuredChatAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
210
210
|
const missingVariables = ["tools", "tool_names", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
211
211
|
if (missingVariables.length > 0) {
|
|
212
212
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -230,5 +230,9 @@ export async function createStructuredChatAgent({ llm, tools, prompt, }) {
|
|
|
230
230
|
toolNames,
|
|
231
231
|
}),
|
|
232
232
|
]);
|
|
233
|
-
return
|
|
233
|
+
return new RunnableSingleActionAgent({
|
|
234
|
+
runnable: agent,
|
|
235
|
+
defaultRunName: "StructuredChatAgent",
|
|
236
|
+
streamRunnable,
|
|
237
|
+
});
|
|
234
238
|
}
|
package/dist/agents/types.d.ts
CHANGED
|
@@ -16,16 +16,34 @@ export interface AgentInput {
|
|
|
16
16
|
allowedTools?: string[];
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
|
-
* Interface defining the input for creating
|
|
20
|
-
*
|
|
19
|
+
* Interface defining the input for creating a single action agent
|
|
20
|
+
* that uses runnables.
|
|
21
21
|
*/
|
|
22
|
-
export interface
|
|
22
|
+
export interface RunnableSingleActionAgentInput {
|
|
23
|
+
runnable: Runnable<ChainValues & {
|
|
24
|
+
agent_scratchpad?: string | BaseMessage[];
|
|
25
|
+
stop?: string[];
|
|
26
|
+
}, AgentAction | AgentFinish>;
|
|
27
|
+
streamRunnable?: boolean;
|
|
28
|
+
defaultRunName?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Interface defining the input for creating a multi-action agent that uses
|
|
32
|
+
* runnables. It includes the Runnable instance, and an optional list of
|
|
33
|
+
* stop strings.
|
|
34
|
+
*/
|
|
35
|
+
export interface RunnableMultiActionAgentInput {
|
|
23
36
|
runnable: Runnable<ChainValues & {
|
|
24
37
|
agent_scratchpad?: string | BaseMessage[];
|
|
25
38
|
stop?: string[];
|
|
26
39
|
}, AgentAction[] | AgentAction | AgentFinish>;
|
|
40
|
+
streamRunnable?: boolean;
|
|
41
|
+
defaultRunName?: string;
|
|
27
42
|
stop?: string[];
|
|
28
43
|
}
|
|
44
|
+
/** @deprecated Renamed to RunnableMultiActionAgentInput. */
|
|
45
|
+
export interface RunnableAgentInput extends RunnableMultiActionAgentInput {
|
|
46
|
+
}
|
|
29
47
|
/**
|
|
30
48
|
* Abstract class representing an output parser specifically for agent
|
|
31
49
|
* actions and finishes in LangChain. It extends the `BaseOutputParser`
|
|
@@ -156,7 +156,7 @@ exports.XMLAgent = XMLAgent;
|
|
|
156
156
|
* });
|
|
157
157
|
* ```
|
|
158
158
|
*/
|
|
159
|
-
async function createXmlAgent({ llm, tools, prompt, }) {
|
|
159
|
+
async function createXmlAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
160
160
|
const missingVariables = ["tools", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
161
161
|
if (missingVariables.length > 0) {
|
|
162
162
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -176,6 +176,10 @@ async function createXmlAgent({ llm, tools, prompt, }) {
|
|
|
176
176
|
llmWithStop,
|
|
177
177
|
new output_parser_js_1.XMLAgentOutputParser(),
|
|
178
178
|
]);
|
|
179
|
-
return
|
|
179
|
+
return new agent_js_1.RunnableSingleActionAgent({
|
|
180
|
+
runnable: agent,
|
|
181
|
+
defaultRunName: "XMLAgent",
|
|
182
|
+
streamRunnable,
|
|
183
|
+
});
|
|
180
184
|
}
|
|
181
185
|
exports.createXmlAgent = createXmlAgent;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
2
2
|
import type { ToolInterface } from "@langchain/core/tools";
|
|
3
|
-
import { RunnableSequence } from "@langchain/core/runnables";
|
|
4
3
|
import type { BasePromptTemplate } from "@langchain/core/prompts";
|
|
5
4
|
import { AgentStep, AgentAction, AgentFinish } from "@langchain/core/agents";
|
|
6
5
|
import { ChainValues } from "@langchain/core/utils/types";
|
|
7
6
|
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
8
7
|
import { CallbackManager } from "@langchain/core/callbacks/manager";
|
|
9
8
|
import { LLMChain } from "../../chains/llm_chain.js";
|
|
10
|
-
import { AgentArgs, BaseSingleActionAgent } from "../agent.js";
|
|
9
|
+
import { AgentArgs, BaseSingleActionAgent, RunnableSingleActionAgent } from "../agent.js";
|
|
11
10
|
import { XMLAgentOutputParser } from "./output_parser.js";
|
|
12
11
|
/**
|
|
13
12
|
* Interface for the input to the XMLAgent class.
|
|
@@ -62,6 +61,11 @@ export type CreateXmlAgentParams = {
|
|
|
62
61
|
* `tools` and `agent_scratchpad`.
|
|
63
62
|
*/
|
|
64
63
|
prompt: BasePromptTemplate;
|
|
64
|
+
/**
|
|
65
|
+
* Whether to invoke the underlying model in streaming mode,
|
|
66
|
+
* allowing streaming of intermediate steps. Defaults to true.
|
|
67
|
+
*/
|
|
68
|
+
streamRunnable?: boolean;
|
|
65
69
|
};
|
|
66
70
|
/**
|
|
67
71
|
* Create an agent that uses XML to format its logic.
|
|
@@ -113,6 +117,4 @@ export type CreateXmlAgentParams = {
|
|
|
113
117
|
* });
|
|
114
118
|
* ```
|
|
115
119
|
*/
|
|
116
|
-
export declare function createXmlAgent({ llm, tools, prompt, }: CreateXmlAgentParams): Promise<
|
|
117
|
-
steps: AgentStep[];
|
|
118
|
-
}, AgentAction | AgentFinish>>;
|
|
120
|
+
export declare function createXmlAgent({ llm, tools, prompt, streamRunnable, }: CreateXmlAgentParams): Promise<RunnableSingleActionAgent>;
|
package/dist/agents/xml/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RunnablePassthrough, RunnableSequence, } 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 } from "../agent.js";
|
|
4
|
+
import { BaseSingleActionAgent, RunnableSingleActionAgent, } 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";
|
|
@@ -152,7 +152,7 @@ export class XMLAgent extends BaseSingleActionAgent {
|
|
|
152
152
|
* });
|
|
153
153
|
* ```
|
|
154
154
|
*/
|
|
155
|
-
export async function createXmlAgent({ llm, tools, prompt, }) {
|
|
155
|
+
export async function createXmlAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
156
156
|
const missingVariables = ["tools", "agent_scratchpad"].filter((v) => !prompt.inputVariables.includes(v));
|
|
157
157
|
if (missingVariables.length > 0) {
|
|
158
158
|
throw new Error(`Provided prompt is missing required input variables: ${JSON.stringify(missingVariables)}`);
|
|
@@ -172,5 +172,9 @@ export async function createXmlAgent({ llm, tools, prompt, }) {
|
|
|
172
172
|
llmWithStop,
|
|
173
173
|
new XMLAgentOutputParser(),
|
|
174
174
|
]);
|
|
175
|
-
return
|
|
175
|
+
return new RunnableSingleActionAgent({
|
|
176
|
+
runnable: agent,
|
|
177
|
+
defaultRunName: "XMLAgent",
|
|
178
|
+
streamRunnable,
|
|
179
|
+
});
|
|
176
180
|
}
|
|
@@ -116,6 +116,30 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
116
116
|
writable: true,
|
|
117
117
|
value: void 0
|
|
118
118
|
});
|
|
119
|
+
Object.defineProperty(this, "multiPageSections", {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
value: void 0
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(this, "combineUnderNChars", {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: void 0
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(this, "newAfterNChars", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: void 0
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(this, "maxCharacters", {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: void 0
|
|
142
|
+
});
|
|
119
143
|
// Temporary shim to avoid breaking existing users
|
|
120
144
|
// Remove when API keys are enforced by Unstructured and existing code will break anyway
|
|
121
145
|
const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
|
|
@@ -138,6 +162,10 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
138
162
|
this.hiResModelName = options.hiResModelName;
|
|
139
163
|
this.includePageBreaks = options.includePageBreaks;
|
|
140
164
|
this.chunkingStrategy = options.chunkingStrategy;
|
|
165
|
+
this.multiPageSections = options.multiPageSections;
|
|
166
|
+
this.combineUnderNChars = options.combineUnderNChars;
|
|
167
|
+
this.newAfterNChars = options.newAfterNChars;
|
|
168
|
+
this.maxCharacters = options.maxCharacters;
|
|
141
169
|
}
|
|
142
170
|
}
|
|
143
171
|
async _partition() {
|
|
@@ -177,6 +205,18 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
177
205
|
if (this.chunkingStrategy) {
|
|
178
206
|
formData.append("chunking_strategy", this.chunkingStrategy);
|
|
179
207
|
}
|
|
208
|
+
if (this.multiPageSections !== undefined) {
|
|
209
|
+
formData.append("multipage_sections", this.multiPageSections ? "true" : "false");
|
|
210
|
+
}
|
|
211
|
+
if (this.combineUnderNChars !== undefined) {
|
|
212
|
+
formData.append("combine_under_n_chars", String(this.combineUnderNChars));
|
|
213
|
+
}
|
|
214
|
+
if (this.newAfterNChars !== undefined) {
|
|
215
|
+
formData.append("new_after_n_chars", String(this.newAfterNChars));
|
|
216
|
+
}
|
|
217
|
+
if (this.maxCharacters !== undefined) {
|
|
218
|
+
formData.append("max_characters", String(this.maxCharacters));
|
|
219
|
+
}
|
|
180
220
|
const headers = {
|
|
181
221
|
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
|
|
182
222
|
};
|
|
@@ -54,6 +54,10 @@ export type UnstructuredLoaderOptions = {
|
|
|
54
54
|
hiResModelName?: StringWithAutocomplete<HiResModelName>;
|
|
55
55
|
includePageBreaks?: boolean;
|
|
56
56
|
chunkingStrategy?: StringWithAutocomplete<ChunkingStrategy>;
|
|
57
|
+
multiPageSections?: boolean;
|
|
58
|
+
combineUnderNChars?: number;
|
|
59
|
+
newAfterNChars?: number;
|
|
60
|
+
maxCharacters?: number;
|
|
57
61
|
};
|
|
58
62
|
type UnstructuredDirectoryLoaderOptions = UnstructuredLoaderOptions & {
|
|
59
63
|
recursive?: boolean;
|
|
@@ -81,6 +85,10 @@ export declare class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
81
85
|
private hiResModelName?;
|
|
82
86
|
private includePageBreaks?;
|
|
83
87
|
private chunkingStrategy?;
|
|
88
|
+
private multiPageSections?;
|
|
89
|
+
private combineUnderNChars?;
|
|
90
|
+
private newAfterNChars?;
|
|
91
|
+
private maxCharacters?;
|
|
84
92
|
constructor(filePathOrLegacyApiUrl: string, optionsOrLegacyFilePath?: UnstructuredLoaderOptions | string);
|
|
85
93
|
_partition(): Promise<Element[]>;
|
|
86
94
|
load(): Promise<Document[]>;
|
|
@@ -112,6 +112,30 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
112
112
|
writable: true,
|
|
113
113
|
value: void 0
|
|
114
114
|
});
|
|
115
|
+
Object.defineProperty(this, "multiPageSections", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
configurable: true,
|
|
118
|
+
writable: true,
|
|
119
|
+
value: void 0
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(this, "combineUnderNChars", {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
configurable: true,
|
|
124
|
+
writable: true,
|
|
125
|
+
value: void 0
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(this, "newAfterNChars", {
|
|
128
|
+
enumerable: true,
|
|
129
|
+
configurable: true,
|
|
130
|
+
writable: true,
|
|
131
|
+
value: void 0
|
|
132
|
+
});
|
|
133
|
+
Object.defineProperty(this, "maxCharacters", {
|
|
134
|
+
enumerable: true,
|
|
135
|
+
configurable: true,
|
|
136
|
+
writable: true,
|
|
137
|
+
value: void 0
|
|
138
|
+
});
|
|
115
139
|
// Temporary shim to avoid breaking existing users
|
|
116
140
|
// Remove when API keys are enforced by Unstructured and existing code will break anyway
|
|
117
141
|
const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
|
|
@@ -134,6 +158,10 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
134
158
|
this.hiResModelName = options.hiResModelName;
|
|
135
159
|
this.includePageBreaks = options.includePageBreaks;
|
|
136
160
|
this.chunkingStrategy = options.chunkingStrategy;
|
|
161
|
+
this.multiPageSections = options.multiPageSections;
|
|
162
|
+
this.combineUnderNChars = options.combineUnderNChars;
|
|
163
|
+
this.newAfterNChars = options.newAfterNChars;
|
|
164
|
+
this.maxCharacters = options.maxCharacters;
|
|
137
165
|
}
|
|
138
166
|
}
|
|
139
167
|
async _partition() {
|
|
@@ -173,6 +201,18 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
173
201
|
if (this.chunkingStrategy) {
|
|
174
202
|
formData.append("chunking_strategy", this.chunkingStrategy);
|
|
175
203
|
}
|
|
204
|
+
if (this.multiPageSections !== undefined) {
|
|
205
|
+
formData.append("multipage_sections", this.multiPageSections ? "true" : "false");
|
|
206
|
+
}
|
|
207
|
+
if (this.combineUnderNChars !== undefined) {
|
|
208
|
+
formData.append("combine_under_n_chars", String(this.combineUnderNChars));
|
|
209
|
+
}
|
|
210
|
+
if (this.newAfterNChars !== undefined) {
|
|
211
|
+
formData.append("new_after_n_chars", String(this.newAfterNChars));
|
|
212
|
+
}
|
|
213
|
+
if (this.maxCharacters !== undefined) {
|
|
214
|
+
formData.append("max_characters", String(this.maxCharacters));
|
|
215
|
+
}
|
|
176
216
|
const headers = {
|
|
177
217
|
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
|
|
178
218
|
};
|
|
@@ -4,6 +4,9 @@ exports.OutputFixingParser = void 0;
|
|
|
4
4
|
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
5
5
|
const llm_chain_js_1 = require("../chains/llm_chain.cjs");
|
|
6
6
|
const prompts_js_1 = require("./prompts.cjs");
|
|
7
|
+
function isLLMChain(x) {
|
|
8
|
+
return (x.prompt !== undefined && x.llm !== undefined);
|
|
9
|
+
}
|
|
7
10
|
/**
|
|
8
11
|
* Class that extends the BaseOutputParser to handle situations where the
|
|
9
12
|
* initial parsing attempt fails. It contains a retryChain for retrying
|
|
@@ -70,13 +73,22 @@ class OutputFixingParser extends output_parsers_1.BaseOutputParser {
|
|
|
70
73
|
catch (e) {
|
|
71
74
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
72
75
|
if (e instanceof output_parsers_1.OutputParserException) {
|
|
73
|
-
const
|
|
76
|
+
const retryInput = {
|
|
74
77
|
instructions: this.parser.getFormatInstructions(),
|
|
75
78
|
completion,
|
|
76
79
|
error: e,
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
};
|
|
81
|
+
if (isLLMChain(this.retryChain)) {
|
|
82
|
+
const result = await this.retryChain.call(retryInput, callbacks);
|
|
83
|
+
const newCompletion = result[this.retryChain.outputKey];
|
|
84
|
+
return this.parser.parse(newCompletion, callbacks);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const result = await this.retryChain.invoke(retryInput, {
|
|
88
|
+
callbacks,
|
|
89
|
+
});
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
80
92
|
}
|
|
81
93
|
throw e;
|
|
82
94
|
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
|
|
2
2
|
import { Callbacks } from "@langchain/core/callbacks/manager";
|
|
3
|
-
import { BaseOutputParser } from "@langchain/core/output_parsers";
|
|
3
|
+
import { BaseOutputParser, OutputParserException } from "@langchain/core/output_parsers";
|
|
4
4
|
import { BasePromptTemplate } from "@langchain/core/prompts";
|
|
5
|
+
import { Runnable } from "@langchain/core/runnables";
|
|
5
6
|
import { LLMChain } from "../chains/llm_chain.js";
|
|
7
|
+
interface OutputFixingParserRetryInput {
|
|
8
|
+
instructions: string;
|
|
9
|
+
completion: string;
|
|
10
|
+
error: OutputParserException;
|
|
11
|
+
}
|
|
6
12
|
/**
|
|
7
13
|
* Class that extends the BaseOutputParser to handle situations where the
|
|
8
14
|
* initial parsing attempt fails. It contains a retryChain for retrying
|
|
@@ -13,7 +19,7 @@ export declare class OutputFixingParser<T> extends BaseOutputParser<T> {
|
|
|
13
19
|
lc_namespace: string[];
|
|
14
20
|
lc_serializable: boolean;
|
|
15
21
|
parser: BaseOutputParser<T>;
|
|
16
|
-
retryChain: LLMChain
|
|
22
|
+
retryChain: LLMChain | Runnable<OutputFixingParserRetryInput, T>;
|
|
17
23
|
/**
|
|
18
24
|
* Static method to create a new instance of OutputFixingParser using a
|
|
19
25
|
* given language model, parser, and optional fields.
|
|
@@ -27,7 +33,7 @@ export declare class OutputFixingParser<T> extends BaseOutputParser<T> {
|
|
|
27
33
|
}): OutputFixingParser<T>;
|
|
28
34
|
constructor({ parser, retryChain, }: {
|
|
29
35
|
parser: BaseOutputParser<T>;
|
|
30
|
-
retryChain: LLMChain
|
|
36
|
+
retryChain: LLMChain | Runnable<OutputFixingParserRetryInput, T>;
|
|
31
37
|
});
|
|
32
38
|
/**
|
|
33
39
|
* Method to parse the completion using the parser. If the initial parsing
|
|
@@ -44,3 +50,4 @@ export declare class OutputFixingParser<T> extends BaseOutputParser<T> {
|
|
|
44
50
|
*/
|
|
45
51
|
getFormatInstructions(): string;
|
|
46
52
|
}
|
|
53
|
+
export {};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { BaseOutputParser, OutputParserException, } from "@langchain/core/output_parsers";
|
|
2
2
|
import { LLMChain } from "../chains/llm_chain.js";
|
|
3
3
|
import { NAIVE_FIX_PROMPT } from "./prompts.js";
|
|
4
|
+
function isLLMChain(x) {
|
|
5
|
+
return (x.prompt !== undefined && x.llm !== undefined);
|
|
6
|
+
}
|
|
4
7
|
/**
|
|
5
8
|
* Class that extends the BaseOutputParser to handle situations where the
|
|
6
9
|
* initial parsing attempt fails. It contains a retryChain for retrying
|
|
@@ -67,13 +70,22 @@ export class OutputFixingParser extends BaseOutputParser {
|
|
|
67
70
|
catch (e) {
|
|
68
71
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
69
72
|
if (e instanceof OutputParserException) {
|
|
70
|
-
const
|
|
73
|
+
const retryInput = {
|
|
71
74
|
instructions: this.parser.getFormatInstructions(),
|
|
72
75
|
completion,
|
|
73
76
|
error: e,
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
};
|
|
78
|
+
if (isLLMChain(this.retryChain)) {
|
|
79
|
+
const result = await this.retryChain.call(retryInput, callbacks);
|
|
80
|
+
const newCompletion = result[this.retryChain.outputKey];
|
|
81
|
+
return this.parser.parse(newCompletion, callbacks);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const result = await this.retryChain.invoke(retryInput, {
|
|
85
|
+
callbacks,
|
|
86
|
+
});
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
77
89
|
}
|
|
78
90
|
throw e;
|
|
79
91
|
}
|