langchain 0.0.154 → 0.0.155
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/callbacks/base.d.ts +42 -28
- package/dist/callbacks/handlers/log_stream.cjs +283 -0
- package/dist/callbacks/handlers/log_stream.d.ts +99 -0
- package/dist/callbacks/handlers/log_stream.js +277 -0
- package/dist/callbacks/handlers/tracer.cjs +34 -18
- package/dist/callbacks/handlers/tracer.d.ts +18 -16
- package/dist/callbacks/handlers/tracer.js +34 -18
- package/dist/document_loaders/web/notionapi.cjs +8 -4
- package/dist/document_loaders/web/notionapi.js +8 -4
- package/dist/document_loaders/web/searchapi.cjs +134 -0
- package/dist/document_loaders/web/searchapi.d.ts +65 -0
- package/dist/document_loaders/web/searchapi.js +130 -0
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/load/import_map.cjs +3 -2
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/schema/runnable/base.cjs +64 -5
- package/dist/schema/runnable/base.d.ts +13 -0
- package/dist/schema/runnable/base.js +64 -5
- package/dist/tools/index.cjs +3 -1
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.js +1 -0
- package/dist/tools/searchapi.cjs +139 -0
- package/dist/tools/searchapi.d.ts +64 -0
- package/dist/tools/searchapi.js +135 -0
- package/dist/util/fast-json-patch/index.cjs +48 -0
- package/dist/util/fast-json-patch/index.d.ts +21 -0
- package/dist/util/fast-json-patch/index.js +15 -0
- package/dist/util/fast-json-patch/src/core.cjs +469 -0
- package/dist/util/fast-json-patch/src/core.d.ts +111 -0
- package/dist/util/fast-json-patch/src/core.js +459 -0
- package/dist/util/fast-json-patch/src/helpers.cjs +194 -0
- package/dist/util/fast-json-patch/src/helpers.d.ts +36 -0
- package/dist/util/fast-json-patch/src/helpers.js +181 -0
- package/dist/util/googlevertexai-webauth.cjs +6 -2
- package/dist/util/googlevertexai-webauth.d.ts +1 -0
- package/dist/util/googlevertexai-webauth.js +6 -2
- package/dist/util/stream.cjs +2 -40
- package/dist/util/stream.d.ts +1 -2
- package/dist/util/stream.js +1 -38
- package/dist/vectorstores/pgvector.cjs +1 -1
- package/dist/vectorstores/pgvector.js +1 -1
- package/dist/vectorstores/vercel_postgres.cjs +300 -0
- package/dist/vectorstores/vercel_postgres.d.ts +145 -0
- package/dist/vectorstores/vercel_postgres.js +296 -0
- package/document_loaders/web/searchapi.cjs +1 -0
- package/document_loaders/web/searchapi.d.ts +1 -0
- package/document_loaders/web/searchapi.js +1 -0
- package/package.json +22 -1
- package/vectorstores/vercel_postgres.cjs +1 -0
- package/vectorstores/vercel_postgres.d.ts +1 -0
- package/vectorstores/vercel_postgres.js +1 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { applyPatch, } from "../../util/fast-json-patch/index.js";
|
|
2
|
+
import { BaseTracer } from "./tracer.js";
|
|
3
|
+
import { IterableReadableStream } from "../../util/stream.js";
|
|
4
|
+
/**
|
|
5
|
+
* List of jsonpatch JSONPatchOperations, which describe how to create the run state
|
|
6
|
+
* from an empty dict. This is the minimal representation of the log, designed to
|
|
7
|
+
* be serialized as JSON and sent over the wire to reconstruct the log on the other
|
|
8
|
+
* side. Reconstruction of the state can be done with any jsonpatch-compliant library,
|
|
9
|
+
* see https://jsonpatch.com for more information.
|
|
10
|
+
*/
|
|
11
|
+
export class RunLogPatch {
|
|
12
|
+
constructor(fields) {
|
|
13
|
+
Object.defineProperty(this, "ops", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
this.ops = fields.ops;
|
|
20
|
+
}
|
|
21
|
+
concat(other) {
|
|
22
|
+
const ops = this.ops.concat(other.ops);
|
|
23
|
+
const states = applyPatch({}, ops);
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
25
|
+
return new RunLog({
|
|
26
|
+
ops,
|
|
27
|
+
state: states[states.length - 1].newDocument,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class RunLog extends RunLogPatch {
|
|
32
|
+
constructor(fields) {
|
|
33
|
+
super(fields);
|
|
34
|
+
Object.defineProperty(this, "state", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
this.state = fields.state;
|
|
41
|
+
}
|
|
42
|
+
concat(other) {
|
|
43
|
+
const ops = this.ops.concat(other.ops);
|
|
44
|
+
const states = applyPatch(this.state, other.ops);
|
|
45
|
+
return new RunLog({ ops, state: states[states.length - 1].newDocument });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Class that extends the `BaseTracer` class from the
|
|
50
|
+
* `langchain.callbacks.tracers.base` module. It represents a callback
|
|
51
|
+
* handler that logs the execution of runs and emits `RunLog` instances to a
|
|
52
|
+
* `RunLogStream`.
|
|
53
|
+
*/
|
|
54
|
+
export class LogStreamCallbackHandler extends BaseTracer {
|
|
55
|
+
constructor(fields) {
|
|
56
|
+
super(fields);
|
|
57
|
+
Object.defineProperty(this, "autoClose", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: true
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(this, "includeNames", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: void 0
|
|
68
|
+
});
|
|
69
|
+
Object.defineProperty(this, "includeTypes", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: void 0
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(this, "includeTags", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: void 0
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(this, "excludeNames", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
writable: true,
|
|
85
|
+
value: void 0
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(this, "excludeTypes", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: true,
|
|
91
|
+
value: void 0
|
|
92
|
+
});
|
|
93
|
+
Object.defineProperty(this, "excludeTags", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: void 0
|
|
98
|
+
});
|
|
99
|
+
Object.defineProperty(this, "indexMap", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: {}
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(this, "transformStream", {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
configurable: true,
|
|
108
|
+
writable: true,
|
|
109
|
+
value: void 0
|
|
110
|
+
});
|
|
111
|
+
Object.defineProperty(this, "writer", {
|
|
112
|
+
enumerable: true,
|
|
113
|
+
configurable: true,
|
|
114
|
+
writable: true,
|
|
115
|
+
value: void 0
|
|
116
|
+
});
|
|
117
|
+
Object.defineProperty(this, "receiveStream", {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
configurable: true,
|
|
120
|
+
writable: true,
|
|
121
|
+
value: void 0
|
|
122
|
+
});
|
|
123
|
+
Object.defineProperty(this, "name", {
|
|
124
|
+
enumerable: true,
|
|
125
|
+
configurable: true,
|
|
126
|
+
writable: true,
|
|
127
|
+
value: "log_stream_tracer"
|
|
128
|
+
});
|
|
129
|
+
this.autoClose = fields?.autoClose ?? true;
|
|
130
|
+
this.includeNames = fields?.includeNames;
|
|
131
|
+
this.includeTypes = fields?.includeTypes;
|
|
132
|
+
this.includeTags = fields?.includeTags;
|
|
133
|
+
this.excludeNames = fields?.excludeNames;
|
|
134
|
+
this.excludeTypes = fields?.excludeTypes;
|
|
135
|
+
this.excludeTags = fields?.excludeTags;
|
|
136
|
+
this.transformStream = new TransformStream();
|
|
137
|
+
this.writer = this.transformStream.writable.getWriter();
|
|
138
|
+
this.receiveStream = IterableReadableStream.fromReadableStream(this.transformStream.readable);
|
|
139
|
+
}
|
|
140
|
+
[Symbol.asyncIterator]() {
|
|
141
|
+
return this.receiveStream;
|
|
142
|
+
}
|
|
143
|
+
async persistRun(_run) {
|
|
144
|
+
// This is a legacy method only called once for an entire run tree
|
|
145
|
+
// and is therefore not useful here
|
|
146
|
+
}
|
|
147
|
+
_includeRun(run) {
|
|
148
|
+
if (run.parent_run_id === undefined) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const runTags = run.tags ?? [];
|
|
152
|
+
let include = this.includeNames === undefined &&
|
|
153
|
+
this.includeTags === undefined &&
|
|
154
|
+
this.includeTypes === undefined;
|
|
155
|
+
if (this.includeNames !== undefined) {
|
|
156
|
+
include = include || this.includeNames.includes(run.name);
|
|
157
|
+
}
|
|
158
|
+
if (this.includeTypes !== undefined) {
|
|
159
|
+
include = include || this.includeTypes.includes(run.run_type);
|
|
160
|
+
}
|
|
161
|
+
if (this.includeTags !== undefined) {
|
|
162
|
+
include =
|
|
163
|
+
include ||
|
|
164
|
+
runTags.find((tag) => this.includeTags?.includes(tag)) !== undefined;
|
|
165
|
+
}
|
|
166
|
+
if (this.excludeNames !== undefined) {
|
|
167
|
+
include = include && !this.excludeNames.includes(run.name);
|
|
168
|
+
}
|
|
169
|
+
if (this.excludeTypes !== undefined) {
|
|
170
|
+
include = include && !this.excludeTypes.includes(run.run_type);
|
|
171
|
+
}
|
|
172
|
+
if (this.excludeTags !== undefined) {
|
|
173
|
+
include =
|
|
174
|
+
include && runTags.every((tag) => !this.excludeTags?.includes(tag));
|
|
175
|
+
}
|
|
176
|
+
return include;
|
|
177
|
+
}
|
|
178
|
+
async onRunCreate(run) {
|
|
179
|
+
if (run.parent_run_id === undefined) {
|
|
180
|
+
await this.writer.write(new RunLogPatch({
|
|
181
|
+
ops: [
|
|
182
|
+
{
|
|
183
|
+
op: "replace",
|
|
184
|
+
path: "",
|
|
185
|
+
value: {
|
|
186
|
+
id: run.id,
|
|
187
|
+
streamed_output: [],
|
|
188
|
+
final_output: undefined,
|
|
189
|
+
logs: [],
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
],
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
if (!this._includeRun(run)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
this.indexMap[run.id] = Math.max(...Object.values(this.indexMap), -1) + 1;
|
|
199
|
+
const logEntry = {
|
|
200
|
+
id: run.id,
|
|
201
|
+
name: run.name,
|
|
202
|
+
type: run.run_type,
|
|
203
|
+
tags: run.tags ?? [],
|
|
204
|
+
metadata: run.extra?.metadata ?? {},
|
|
205
|
+
start_time: new Date(run.start_time).toISOString(),
|
|
206
|
+
streamed_output_str: [],
|
|
207
|
+
final_output: undefined,
|
|
208
|
+
end_time: undefined,
|
|
209
|
+
};
|
|
210
|
+
await this.writer.write(new RunLogPatch({
|
|
211
|
+
ops: [
|
|
212
|
+
{
|
|
213
|
+
op: "add",
|
|
214
|
+
path: `/logs/${this.indexMap[run.id]}`,
|
|
215
|
+
value: logEntry,
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
async onRunUpdate(run) {
|
|
221
|
+
try {
|
|
222
|
+
const index = this.indexMap[run.id];
|
|
223
|
+
if (index === undefined) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const ops = [
|
|
227
|
+
{
|
|
228
|
+
op: "add",
|
|
229
|
+
path: `/logs/${index}/final_output`,
|
|
230
|
+
value: run.outputs,
|
|
231
|
+
},
|
|
232
|
+
];
|
|
233
|
+
if (run.end_time !== undefined) {
|
|
234
|
+
ops.push({
|
|
235
|
+
op: "add",
|
|
236
|
+
path: `/logs/${index}/end_time`,
|
|
237
|
+
value: new Date(run.end_time).toISOString(),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
const patch = new RunLogPatch({ ops });
|
|
241
|
+
await this.writer.write(patch);
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
if (run.parent_run_id === undefined) {
|
|
245
|
+
const patch = new RunLogPatch({
|
|
246
|
+
ops: [
|
|
247
|
+
{
|
|
248
|
+
op: "replace",
|
|
249
|
+
path: "/final_output",
|
|
250
|
+
value: run.outputs,
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
});
|
|
254
|
+
await this.writer.write(patch);
|
|
255
|
+
if (this.autoClose) {
|
|
256
|
+
await this.writer.close();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async onLLMNewToken(run, token) {
|
|
262
|
+
const index = this.indexMap[run.id];
|
|
263
|
+
if (index === undefined) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const patch = new RunLogPatch({
|
|
267
|
+
ops: [
|
|
268
|
+
{
|
|
269
|
+
op: "add",
|
|
270
|
+
path: `/logs/${index}/streamed_output_str/-`,
|
|
271
|
+
value: token,
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
});
|
|
275
|
+
await this.writer.write(patch);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -24,7 +24,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
24
24
|
_addChildRun(parentRun, childRun) {
|
|
25
25
|
parentRun.child_runs.push(childRun);
|
|
26
26
|
}
|
|
27
|
-
_startTrace(run) {
|
|
27
|
+
async _startTrace(run) {
|
|
28
28
|
if (run.parent_run_id !== undefined) {
|
|
29
29
|
const parentRun = this.runMap.get(run.parent_run_id);
|
|
30
30
|
if (parentRun) {
|
|
@@ -33,6 +33,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
this.runMap.set(run.id, run);
|
|
36
|
+
await this.onRunCreate?.(run);
|
|
36
37
|
}
|
|
37
38
|
async _endTrace(run) {
|
|
38
39
|
const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
|
|
@@ -43,6 +44,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
43
44
|
await this.persistRun(run);
|
|
44
45
|
}
|
|
45
46
|
this.runMap.delete(run.id);
|
|
47
|
+
await this.onRunUpdate?.(run);
|
|
46
48
|
}
|
|
47
49
|
_getExecutionOrder(parentRunId) {
|
|
48
50
|
const parentRun = parentRunId !== undefined && this.runMap.get(parentRunId);
|
|
@@ -52,7 +54,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
52
54
|
}
|
|
53
55
|
return parentRun.child_execution_order + 1;
|
|
54
56
|
}
|
|
55
|
-
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
57
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
56
58
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
57
59
|
const start_time = Date.now();
|
|
58
60
|
const finalExtraParams = metadata
|
|
@@ -60,7 +62,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
60
62
|
: extraParams;
|
|
61
63
|
const run = {
|
|
62
64
|
id: runId,
|
|
63
|
-
name: llm.id[llm.id.length - 1],
|
|
65
|
+
name: name ?? llm.id[llm.id.length - 1],
|
|
64
66
|
parent_run_id: parentRunId,
|
|
65
67
|
start_time,
|
|
66
68
|
serialized: llm,
|
|
@@ -78,10 +80,11 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
78
80
|
extra: finalExtraParams ?? {},
|
|
79
81
|
tags: tags || [],
|
|
80
82
|
};
|
|
81
|
-
this._startTrace(run);
|
|
83
|
+
await this._startTrace(run);
|
|
82
84
|
await this.onLLMStart?.(run);
|
|
85
|
+
return run;
|
|
83
86
|
}
|
|
84
|
-
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata) {
|
|
87
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name) {
|
|
85
88
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
86
89
|
const start_time = Date.now();
|
|
87
90
|
const finalExtraParams = metadata
|
|
@@ -89,7 +92,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
89
92
|
: extraParams;
|
|
90
93
|
const run = {
|
|
91
94
|
id: runId,
|
|
92
|
-
name: llm.id[llm.id.length - 1],
|
|
95
|
+
name: name ?? llm.id[llm.id.length - 1],
|
|
93
96
|
parent_run_id: parentRunId,
|
|
94
97
|
start_time,
|
|
95
98
|
serialized: llm,
|
|
@@ -107,8 +110,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
107
110
|
extra: finalExtraParams ?? {},
|
|
108
111
|
tags: tags || [],
|
|
109
112
|
};
|
|
110
|
-
this._startTrace(run);
|
|
113
|
+
await this._startTrace(run);
|
|
111
114
|
await this.onLLMStart?.(run);
|
|
115
|
+
return run;
|
|
112
116
|
}
|
|
113
117
|
async handleLLMEnd(output, runId) {
|
|
114
118
|
const run = this.runMap.get(runId);
|
|
@@ -123,6 +127,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
123
127
|
});
|
|
124
128
|
await this.onLLMEnd?.(run);
|
|
125
129
|
await this._endTrace(run);
|
|
130
|
+
return run;
|
|
126
131
|
}
|
|
127
132
|
async handleLLMError(error, runId) {
|
|
128
133
|
const run = this.runMap.get(runId);
|
|
@@ -137,13 +142,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
137
142
|
});
|
|
138
143
|
await this.onLLMError?.(run);
|
|
139
144
|
await this._endTrace(run);
|
|
145
|
+
return run;
|
|
140
146
|
}
|
|
141
|
-
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType) {
|
|
147
|
+
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name) {
|
|
142
148
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
143
149
|
const start_time = Date.now();
|
|
144
150
|
const run = {
|
|
145
151
|
id: runId,
|
|
146
|
-
name: chain.id[chain.id.length - 1],
|
|
152
|
+
name: name ?? chain.id[chain.id.length - 1],
|
|
147
153
|
parent_run_id: parentRunId,
|
|
148
154
|
start_time,
|
|
149
155
|
serialized: chain,
|
|
@@ -161,8 +167,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
161
167
|
extra: metadata ? { metadata } : {},
|
|
162
168
|
tags: tags || [],
|
|
163
169
|
};
|
|
164
|
-
this._startTrace(run);
|
|
170
|
+
await this._startTrace(run);
|
|
165
171
|
await this.onChainStart?.(run);
|
|
172
|
+
return run;
|
|
166
173
|
}
|
|
167
174
|
async handleChainEnd(outputs, runId, _parentRunId, _tags, kwargs) {
|
|
168
175
|
const run = this.runMap.get(runId);
|
|
@@ -180,6 +187,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
180
187
|
}
|
|
181
188
|
await this.onChainEnd?.(run);
|
|
182
189
|
await this._endTrace(run);
|
|
190
|
+
return run;
|
|
183
191
|
}
|
|
184
192
|
async handleChainError(error, runId, _parentRunId, _tags, kwargs) {
|
|
185
193
|
const run = this.runMap.get(runId);
|
|
@@ -197,13 +205,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
197
205
|
}
|
|
198
206
|
await this.onChainError?.(run);
|
|
199
207
|
await this._endTrace(run);
|
|
208
|
+
return run;
|
|
200
209
|
}
|
|
201
|
-
async handleToolStart(tool, input, runId, parentRunId, tags, metadata) {
|
|
210
|
+
async handleToolStart(tool, input, runId, parentRunId, tags, metadata, name) {
|
|
202
211
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
203
212
|
const start_time = Date.now();
|
|
204
213
|
const run = {
|
|
205
214
|
id: runId,
|
|
206
|
-
name: tool.id[tool.id.length - 1],
|
|
215
|
+
name: name ?? tool.id[tool.id.length - 1],
|
|
207
216
|
parent_run_id: parentRunId,
|
|
208
217
|
start_time,
|
|
209
218
|
serialized: tool,
|
|
@@ -221,8 +230,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
221
230
|
extra: metadata ? { metadata } : {},
|
|
222
231
|
tags: tags || [],
|
|
223
232
|
};
|
|
224
|
-
this._startTrace(run);
|
|
233
|
+
await this._startTrace(run);
|
|
225
234
|
await this.onToolStart?.(run);
|
|
235
|
+
return run;
|
|
226
236
|
}
|
|
227
237
|
async handleToolEnd(output, runId) {
|
|
228
238
|
const run = this.runMap.get(runId);
|
|
@@ -237,6 +247,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
237
247
|
});
|
|
238
248
|
await this.onToolEnd?.(run);
|
|
239
249
|
await this._endTrace(run);
|
|
250
|
+
return run;
|
|
240
251
|
}
|
|
241
252
|
async handleToolError(error, runId) {
|
|
242
253
|
const run = this.runMap.get(runId);
|
|
@@ -251,6 +262,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
251
262
|
});
|
|
252
263
|
await this.onToolError?.(run);
|
|
253
264
|
await this._endTrace(run);
|
|
265
|
+
return run;
|
|
254
266
|
}
|
|
255
267
|
async handleAgentAction(action, runId) {
|
|
256
268
|
const run = this.runMap.get(runId);
|
|
@@ -279,12 +291,12 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
279
291
|
});
|
|
280
292
|
await this.onAgentEnd?.(run);
|
|
281
293
|
}
|
|
282
|
-
async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata) {
|
|
294
|
+
async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
|
|
283
295
|
const execution_order = this._getExecutionOrder(parentRunId);
|
|
284
296
|
const start_time = Date.now();
|
|
285
297
|
const run = {
|
|
286
298
|
id: runId,
|
|
287
|
-
name: retriever.id[retriever.id.length - 1],
|
|
299
|
+
name: name ?? retriever.id[retriever.id.length - 1],
|
|
288
300
|
parent_run_id: parentRunId,
|
|
289
301
|
start_time,
|
|
290
302
|
serialized: retriever,
|
|
@@ -302,8 +314,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
302
314
|
extra: metadata ? { metadata } : {},
|
|
303
315
|
tags: tags || [],
|
|
304
316
|
};
|
|
305
|
-
this._startTrace(run);
|
|
317
|
+
await this._startTrace(run);
|
|
306
318
|
await this.onRetrieverStart?.(run);
|
|
319
|
+
return run;
|
|
307
320
|
}
|
|
308
321
|
async handleRetrieverEnd(documents, runId) {
|
|
309
322
|
const run = this.runMap.get(runId);
|
|
@@ -318,6 +331,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
318
331
|
});
|
|
319
332
|
await this.onRetrieverEnd?.(run);
|
|
320
333
|
await this._endTrace(run);
|
|
334
|
+
return run;
|
|
321
335
|
}
|
|
322
336
|
async handleRetrieverError(error, runId) {
|
|
323
337
|
const run = this.runMap.get(runId);
|
|
@@ -332,6 +346,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
332
346
|
});
|
|
333
347
|
await this.onRetrieverError?.(run);
|
|
334
348
|
await this._endTrace(run);
|
|
349
|
+
return run;
|
|
335
350
|
}
|
|
336
351
|
async handleText(text, runId) {
|
|
337
352
|
const run = this.runMap.get(runId);
|
|
@@ -348,14 +363,15 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
348
363
|
async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
|
|
349
364
|
const run = this.runMap.get(runId);
|
|
350
365
|
if (!run || run?.run_type !== "llm") {
|
|
351
|
-
|
|
366
|
+
throw new Error(`Invalid "runId" provided to "handleLLMNewToken" callback.`);
|
|
352
367
|
}
|
|
353
368
|
run.events.push({
|
|
354
369
|
name: "new_token",
|
|
355
370
|
time: new Date().toISOString(),
|
|
356
371
|
kwargs: { token, idx, chunk: fields?.chunk },
|
|
357
372
|
});
|
|
358
|
-
await this.onLLMNewToken?.(run);
|
|
373
|
+
await this.onLLMNewToken?.(run, token);
|
|
374
|
+
return run;
|
|
359
375
|
}
|
|
360
376
|
}
|
|
361
377
|
exports.BaseTracer = BaseTracer;
|
|
@@ -25,30 +25,32 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
25
25
|
copy(): this;
|
|
26
26
|
protected abstract persistRun(run: Run): Promise<void>;
|
|
27
27
|
protected _addChildRun(parentRun: Run, childRun: Run): void;
|
|
28
|
-
protected _startTrace(run: Run): void
|
|
28
|
+
protected _startTrace(run: Run): Promise<void>;
|
|
29
29
|
protected _endTrace(run: Run): Promise<void>;
|
|
30
30
|
protected _getExecutionOrder(parentRunId: string | undefined): number;
|
|
31
|
-
handleLLMStart(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap): Promise<
|
|
32
|
-
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap): Promise<
|
|
33
|
-
handleLLMEnd(output: LLMResult, runId: string): Promise<
|
|
34
|
-
handleLLMError(error: Error, runId: string): Promise<
|
|
35
|
-
handleChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string): Promise<
|
|
31
|
+
handleLLMStart(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
32
|
+
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
33
|
+
handleLLMEnd(output: LLMResult, runId: string): Promise<Run>;
|
|
34
|
+
handleLLMError(error: Error, runId: string): Promise<Run>;
|
|
35
|
+
handleChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string, name?: string): Promise<Run>;
|
|
36
36
|
handleChainEnd(outputs: ChainValues, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
|
|
37
37
|
inputs?: Record<string, unknown>;
|
|
38
|
-
}): Promise<
|
|
38
|
+
}): Promise<Run>;
|
|
39
39
|
handleChainError(error: Error, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
|
|
40
40
|
inputs?: Record<string, unknown>;
|
|
41
|
-
}): Promise<
|
|
42
|
-
handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap): Promise<
|
|
43
|
-
handleToolEnd(output: string, runId: string): Promise<
|
|
44
|
-
handleToolError(error: Error, runId: string): Promise<
|
|
41
|
+
}): Promise<Run>;
|
|
42
|
+
handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
43
|
+
handleToolEnd(output: string, runId: string): Promise<Run>;
|
|
44
|
+
handleToolError(error: Error, runId: string): Promise<Run>;
|
|
45
45
|
handleAgentAction(action: AgentAction, runId: string): Promise<void>;
|
|
46
46
|
handleAgentEnd(action: AgentFinish, runId: string): Promise<void>;
|
|
47
|
-
handleRetrieverStart(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap): Promise<
|
|
48
|
-
handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<
|
|
49
|
-
handleRetrieverError(error: Error, runId: string): Promise<
|
|
47
|
+
handleRetrieverStart(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
48
|
+
handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<Run>;
|
|
49
|
+
handleRetrieverError(error: Error, runId: string): Promise<Run>;
|
|
50
50
|
handleText(text: string, runId: string): Promise<void>;
|
|
51
|
-
handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<
|
|
51
|
+
handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<Run>;
|
|
52
|
+
onRunCreate?(run: Run): void | Promise<void>;
|
|
53
|
+
onRunUpdate?(run: Run): void | Promise<void>;
|
|
52
54
|
onLLMStart?(run: Run): void | Promise<void>;
|
|
53
55
|
onLLMEnd?(run: Run): void | Promise<void>;
|
|
54
56
|
onLLMError?(run: Run): void | Promise<void>;
|
|
@@ -64,5 +66,5 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
64
66
|
onRetrieverEnd?(run: Run): void | Promise<void>;
|
|
65
67
|
onRetrieverError?(run: Run): void | Promise<void>;
|
|
66
68
|
onText?(run: Run): void | Promise<void>;
|
|
67
|
-
onLLMNewToken?(run: Run): void | Promise<void>;
|
|
69
|
+
onLLMNewToken?(run: Run, token: string): void | Promise<void>;
|
|
68
70
|
}
|