langchain 0.0.65 → 0.0.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/chat/outputParser.cjs +4 -3
- package/dist/agents/chat/outputParser.js +4 -3
- package/dist/agents/chat_convo/outputParser.cjs +6 -7
- package/dist/agents/chat_convo/outputParser.js +6 -7
- package/dist/callbacks/handlers/console.cjs +104 -22
- package/dist/callbacks/handlers/console.d.ts +25 -17
- package/dist/callbacks/handlers/console.js +101 -22
- package/dist/callbacks/handlers/tracers.cjs +49 -23
- package/dist/callbacks/handlers/tracers.d.ts +24 -8
- package/dist/callbacks/handlers/tracers.js +49 -23
- package/dist/callbacks/manager.cjs +14 -3
- package/dist/callbacks/manager.js +14 -3
- package/dist/chains/index.d.ts +1 -1
- package/dist/chains/index.js +1 -1
- package/dist/chains/sql_db/sql_db_chain.cjs +1 -1
- package/dist/chains/sql_db/sql_db_chain.js +1 -1
- package/dist/chains/summarization/load.cjs +18 -5
- package/dist/chains/summarization/load.d.ts +11 -6
- package/dist/chains/summarization/load.js +19 -6
- package/dist/chains/summarization/refine_prompts.cjs +20 -0
- package/dist/chains/summarization/refine_prompts.d.ts +2 -0
- package/dist/chains/summarization/refine_prompts.js +17 -0
- package/dist/chat_models/base.cjs +2 -2
- package/dist/chat_models/base.js +2 -2
- package/dist/chat_models/openai.cjs +1 -1
- package/dist/chat_models/openai.js +1 -1
- package/dist/embeddings/hf.cjs +52 -0
- package/dist/embeddings/hf.d.ts +15 -0
- package/dist/embeddings/hf.js +48 -0
- package/dist/llms/openai-chat.cjs +60 -2
- package/dist/llms/openai-chat.d.ts +12 -0
- package/dist/llms/openai-chat.js +58 -1
- package/dist/llms/openai.cjs +2 -1
- package/dist/llms/openai.d.ts +1 -1
- package/dist/llms/openai.js +1 -1
- package/dist/output_parsers/combining.cjs +20 -8
- package/dist/output_parsers/combining.js +20 -8
- package/dist/output_parsers/structured.cjs +11 -61
- package/dist/output_parsers/structured.js +12 -62
- package/dist/prompts/base.cjs +2 -1
- package/dist/prompts/base.d.ts +1 -1
- package/dist/prompts/base.js +3 -2
- package/dist/retrievers/hyde.cjs +97 -0
- package/dist/retrievers/hyde.d.ts +16 -0
- package/dist/retrievers/hyde.js +92 -0
- package/dist/tools/aiplugin.cjs +1 -1
- package/dist/tools/aiplugin.js +1 -1
- package/dist/tools/base.cjs +6 -0
- package/dist/tools/base.d.ts +6 -0
- package/dist/tools/base.js +6 -0
- package/dist/tools/dynamic.cjs +3 -0
- package/dist/tools/dynamic.d.ts +3 -0
- package/dist/tools/dynamic.js +3 -0
- package/dist/tools/requests.cjs +20 -4
- package/dist/tools/requests.d.ts +9 -2
- package/dist/tools/requests.js +20 -4
- package/dist/tools/webbrowser.cjs +14 -9
- package/dist/tools/webbrowser.d.ts +1 -0
- package/dist/tools/webbrowser.js +12 -8
- package/dist/util/axios-fetch-adapter.cjs +24 -1
- package/dist/util/axios-fetch-adapter.js +24 -1
- package/dist/vectorstores/base.d.ts +6 -5
- package/dist/vectorstores/hnswlib.cjs +13 -2
- package/dist/vectorstores/hnswlib.d.ts +2 -1
- package/dist/vectorstores/hnswlib.js +13 -2
- package/embeddings/hf.cjs +1 -0
- package/embeddings/hf.d.ts +1 -0
- package/embeddings/hf.js +1 -0
- package/package.json +19 -2
- package/retrievers/hyde.cjs +1 -0
- package/retrievers/hyde.d.ts +1 -0
- package/retrievers/hyde.js +1 -0
|
@@ -6,13 +6,14 @@ const prompt_js_1 = require("./prompt.cjs");
|
|
|
6
6
|
exports.FINAL_ANSWER_ACTION = "Final Answer:";
|
|
7
7
|
class ChatAgentOutputParser extends types_js_1.AgentActionOutputParser {
|
|
8
8
|
async parse(text) {
|
|
9
|
-
if (text.includes(exports.FINAL_ANSWER_ACTION)) {
|
|
9
|
+
if (text.includes(exports.FINAL_ANSWER_ACTION) || !text.includes(`"action":`)) {
|
|
10
10
|
const parts = text.split(exports.FINAL_ANSWER_ACTION);
|
|
11
11
|
const output = parts[parts.length - 1].trim();
|
|
12
12
|
return { returnValues: { output }, log: text };
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const action = text.includes("```")
|
|
15
|
+
? text.trim().split(/```(?:json)?/)[1]
|
|
16
|
+
: text.trim();
|
|
16
17
|
try {
|
|
17
18
|
const response = JSON.parse(action.trim());
|
|
18
19
|
return {
|
|
@@ -3,13 +3,14 @@ import { FORMAT_INSTRUCTIONS } from "./prompt.js";
|
|
|
3
3
|
export const FINAL_ANSWER_ACTION = "Final Answer:";
|
|
4
4
|
export class ChatAgentOutputParser extends AgentActionOutputParser {
|
|
5
5
|
async parse(text) {
|
|
6
|
-
if (text.includes(FINAL_ANSWER_ACTION)) {
|
|
6
|
+
if (text.includes(FINAL_ANSWER_ACTION) || !text.includes(`"action":`)) {
|
|
7
7
|
const parts = text.split(FINAL_ANSWER_ACTION);
|
|
8
8
|
const output = parts[parts.length - 1].trim();
|
|
9
9
|
return { returnValues: { output }, log: text };
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const action = text.includes("```")
|
|
12
|
+
? text.trim().split(/```(?:json)?/)[1]
|
|
13
|
+
: text.trim();
|
|
13
14
|
try {
|
|
14
15
|
const response = JSON.parse(action.trim());
|
|
15
16
|
return {
|
|
@@ -9,14 +9,13 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
|
|
|
9
9
|
if (jsonOutput.includes("```json")) {
|
|
10
10
|
jsonOutput = jsonOutput.split("```json")[1].trimStart();
|
|
11
11
|
}
|
|
12
|
-
if (jsonOutput.includes("```")) {
|
|
13
|
-
|
|
12
|
+
else if (jsonOutput.includes("```")) {
|
|
13
|
+
const firstIndex = jsonOutput.indexOf("```");
|
|
14
|
+
jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (jsonOutput.endsWith("```")) {
|
|
19
|
-
jsonOutput = jsonOutput.slice(0, -3).trimEnd();
|
|
16
|
+
const lastIndex = jsonOutput.lastIndexOf("```");
|
|
17
|
+
if (lastIndex !== -1) {
|
|
18
|
+
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
20
19
|
}
|
|
21
20
|
const response = JSON.parse(jsonOutput);
|
|
22
21
|
const { action, action_input } = response;
|
|
@@ -6,14 +6,13 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
|
|
|
6
6
|
if (jsonOutput.includes("```json")) {
|
|
7
7
|
jsonOutput = jsonOutput.split("```json")[1].trimStart();
|
|
8
8
|
}
|
|
9
|
-
if (jsonOutput.includes("```")) {
|
|
10
|
-
|
|
9
|
+
else if (jsonOutput.includes("```")) {
|
|
10
|
+
const firstIndex = jsonOutput.indexOf("```");
|
|
11
|
+
jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (jsonOutput.endsWith("```")) {
|
|
16
|
-
jsonOutput = jsonOutput.slice(0, -3).trimEnd();
|
|
13
|
+
const lastIndex = jsonOutput.lastIndexOf("```");
|
|
14
|
+
if (lastIndex !== -1) {
|
|
15
|
+
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
17
16
|
}
|
|
18
17
|
const response = JSON.parse(jsonOutput);
|
|
19
18
|
const { action, action_input } = response;
|
|
@@ -1,44 +1,126 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.ConsoleCallbackHandler = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
7
|
+
const ansi_styles_1 = __importDefault(require("ansi-styles"));
|
|
8
|
+
const tracers_js_1 = require("./tracers.cjs");
|
|
9
|
+
function wrap(style, text) {
|
|
10
|
+
return `${style.open}${text}${style.close}`;
|
|
11
|
+
}
|
|
12
|
+
function tryJsonStringify(obj, fallback) {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.stringify(obj, null, 2);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function elapsed(run) {
|
|
21
|
+
const elapsed = run.end_time - run.start_time;
|
|
22
|
+
if (elapsed < 1000) {
|
|
23
|
+
return `${elapsed}ms`;
|
|
24
|
+
}
|
|
25
|
+
return `${(elapsed / 1000).toFixed(2)}s`;
|
|
26
|
+
}
|
|
27
|
+
const { color } = ansi_styles_1.default;
|
|
28
|
+
class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
29
|
+
// boilerplate to work with the base tracer class
|
|
6
30
|
constructor() {
|
|
7
|
-
super(
|
|
31
|
+
super();
|
|
8
32
|
Object.defineProperty(this, "name", {
|
|
9
33
|
enumerable: true,
|
|
10
34
|
configurable: true,
|
|
11
35
|
writable: true,
|
|
12
36
|
value: "console_callback_handler"
|
|
13
37
|
});
|
|
38
|
+
Object.defineProperty(this, "i", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: 0
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
persistSession(session) {
|
|
46
|
+
// eslint-disable-next-line no-plusplus
|
|
47
|
+
return Promise.resolve({ ...session, id: this.i++ });
|
|
48
|
+
}
|
|
49
|
+
persistRun(_run) {
|
|
50
|
+
return Promise.resolve();
|
|
51
|
+
}
|
|
52
|
+
loadDefaultSession() {
|
|
53
|
+
return this.newSession();
|
|
54
|
+
}
|
|
55
|
+
loadSession(sessionName) {
|
|
56
|
+
return this.newSession(sessionName);
|
|
57
|
+
}
|
|
58
|
+
// utility methods
|
|
59
|
+
getParents(run) {
|
|
60
|
+
const parents = [];
|
|
61
|
+
let currentRun = run;
|
|
62
|
+
while (currentRun.parent_uuid) {
|
|
63
|
+
const parent = this.runMap.get(currentRun.parent_uuid);
|
|
64
|
+
if (parent) {
|
|
65
|
+
parents.push(parent);
|
|
66
|
+
currentRun = parent;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return parents;
|
|
73
|
+
}
|
|
74
|
+
getBreadcrumbs(run) {
|
|
75
|
+
const parents = this.getParents(run).reverse();
|
|
76
|
+
const string = [...parents, run]
|
|
77
|
+
.map((parent, i, arr) => {
|
|
78
|
+
const name = `${parent.execution_order}:${parent.type}:${parent.serialized?.name}`;
|
|
79
|
+
return i === arr.length - 1 ? wrap(ansi_styles_1.default.bold, name) : name;
|
|
80
|
+
})
|
|
81
|
+
.join(" > ");
|
|
82
|
+
return wrap(color.grey, string);
|
|
83
|
+
}
|
|
84
|
+
// logging methods
|
|
85
|
+
onChainStart(run) {
|
|
86
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
87
|
+
console.log(`${wrap(color.green, "[chain/start]")} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
|
|
14
88
|
}
|
|
15
|
-
|
|
16
|
-
|
|
89
|
+
onChainEnd(run) {
|
|
90
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
91
|
+
console.log(`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(run)}] Exiting Chain run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
|
|
17
92
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
console.log(
|
|
93
|
+
onChainError(run) {
|
|
94
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
95
|
+
console.log(`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(run)}] Chain run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
21
96
|
}
|
|
22
|
-
|
|
23
|
-
|
|
97
|
+
onLLMStart(run) {
|
|
98
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
99
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify({ prompts: run.prompts.map((p) => p.trim()) }, "[inputs]")}`);
|
|
24
100
|
}
|
|
25
|
-
|
|
26
|
-
|
|
101
|
+
onLLMEnd(run) {
|
|
102
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
103
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.response, "[response]")}`);
|
|
27
104
|
}
|
|
28
|
-
|
|
29
|
-
|
|
105
|
+
onLLMError(run) {
|
|
106
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
107
|
+
console.log(`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(run)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
30
108
|
}
|
|
31
|
-
|
|
32
|
-
|
|
109
|
+
onToolStart(run) {
|
|
110
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
111
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.tool_input?.trim()}"`);
|
|
33
112
|
}
|
|
34
|
-
|
|
35
|
-
|
|
113
|
+
onToolEnd(run) {
|
|
114
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
115
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
36
116
|
}
|
|
37
|
-
|
|
38
|
-
|
|
117
|
+
onToolError(run) {
|
|
118
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
119
|
+
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
39
120
|
}
|
|
40
|
-
|
|
41
|
-
|
|
121
|
+
onAgentAction(run) {
|
|
122
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
123
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(run.actions[run.actions.length - 1], "[action]")}`);
|
|
42
124
|
}
|
|
43
125
|
}
|
|
44
126
|
exports.ConsoleCallbackHandler = ConsoleCallbackHandler;
|
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import { AgentRun, BaseTracer, BaseTracerSession, ChainRun, LLMRun, Run, ToolRun } from "./tracers.js";
|
|
2
|
+
export declare class ConsoleCallbackHandler extends BaseTracer {
|
|
3
|
+
name: "console_callback_handler";
|
|
4
|
+
constructor();
|
|
5
|
+
i: number;
|
|
6
|
+
protected persistSession(session: BaseTracerSession): Promise<{
|
|
7
|
+
id: number;
|
|
8
|
+
start_time: number;
|
|
9
|
+
name?: string | undefined;
|
|
10
|
+
}>;
|
|
11
|
+
protected persistRun(_run: Run): Promise<void>;
|
|
12
|
+
loadDefaultSession(): Promise<import("./tracers.js").TracerSession>;
|
|
13
|
+
loadSession(sessionName: string): Promise<import("./tracers.js").TracerSession>;
|
|
14
|
+
getParents(run: Run): Run[];
|
|
15
|
+
getBreadcrumbs(run: Run): string;
|
|
16
|
+
onChainStart(run: ChainRun): void;
|
|
17
|
+
onChainEnd(run: ChainRun): void;
|
|
18
|
+
onChainError(run: ChainRun): void;
|
|
19
|
+
onLLMStart(run: LLMRun): void;
|
|
20
|
+
onLLMEnd(run: LLMRun): void;
|
|
21
|
+
onLLMError(run: LLMRun): void;
|
|
22
|
+
onToolStart(run: ToolRun): void;
|
|
23
|
+
onToolEnd(run: ToolRun): void;
|
|
24
|
+
onToolError(run: ToolRun): void;
|
|
25
|
+
onAgentAction(run: AgentRun): void;
|
|
18
26
|
}
|
|
@@ -1,40 +1,119 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import styles from "ansi-styles";
|
|
2
|
+
import { BaseTracer, } from "./tracers.js";
|
|
3
|
+
function wrap(style, text) {
|
|
4
|
+
return `${style.open}${text}${style.close}`;
|
|
5
|
+
}
|
|
6
|
+
function tryJsonStringify(obj, fallback) {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.stringify(obj, null, 2);
|
|
9
|
+
}
|
|
10
|
+
catch (err) {
|
|
11
|
+
return fallback;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function elapsed(run) {
|
|
15
|
+
const elapsed = run.end_time - run.start_time;
|
|
16
|
+
if (elapsed < 1000) {
|
|
17
|
+
return `${elapsed}ms`;
|
|
18
|
+
}
|
|
19
|
+
return `${(elapsed / 1000).toFixed(2)}s`;
|
|
20
|
+
}
|
|
21
|
+
const { color } = styles;
|
|
22
|
+
export class ConsoleCallbackHandler extends BaseTracer {
|
|
23
|
+
// boilerplate to work with the base tracer class
|
|
3
24
|
constructor() {
|
|
4
|
-
super(
|
|
25
|
+
super();
|
|
5
26
|
Object.defineProperty(this, "name", {
|
|
6
27
|
enumerable: true,
|
|
7
28
|
configurable: true,
|
|
8
29
|
writable: true,
|
|
9
30
|
value: "console_callback_handler"
|
|
10
31
|
});
|
|
32
|
+
Object.defineProperty(this, "i", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: 0
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
persistSession(session) {
|
|
40
|
+
// eslint-disable-next-line no-plusplus
|
|
41
|
+
return Promise.resolve({ ...session, id: this.i++ });
|
|
42
|
+
}
|
|
43
|
+
persistRun(_run) {
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
}
|
|
46
|
+
loadDefaultSession() {
|
|
47
|
+
return this.newSession();
|
|
48
|
+
}
|
|
49
|
+
loadSession(sessionName) {
|
|
50
|
+
return this.newSession(sessionName);
|
|
51
|
+
}
|
|
52
|
+
// utility methods
|
|
53
|
+
getParents(run) {
|
|
54
|
+
const parents = [];
|
|
55
|
+
let currentRun = run;
|
|
56
|
+
while (currentRun.parent_uuid) {
|
|
57
|
+
const parent = this.runMap.get(currentRun.parent_uuid);
|
|
58
|
+
if (parent) {
|
|
59
|
+
parents.push(parent);
|
|
60
|
+
currentRun = parent;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return parents;
|
|
67
|
+
}
|
|
68
|
+
getBreadcrumbs(run) {
|
|
69
|
+
const parents = this.getParents(run).reverse();
|
|
70
|
+
const string = [...parents, run]
|
|
71
|
+
.map((parent, i, arr) => {
|
|
72
|
+
const name = `${parent.execution_order}:${parent.type}:${parent.serialized?.name}`;
|
|
73
|
+
return i === arr.length - 1 ? wrap(styles.bold, name) : name;
|
|
74
|
+
})
|
|
75
|
+
.join(" > ");
|
|
76
|
+
return wrap(color.grey, string);
|
|
77
|
+
}
|
|
78
|
+
// logging methods
|
|
79
|
+
onChainStart(run) {
|
|
80
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
81
|
+
console.log(`${wrap(color.green, "[chain/start]")} [${crumbs}] Entering Chain run with input: ${tryJsonStringify(run.inputs, "[inputs]")}`);
|
|
11
82
|
}
|
|
12
|
-
|
|
13
|
-
|
|
83
|
+
onChainEnd(run) {
|
|
84
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
85
|
+
console.log(`${wrap(color.cyan, "[chain/end]")} [${crumbs}] [${elapsed(run)}] Exiting Chain run with output: ${tryJsonStringify(run.outputs, "[outputs]")}`);
|
|
14
86
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
console.log(
|
|
87
|
+
onChainError(run) {
|
|
88
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
89
|
+
console.log(`${wrap(color.red, "[chain/error]")} [${crumbs}] [${elapsed(run)}] Chain run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
18
90
|
}
|
|
19
|
-
|
|
20
|
-
|
|
91
|
+
onLLMStart(run) {
|
|
92
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
93
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify({ prompts: run.prompts.map((p) => p.trim()) }, "[inputs]")}`);
|
|
21
94
|
}
|
|
22
|
-
|
|
23
|
-
|
|
95
|
+
onLLMEnd(run) {
|
|
96
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
97
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.response, "[response]")}`);
|
|
24
98
|
}
|
|
25
|
-
|
|
26
|
-
|
|
99
|
+
onLLMError(run) {
|
|
100
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
101
|
+
console.log(`${wrap(color.red, "[llm/error]")} [${crumbs}] [${elapsed(run)}] LLM run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
27
102
|
}
|
|
28
|
-
|
|
29
|
-
|
|
103
|
+
onToolStart(run) {
|
|
104
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
105
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.tool_input?.trim()}"`);
|
|
30
106
|
}
|
|
31
|
-
|
|
32
|
-
|
|
107
|
+
onToolEnd(run) {
|
|
108
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
109
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
33
110
|
}
|
|
34
|
-
|
|
35
|
-
|
|
111
|
+
onToolError(run) {
|
|
112
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
113
|
+
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
36
114
|
}
|
|
37
|
-
|
|
38
|
-
|
|
115
|
+
onAgentAction(run) {
|
|
116
|
+
const crumbs = this.getBreadcrumbs(run);
|
|
117
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(run.actions[run.actions.length - 1], "[action]")}`);
|
|
39
118
|
}
|
|
40
119
|
}
|
|
@@ -17,12 +17,9 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
17
17
|
writable: true,
|
|
18
18
|
value: new Map()
|
|
19
19
|
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
writable: true,
|
|
24
|
-
value: 1
|
|
25
|
-
});
|
|
20
|
+
}
|
|
21
|
+
copy() {
|
|
22
|
+
return this;
|
|
26
23
|
}
|
|
27
24
|
async newSession(sessionName) {
|
|
28
25
|
const sessionCreate = {
|
|
@@ -48,7 +45,6 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
48
45
|
}
|
|
49
46
|
}
|
|
50
47
|
_startTrace(run) {
|
|
51
|
-
this.executionOrder += 1;
|
|
52
48
|
if (run.parent_uuid) {
|
|
53
49
|
const parentRun = this.runMap.get(run.parent_uuid);
|
|
54
50
|
if (parentRun) {
|
|
@@ -68,14 +64,32 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
68
64
|
async _endTrace(run) {
|
|
69
65
|
if (!run.parent_uuid) {
|
|
70
66
|
await this.persistRun(run);
|
|
71
|
-
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const parentRun = this.runMap.get(run.parent_uuid);
|
|
70
|
+
if (parentRun === undefined) {
|
|
71
|
+
throw new Error(`Parent run ${run.parent_uuid} not found`);
|
|
72
|
+
}
|
|
73
|
+
parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
|
|
72
74
|
}
|
|
73
75
|
this.runMap.delete(run.uuid);
|
|
74
76
|
}
|
|
77
|
+
_getExecutionOrder(parentRunId) {
|
|
78
|
+
// If a run has no parent then execution order is 1
|
|
79
|
+
if (parentRunId === undefined) {
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
const parentRun = this.runMap.get(parentRunId);
|
|
83
|
+
if (parentRun === undefined) {
|
|
84
|
+
throw new Error(`Parent run ${parentRunId} not found`);
|
|
85
|
+
}
|
|
86
|
+
return parentRun.child_execution_order + 1;
|
|
87
|
+
}
|
|
75
88
|
async handleLLMStart(llm, prompts, runId, parentRunId) {
|
|
76
89
|
if (this.session === undefined) {
|
|
77
90
|
this.session = await this.loadDefaultSession();
|
|
78
91
|
}
|
|
92
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
79
93
|
const run = {
|
|
80
94
|
uuid: runId,
|
|
81
95
|
parent_uuid: parentRunId,
|
|
@@ -84,10 +98,12 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
84
98
|
serialized: llm,
|
|
85
99
|
prompts,
|
|
86
100
|
session_id: this.session.id,
|
|
87
|
-
execution_order
|
|
101
|
+
execution_order,
|
|
102
|
+
child_execution_order: execution_order,
|
|
88
103
|
type: "llm",
|
|
89
104
|
};
|
|
90
105
|
this._startTrace(run);
|
|
106
|
+
await this.onLLMStart?.(run);
|
|
91
107
|
}
|
|
92
108
|
async handleLLMEnd(output, runId) {
|
|
93
109
|
const run = this.runMap.get(runId);
|
|
@@ -97,6 +113,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
97
113
|
const llmRun = run;
|
|
98
114
|
llmRun.end_time = Date.now();
|
|
99
115
|
llmRun.response = output;
|
|
116
|
+
await this.onLLMEnd?.(llmRun);
|
|
100
117
|
await this._endTrace(llmRun);
|
|
101
118
|
}
|
|
102
119
|
async handleLLMError(error, runId) {
|
|
@@ -107,12 +124,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
107
124
|
const llmRun = run;
|
|
108
125
|
llmRun.end_time = Date.now();
|
|
109
126
|
llmRun.error = error.message;
|
|
127
|
+
await this.onLLMError?.(llmRun);
|
|
110
128
|
await this._endTrace(llmRun);
|
|
111
129
|
}
|
|
112
130
|
async handleChainStart(chain, inputs, runId, parentRunId) {
|
|
113
131
|
if (this.session === undefined) {
|
|
114
132
|
this.session = await this.loadDefaultSession();
|
|
115
133
|
}
|
|
134
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
116
135
|
const run = {
|
|
117
136
|
uuid: runId,
|
|
118
137
|
parent_uuid: parentRunId,
|
|
@@ -121,13 +140,15 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
121
140
|
serialized: chain,
|
|
122
141
|
inputs,
|
|
123
142
|
session_id: this.session.id,
|
|
124
|
-
execution_order
|
|
143
|
+
execution_order,
|
|
144
|
+
child_execution_order: execution_order,
|
|
125
145
|
type: "chain",
|
|
126
146
|
child_llm_runs: [],
|
|
127
147
|
child_chain_runs: [],
|
|
128
148
|
child_tool_runs: [],
|
|
129
149
|
};
|
|
130
150
|
this._startTrace(run);
|
|
151
|
+
await this.onChainStart?.(run);
|
|
131
152
|
}
|
|
132
153
|
async handleChainEnd(outputs, runId) {
|
|
133
154
|
const run = this.runMap.get(runId);
|
|
@@ -137,6 +158,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
137
158
|
const chainRun = run;
|
|
138
159
|
chainRun.end_time = Date.now();
|
|
139
160
|
chainRun.outputs = outputs;
|
|
161
|
+
await this.onChainEnd?.(chainRun);
|
|
140
162
|
await this._endTrace(chainRun);
|
|
141
163
|
}
|
|
142
164
|
async handleChainError(error, runId) {
|
|
@@ -147,12 +169,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
147
169
|
const chainRun = run;
|
|
148
170
|
chainRun.end_time = Date.now();
|
|
149
171
|
chainRun.error = error.message;
|
|
172
|
+
await this.onChainError?.(chainRun);
|
|
150
173
|
await this._endTrace(chainRun);
|
|
151
174
|
}
|
|
152
175
|
async handleToolStart(tool, input, runId, parentRunId) {
|
|
153
176
|
if (this.session === undefined) {
|
|
154
177
|
this.session = await this.loadDefaultSession();
|
|
155
178
|
}
|
|
179
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
156
180
|
const run = {
|
|
157
181
|
uuid: runId,
|
|
158
182
|
parent_uuid: parentRunId,
|
|
@@ -161,7 +185,8 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
161
185
|
serialized: tool,
|
|
162
186
|
tool_input: input,
|
|
163
187
|
session_id: this.session.id,
|
|
164
|
-
execution_order
|
|
188
|
+
execution_order,
|
|
189
|
+
child_execution_order: execution_order,
|
|
165
190
|
type: "tool",
|
|
166
191
|
action: JSON.stringify(tool),
|
|
167
192
|
child_llm_runs: [],
|
|
@@ -169,6 +194,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
169
194
|
child_tool_runs: [],
|
|
170
195
|
};
|
|
171
196
|
this._startTrace(run);
|
|
197
|
+
await this.onToolStart?.(run);
|
|
172
198
|
}
|
|
173
199
|
async handleToolEnd(output, runId) {
|
|
174
200
|
const run = this.runMap.get(runId);
|
|
@@ -178,6 +204,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
178
204
|
const toolRun = run;
|
|
179
205
|
toolRun.end_time = Date.now();
|
|
180
206
|
toolRun.output = output;
|
|
207
|
+
await this.onToolEnd?.(toolRun);
|
|
181
208
|
await this._endTrace(toolRun);
|
|
182
209
|
}
|
|
183
210
|
async handleToolError(error, runId) {
|
|
@@ -188,8 +215,19 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
188
215
|
const toolRun = run;
|
|
189
216
|
toolRun.end_time = Date.now();
|
|
190
217
|
toolRun.error = error.message;
|
|
218
|
+
await this.onToolError?.(toolRun);
|
|
191
219
|
await this._endTrace(toolRun);
|
|
192
220
|
}
|
|
221
|
+
async handleAgentAction(action, runId) {
|
|
222
|
+
const run = this.runMap.get(runId);
|
|
223
|
+
if (!run || run?.type !== "chain") {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const agentRun = run;
|
|
227
|
+
agentRun.actions = agentRun.actions || [];
|
|
228
|
+
agentRun.actions.push(action);
|
|
229
|
+
await this.onAgentAction?.(run);
|
|
230
|
+
}
|
|
193
231
|
}
|
|
194
232
|
exports.BaseTracer = BaseTracer;
|
|
195
233
|
class LangChainTracer extends BaseTracer {
|
|
@@ -299,17 +337,5 @@ class LangChainTracer extends BaseTracer {
|
|
|
299
337
|
this.session = tracerSession;
|
|
300
338
|
return tracerSession;
|
|
301
339
|
}
|
|
302
|
-
copy() {
|
|
303
|
-
// TODO: this is a hack to get tracing to work with the current backend
|
|
304
|
-
// we need to not use execution order, then remove this check
|
|
305
|
-
if (this.executionOrder === 1) {
|
|
306
|
-
const copy = new LangChainTracer();
|
|
307
|
-
copy.session = this.session;
|
|
308
|
-
copy.runMap = new Map(this.runMap);
|
|
309
|
-
copy.executionOrder = this.executionOrder;
|
|
310
|
-
return copy;
|
|
311
|
-
}
|
|
312
|
-
return this;
|
|
313
|
-
}
|
|
314
340
|
}
|
|
315
341
|
exports.LangChainTracer = LangChainTracer;
|