-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
console.ts
318 lines (295 loc) Β· 7.89 KB
/
console.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import type { CSPair } from "ansi-styles";
import styles from "ansi-styles";
import { BaseTracer, type AgentRun, type Run } from "./base.js";
function wrap(style: CSPair, text: string) {
return `${style.open}${text}${style.close}`;
}
function tryJsonStringify(obj: unknown, fallback: string) {
try {
return JSON.stringify(obj, null, 2);
} catch (err) {
return fallback;
}
}
function elapsed(run: Run): string {
if (!run.end_time) return "";
const elapsed = run.end_time - run.start_time;
if (elapsed < 1000) {
return `${elapsed}ms`;
}
return `${(elapsed / 1000).toFixed(2)}s`;
}
const { color } = styles;
/**
* A tracer that logs all events to the console. It extends from the
* `BaseTracer` class and overrides its methods to provide custom logging
* functionality.
* @example
* ```typescript
*
* const llm = new ChatAnthropic({
* temperature: 0,
* tags: ["example", "callbacks", "constructor"],
* callbacks: [new ConsoleCallbackHandler()],
* });
*
* ```
*/
export class ConsoleCallbackHandler extends BaseTracer {
name = "console_callback_handler" as const;
/**
* Method used to persist the run. In this case, it simply returns a
* resolved promise as there's no persistence logic.
* @param _run The run to persist.
* @returns A resolved promise.
*/
protected persistRun(_run: Run) {
return Promise.resolve();
}
// utility methods
/**
* Method used to get all the parent runs of a given run.
* @param run The run whose parents are to be retrieved.
* @returns An array of parent runs.
*/
getParents(run: Run) {
const parents: Run[] = [];
let currentRun = run;
while (currentRun.parent_run_id) {
const parent = this.runMap.get(currentRun.parent_run_id);
if (parent) {
parents.push(parent);
currentRun = parent;
} else {
break;
}
}
return parents;
}
/**
* Method used to get a string representation of the run's lineage, which
* is used in logging.
* @param run The run whose lineage is to be retrieved.
* @returns A string representation of the run's lineage.
*/
getBreadcrumbs(run: Run) {
const parents = this.getParents(run).reverse();
const string = [...parents, run]
.map((parent, i, arr) => {
const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
return i === arr.length - 1 ? wrap(styles.bold, name) : name;
})
.join(" > ");
return wrap(color.grey, string);
}
// logging methods
/**
* Method used to log the start of a chain run.
* @param run The chain run that has started.
* @returns void
*/
onChainStart(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(
color.green,
"[chain/start]"
)} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(
run.inputs,
"[inputs]"
)}`
);
}
/**
* Method used to log the end of a chain run.
* @param run The chain run that has ended.
* @returns void
*/
onChainEnd(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(
run
)}] Exiting Chain run with output: ${tryJsonStringify(
run.outputs,
"[outputs]"
)}`
);
}
/**
* Method used to log any errors of a chain run.
* @param run The chain run that has errored.
* @returns void
*/
onChainError(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(
run
)}] Chain run errored with error: ${tryJsonStringify(
run.error,
"[error]"
)}`
);
}
/**
* Method used to log the start of an LLM run.
* @param run The LLM run that has started.
* @returns void
*/
onLLMStart(run: Run) {
const crumbs = this.getBreadcrumbs(run);
const inputs =
"prompts" in run.inputs
? { prompts: (run.inputs.prompts as string[]).map((p) => p.trim()) }
: run.inputs;
console.log(
`${wrap(
color.green,
"[llm/start]"
)} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(
inputs,
"[inputs]"
)}`
);
}
/**
* Method used to log the end of an LLM run.
* @param run The LLM run that has ended.
* @returns void
*/
onLLMEnd(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(
run
)}] Exiting LLM run with output: ${tryJsonStringify(
run.outputs,
"[response]"
)}`
);
}
/**
* Method used to log any errors of an LLM run.
* @param run The LLM run that has errored.
* @returns void
*/
onLLMError(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(
run
)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`
);
}
/**
* Method used to log the start of a tool run.
* @param run The tool run that has started.
* @returns void
*/
onToolStart(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(
color.green,
"[tool/start]"
)} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`
);
}
/**
* Method used to log the end of a tool run.
* @param run The tool run that has ended.
* @returns void
*/
onToolEnd(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(
run
)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`
);
}
/**
* Method used to log any errors of a tool run.
* @param run The tool run that has errored.
* @returns void
*/
onToolError(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(
run
)}] Tool run errored with error: ${tryJsonStringify(
run.error,
"[error]"
)}`
);
}
/**
* Method used to log the start of a retriever run.
* @param run The retriever run that has started.
* @returns void
*/
onRetrieverStart(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(
color.green,
"[retriever/start]"
)} [${crumbs}] Entering Retriever run with input: ${tryJsonStringify(
run.inputs,
"[inputs]"
)}`
);
}
/**
* Method used to log the end of a retriever run.
* @param run The retriever run that has ended.
* @returns void
*/
onRetrieverEnd(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.cyan, "[retriever/end]")} [${crumbs}] [${elapsed(
run
)}] Exiting Retriever run with output: ${tryJsonStringify(
run.outputs,
"[outputs]"
)}`
);
}
/**
* Method used to log any errors of a retriever run.
* @param run The retriever run that has errored.
* @returns void
*/
onRetrieverError(run: Run) {
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(color.red, "[retriever/error]")} [${crumbs}] [${elapsed(
run
)}] Retriever run errored with error: ${tryJsonStringify(
run.error,
"[error]"
)}`
);
}
/**
* Method used to log the action selected by the agent.
* @param run The run in which the agent action occurred.
* @returns void
*/
onAgentAction(run: Run) {
const agentRun = run as AgentRun;
const crumbs = this.getBreadcrumbs(run);
console.log(
`${wrap(
color.blue,
"[agent/action]"
)} [${crumbs}] Agent selected action: ${tryJsonStringify(
agentRun.actions[agentRun.actions.length - 1],
"[action]"
)}`
);
}
}