langchain 0.0.161 → 0.0.163
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/README.md +3 -2
- package/dist/callbacks/base.d.ts +10 -10
- package/dist/callbacks/handlers/log_stream.cjs +23 -11
- package/dist/callbacks/handlers/log_stream.d.ts +3 -2
- package/dist/callbacks/handlers/log_stream.js +23 -11
- package/dist/callbacks/manager.cjs +13 -12
- package/dist/callbacks/manager.d.ts +9 -5
- package/dist/callbacks/manager.js +13 -12
- package/dist/chains/base.cjs +1 -1
- package/dist/chains/base.js +1 -1
- package/dist/chat_models/base.cjs +2 -2
- package/dist/chat_models/base.js +2 -2
- package/dist/llms/base.cjs +2 -2
- package/dist/llms/base.js +2 -2
- package/dist/prompts/chat.cjs +4 -3
- package/dist/prompts/chat.js +2 -1
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +9 -2
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +7 -5
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +9 -2
- package/dist/schema/retriever.cjs +1 -1
- package/dist/schema/retriever.js +1 -1
- package/dist/schema/runnable/base.cjs +26 -10
- package/dist/schema/runnable/base.d.ts +4 -1
- package/dist/schema/runnable/base.js +26 -10
- package/dist/stores/message/dynamodb.cjs +9 -6
- package/dist/stores/message/dynamodb.d.ts +3 -2
- package/dist/stores/message/dynamodb.js +9 -6
- package/dist/tools/base.cjs +1 -1
- package/dist/tools/base.js +1 -1
- package/dist/util/bedrock.cjs +2 -1
- package/dist/util/bedrock.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
Looking for the Python version? Check out [LangChain](https://github.com/hwchase17/langchain).
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
To help you ship LangChain apps to production faster, check out [LangSmith](https://smith.langchain.com).
|
|
11
|
+
[LangSmith](https://smith.langchain.com) is a unified developer platform for building, testing, and monitoring LLM applications.
|
|
12
|
+
Fill out [this form](https://airtable.com/appwQzlErAS2qiP0L/shrGtGaVBVAz7NcV2) to get off the waitlist or speak with our sales team
|
|
12
13
|
|
|
13
14
|
## Quick Install
|
|
14
15
|
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
35
35
|
* Called at the start of an LLM or Chat Model run, with the prompt(s)
|
|
36
36
|
* and the run ID.
|
|
37
37
|
*/
|
|
38
|
-
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown
|
|
38
|
+
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
39
|
Promise<any> | any;
|
|
40
40
|
/**
|
|
41
41
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
@@ -63,13 +63,13 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
63
63
|
* Called at the start of a Chat Model run, with the prompt(s)
|
|
64
64
|
* and the run ID.
|
|
65
65
|
*/
|
|
66
|
-
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown
|
|
66
|
+
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
67
|
Promise<any> | any;
|
|
68
68
|
/**
|
|
69
69
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
70
70
|
* and the run ID.
|
|
71
71
|
*/
|
|
72
|
-
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
73
|
Promise<any> | any;
|
|
74
74
|
/**
|
|
75
75
|
* Called if a Chain run encounters an error
|
|
@@ -89,7 +89,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
89
89
|
* Called at the start of a Tool run, with the tool name and input
|
|
90
90
|
* and the run ID.
|
|
91
91
|
*/
|
|
92
|
-
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown
|
|
92
|
+
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
93
|
Promise<any> | any;
|
|
94
94
|
/**
|
|
95
95
|
* Called if a Tool run encounters an error
|
|
@@ -112,7 +112,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
112
112
|
* with the final output and the run ID.
|
|
113
113
|
*/
|
|
114
114
|
handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
|
|
115
|
-
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown
|
|
115
|
+
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
116
|
Promise<any> | any;
|
|
117
117
|
handleRetrieverEnd?(documents: Document[], runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
118
|
Promise<any> | any;
|
|
@@ -199,7 +199,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
199
199
|
* Called at the start of an LLM or Chat Model run, with the prompt(s)
|
|
200
200
|
* and the run ID.
|
|
201
201
|
*/
|
|
202
|
-
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
202
|
+
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
203
203
|
/**
|
|
204
204
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
205
205
|
*/
|
|
@@ -216,12 +216,12 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
216
216
|
* Called at the start of a Chat Model run, with the prompt(s)
|
|
217
217
|
* and the run ID.
|
|
218
218
|
*/
|
|
219
|
-
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
219
|
+
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
220
220
|
/**
|
|
221
221
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
222
222
|
* and the run ID.
|
|
223
223
|
*/
|
|
224
|
-
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined): any;
|
|
224
|
+
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined, name?: string | undefined): any;
|
|
225
225
|
/**
|
|
226
226
|
* Called if a Chain run encounters an error
|
|
227
227
|
*/
|
|
@@ -238,7 +238,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
238
238
|
* Called at the start of a Tool run, with the tool name and input
|
|
239
239
|
* and the run ID.
|
|
240
240
|
*/
|
|
241
|
-
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
241
|
+
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
242
242
|
/**
|
|
243
243
|
* Called if a Tool run encounters an error
|
|
244
244
|
*/
|
|
@@ -258,7 +258,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
258
258
|
* with the final output and the run ID.
|
|
259
259
|
*/
|
|
260
260
|
handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
|
|
261
|
-
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
261
|
+
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
262
262
|
handleRetrieverEnd?(documents: Document<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
|
|
263
263
|
handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
|
|
264
264
|
};
|
|
@@ -101,7 +101,13 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
101
101
|
writable: true,
|
|
102
102
|
value: void 0
|
|
103
103
|
});
|
|
104
|
-
Object.defineProperty(this, "
|
|
104
|
+
Object.defineProperty(this, "keyMapByRunId", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: {}
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "counterMapByRunName", {
|
|
105
111
|
enumerable: true,
|
|
106
112
|
configurable: true,
|
|
107
113
|
writable: true,
|
|
@@ -191,7 +197,7 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
191
197
|
id: run.id,
|
|
192
198
|
streamed_output: [],
|
|
193
199
|
final_output: undefined,
|
|
194
|
-
logs:
|
|
200
|
+
logs: {},
|
|
195
201
|
},
|
|
196
202
|
},
|
|
197
203
|
],
|
|
@@ -200,7 +206,13 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
200
206
|
if (!this._includeRun(run)) {
|
|
201
207
|
return;
|
|
202
208
|
}
|
|
203
|
-
this.
|
|
209
|
+
if (this.counterMapByRunName[run.name] === undefined) {
|
|
210
|
+
this.counterMapByRunName[run.name] = 0;
|
|
211
|
+
}
|
|
212
|
+
this.counterMapByRunName[run.name] += 1;
|
|
213
|
+
const count = this.counterMapByRunName[run.name];
|
|
214
|
+
this.keyMapByRunId[run.id] =
|
|
215
|
+
count === 1 ? run.name : `${run.name}:${count}`;
|
|
204
216
|
const logEntry = {
|
|
205
217
|
id: run.id,
|
|
206
218
|
name: run.name,
|
|
@@ -216,7 +228,7 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
216
228
|
ops: [
|
|
217
229
|
{
|
|
218
230
|
op: "add",
|
|
219
|
-
path: `/logs/${this.
|
|
231
|
+
path: `/logs/${this.keyMapByRunId[run.id]}`,
|
|
220
232
|
value: logEntry,
|
|
221
233
|
},
|
|
222
234
|
],
|
|
@@ -224,21 +236,21 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
224
236
|
}
|
|
225
237
|
async onRunUpdate(run) {
|
|
226
238
|
try {
|
|
227
|
-
const
|
|
228
|
-
if (
|
|
239
|
+
const runName = this.keyMapByRunId[run.id];
|
|
240
|
+
if (runName === undefined) {
|
|
229
241
|
return;
|
|
230
242
|
}
|
|
231
243
|
const ops = [
|
|
232
244
|
{
|
|
233
245
|
op: "add",
|
|
234
|
-
path: `/logs/${
|
|
246
|
+
path: `/logs/${runName}/final_output`,
|
|
235
247
|
value: run.outputs,
|
|
236
248
|
},
|
|
237
249
|
];
|
|
238
250
|
if (run.end_time !== undefined) {
|
|
239
251
|
ops.push({
|
|
240
252
|
op: "add",
|
|
241
|
-
path: `/logs/${
|
|
253
|
+
path: `/logs/${runName}/end_time`,
|
|
242
254
|
value: new Date(run.end_time).toISOString(),
|
|
243
255
|
});
|
|
244
256
|
}
|
|
@@ -264,15 +276,15 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
264
276
|
}
|
|
265
277
|
}
|
|
266
278
|
async onLLMNewToken(run, token) {
|
|
267
|
-
const
|
|
268
|
-
if (
|
|
279
|
+
const runName = this.keyMapByRunId[run.id];
|
|
280
|
+
if (runName === undefined) {
|
|
269
281
|
return;
|
|
270
282
|
}
|
|
271
283
|
const patch = new RunLogPatch({
|
|
272
284
|
ops: [
|
|
273
285
|
{
|
|
274
286
|
op: "add",
|
|
275
|
-
path: `/logs/${
|
|
287
|
+
path: `/logs/${runName}/streamed_output_str/-`,
|
|
276
288
|
value: token,
|
|
277
289
|
},
|
|
278
290
|
],
|
|
@@ -37,7 +37,7 @@ export type RunState = {
|
|
|
37
37
|
* List of sub-runs contained in this run, if any, in the order they were started.
|
|
38
38
|
* If filters were supplied, this list will contain only the runs that matched the filters.
|
|
39
39
|
*/
|
|
40
|
-
logs: LogEntry
|
|
40
|
+
logs: Record<string, LogEntry>;
|
|
41
41
|
};
|
|
42
42
|
/**
|
|
43
43
|
* List of jsonpatch JSONPatchOperations, which describe how to create the run state
|
|
@@ -84,7 +84,8 @@ export declare class LogStreamCallbackHandler extends BaseTracer {
|
|
|
84
84
|
protected excludeNames?: string[];
|
|
85
85
|
protected excludeTypes?: string[];
|
|
86
86
|
protected excludeTags?: string[];
|
|
87
|
-
private
|
|
87
|
+
private keyMapByRunId;
|
|
88
|
+
private counterMapByRunName;
|
|
88
89
|
protected transformStream: TransformStream;
|
|
89
90
|
writer: WritableStreamDefaultWriter;
|
|
90
91
|
receiveStream: IterableReadableStream<RunLogPatch>;
|
|
@@ -96,7 +96,13 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
96
96
|
writable: true,
|
|
97
97
|
value: void 0
|
|
98
98
|
});
|
|
99
|
-
Object.defineProperty(this, "
|
|
99
|
+
Object.defineProperty(this, "keyMapByRunId", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: {}
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(this, "counterMapByRunName", {
|
|
100
106
|
enumerable: true,
|
|
101
107
|
configurable: true,
|
|
102
108
|
writable: true,
|
|
@@ -186,7 +192,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
186
192
|
id: run.id,
|
|
187
193
|
streamed_output: [],
|
|
188
194
|
final_output: undefined,
|
|
189
|
-
logs:
|
|
195
|
+
logs: {},
|
|
190
196
|
},
|
|
191
197
|
},
|
|
192
198
|
],
|
|
@@ -195,7 +201,13 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
195
201
|
if (!this._includeRun(run)) {
|
|
196
202
|
return;
|
|
197
203
|
}
|
|
198
|
-
this.
|
|
204
|
+
if (this.counterMapByRunName[run.name] === undefined) {
|
|
205
|
+
this.counterMapByRunName[run.name] = 0;
|
|
206
|
+
}
|
|
207
|
+
this.counterMapByRunName[run.name] += 1;
|
|
208
|
+
const count = this.counterMapByRunName[run.name];
|
|
209
|
+
this.keyMapByRunId[run.id] =
|
|
210
|
+
count === 1 ? run.name : `${run.name}:${count}`;
|
|
199
211
|
const logEntry = {
|
|
200
212
|
id: run.id,
|
|
201
213
|
name: run.name,
|
|
@@ -211,7 +223,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
211
223
|
ops: [
|
|
212
224
|
{
|
|
213
225
|
op: "add",
|
|
214
|
-
path: `/logs/${this.
|
|
226
|
+
path: `/logs/${this.keyMapByRunId[run.id]}`,
|
|
215
227
|
value: logEntry,
|
|
216
228
|
},
|
|
217
229
|
],
|
|
@@ -219,21 +231,21 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
219
231
|
}
|
|
220
232
|
async onRunUpdate(run) {
|
|
221
233
|
try {
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
234
|
+
const runName = this.keyMapByRunId[run.id];
|
|
235
|
+
if (runName === undefined) {
|
|
224
236
|
return;
|
|
225
237
|
}
|
|
226
238
|
const ops = [
|
|
227
239
|
{
|
|
228
240
|
op: "add",
|
|
229
|
-
path: `/logs/${
|
|
241
|
+
path: `/logs/${runName}/final_output`,
|
|
230
242
|
value: run.outputs,
|
|
231
243
|
},
|
|
232
244
|
];
|
|
233
245
|
if (run.end_time !== undefined) {
|
|
234
246
|
ops.push({
|
|
235
247
|
op: "add",
|
|
236
|
-
path: `/logs/${
|
|
248
|
+
path: `/logs/${runName}/end_time`,
|
|
237
249
|
value: new Date(run.end_time).toISOString(),
|
|
238
250
|
});
|
|
239
251
|
}
|
|
@@ -259,15 +271,15 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
259
271
|
}
|
|
260
272
|
}
|
|
261
273
|
async onLLMNewToken(run, token) {
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
274
|
+
const runName = this.keyMapByRunId[run.id];
|
|
275
|
+
if (runName === undefined) {
|
|
264
276
|
return;
|
|
265
277
|
}
|
|
266
278
|
const patch = new RunLogPatch({
|
|
267
279
|
ops: [
|
|
268
280
|
{
|
|
269
281
|
op: "add",
|
|
270
|
-
path: `/logs/${
|
|
282
|
+
path: `/logs/${runName}/streamed_output_str/-`,
|
|
271
283
|
value: token,
|
|
272
284
|
},
|
|
273
285
|
],
|
|
@@ -330,13 +330,13 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
330
330
|
this.inheritableHandlers = [];
|
|
331
331
|
this._parentRunId = parentRunId;
|
|
332
332
|
}
|
|
333
|
-
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
333
|
+
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
334
334
|
return Promise.all(prompts.map(async (prompt) => {
|
|
335
335
|
const runId = (0, uuid_1.v4)();
|
|
336
336
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
337
337
|
if (!handler.ignoreLLM) {
|
|
338
338
|
try {
|
|
339
|
-
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
339
|
+
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
340
340
|
}
|
|
341
341
|
catch (err) {
|
|
342
342
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
|
|
@@ -346,17 +346,18 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
346
346
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
347
347
|
}));
|
|
348
348
|
}
|
|
349
|
-
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
349
|
+
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
350
350
|
return Promise.all(messages.map(async (messageGroup) => {
|
|
351
351
|
const runId = (0, uuid_1.v4)();
|
|
352
352
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
353
353
|
if (!handler.ignoreLLM) {
|
|
354
354
|
try {
|
|
355
|
-
if (handler.handleChatModelStart)
|
|
356
|
-
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
355
|
+
if (handler.handleChatModelStart) {
|
|
356
|
+
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
357
|
+
}
|
|
357
358
|
else if (handler.handleLLMStart) {
|
|
358
359
|
const messageString = (0, base_js_2.getBufferString)(messageGroup);
|
|
359
|
-
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
360
|
+
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
360
361
|
}
|
|
361
362
|
}
|
|
362
363
|
catch (err) {
|
|
@@ -367,11 +368,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
367
368
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
368
369
|
}));
|
|
369
370
|
}
|
|
370
|
-
async handleChainStart(chain, inputs, runId = (0, uuid_1.v4)(), runType = undefined) {
|
|
371
|
+
async handleChainStart(chain, inputs, runId = (0, uuid_1.v4)(), runType = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
371
372
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
372
373
|
if (!handler.ignoreChain) {
|
|
373
374
|
try {
|
|
374
|
-
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType);
|
|
375
|
+
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
|
|
375
376
|
}
|
|
376
377
|
catch (err) {
|
|
377
378
|
console.error(`Error in handler ${handler.constructor.name}, handleChainStart: ${err}`);
|
|
@@ -380,11 +381,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
380
381
|
}, handler.awaitHandlers)));
|
|
381
382
|
return new CallbackManagerForChainRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
382
383
|
}
|
|
383
|
-
async handleToolStart(tool, input, runId = (0, uuid_1.v4)()) {
|
|
384
|
+
async handleToolStart(tool, input, runId = (0, uuid_1.v4)(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
384
385
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
385
386
|
if (!handler.ignoreAgent) {
|
|
386
387
|
try {
|
|
387
|
-
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata);
|
|
388
|
+
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
388
389
|
}
|
|
389
390
|
catch (err) {
|
|
390
391
|
console.error(`Error in handler ${handler.constructor.name}, handleToolStart: ${err}`);
|
|
@@ -393,11 +394,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
393
394
|
}, handler.awaitHandlers)));
|
|
394
395
|
return new CallbackManagerForToolRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
395
396
|
}
|
|
396
|
-
async handleRetrieverStart(retriever, query, runId = (0, uuid_1.v4)(), _parentRunId = undefined) {
|
|
397
|
+
async handleRetrieverStart(retriever, query, runId = (0, uuid_1.v4)(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
397
398
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
398
399
|
if (!handler.ignoreRetriever) {
|
|
399
400
|
try {
|
|
400
|
-
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata);
|
|
401
|
+
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
401
402
|
}
|
|
402
403
|
catch (err) {
|
|
403
404
|
console.error(`Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}`);
|
|
@@ -12,6 +12,10 @@ export interface CallbackManagerOptions {
|
|
|
12
12
|
}
|
|
13
13
|
export type Callbacks = CallbackManager | (BaseCallbackHandler | CallbackHandlerMethods)[];
|
|
14
14
|
export interface BaseCallbackConfig {
|
|
15
|
+
/**
|
|
16
|
+
* Name for the tracer run for this call. Defaults to the name of the class.
|
|
17
|
+
*/
|
|
18
|
+
runName?: string;
|
|
15
19
|
/**
|
|
16
20
|
* Tags for this call and any sub-calls (eg. a Chain calling an LLM).
|
|
17
21
|
* You can use these to filter calls.
|
|
@@ -92,11 +96,11 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
|
|
|
92
96
|
name: string;
|
|
93
97
|
private readonly _parentRunId?;
|
|
94
98
|
constructor(parentRunId?: string);
|
|
95
|
-
handleLLMStart(llm: Serialized, prompts: string[], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
96
|
-
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
97
|
-
handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined): Promise<CallbackManagerForChainRun>;
|
|
98
|
-
handleToolStart(tool: Serialized, input: string, runId?: string): Promise<CallbackManagerForToolRun>;
|
|
99
|
-
handleRetrieverStart(retriever: Serialized, query: string, runId?: string, _parentRunId?: string | undefined): Promise<CallbackManagerForRetrieverRun>;
|
|
99
|
+
handleLLMStart(llm: Serialized, prompts: string[], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
100
|
+
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
101
|
+
handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForChainRun>;
|
|
102
|
+
handleToolStart(tool: Serialized, input: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForToolRun>;
|
|
103
|
+
handleRetrieverStart(retriever: Serialized, query: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForRetrieverRun>;
|
|
100
104
|
addHandler(handler: BaseCallbackHandler, inherit?: boolean): void;
|
|
101
105
|
removeHandler(handler: BaseCallbackHandler): void;
|
|
102
106
|
setHandlers(handlers: BaseCallbackHandler[], inherit?: boolean): void;
|
|
@@ -321,13 +321,13 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
321
321
|
this.inheritableHandlers = [];
|
|
322
322
|
this._parentRunId = parentRunId;
|
|
323
323
|
}
|
|
324
|
-
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
324
|
+
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
325
325
|
return Promise.all(prompts.map(async (prompt) => {
|
|
326
326
|
const runId = uuidv4();
|
|
327
327
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
328
328
|
if (!handler.ignoreLLM) {
|
|
329
329
|
try {
|
|
330
|
-
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
330
|
+
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
331
331
|
}
|
|
332
332
|
catch (err) {
|
|
333
333
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
|
|
@@ -337,17 +337,18 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
337
337
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
338
338
|
}));
|
|
339
339
|
}
|
|
340
|
-
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
340
|
+
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
341
341
|
return Promise.all(messages.map(async (messageGroup) => {
|
|
342
342
|
const runId = uuidv4();
|
|
343
343
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
344
344
|
if (!handler.ignoreLLM) {
|
|
345
345
|
try {
|
|
346
|
-
if (handler.handleChatModelStart)
|
|
347
|
-
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
346
|
+
if (handler.handleChatModelStart) {
|
|
347
|
+
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
348
|
+
}
|
|
348
349
|
else if (handler.handleLLMStart) {
|
|
349
350
|
const messageString = getBufferString(messageGroup);
|
|
350
|
-
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
351
|
+
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
351
352
|
}
|
|
352
353
|
}
|
|
353
354
|
catch (err) {
|
|
@@ -358,11 +359,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
358
359
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
359
360
|
}));
|
|
360
361
|
}
|
|
361
|
-
async handleChainStart(chain, inputs, runId = uuidv4(), runType = undefined) {
|
|
362
|
+
async handleChainStart(chain, inputs, runId = uuidv4(), runType = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
362
363
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
363
364
|
if (!handler.ignoreChain) {
|
|
364
365
|
try {
|
|
365
|
-
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType);
|
|
366
|
+
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
|
|
366
367
|
}
|
|
367
368
|
catch (err) {
|
|
368
369
|
console.error(`Error in handler ${handler.constructor.name}, handleChainStart: ${err}`);
|
|
@@ -371,11 +372,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
371
372
|
}, handler.awaitHandlers)));
|
|
372
373
|
return new CallbackManagerForChainRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
373
374
|
}
|
|
374
|
-
async handleToolStart(tool, input, runId = uuidv4()) {
|
|
375
|
+
async handleToolStart(tool, input, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
375
376
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
376
377
|
if (!handler.ignoreAgent) {
|
|
377
378
|
try {
|
|
378
|
-
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata);
|
|
379
|
+
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
379
380
|
}
|
|
380
381
|
catch (err) {
|
|
381
382
|
console.error(`Error in handler ${handler.constructor.name}, handleToolStart: ${err}`);
|
|
@@ -384,11 +385,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
384
385
|
}, handler.awaitHandlers)));
|
|
385
386
|
return new CallbackManagerForToolRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
386
387
|
}
|
|
387
|
-
async handleRetrieverStart(retriever, query, runId = uuidv4(), _parentRunId = undefined) {
|
|
388
|
+
async handleRetrieverStart(retriever, query, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
388
389
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
389
390
|
if (!handler.ignoreRetriever) {
|
|
390
391
|
try {
|
|
391
|
-
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata);
|
|
392
|
+
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
392
393
|
}
|
|
393
394
|
catch (err) {
|
|
394
395
|
console.error(`Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}`);
|
package/dist/chains/base.cjs
CHANGED
|
@@ -98,7 +98,7 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
98
98
|
const fullValues = await this._formatValues(values);
|
|
99
99
|
const parsedConfig = (0, manager_js_1.parseCallbackConfigArg)(config);
|
|
100
100
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags || tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
101
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues);
|
|
101
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
102
102
|
let outputValues;
|
|
103
103
|
try {
|
|
104
104
|
outputValues = await (values.signal
|
package/dist/chains/base.js
CHANGED
|
@@ -95,7 +95,7 @@ export class BaseChain extends BaseLangChain {
|
|
|
95
95
|
const fullValues = await this._formatValues(values);
|
|
96
96
|
const parsedConfig = parseCallbackConfigArg(config);
|
|
97
97
|
const callbackManager_ = await CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags || tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
98
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues);
|
|
98
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
99
99
|
let outputValues;
|
|
100
100
|
try {
|
|
101
101
|
outputValues = await (values.signal
|
|
@@ -71,7 +71,7 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
|
|
|
71
71
|
options: callOptions,
|
|
72
72
|
invocation_params: this?.invocationParams(callOptions),
|
|
73
73
|
};
|
|
74
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra);
|
|
74
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
75
75
|
let generationChunk;
|
|
76
76
|
try {
|
|
77
77
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
@@ -103,7 +103,7 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
|
|
|
103
103
|
options: parsedOptions,
|
|
104
104
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
105
105
|
};
|
|
106
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
|
|
106
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra, undefined, undefined, handledOptions.runName);
|
|
107
107
|
// generate results
|
|
108
108
|
const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
|
|
109
109
|
// handle results
|
package/dist/chat_models/base.js
CHANGED
|
@@ -67,7 +67,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
67
67
|
options: callOptions,
|
|
68
68
|
invocation_params: this?.invocationParams(callOptions),
|
|
69
69
|
};
|
|
70
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra);
|
|
70
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
71
71
|
let generationChunk;
|
|
72
72
|
try {
|
|
73
73
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
@@ -99,7 +99,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
99
99
|
options: parsedOptions,
|
|
100
100
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
101
101
|
};
|
|
102
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
|
|
102
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra, undefined, undefined, handledOptions.runName);
|
|
103
103
|
// generate results
|
|
104
104
|
const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
|
|
105
105
|
// handle results
|
package/dist/llms/base.cjs
CHANGED
|
@@ -55,7 +55,7 @@ class BaseLLM extends index_js_2.BaseLanguageModel {
|
|
|
55
55
|
options: callOptions,
|
|
56
56
|
invocation_params: this?.invocationParams(callOptions),
|
|
57
57
|
};
|
|
58
|
-
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), [prompt.toString()], undefined, undefined, extra);
|
|
58
|
+
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), [prompt.toString()], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
59
59
|
let generation = new index_js_1.GenerationChunk({
|
|
60
60
|
text: "",
|
|
61
61
|
});
|
|
@@ -129,7 +129,7 @@ class BaseLLM extends index_js_2.BaseLanguageModel {
|
|
|
129
129
|
options: parsedOptions,
|
|
130
130
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
131
131
|
};
|
|
132
|
-
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), prompts, undefined, undefined, extra);
|
|
132
|
+
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), prompts, undefined, undefined, extra, undefined, undefined, handledOptions?.runName);
|
|
133
133
|
let output;
|
|
134
134
|
try {
|
|
135
135
|
output = await this._generate(prompts, parsedOptions, runManagers?.[0]);
|
package/dist/llms/base.js
CHANGED
|
@@ -52,7 +52,7 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
52
52
|
options: callOptions,
|
|
53
53
|
invocation_params: this?.invocationParams(callOptions),
|
|
54
54
|
};
|
|
55
|
-
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), [prompt.toString()], undefined, undefined, extra);
|
|
55
|
+
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), [prompt.toString()], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
56
56
|
let generation = new GenerationChunk({
|
|
57
57
|
text: "",
|
|
58
58
|
});
|
|
@@ -126,7 +126,7 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
126
126
|
options: parsedOptions,
|
|
127
127
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
128
128
|
};
|
|
129
|
-
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), prompts, undefined, undefined, extra);
|
|
129
|
+
const runManagers = await callbackManager_?.handleLLMStart(this.toJSON(), prompts, undefined, undefined, extra, undefined, undefined, handledOptions?.runName);
|
|
130
130
|
let output;
|
|
131
131
|
try {
|
|
132
132
|
output = await this._generate(prompts, parsedOptions, runManagers?.[0]);
|
package/dist/prompts/chat.cjs
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
// Replace with "string" when we are comfortable with a breaking change.
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.ChatPromptTemplate = exports.SystemMessagePromptTemplate = exports.AIMessagePromptTemplate = exports.HumanMessagePromptTemplate = exports.ChatMessagePromptTemplate = exports.BaseChatPromptTemplate = exports.BaseMessageStringPromptTemplate = exports.MessagesPlaceholder = exports.ChatPromptValue = exports.BaseMessagePromptTemplate = void 0;
|
|
6
|
+
const base_js_1 = require("../memory/base.cjs");
|
|
6
7
|
const index_js_1 = require("../schema/index.cjs");
|
|
7
8
|
const index_js_2 = require("../schema/runnable/index.cjs");
|
|
8
|
-
const
|
|
9
|
+
const base_js_2 = require("./base.cjs");
|
|
9
10
|
const prompt_js_1 = require("./prompt.cjs");
|
|
10
11
|
/**
|
|
11
12
|
* Abstract class that serves as a base for creating message prompt
|
|
@@ -74,7 +75,7 @@ class ChatPromptValue extends index_js_1.BasePromptValue {
|
|
|
74
75
|
this.messages = fields.messages;
|
|
75
76
|
}
|
|
76
77
|
toString() {
|
|
77
|
-
return
|
|
78
|
+
return (0, base_js_1.getBufferString)(this.messages);
|
|
78
79
|
}
|
|
79
80
|
toChatMessages() {
|
|
80
81
|
return this.messages;
|
|
@@ -142,7 +143,7 @@ exports.BaseMessageStringPromptTemplate = BaseMessageStringPromptTemplate;
|
|
|
142
143
|
* Abstract class that serves as a base for creating chat prompt
|
|
143
144
|
* templates. It extends the BasePromptTemplate.
|
|
144
145
|
*/
|
|
145
|
-
class BaseChatPromptTemplate extends
|
|
146
|
+
class BaseChatPromptTemplate extends base_js_2.BasePromptTemplate {
|
|
146
147
|
constructor(input) {
|
|
147
148
|
super(input);
|
|
148
149
|
}
|
package/dist/prompts/chat.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Default generic "any" values are for backwards compatibility.
|
|
2
2
|
// Replace with "string" when we are comfortable with a breaking change.
|
|
3
|
+
import { getBufferString } from "../memory/base.js";
|
|
3
4
|
import { AIMessage, BaseMessage, BasePromptValue, ChatMessage, HumanMessage, SystemMessage, coerceMessageLikeToMessage, isBaseMessage, } from "../schema/index.js";
|
|
4
5
|
import { Runnable } from "../schema/runnable/index.js";
|
|
5
6
|
import { BasePromptTemplate, } from "./base.js";
|
|
@@ -70,7 +71,7 @@ export class ChatPromptValue extends BasePromptValue {
|
|
|
70
71
|
this.messages = fields.messages;
|
|
71
72
|
}
|
|
72
73
|
toString() {
|
|
73
|
-
return
|
|
74
|
+
return getBufferString(this.messages);
|
|
74
75
|
}
|
|
75
76
|
toChatMessages() {
|
|
76
77
|
return this.messages;
|
|
@@ -39,10 +39,17 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
39
39
|
writable: true,
|
|
40
40
|
value: void 0
|
|
41
41
|
});
|
|
42
|
+
Object.defineProperty(this, "filter", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
42
48
|
this.vectorStore = data.vectorStore;
|
|
43
49
|
this.k = data.k ?? 4;
|
|
44
50
|
this.exampleKeys = data.exampleKeys;
|
|
45
51
|
this.inputKeys = data.inputKeys;
|
|
52
|
+
this.filter = data.filter;
|
|
46
53
|
}
|
|
47
54
|
/**
|
|
48
55
|
* Method that adds a new example to the vectorStore. The example is
|
|
@@ -56,7 +63,7 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
56
63
|
await this.vectorStore.addDocuments([
|
|
57
64
|
new document_js_1.Document({
|
|
58
65
|
pageContent: stringExample,
|
|
59
|
-
metadata:
|
|
66
|
+
metadata: example,
|
|
60
67
|
}),
|
|
61
68
|
]);
|
|
62
69
|
}
|
|
@@ -70,7 +77,7 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
70
77
|
async selectExamples(inputVariables) {
|
|
71
78
|
const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
|
|
72
79
|
const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
|
|
73
|
-
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k);
|
|
80
|
+
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k, this.filter);
|
|
74
81
|
const examples = exampleDocs.map((doc) => doc.metadata);
|
|
75
82
|
if (this.exampleKeys) {
|
|
76
83
|
// If example keys are provided, filter examples to those keys.
|
|
@@ -6,9 +6,10 @@ import { BaseExampleSelector } from "../base.js";
|
|
|
6
6
|
* Interface for the input data of the SemanticSimilarityExampleSelector
|
|
7
7
|
* class.
|
|
8
8
|
*/
|
|
9
|
-
export interface SemanticSimilarityExampleSelectorInput {
|
|
10
|
-
vectorStore:
|
|
9
|
+
export interface SemanticSimilarityExampleSelectorInput<V extends VectorStore = VectorStore> {
|
|
10
|
+
vectorStore: V;
|
|
11
11
|
k?: number;
|
|
12
|
+
filter?: V["FilterType"];
|
|
12
13
|
exampleKeys?: string[];
|
|
13
14
|
inputKeys?: string[];
|
|
14
15
|
}
|
|
@@ -16,12 +17,13 @@ export interface SemanticSimilarityExampleSelectorInput {
|
|
|
16
17
|
* Class that selects examples based on semantic similarity. It extends
|
|
17
18
|
* the BaseExampleSelector class.
|
|
18
19
|
*/
|
|
19
|
-
export declare class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
20
|
-
vectorStore:
|
|
20
|
+
export declare class SemanticSimilarityExampleSelector<V extends VectorStore = VectorStore> extends BaseExampleSelector {
|
|
21
|
+
vectorStore: V;
|
|
21
22
|
k: number;
|
|
22
23
|
exampleKeys?: string[];
|
|
23
24
|
inputKeys?: string[];
|
|
24
|
-
|
|
25
|
+
filter?: V["FilterType"];
|
|
26
|
+
constructor(data: SemanticSimilarityExampleSelectorInput<V>);
|
|
25
27
|
/**
|
|
26
28
|
* Method that adds a new example to the vectorStore. The example is
|
|
27
29
|
* converted to a string and added to the vectorStore as a document.
|
|
@@ -36,10 +36,17 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
36
36
|
writable: true,
|
|
37
37
|
value: void 0
|
|
38
38
|
});
|
|
39
|
+
Object.defineProperty(this, "filter", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
39
45
|
this.vectorStore = data.vectorStore;
|
|
40
46
|
this.k = data.k ?? 4;
|
|
41
47
|
this.exampleKeys = data.exampleKeys;
|
|
42
48
|
this.inputKeys = data.inputKeys;
|
|
49
|
+
this.filter = data.filter;
|
|
43
50
|
}
|
|
44
51
|
/**
|
|
45
52
|
* Method that adds a new example to the vectorStore. The example is
|
|
@@ -53,7 +60,7 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
53
60
|
await this.vectorStore.addDocuments([
|
|
54
61
|
new Document({
|
|
55
62
|
pageContent: stringExample,
|
|
56
|
-
metadata:
|
|
63
|
+
metadata: example,
|
|
57
64
|
}),
|
|
58
65
|
]);
|
|
59
66
|
}
|
|
@@ -67,7 +74,7 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
67
74
|
async selectExamples(inputVariables) {
|
|
68
75
|
const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
|
|
69
76
|
const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
|
|
70
|
-
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k);
|
|
77
|
+
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k, this.filter);
|
|
71
78
|
const examples = exampleDocs.map((doc) => doc.metadata);
|
|
72
79
|
if (this.exampleKeys) {
|
|
73
80
|
// If example keys are provided, filter examples to those keys.
|
|
@@ -64,7 +64,7 @@ class BaseRetriever extends index_js_1.Runnable {
|
|
|
64
64
|
async getRelevantDocuments(query, config) {
|
|
65
65
|
const parsedConfig = (0, manager_js_1.parseCallbackConfigArg)(config);
|
|
66
66
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
67
|
-
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query);
|
|
67
|
+
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
68
68
|
try {
|
|
69
69
|
const results = await this._getRelevantDocuments(query, runManager);
|
|
70
70
|
await runManager?.handleRetrieverEnd(results);
|
package/dist/schema/retriever.js
CHANGED
|
@@ -61,7 +61,7 @@ export class BaseRetriever extends Runnable {
|
|
|
61
61
|
async getRelevantDocuments(query, config) {
|
|
62
62
|
const parsedConfig = parseCallbackConfigArg(config);
|
|
63
63
|
const callbackManager_ = await CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
64
|
-
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query);
|
|
64
|
+
const runManager = await callbackManager_?.handleRetrieverStart(this.toJSON(), query, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
65
65
|
try {
|
|
66
66
|
const results = await this._getRelevantDocuments(query, runManager);
|
|
67
67
|
await runManager?.handleRetrieverEnd(results);
|
|
@@ -143,16 +143,18 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
143
143
|
callbacks: options.callbacks,
|
|
144
144
|
tags: options.tags,
|
|
145
145
|
metadata: options.metadata,
|
|
146
|
+
runName: options.runName,
|
|
146
147
|
};
|
|
147
148
|
const callOptions = { ...options };
|
|
148
149
|
delete callOptions.callbacks;
|
|
149
150
|
delete callOptions.tags;
|
|
150
151
|
delete callOptions.metadata;
|
|
152
|
+
delete callOptions.runName;
|
|
151
153
|
return [runnableConfig, callOptions];
|
|
152
154
|
}
|
|
153
155
|
async _callWithConfig(func, input, options) {
|
|
154
156
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
155
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
|
|
157
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
|
|
156
158
|
let output;
|
|
157
159
|
try {
|
|
158
160
|
output = await func.bind(this)(input, options, runManager);
|
|
@@ -176,7 +178,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
176
178
|
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
177
179
|
const configs = this._getOptionsList((options ?? {}), inputs.length);
|
|
178
180
|
const callbackManagers = await Promise.all(configs.map(config_js_1.getCallbackMangerForConfig));
|
|
179
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
181
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, configs[i].runType, undefined, undefined, configs[i].runName)));
|
|
180
182
|
let outputs;
|
|
181
183
|
try {
|
|
182
184
|
outputs = await func(inputs, configs, runManagers, batchOptions);
|
|
@@ -206,7 +208,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
206
208
|
if (!runManager) {
|
|
207
209
|
// Start the run manager AFTER the iterator starts to preserve
|
|
208
210
|
// tracing order
|
|
209
|
-
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType);
|
|
211
|
+
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
|
|
210
212
|
}
|
|
211
213
|
if (finalInputSupported) {
|
|
212
214
|
if (finalInput === undefined) {
|
|
@@ -257,7 +259,16 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
257
259
|
await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
|
|
258
260
|
}
|
|
259
261
|
_patchConfig(config = {}, callbackManager = undefined) {
|
|
260
|
-
|
|
262
|
+
const newConfig = { ...config };
|
|
263
|
+
if (callbackManager !== undefined) {
|
|
264
|
+
/**
|
|
265
|
+
* If we're replacing callbacks we need to unset runName
|
|
266
|
+
* since that should apply only to the same run as the original callbacks
|
|
267
|
+
*/
|
|
268
|
+
delete newConfig.runName;
|
|
269
|
+
return { ...newConfig, callbacks: callbackManager };
|
|
270
|
+
}
|
|
271
|
+
return newConfig;
|
|
261
272
|
}
|
|
262
273
|
/**
|
|
263
274
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
@@ -690,7 +701,7 @@ class RunnableSequence extends Runnable {
|
|
|
690
701
|
}
|
|
691
702
|
async invoke(input, options) {
|
|
692
703
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
693
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
704
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
694
705
|
let nextStepInput = input;
|
|
695
706
|
let finalOutput;
|
|
696
707
|
try {
|
|
@@ -712,7 +723,7 @@ class RunnableSequence extends Runnable {
|
|
|
712
723
|
async batch(inputs, options, batchOptions) {
|
|
713
724
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
714
725
|
const callbackManagers = await Promise.all(configList.map(config_js_1.getCallbackMangerForConfig));
|
|
715
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
726
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
716
727
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
717
728
|
let nextStepInputs = inputs;
|
|
718
729
|
let finalOutputs;
|
|
@@ -733,7 +744,7 @@ class RunnableSequence extends Runnable {
|
|
|
733
744
|
}
|
|
734
745
|
async *_streamIterator(input, options) {
|
|
735
746
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
736
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
747
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
737
748
|
let nextStepInput = input;
|
|
738
749
|
const steps = [this.first, ...this.middle, this.last];
|
|
739
750
|
// Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
|
|
@@ -867,7 +878,7 @@ class RunnableMap extends Runnable {
|
|
|
867
878
|
const callbackManager_ = await (0, config_js_1.getCallbackMangerForConfig)(options);
|
|
868
879
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
|
|
869
880
|
input,
|
|
870
|
-
});
|
|
881
|
+
}, undefined, undefined, undefined, undefined, options?.runName);
|
|
871
882
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
872
883
|
const output = {};
|
|
873
884
|
try {
|
|
@@ -907,6 +918,11 @@ class RunnableLambda extends Runnable {
|
|
|
907
918
|
});
|
|
908
919
|
this.func = fields.func;
|
|
909
920
|
}
|
|
921
|
+
static from(func) {
|
|
922
|
+
return new RunnableLambda({
|
|
923
|
+
func,
|
|
924
|
+
});
|
|
925
|
+
}
|
|
910
926
|
async _invoke(input, config, runManager) {
|
|
911
927
|
let output = await this.func(input);
|
|
912
928
|
if (output && Runnable.isRunnable(output)) {
|
|
@@ -963,7 +979,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
963
979
|
}
|
|
964
980
|
async invoke(input, options) {
|
|
965
981
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
|
|
966
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
982
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
967
983
|
let firstError;
|
|
968
984
|
for (const runnable of this.runnables()) {
|
|
969
985
|
try {
|
|
@@ -989,7 +1005,7 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
989
1005
|
}
|
|
990
1006
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
991
1007
|
const callbackManagers = await Promise.all(configList.map((config) => manager_js_1.CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
|
|
992
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
1008
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
993
1009
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
994
1010
|
let firstError;
|
|
995
1011
|
for (const runnable of this.runnables()) {
|
|
@@ -57,7 +57,9 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
57
57
|
withFallbacks(fields: {
|
|
58
58
|
fallbacks: Runnable<RunInput, RunOutput>[];
|
|
59
59
|
}): RunnableWithFallbacks<RunInput, RunOutput>;
|
|
60
|
-
protected _getOptionsList(options: Partial<CallOptions> | Partial<CallOptions>[], length?: number): Partial<CallOptions
|
|
60
|
+
protected _getOptionsList(options: Partial<CallOptions> | Partial<CallOptions>[], length?: number): Partial<CallOptions & {
|
|
61
|
+
runType?: string;
|
|
62
|
+
}>[];
|
|
61
63
|
/**
|
|
62
64
|
* Default implementation of batch, which calls invoke N times.
|
|
63
65
|
* Subclasses should override this method if they can batch more efficiently.
|
|
@@ -300,6 +302,7 @@ export declare class RunnableLambda<RunInput, RunOutput> extends Runnable<RunInp
|
|
|
300
302
|
constructor(fields: {
|
|
301
303
|
func: RunnableFunc<RunInput, RunOutput>;
|
|
302
304
|
});
|
|
305
|
+
static from<RunInput, RunOutput>(func: RunnableFunc<RunInput, RunOutput>): RunnableLambda<RunInput, RunOutput>;
|
|
303
306
|
_invoke(input: RunInput, config?: Partial<BaseCallbackConfig>, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
|
|
304
307
|
invoke(input: RunInput, options?: Partial<BaseCallbackConfig>): Promise<RunOutput>;
|
|
305
308
|
}
|
|
@@ -137,16 +137,18 @@ export class Runnable extends Serializable {
|
|
|
137
137
|
callbacks: options.callbacks,
|
|
138
138
|
tags: options.tags,
|
|
139
139
|
metadata: options.metadata,
|
|
140
|
+
runName: options.runName,
|
|
140
141
|
};
|
|
141
142
|
const callOptions = { ...options };
|
|
142
143
|
delete callOptions.callbacks;
|
|
143
144
|
delete callOptions.tags;
|
|
144
145
|
delete callOptions.metadata;
|
|
146
|
+
delete callOptions.runName;
|
|
145
147
|
return [runnableConfig, callOptions];
|
|
146
148
|
}
|
|
147
149
|
async _callWithConfig(func, input, options) {
|
|
148
150
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
149
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType);
|
|
151
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
|
|
150
152
|
let output;
|
|
151
153
|
try {
|
|
152
154
|
output = await func.bind(this)(input, options, runManager);
|
|
@@ -170,7 +172,7 @@ export class Runnable extends Serializable {
|
|
|
170
172
|
async _batchWithConfig(func, inputs, options, batchOptions) {
|
|
171
173
|
const configs = this._getOptionsList((options ?? {}), inputs.length);
|
|
172
174
|
const callbackManagers = await Promise.all(configs.map(getCallbackMangerForConfig));
|
|
173
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
175
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, configs[i].runType, undefined, undefined, configs[i].runName)));
|
|
174
176
|
let outputs;
|
|
175
177
|
try {
|
|
176
178
|
outputs = await func(inputs, configs, runManagers, batchOptions);
|
|
@@ -200,7 +202,7 @@ export class Runnable extends Serializable {
|
|
|
200
202
|
if (!runManager) {
|
|
201
203
|
// Start the run manager AFTER the iterator starts to preserve
|
|
202
204
|
// tracing order
|
|
203
|
-
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType);
|
|
205
|
+
runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
|
|
204
206
|
}
|
|
205
207
|
if (finalInputSupported) {
|
|
206
208
|
if (finalInput === undefined) {
|
|
@@ -251,7 +253,16 @@ export class Runnable extends Serializable {
|
|
|
251
253
|
await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
|
|
252
254
|
}
|
|
253
255
|
_patchConfig(config = {}, callbackManager = undefined) {
|
|
254
|
-
|
|
256
|
+
const newConfig = { ...config };
|
|
257
|
+
if (callbackManager !== undefined) {
|
|
258
|
+
/**
|
|
259
|
+
* If we're replacing callbacks we need to unset runName
|
|
260
|
+
* since that should apply only to the same run as the original callbacks
|
|
261
|
+
*/
|
|
262
|
+
delete newConfig.runName;
|
|
263
|
+
return { ...newConfig, callbacks: callbackManager };
|
|
264
|
+
}
|
|
265
|
+
return newConfig;
|
|
255
266
|
}
|
|
256
267
|
/**
|
|
257
268
|
* Create a new runnable sequence that runs each individual runnable in series,
|
|
@@ -680,7 +691,7 @@ export class RunnableSequence extends Runnable {
|
|
|
680
691
|
}
|
|
681
692
|
async invoke(input, options) {
|
|
682
693
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
683
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
694
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
684
695
|
let nextStepInput = input;
|
|
685
696
|
let finalOutput;
|
|
686
697
|
try {
|
|
@@ -702,7 +713,7 @@ export class RunnableSequence extends Runnable {
|
|
|
702
713
|
async batch(inputs, options, batchOptions) {
|
|
703
714
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
704
715
|
const callbackManagers = await Promise.all(configList.map(getCallbackMangerForConfig));
|
|
705
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
716
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
706
717
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
707
718
|
let nextStepInputs = inputs;
|
|
708
719
|
let finalOutputs;
|
|
@@ -723,7 +734,7 @@ export class RunnableSequence extends Runnable {
|
|
|
723
734
|
}
|
|
724
735
|
async *_streamIterator(input, options) {
|
|
725
736
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
726
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
737
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
727
738
|
let nextStepInput = input;
|
|
728
739
|
const steps = [this.first, ...this.middle, this.last];
|
|
729
740
|
// Find the index of the last runnable in the sequence that doesn't have an overridden .transform() method
|
|
@@ -856,7 +867,7 @@ export class RunnableMap extends Runnable {
|
|
|
856
867
|
const callbackManager_ = await getCallbackMangerForConfig(options);
|
|
857
868
|
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
|
|
858
869
|
input,
|
|
859
|
-
});
|
|
870
|
+
}, undefined, undefined, undefined, undefined, options?.runName);
|
|
860
871
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
861
872
|
const output = {};
|
|
862
873
|
try {
|
|
@@ -895,6 +906,11 @@ export class RunnableLambda extends Runnable {
|
|
|
895
906
|
});
|
|
896
907
|
this.func = fields.func;
|
|
897
908
|
}
|
|
909
|
+
static from(func) {
|
|
910
|
+
return new RunnableLambda({
|
|
911
|
+
func,
|
|
912
|
+
});
|
|
913
|
+
}
|
|
898
914
|
async _invoke(input, config, runManager) {
|
|
899
915
|
let output = await this.func(input);
|
|
900
916
|
if (output && Runnable.isRunnable(output)) {
|
|
@@ -950,7 +966,7 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
950
966
|
}
|
|
951
967
|
async invoke(input, options) {
|
|
952
968
|
const callbackManager_ = await CallbackManager.configure(options?.callbacks, undefined, options?.tags, undefined, options?.metadata);
|
|
953
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"));
|
|
969
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
|
|
954
970
|
let firstError;
|
|
955
971
|
for (const runnable of this.runnables()) {
|
|
956
972
|
try {
|
|
@@ -976,7 +992,7 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
976
992
|
}
|
|
977
993
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
978
994
|
const callbackManagers = await Promise.all(configList.map((config) => CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata)));
|
|
979
|
-
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"))));
|
|
995
|
+
const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
|
|
980
996
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
981
997
|
let firstError;
|
|
982
998
|
for (const runnable of this.runnables()) {
|
|
@@ -17,7 +17,7 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
17
17
|
"config.credentials.sessionToken": "AWS_SESSION_TOKEN",
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }) {
|
|
20
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key = {}, }) {
|
|
21
21
|
super();
|
|
22
22
|
Object.defineProperty(this, "lc_namespace", {
|
|
23
23
|
enumerable: true,
|
|
@@ -65,7 +65,7 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
65
65
|
enumerable: true,
|
|
66
66
|
configurable: true,
|
|
67
67
|
writable: true,
|
|
68
|
-
value:
|
|
68
|
+
value: {}
|
|
69
69
|
});
|
|
70
70
|
this.tableName = tableName;
|
|
71
71
|
this.sessionId = sessionId;
|
|
@@ -74,10 +74,13 @@ class DynamoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
|
74
74
|
this.sortKey = sortKey;
|
|
75
75
|
this.messageAttributeName =
|
|
76
76
|
messageAttributeName ?? this.messageAttributeName;
|
|
77
|
-
this.dynamoKey =
|
|
78
|
-
|
|
79
|
-
if (this.
|
|
80
|
-
this.dynamoKey[this.
|
|
77
|
+
this.dynamoKey = key;
|
|
78
|
+
// override dynamoKey with partition key and sort key when key not specified
|
|
79
|
+
if (Object.keys(this.dynamoKey).length === 0) {
|
|
80
|
+
this.dynamoKey[this.partitionKey] = { S: this.sessionId };
|
|
81
|
+
if (this.sortKey) {
|
|
82
|
+
this.dynamoKey[this.sortKey] = { S: this.sortKey };
|
|
83
|
+
}
|
|
81
84
|
}
|
|
82
85
|
}
|
|
83
86
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
|
|
1
|
+
import { DynamoDBClientConfig, AttributeValue } from "@aws-sdk/client-dynamodb";
|
|
2
2
|
import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Interface defining the fields required to create an instance of
|
|
@@ -13,6 +13,7 @@ export interface DynamoDBChatMessageHistoryFields {
|
|
|
13
13
|
sortKey?: string;
|
|
14
14
|
messageAttributeName?: string;
|
|
15
15
|
config?: DynamoDBClientConfig;
|
|
16
|
+
key?: Record<string, AttributeValue>;
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Class providing methods to interact with a DynamoDB table to store and
|
|
@@ -31,7 +32,7 @@ export declare class DynamoDBChatMessageHistory extends BaseListChatMessageHisto
|
|
|
31
32
|
private sortKey?;
|
|
32
33
|
private messageAttributeName;
|
|
33
34
|
private dynamoKey;
|
|
34
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }: DynamoDBChatMessageHistoryFields);
|
|
35
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key, }: DynamoDBChatMessageHistoryFields);
|
|
35
36
|
/**
|
|
36
37
|
* Retrieves all messages from the DynamoDB table and returns them as an
|
|
37
38
|
* array of `BaseMessage` instances.
|
|
@@ -14,7 +14,7 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
14
14
|
"config.credentials.sessionToken": "AWS_SESSION_TOKEN",
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, }) {
|
|
17
|
+
constructor({ tableName, sessionId, partitionKey, sortKey, messageAttributeName, config, key = {}, }) {
|
|
18
18
|
super();
|
|
19
19
|
Object.defineProperty(this, "lc_namespace", {
|
|
20
20
|
enumerable: true,
|
|
@@ -62,7 +62,7 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
62
62
|
enumerable: true,
|
|
63
63
|
configurable: true,
|
|
64
64
|
writable: true,
|
|
65
|
-
value:
|
|
65
|
+
value: {}
|
|
66
66
|
});
|
|
67
67
|
this.tableName = tableName;
|
|
68
68
|
this.sessionId = sessionId;
|
|
@@ -71,10 +71,13 @@ export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
|
|
|
71
71
|
this.sortKey = sortKey;
|
|
72
72
|
this.messageAttributeName =
|
|
73
73
|
messageAttributeName ?? this.messageAttributeName;
|
|
74
|
-
this.dynamoKey =
|
|
75
|
-
|
|
76
|
-
if (this.
|
|
77
|
-
this.dynamoKey[this.
|
|
74
|
+
this.dynamoKey = key;
|
|
75
|
+
// override dynamoKey with partition key and sort key when key not specified
|
|
76
|
+
if (Object.keys(this.dynamoKey).length === 0) {
|
|
77
|
+
this.dynamoKey[this.partitionKey] = { S: this.sessionId };
|
|
78
|
+
if (this.sortKey) {
|
|
79
|
+
this.dynamoKey[this.sortKey] = { S: this.sortKey };
|
|
80
|
+
}
|
|
78
81
|
}
|
|
79
82
|
}
|
|
80
83
|
/**
|
package/dist/tools/base.cjs
CHANGED
|
@@ -68,7 +68,7 @@ class StructuredTool extends index_js_1.BaseLangChain {
|
|
|
68
68
|
}
|
|
69
69
|
const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
|
|
70
70
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
|
|
71
|
-
const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed));
|
|
71
|
+
const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed), undefined, undefined, undefined, undefined, config.runName);
|
|
72
72
|
let result;
|
|
73
73
|
try {
|
|
74
74
|
result = await this._call(parsed, runManager);
|
package/dist/tools/base.js
CHANGED
|
@@ -64,7 +64,7 @@ export class StructuredTool extends BaseLangChain {
|
|
|
64
64
|
}
|
|
65
65
|
const config = parseCallbackConfigArg(configArg);
|
|
66
66
|
const callbackManager_ = await CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
|
|
67
|
-
const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed));
|
|
67
|
+
const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed), undefined, undefined, undefined, undefined, config.runName);
|
|
68
68
|
let result;
|
|
69
69
|
try {
|
|
70
70
|
result = await this._call(parsed, runManager);
|
package/dist/util/bedrock.cjs
CHANGED
|
@@ -48,7 +48,8 @@ class BedrockLLMInputOutputAdapter {
|
|
|
48
48
|
else if (provider === "ai21") {
|
|
49
49
|
return responseBody?.completions?.[0]?.data?.text ?? "";
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
// I haven't been able to get a response with more than one result in it.
|
|
52
|
+
return responseBody.results?.[0]?.outputText;
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
exports.BedrockLLMInputOutputAdapter = BedrockLLMInputOutputAdapter;
|
package/dist/util/bedrock.js
CHANGED
|
@@ -45,6 +45,7 @@ export class BedrockLLMInputOutputAdapter {
|
|
|
45
45
|
else if (provider === "ai21") {
|
|
46
46
|
return responseBody?.completions?.[0]?.data?.text ?? "";
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
// I haven't been able to get a response with more than one result in it.
|
|
49
|
+
return responseBody.results?.[0]?.outputText;
|
|
49
50
|
}
|
|
50
51
|
}
|