langchain 0.0.74 → 0.0.75
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/client.cjs +1 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/base_language/index.cjs +6 -0
- package/dist/base_language/index.d.ts +19 -1
- package/dist/base_language/index.js +6 -0
- package/dist/callbacks/base.d.ts +15 -1
- package/dist/callbacks/handlers/console.cjs +15 -28
- package/dist/callbacks/handlers/console.d.ts +11 -20
- package/dist/callbacks/handlers/console.js +14 -27
- package/dist/callbacks/handlers/initialize.cjs +15 -3
- package/dist/callbacks/handlers/initialize.d.ts +4 -2
- package/dist/callbacks/handlers/initialize.js +12 -1
- package/dist/callbacks/handlers/tracer.cjs +204 -0
- package/dist/callbacks/handlers/tracer.d.ts +65 -0
- package/dist/callbacks/handlers/tracer.js +200 -0
- package/dist/callbacks/handlers/tracer_langchain.cjs +202 -0
- package/dist/callbacks/handlers/tracer_langchain.d.ts +36 -0
- package/dist/callbacks/handlers/tracer_langchain.js +175 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.cjs +198 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +57 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.js +194 -0
- package/dist/callbacks/index.cjs +10 -5
- package/dist/callbacks/index.d.ts +5 -3
- package/dist/callbacks/index.js +5 -3
- package/dist/callbacks/manager.cjs +33 -3
- package/dist/callbacks/manager.d.ts +4 -1
- package/dist/callbacks/manager.js +34 -4
- package/dist/chains/llm_chain.cjs +17 -5
- package/dist/chains/llm_chain.d.ts +9 -3
- package/dist/chains/llm_chain.js +17 -5
- package/dist/chains/summarization/load.cjs +14 -5
- package/dist/chains/summarization/load.d.ts +7 -2
- package/dist/chains/summarization/load.js +14 -5
- package/dist/chat_models/anthropic.cjs +36 -9
- package/dist/chat_models/anthropic.d.ts +6 -3
- package/dist/chat_models/anthropic.js +36 -9
- package/dist/chat_models/base.cjs +24 -13
- package/dist/chat_models/base.d.ts +8 -7
- package/dist/chat_models/base.js +24 -13
- package/dist/chat_models/openai.cjs +18 -12
- package/dist/chat_models/openai.d.ts +2 -1
- package/dist/chat_models/openai.js +18 -12
- package/dist/client/index.cjs +5 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/langchainplus.cjs +344 -0
- package/dist/client/langchainplus.d.ts +63 -0
- package/dist/client/langchainplus.js +337 -0
- package/dist/llms/base.cjs +25 -16
- package/dist/llms/base.d.ts +9 -8
- package/dist/llms/base.js +25 -16
- package/dist/llms/cohere.cjs +3 -2
- package/dist/llms/cohere.d.ts +1 -1
- package/dist/llms/cohere.js +3 -2
- package/dist/llms/hf.cjs +2 -2
- package/dist/llms/hf.d.ts +1 -1
- package/dist/llms/hf.js +2 -2
- package/dist/llms/openai-chat.cjs +11 -12
- package/dist/llms/openai-chat.d.ts +2 -1
- package/dist/llms/openai-chat.js +11 -12
- package/dist/llms/openai.cjs +13 -11
- package/dist/llms/openai.d.ts +4 -3
- package/dist/llms/openai.js +13 -11
- package/dist/llms/replicate.cjs +2 -2
- package/dist/llms/replicate.d.ts +1 -1
- package/dist/llms/replicate.js +2 -2
- package/dist/retrievers/document_compressors/chain_extract.cjs +9 -11
- package/dist/retrievers/document_compressors/chain_extract.js +9 -11
- package/dist/schema/index.d.ts +2 -0
- package/dist/stores/message/redis.cjs +67 -0
- package/dist/stores/message/redis.d.ts +18 -0
- package/dist/stores/message/redis.js +63 -0
- package/dist/types/openai-types.d.ts +3 -2
- package/dist/util/async_caller.cjs +16 -0
- package/dist/util/async_caller.d.ts +4 -0
- package/dist/util/async_caller.js +16 -0
- package/dist/util/axios-fetch-adapter.cjs +6 -0
- package/dist/util/axios-fetch-adapter.js +6 -0
- package/package.json +17 -1
- package/stores/message/redis.cjs +1 -0
- package/stores/message/redis.d.ts +1 -0
- package/stores/message/redis.js +1 -0
- package/dist/callbacks/handlers/tracers.cjs +0 -341
- package/dist/callbacks/handlers/tracers.d.ts +0 -100
- package/dist/callbacks/handlers/tracers.js +0 -336
package/client.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/client/index.cjs');
|
package/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/client/index.js'
|
package/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/client/index.js'
|
|
@@ -33,6 +33,12 @@ exports.BaseLangChain = BaseLangChain;
|
|
|
33
33
|
* Base class for language models.
|
|
34
34
|
*/
|
|
35
35
|
class BaseLanguageModel extends BaseLangChain {
|
|
36
|
+
/**
|
|
37
|
+
* Keys that the language model accepts as call options.
|
|
38
|
+
*/
|
|
39
|
+
get callKeys() {
|
|
40
|
+
return ["stop", "timeout", "signal"];
|
|
41
|
+
}
|
|
36
42
|
constructor(params) {
|
|
37
43
|
super({
|
|
38
44
|
verbose: params.verbose,
|
|
@@ -32,19 +32,37 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
|
|
|
32
32
|
callbackManager?: CallbackManager;
|
|
33
33
|
}
|
|
34
34
|
export interface BaseLanguageModelCallOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Stop tokens to use for this call.
|
|
37
|
+
* If not provided, the default stop tokens for the model will be used.
|
|
38
|
+
*/
|
|
39
|
+
stop?: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Timeout for this call in milliseconds.
|
|
42
|
+
*/
|
|
43
|
+
timeout?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Abort signal for this call.
|
|
46
|
+
* If provided, the call will be aborted when the signal is aborted.
|
|
47
|
+
*/
|
|
48
|
+
signal?: AbortSignal;
|
|
35
49
|
}
|
|
36
50
|
/**
|
|
37
51
|
* Base class for language models.
|
|
38
52
|
*/
|
|
39
53
|
export declare abstract class BaseLanguageModel extends BaseLangChain implements BaseLanguageModelParams {
|
|
40
54
|
CallOptions: BaseLanguageModelCallOptions;
|
|
55
|
+
/**
|
|
56
|
+
* Keys that the language model accepts as call options.
|
|
57
|
+
*/
|
|
58
|
+
get callKeys(): string[];
|
|
41
59
|
/**
|
|
42
60
|
* The async caller should be used by subclasses to make any async calls,
|
|
43
61
|
* which will thus benefit from the concurrency and retry logic.
|
|
44
62
|
*/
|
|
45
63
|
caller: AsyncCaller;
|
|
46
64
|
constructor(params: BaseLanguageModelParams);
|
|
47
|
-
abstract generatePrompt(promptValues: BasePromptValue[],
|
|
65
|
+
abstract generatePrompt(promptValues: BasePromptValue[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
48
66
|
abstract _modelType(): string;
|
|
49
67
|
abstract _llmType(): string;
|
|
50
68
|
private _encoding?;
|
|
@@ -29,6 +29,12 @@ export class BaseLangChain {
|
|
|
29
29
|
* Base class for language models.
|
|
30
30
|
*/
|
|
31
31
|
export class BaseLanguageModel extends BaseLangChain {
|
|
32
|
+
/**
|
|
33
|
+
* Keys that the language model accepts as call options.
|
|
34
|
+
*/
|
|
35
|
+
get callKeys() {
|
|
36
|
+
return ["stop", "timeout", "signal"];
|
|
37
|
+
}
|
|
32
38
|
constructor(params) {
|
|
33
39
|
super({
|
|
34
40
|
verbose: params.verbose,
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentAction, AgentFinish, ChainValues, LLMResult } from "../schema/index.js";
|
|
1
|
+
import { AgentAction, AgentFinish, BaseChatMessage, ChainValues, LLMResult } from "../schema/index.js";
|
|
2
2
|
type Error = any;
|
|
3
3
|
export interface BaseCallbackHandlerInput {
|
|
4
4
|
ignoreLLM?: boolean;
|
|
@@ -25,6 +25,13 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
25
25
|
* Called at the end of an LLM/ChatModel run, with the output and the run ID.
|
|
26
26
|
*/
|
|
27
27
|
handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string): Promise<void> | void;
|
|
28
|
+
/**
|
|
29
|
+
* Called at the start of a Chat Model run, with the prompt(s)
|
|
30
|
+
* and the run ID.
|
|
31
|
+
*/
|
|
32
|
+
handleChatModelStart?(llm: {
|
|
33
|
+
name: string;
|
|
34
|
+
}, messages: BaseChatMessage[][], runId: string, parentRunId?: string): Promise<void> | void;
|
|
28
35
|
/**
|
|
29
36
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
30
37
|
* and the run ID.
|
|
@@ -109,6 +116,13 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
109
116
|
* Called at the end of an LLM/ChatModel run, with the output and the run ID.
|
|
110
117
|
*/
|
|
111
118
|
handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined): void | Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Called at the start of a Chat Model run, with the prompt(s)
|
|
121
|
+
* and the run ID.
|
|
122
|
+
*/
|
|
123
|
+
handleChatModelStart?(llm: {
|
|
124
|
+
name: string;
|
|
125
|
+
}, messages: BaseChatMessage[][], runId: string, parentRunId?: string | undefined): void | Promise<void>;
|
|
112
126
|
/**
|
|
113
127
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
114
128
|
* and the run ID.
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ConsoleCallbackHandler = void 0;
|
|
7
7
|
const ansi_styles_1 = __importDefault(require("ansi-styles"));
|
|
8
|
-
const
|
|
8
|
+
const tracer_js_1 = require("./tracer.cjs");
|
|
9
9
|
function wrap(style, text) {
|
|
10
10
|
return `${style.open}${text}${style.close}`;
|
|
11
11
|
}
|
|
@@ -25,42 +25,25 @@ function elapsed(run) {
|
|
|
25
25
|
return `${(elapsed / 1000).toFixed(2)}s`;
|
|
26
26
|
}
|
|
27
27
|
const { color } = ansi_styles_1.default;
|
|
28
|
-
class ConsoleCallbackHandler extends
|
|
29
|
-
// boilerplate to work with the base tracer class
|
|
28
|
+
class ConsoleCallbackHandler extends tracer_js_1.BaseTracer {
|
|
30
29
|
constructor() {
|
|
31
|
-
super();
|
|
30
|
+
super(...arguments);
|
|
32
31
|
Object.defineProperty(this, "name", {
|
|
33
32
|
enumerable: true,
|
|
34
33
|
configurable: true,
|
|
35
34
|
writable: true,
|
|
36
35
|
value: "console_callback_handler"
|
|
37
36
|
});
|
|
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
37
|
}
|
|
49
38
|
persistRun(_run) {
|
|
50
39
|
return Promise.resolve();
|
|
51
40
|
}
|
|
52
|
-
loadDefaultSession() {
|
|
53
|
-
return this.newSession();
|
|
54
|
-
}
|
|
55
|
-
loadSession(sessionName) {
|
|
56
|
-
return this.newSession(sessionName);
|
|
57
|
-
}
|
|
58
41
|
// utility methods
|
|
59
42
|
getParents(run) {
|
|
60
43
|
const parents = [];
|
|
61
44
|
let currentRun = run;
|
|
62
|
-
while (currentRun.
|
|
63
|
-
const parent = this.runMap.get(currentRun.
|
|
45
|
+
while (currentRun.parent_run_id) {
|
|
46
|
+
const parent = this.runMap.get(currentRun.parent_run_id);
|
|
64
47
|
if (parent) {
|
|
65
48
|
parents.push(parent);
|
|
66
49
|
currentRun = parent;
|
|
@@ -75,7 +58,7 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
75
58
|
const parents = this.getParents(run).reverse();
|
|
76
59
|
const string = [...parents, run]
|
|
77
60
|
.map((parent, i, arr) => {
|
|
78
|
-
const name = `${parent.execution_order}:${parent.
|
|
61
|
+
const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
|
|
79
62
|
return i === arr.length - 1 ? wrap(ansi_styles_1.default.bold, name) : name;
|
|
80
63
|
})
|
|
81
64
|
.join(" > ");
|
|
@@ -96,11 +79,14 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
96
79
|
}
|
|
97
80
|
onLLMStart(run) {
|
|
98
81
|
const crumbs = this.getBreadcrumbs(run);
|
|
99
|
-
|
|
82
|
+
const inputs = "prompts" in run.inputs
|
|
83
|
+
? { prompts: run.inputs.prompts.map((p) => p.trim()) }
|
|
84
|
+
: run.inputs;
|
|
85
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(inputs, "[inputs]")}`);
|
|
100
86
|
}
|
|
101
87
|
onLLMEnd(run) {
|
|
102
88
|
const crumbs = this.getBreadcrumbs(run);
|
|
103
|
-
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.
|
|
89
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.outputs, "[response]")}`);
|
|
104
90
|
}
|
|
105
91
|
onLLMError(run) {
|
|
106
92
|
const crumbs = this.getBreadcrumbs(run);
|
|
@@ -108,19 +94,20 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
108
94
|
}
|
|
109
95
|
onToolStart(run) {
|
|
110
96
|
const crumbs = this.getBreadcrumbs(run);
|
|
111
|
-
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.
|
|
97
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`);
|
|
112
98
|
}
|
|
113
99
|
onToolEnd(run) {
|
|
114
100
|
const crumbs = this.getBreadcrumbs(run);
|
|
115
|
-
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
101
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`);
|
|
116
102
|
}
|
|
117
103
|
onToolError(run) {
|
|
118
104
|
const crumbs = this.getBreadcrumbs(run);
|
|
119
105
|
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
120
106
|
}
|
|
121
107
|
onAgentAction(run) {
|
|
108
|
+
const agentRun = run;
|
|
122
109
|
const crumbs = this.getBreadcrumbs(run);
|
|
123
|
-
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(
|
|
110
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(agentRun.actions[agentRun.actions.length - 1], "[action]")}`);
|
|
124
111
|
}
|
|
125
112
|
}
|
|
126
113
|
exports.ConsoleCallbackHandler = ConsoleCallbackHandler;
|
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseTracer, Run } from "./tracer.js";
|
|
2
2
|
export declare class ConsoleCallbackHandler extends BaseTracer {
|
|
3
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
4
|
protected persistRun(_run: Run): Promise<void>;
|
|
12
|
-
loadDefaultSession(): Promise<import("./tracers.js").TracerSession>;
|
|
13
|
-
loadSession(sessionName: string): Promise<import("./tracers.js").TracerSession>;
|
|
14
5
|
getParents(run: Run): Run[];
|
|
15
6
|
getBreadcrumbs(run: Run): string;
|
|
16
|
-
onChainStart(run:
|
|
17
|
-
onChainEnd(run:
|
|
18
|
-
onChainError(run:
|
|
19
|
-
onLLMStart(run:
|
|
20
|
-
onLLMEnd(run:
|
|
21
|
-
onLLMError(run:
|
|
22
|
-
onToolStart(run:
|
|
23
|
-
onToolEnd(run:
|
|
24
|
-
onToolError(run:
|
|
25
|
-
onAgentAction(run:
|
|
7
|
+
onChainStart(run: Run): void;
|
|
8
|
+
onChainEnd(run: Run): void;
|
|
9
|
+
onChainError(run: Run): void;
|
|
10
|
+
onLLMStart(run: Run): void;
|
|
11
|
+
onLLMEnd(run: Run): void;
|
|
12
|
+
onLLMError(run: Run): void;
|
|
13
|
+
onToolStart(run: Run): void;
|
|
14
|
+
onToolEnd(run: Run): void;
|
|
15
|
+
onToolError(run: Run): void;
|
|
16
|
+
onAgentAction(run: Run): void;
|
|
26
17
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import styles from "ansi-styles";
|
|
2
|
-
import { BaseTracer
|
|
2
|
+
import { BaseTracer } from "./tracer.js";
|
|
3
3
|
function wrap(style, text) {
|
|
4
4
|
return `${style.open}${text}${style.close}`;
|
|
5
5
|
}
|
|
@@ -20,41 +20,24 @@ function elapsed(run) {
|
|
|
20
20
|
}
|
|
21
21
|
const { color } = styles;
|
|
22
22
|
export class ConsoleCallbackHandler extends BaseTracer {
|
|
23
|
-
// boilerplate to work with the base tracer class
|
|
24
23
|
constructor() {
|
|
25
|
-
super();
|
|
24
|
+
super(...arguments);
|
|
26
25
|
Object.defineProperty(this, "name", {
|
|
27
26
|
enumerable: true,
|
|
28
27
|
configurable: true,
|
|
29
28
|
writable: true,
|
|
30
29
|
value: "console_callback_handler"
|
|
31
30
|
});
|
|
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
31
|
}
|
|
43
32
|
persistRun(_run) {
|
|
44
33
|
return Promise.resolve();
|
|
45
34
|
}
|
|
46
|
-
loadDefaultSession() {
|
|
47
|
-
return this.newSession();
|
|
48
|
-
}
|
|
49
|
-
loadSession(sessionName) {
|
|
50
|
-
return this.newSession(sessionName);
|
|
51
|
-
}
|
|
52
35
|
// utility methods
|
|
53
36
|
getParents(run) {
|
|
54
37
|
const parents = [];
|
|
55
38
|
let currentRun = run;
|
|
56
|
-
while (currentRun.
|
|
57
|
-
const parent = this.runMap.get(currentRun.
|
|
39
|
+
while (currentRun.parent_run_id) {
|
|
40
|
+
const parent = this.runMap.get(currentRun.parent_run_id);
|
|
58
41
|
if (parent) {
|
|
59
42
|
parents.push(parent);
|
|
60
43
|
currentRun = parent;
|
|
@@ -69,7 +52,7 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
69
52
|
const parents = this.getParents(run).reverse();
|
|
70
53
|
const string = [...parents, run]
|
|
71
54
|
.map((parent, i, arr) => {
|
|
72
|
-
const name = `${parent.execution_order}:${parent.
|
|
55
|
+
const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
|
|
73
56
|
return i === arr.length - 1 ? wrap(styles.bold, name) : name;
|
|
74
57
|
})
|
|
75
58
|
.join(" > ");
|
|
@@ -90,11 +73,14 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
90
73
|
}
|
|
91
74
|
onLLMStart(run) {
|
|
92
75
|
const crumbs = this.getBreadcrumbs(run);
|
|
93
|
-
|
|
76
|
+
const inputs = "prompts" in run.inputs
|
|
77
|
+
? { prompts: run.inputs.prompts.map((p) => p.trim()) }
|
|
78
|
+
: run.inputs;
|
|
79
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(inputs, "[inputs]")}`);
|
|
94
80
|
}
|
|
95
81
|
onLLMEnd(run) {
|
|
96
82
|
const crumbs = this.getBreadcrumbs(run);
|
|
97
|
-
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.
|
|
83
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.outputs, "[response]")}`);
|
|
98
84
|
}
|
|
99
85
|
onLLMError(run) {
|
|
100
86
|
const crumbs = this.getBreadcrumbs(run);
|
|
@@ -102,18 +88,19 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
102
88
|
}
|
|
103
89
|
onToolStart(run) {
|
|
104
90
|
const crumbs = this.getBreadcrumbs(run);
|
|
105
|
-
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.
|
|
91
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`);
|
|
106
92
|
}
|
|
107
93
|
onToolEnd(run) {
|
|
108
94
|
const crumbs = this.getBreadcrumbs(run);
|
|
109
|
-
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
95
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`);
|
|
110
96
|
}
|
|
111
97
|
onToolError(run) {
|
|
112
98
|
const crumbs = this.getBreadcrumbs(run);
|
|
113
99
|
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
114
100
|
}
|
|
115
101
|
onAgentAction(run) {
|
|
102
|
+
const agentRun = run;
|
|
116
103
|
const crumbs = this.getBreadcrumbs(run);
|
|
117
|
-
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(
|
|
104
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(agentRun.actions[agentRun.actions.length - 1], "[action]")}`);
|
|
118
105
|
}
|
|
119
106
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTracingCallbackHandler = void 0;
|
|
4
|
-
const
|
|
3
|
+
exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = void 0;
|
|
4
|
+
const tracer_langchain_js_1 = require("./tracer_langchain.cjs");
|
|
5
|
+
const tracer_langchain_v1_js_1 = require("./tracer_langchain_v1.cjs");
|
|
5
6
|
async function getTracingCallbackHandler(session) {
|
|
6
|
-
const tracer = new
|
|
7
|
+
const tracer = new tracer_langchain_v1_js_1.LangChainTracerV1();
|
|
7
8
|
if (session) {
|
|
8
9
|
await tracer.loadSession(session);
|
|
9
10
|
}
|
|
@@ -13,3 +14,14 @@ async function getTracingCallbackHandler(session) {
|
|
|
13
14
|
return tracer;
|
|
14
15
|
}
|
|
15
16
|
exports.getTracingCallbackHandler = getTracingCallbackHandler;
|
|
17
|
+
async function getTracingV2CallbackHandler(session) {
|
|
18
|
+
const tracer = new tracer_langchain_js_1.LangChainTracer();
|
|
19
|
+
if (session) {
|
|
20
|
+
await tracer.loadSession(session);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
await tracer.loadDefaultSession();
|
|
24
|
+
}
|
|
25
|
+
return tracer;
|
|
26
|
+
}
|
|
27
|
+
exports.getTracingV2CallbackHandler = getTracingV2CallbackHandler;
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import { LangChainTracer } from "./
|
|
2
|
-
|
|
1
|
+
import { LangChainTracer } from "./tracer_langchain.js";
|
|
2
|
+
import { LangChainTracerV1 } from "./tracer_langchain_v1.js";
|
|
3
|
+
export declare function getTracingCallbackHandler(session?: string): Promise<LangChainTracerV1>;
|
|
4
|
+
export declare function getTracingV2CallbackHandler(session?: string): Promise<LangChainTracer>;
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import { LangChainTracer } from "./
|
|
1
|
+
import { LangChainTracer } from "./tracer_langchain.js";
|
|
2
|
+
import { LangChainTracerV1 } from "./tracer_langchain_v1.js";
|
|
2
3
|
export async function getTracingCallbackHandler(session) {
|
|
4
|
+
const tracer = new LangChainTracerV1();
|
|
5
|
+
if (session) {
|
|
6
|
+
await tracer.loadSession(session);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
await tracer.loadDefaultSession();
|
|
10
|
+
}
|
|
11
|
+
return tracer;
|
|
12
|
+
}
|
|
13
|
+
export async function getTracingV2CallbackHandler(session) {
|
|
3
14
|
const tracer = new LangChainTracer();
|
|
4
15
|
if (session) {
|
|
5
16
|
await tracer.loadSession(session);
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseTracer = void 0;
|
|
4
|
+
const base_js_1 = require("../base.cjs");
|
|
5
|
+
class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
Object.defineProperty(this, "runMap", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: new Map()
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
copy() {
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
_addChildRun(parentRun, childRun) {
|
|
19
|
+
parentRun.child_runs.push(childRun);
|
|
20
|
+
}
|
|
21
|
+
_startTrace(run) {
|
|
22
|
+
if (run.parent_run_id !== undefined) {
|
|
23
|
+
const parentRun = this.runMap.get(run.parent_run_id);
|
|
24
|
+
if (parentRun) {
|
|
25
|
+
this._addChildRun(parentRun, run);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error(`Caller run ${run.parent_run_id} not found`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
this.runMap.set(run.id, run);
|
|
32
|
+
}
|
|
33
|
+
async _endTrace(run) {
|
|
34
|
+
if (!run.parent_run_id) {
|
|
35
|
+
await this.persistRun(run);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const parentRun = this.runMap.get(run.parent_run_id);
|
|
39
|
+
if (parentRun === undefined) {
|
|
40
|
+
throw new Error(`Parent run ${run.parent_run_id} not found`);
|
|
41
|
+
}
|
|
42
|
+
parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
|
|
43
|
+
}
|
|
44
|
+
this.runMap.delete(run.id);
|
|
45
|
+
}
|
|
46
|
+
_getExecutionOrder(parentRunId) {
|
|
47
|
+
// If a run has no parent then execution order is 1
|
|
48
|
+
if (parentRunId === undefined) {
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
const parentRun = this.runMap.get(parentRunId);
|
|
52
|
+
if (parentRun === undefined) {
|
|
53
|
+
throw new Error(`Parent run ${parentRunId} not found`);
|
|
54
|
+
}
|
|
55
|
+
return parentRun.child_execution_order + 1;
|
|
56
|
+
}
|
|
57
|
+
async handleLLMStart(llm, prompts, runId, parentRunId) {
|
|
58
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
59
|
+
const run = {
|
|
60
|
+
id: runId,
|
|
61
|
+
name: llm.name,
|
|
62
|
+
parent_run_id: parentRunId,
|
|
63
|
+
start_time: Date.now(),
|
|
64
|
+
end_time: 0,
|
|
65
|
+
serialized: llm,
|
|
66
|
+
inputs: { prompts },
|
|
67
|
+
execution_order,
|
|
68
|
+
child_runs: [],
|
|
69
|
+
child_execution_order: execution_order,
|
|
70
|
+
run_type: "llm",
|
|
71
|
+
};
|
|
72
|
+
this._startTrace(run);
|
|
73
|
+
await this.onLLMStart?.(run);
|
|
74
|
+
}
|
|
75
|
+
async handleChatModelStart(llm, messages, runId, parentRunId) {
|
|
76
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
77
|
+
const convertedMessages = messages.map((batch) => batch.map((message) => ({
|
|
78
|
+
_type: message._getType(),
|
|
79
|
+
content: message.text, // TODO: Unify serialization btwn languages
|
|
80
|
+
})));
|
|
81
|
+
const run = {
|
|
82
|
+
id: runId,
|
|
83
|
+
name: llm.name,
|
|
84
|
+
parent_run_id: parentRunId,
|
|
85
|
+
start_time: Date.now(),
|
|
86
|
+
end_time: 0,
|
|
87
|
+
serialized: llm,
|
|
88
|
+
inputs: { messages: convertedMessages },
|
|
89
|
+
execution_order,
|
|
90
|
+
child_runs: [],
|
|
91
|
+
child_execution_order: execution_order,
|
|
92
|
+
run_type: "llm",
|
|
93
|
+
};
|
|
94
|
+
this._startTrace(run);
|
|
95
|
+
await this.onLLMStart?.(run);
|
|
96
|
+
}
|
|
97
|
+
async handleLLMEnd(output, runId) {
|
|
98
|
+
const run = this.runMap.get(runId);
|
|
99
|
+
if (!run || run?.run_type !== "llm") {
|
|
100
|
+
throw new Error("No LLM run to end.");
|
|
101
|
+
}
|
|
102
|
+
run.end_time = Date.now();
|
|
103
|
+
run.outputs = output;
|
|
104
|
+
await this.onLLMEnd?.(run);
|
|
105
|
+
await this._endTrace(run);
|
|
106
|
+
}
|
|
107
|
+
async handleLLMError(error, runId) {
|
|
108
|
+
const run = this.runMap.get(runId);
|
|
109
|
+
if (!run || run?.run_type !== "llm") {
|
|
110
|
+
throw new Error("No LLM run to end.");
|
|
111
|
+
}
|
|
112
|
+
run.end_time = Date.now();
|
|
113
|
+
run.error = error.message;
|
|
114
|
+
await this.onLLMError?.(run);
|
|
115
|
+
await this._endTrace(run);
|
|
116
|
+
}
|
|
117
|
+
async handleChainStart(chain, inputs, runId, parentRunId) {
|
|
118
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
119
|
+
const run = {
|
|
120
|
+
id: runId,
|
|
121
|
+
name: chain.name,
|
|
122
|
+
parent_run_id: parentRunId,
|
|
123
|
+
start_time: Date.now(),
|
|
124
|
+
end_time: 0,
|
|
125
|
+
serialized: chain,
|
|
126
|
+
inputs,
|
|
127
|
+
execution_order,
|
|
128
|
+
child_execution_order: execution_order,
|
|
129
|
+
run_type: "chain",
|
|
130
|
+
child_runs: [],
|
|
131
|
+
};
|
|
132
|
+
this._startTrace(run);
|
|
133
|
+
await this.onChainStart?.(run);
|
|
134
|
+
}
|
|
135
|
+
async handleChainEnd(outputs, runId) {
|
|
136
|
+
const run = this.runMap.get(runId);
|
|
137
|
+
if (!run || run?.run_type !== "chain") {
|
|
138
|
+
throw new Error("No chain run to end.");
|
|
139
|
+
}
|
|
140
|
+
run.end_time = Date.now();
|
|
141
|
+
run.outputs = outputs;
|
|
142
|
+
await this.onChainEnd?.(run);
|
|
143
|
+
await this._endTrace(run);
|
|
144
|
+
}
|
|
145
|
+
async handleChainError(error, runId) {
|
|
146
|
+
const run = this.runMap.get(runId);
|
|
147
|
+
if (!run || run?.run_type !== "chain") {
|
|
148
|
+
throw new Error("No chain run to end.");
|
|
149
|
+
}
|
|
150
|
+
run.end_time = Date.now();
|
|
151
|
+
run.error = error.message;
|
|
152
|
+
await this.onChainError?.(run);
|
|
153
|
+
await this._endTrace(run);
|
|
154
|
+
}
|
|
155
|
+
async handleToolStart(tool, input, runId, parentRunId) {
|
|
156
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
157
|
+
const run = {
|
|
158
|
+
id: runId,
|
|
159
|
+
name: tool.name,
|
|
160
|
+
parent_run_id: parentRunId,
|
|
161
|
+
start_time: Date.now(),
|
|
162
|
+
end_time: 0,
|
|
163
|
+
serialized: tool,
|
|
164
|
+
inputs: { input },
|
|
165
|
+
execution_order,
|
|
166
|
+
child_execution_order: execution_order,
|
|
167
|
+
run_type: "tool",
|
|
168
|
+
child_runs: [],
|
|
169
|
+
};
|
|
170
|
+
this._startTrace(run);
|
|
171
|
+
await this.onToolStart?.(run);
|
|
172
|
+
}
|
|
173
|
+
async handleToolEnd(output, runId) {
|
|
174
|
+
const run = this.runMap.get(runId);
|
|
175
|
+
if (!run || run?.run_type !== "tool") {
|
|
176
|
+
throw new Error("No tool run to end");
|
|
177
|
+
}
|
|
178
|
+
run.end_time = Date.now();
|
|
179
|
+
run.outputs = { output };
|
|
180
|
+
await this.onToolEnd?.(run);
|
|
181
|
+
await this._endTrace(run);
|
|
182
|
+
}
|
|
183
|
+
async handleToolError(error, runId) {
|
|
184
|
+
const run = this.runMap.get(runId);
|
|
185
|
+
if (!run || run?.run_type !== "tool") {
|
|
186
|
+
throw new Error("No tool run to end");
|
|
187
|
+
}
|
|
188
|
+
run.end_time = Date.now();
|
|
189
|
+
run.error = error.message;
|
|
190
|
+
await this.onToolError?.(run);
|
|
191
|
+
await this._endTrace(run);
|
|
192
|
+
}
|
|
193
|
+
async handleAgentAction(action, runId) {
|
|
194
|
+
const run = this.runMap.get(runId);
|
|
195
|
+
if (!run || run?.run_type !== "chain") {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const agentRun = run;
|
|
199
|
+
agentRun.actions = agentRun.actions || [];
|
|
200
|
+
agentRun.actions.push(action);
|
|
201
|
+
await this.onAgentAction?.(run);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.BaseTracer = BaseTracer;
|