langchain 0.0.74 → 0.0.76
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/count_tokens.cjs +5 -21
- package/dist/base_language/count_tokens.d.ts +1 -6
- package/dist/base_language/count_tokens.js +4 -19
- package/dist/base_language/index.cjs +16 -24
- package/dist/base_language/index.d.ts +22 -3
- package/dist/base_language/index.js +17 -25
- package/dist/cache/redis.d.ts +3 -1
- package/dist/callbacks/base.d.ts +17 -3
- 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 +8 -3
- package/dist/callbacks/handlers/initialize.d.ts +4 -2
- package/dist/callbacks/handlers/initialize.js +6 -2
- package/dist/callbacks/handlers/tracer.cjs +193 -0
- package/dist/callbacks/handlers/tracer.d.ts +65 -0
- package/dist/callbacks/handlers/tracer.js +189 -0
- package/dist/callbacks/handlers/tracer_langchain.cjs +163 -0
- package/dist/callbacks/handlers/tracer_langchain.d.ts +39 -0
- package/dist/callbacks/handlers/tracer_langchain.js +159 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.cjs +202 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +57 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.js +198 -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 +39 -9
- package/dist/callbacks/manager.d.ts +5 -2
- package/dist/callbacks/manager.js +40 -10
- 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/question_answering/map_reduce_prompts.cjs +5 -5
- package/dist/chains/question_answering/map_reduce_prompts.d.ts +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.js +1 -1
- package/dist/chains/question_answering/refine_prompts.cjs +5 -5
- package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
- package/dist/chains/question_answering/refine_prompts.js +1 -1
- package/dist/chains/question_answering/stuff_prompts.cjs +2 -2
- package/dist/chains/question_answering/stuff_prompts.d.ts +1 -1
- package/dist/chains/question_answering/stuff_prompts.js +1 -1
- package/dist/chains/sequential_chain.cjs +2 -2
- package/dist/chains/sequential_chain.d.ts +2 -2
- package/dist/chains/sequential_chain.js +2 -2
- package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +1 -0
- package/dist/chains/sql_db/sql_db_prompt.js +19 -0
- 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 +40 -13
- package/dist/chat_models/base.d.ts +14 -7
- package/dist/chat_models/base.js +41 -14
- package/dist/chat_models/openai.cjs +20 -14
- package/dist/chat_models/openai.d.ts +2 -1
- package/dist/chat_models/openai.js +20 -14
- 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 +405 -0
- package/dist/client/langchainplus.d.ts +65 -0
- package/dist/client/langchainplus.js +398 -0
- package/dist/document_loaders/fs/unstructured.cjs +8 -0
- package/dist/document_loaders/fs/unstructured.d.ts +3 -0
- package/dist/document_loaders/fs/unstructured.js +8 -0
- package/dist/document_loaders/web/apify_dataset.cjs +64 -0
- package/dist/document_loaders/web/apify_dataset.d.ts +28 -0
- package/dist/document_loaders/web/apify_dataset.js +60 -0
- package/dist/embeddings/openai.cjs +2 -2
- package/dist/embeddings/openai.js +2 -2
- package/dist/llms/base.cjs +43 -17
- package/dist/llms/base.d.ts +16 -9
- package/dist/llms/base.js +44 -18
- 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 +13 -14
- package/dist/llms/openai-chat.d.ts +2 -1
- package/dist/llms/openai-chat.js +13 -14
- package/dist/llms/openai.cjs +15 -13
- package/dist/llms/openai.d.ts +4 -3
- package/dist/llms/openai.js +15 -13
- package/dist/llms/replicate.cjs +2 -2
- package/dist/llms/replicate.d.ts +1 -1
- package/dist/llms/replicate.js +2 -2
- package/dist/memory/base.cjs +9 -1
- package/dist/memory/base.d.ts +1 -0
- package/dist/memory/base.js +7 -0
- package/dist/memory/entity_memory.cjs +151 -0
- package/dist/memory/entity_memory.d.ts +35 -0
- package/dist/memory/entity_memory.js +147 -0
- package/dist/memory/index.cjs +5 -1
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +2 -0
- package/dist/memory/prompt.cjs +84 -1
- package/dist/memory/prompt.d.ts +6 -0
- package/dist/memory/prompt.js +83 -0
- package/dist/memory/stores/entity/in_memory.cjs +32 -0
- package/dist/memory/stores/entity/in_memory.d.ts +10 -0
- package/dist/memory/stores/entity/in_memory.js +28 -0
- package/dist/prompts/index.cjs +6 -1
- package/dist/prompts/index.d.ts +1 -0
- package/dist/prompts/index.js +1 -0
- package/dist/{chains/prompt_selector.d.ts → prompts/selectors/conditional.d.ts} +4 -4
- package/dist/retrievers/document_compressors/chain_extract.cjs +9 -11
- package/dist/retrievers/document_compressors/chain_extract.js +9 -11
- package/dist/schema/index.cjs +13 -1
- package/dist/schema/index.d.ts +19 -0
- package/dist/schema/index.js +11 -0
- package/dist/stores/message/dynamodb.cjs +8 -6
- package/dist/stores/message/dynamodb.js +8 -6
- package/dist/stores/message/redis.cjs +69 -0
- package/dist/stores/message/redis.d.ts +18 -0
- package/dist/stores/message/redis.js +65 -0
- package/dist/stores/message/utils.cjs +30 -15
- package/dist/stores/message/utils.d.ts +4 -2
- package/dist/stores/message/utils.js +28 -14
- package/dist/text_splitter.cjs +3 -23
- package/dist/text_splitter.d.ts +1 -3
- package/dist/text_splitter.js +3 -23
- package/dist/tools/webbrowser.cjs +5 -7
- package/dist/tools/webbrowser.js +3 -5
- 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/dist/util/env.cjs +39 -7
- package/dist/util/env.d.ts +19 -0
- package/dist/util/env.js +32 -6
- package/dist/util/sql_utils.cjs +18 -0
- package/dist/util/sql_utils.js +19 -1
- package/dist/util/tiktoken.cjs +26 -0
- package/dist/util/tiktoken.d.ts +9 -0
- package/dist/util/tiktoken.js +21 -0
- package/dist/vectorstores/redis.cjs +236 -0
- package/dist/vectorstores/redis.d.ts +80 -0
- package/dist/vectorstores/redis.js +232 -0
- package/document_loaders/web/apify_dataset.cjs +1 -0
- package/document_loaders/web/apify_dataset.d.ts +1 -0
- package/document_loaders/web/apify_dataset.js +1 -0
- package/package.json +41 -5
- package/stores/message/redis.cjs +1 -0
- package/stores/message/redis.d.ts +1 -0
- package/stores/message/redis.js +1 -0
- package/vectorstores/redis.cjs +1 -0
- package/vectorstores/redis.d.ts +1 -0
- package/vectorstores/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/dist/{chains/prompt_selector.cjs → prompts/selectors/conditional.cjs} +0 -0
- /package/dist/{chains/prompt_selector.js → prompts/selectors/conditional.js} +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
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) {
|
|
3
|
-
const tracer = new
|
|
4
|
+
const tracer = new LangChainTracerV1();
|
|
4
5
|
if (session) {
|
|
5
6
|
await tracer.loadSession(session);
|
|
6
7
|
}
|
|
@@ -9,3 +10,6 @@ export async function getTracingCallbackHandler(session) {
|
|
|
9
10
|
}
|
|
10
11
|
return tracer;
|
|
11
12
|
}
|
|
13
|
+
export async function getTracingV2CallbackHandler() {
|
|
14
|
+
return new LangChainTracer();
|
|
15
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
}
|
|
28
|
+
this.runMap.set(run.id, run);
|
|
29
|
+
}
|
|
30
|
+
async _endTrace(run) {
|
|
31
|
+
const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
|
|
32
|
+
if (parentRun) {
|
|
33
|
+
parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
await this.persistRun(run);
|
|
37
|
+
}
|
|
38
|
+
this.runMap.delete(run.id);
|
|
39
|
+
}
|
|
40
|
+
_getExecutionOrder(parentRunId) {
|
|
41
|
+
const parentRun = parentRunId !== undefined && this.runMap.get(parentRunId);
|
|
42
|
+
// If a run has no parent then execution order is 1
|
|
43
|
+
if (!parentRun) {
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
return parentRun.child_execution_order + 1;
|
|
47
|
+
}
|
|
48
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams) {
|
|
49
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
50
|
+
const run = {
|
|
51
|
+
id: runId,
|
|
52
|
+
name: llm.name,
|
|
53
|
+
parent_run_id: parentRunId,
|
|
54
|
+
start_time: Date.now(),
|
|
55
|
+
end_time: 0,
|
|
56
|
+
serialized: llm,
|
|
57
|
+
inputs: { prompts },
|
|
58
|
+
execution_order,
|
|
59
|
+
child_runs: [],
|
|
60
|
+
child_execution_order: execution_order,
|
|
61
|
+
run_type: "llm",
|
|
62
|
+
extra: extraParams,
|
|
63
|
+
};
|
|
64
|
+
this._startTrace(run);
|
|
65
|
+
await this.onLLMStart?.(run);
|
|
66
|
+
}
|
|
67
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams) {
|
|
68
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
69
|
+
const run = {
|
|
70
|
+
id: runId,
|
|
71
|
+
name: llm.name,
|
|
72
|
+
parent_run_id: parentRunId,
|
|
73
|
+
start_time: Date.now(),
|
|
74
|
+
end_time: 0,
|
|
75
|
+
serialized: llm,
|
|
76
|
+
inputs: { messages },
|
|
77
|
+
execution_order,
|
|
78
|
+
child_runs: [],
|
|
79
|
+
child_execution_order: execution_order,
|
|
80
|
+
run_type: "llm",
|
|
81
|
+
extra: extraParams,
|
|
82
|
+
};
|
|
83
|
+
this._startTrace(run);
|
|
84
|
+
await this.onLLMStart?.(run);
|
|
85
|
+
}
|
|
86
|
+
async handleLLMEnd(output, runId) {
|
|
87
|
+
const run = this.runMap.get(runId);
|
|
88
|
+
if (!run || run?.run_type !== "llm") {
|
|
89
|
+
throw new Error("No LLM run to end.");
|
|
90
|
+
}
|
|
91
|
+
run.end_time = Date.now();
|
|
92
|
+
run.outputs = output;
|
|
93
|
+
await this.onLLMEnd?.(run);
|
|
94
|
+
await this._endTrace(run);
|
|
95
|
+
}
|
|
96
|
+
async handleLLMError(error, runId) {
|
|
97
|
+
const run = this.runMap.get(runId);
|
|
98
|
+
if (!run || run?.run_type !== "llm") {
|
|
99
|
+
throw new Error("No LLM run to end.");
|
|
100
|
+
}
|
|
101
|
+
run.end_time = Date.now();
|
|
102
|
+
run.error = error.message;
|
|
103
|
+
await this.onLLMError?.(run);
|
|
104
|
+
await this._endTrace(run);
|
|
105
|
+
}
|
|
106
|
+
async handleChainStart(chain, inputs, runId, parentRunId) {
|
|
107
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
108
|
+
const run = {
|
|
109
|
+
id: runId,
|
|
110
|
+
name: chain.name,
|
|
111
|
+
parent_run_id: parentRunId,
|
|
112
|
+
start_time: Date.now(),
|
|
113
|
+
end_time: 0,
|
|
114
|
+
serialized: chain,
|
|
115
|
+
inputs,
|
|
116
|
+
execution_order,
|
|
117
|
+
child_execution_order: execution_order,
|
|
118
|
+
run_type: "chain",
|
|
119
|
+
child_runs: [],
|
|
120
|
+
};
|
|
121
|
+
this._startTrace(run);
|
|
122
|
+
await this.onChainStart?.(run);
|
|
123
|
+
}
|
|
124
|
+
async handleChainEnd(outputs, runId) {
|
|
125
|
+
const run = this.runMap.get(runId);
|
|
126
|
+
if (!run || run?.run_type !== "chain") {
|
|
127
|
+
throw new Error("No chain run to end.");
|
|
128
|
+
}
|
|
129
|
+
run.end_time = Date.now();
|
|
130
|
+
run.outputs = outputs;
|
|
131
|
+
await this.onChainEnd?.(run);
|
|
132
|
+
await this._endTrace(run);
|
|
133
|
+
}
|
|
134
|
+
async handleChainError(error, runId) {
|
|
135
|
+
const run = this.runMap.get(runId);
|
|
136
|
+
if (!run || run?.run_type !== "chain") {
|
|
137
|
+
throw new Error("No chain run to end.");
|
|
138
|
+
}
|
|
139
|
+
run.end_time = Date.now();
|
|
140
|
+
run.error = error.message;
|
|
141
|
+
await this.onChainError?.(run);
|
|
142
|
+
await this._endTrace(run);
|
|
143
|
+
}
|
|
144
|
+
async handleToolStart(tool, input, runId, parentRunId) {
|
|
145
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
146
|
+
const run = {
|
|
147
|
+
id: runId,
|
|
148
|
+
name: tool.name,
|
|
149
|
+
parent_run_id: parentRunId,
|
|
150
|
+
start_time: Date.now(),
|
|
151
|
+
end_time: 0,
|
|
152
|
+
serialized: tool,
|
|
153
|
+
inputs: { input },
|
|
154
|
+
execution_order,
|
|
155
|
+
child_execution_order: execution_order,
|
|
156
|
+
run_type: "tool",
|
|
157
|
+
child_runs: [],
|
|
158
|
+
};
|
|
159
|
+
this._startTrace(run);
|
|
160
|
+
await this.onToolStart?.(run);
|
|
161
|
+
}
|
|
162
|
+
async handleToolEnd(output, runId) {
|
|
163
|
+
const run = this.runMap.get(runId);
|
|
164
|
+
if (!run || run?.run_type !== "tool") {
|
|
165
|
+
throw new Error("No tool run to end");
|
|
166
|
+
}
|
|
167
|
+
run.end_time = Date.now();
|
|
168
|
+
run.outputs = { output };
|
|
169
|
+
await this.onToolEnd?.(run);
|
|
170
|
+
await this._endTrace(run);
|
|
171
|
+
}
|
|
172
|
+
async handleToolError(error, runId) {
|
|
173
|
+
const run = this.runMap.get(runId);
|
|
174
|
+
if (!run || run?.run_type !== "tool") {
|
|
175
|
+
throw new Error("No tool run to end");
|
|
176
|
+
}
|
|
177
|
+
run.end_time = Date.now();
|
|
178
|
+
run.error = error.message;
|
|
179
|
+
await this.onToolError?.(run);
|
|
180
|
+
await this._endTrace(run);
|
|
181
|
+
}
|
|
182
|
+
async handleAgentAction(action, runId) {
|
|
183
|
+
const run = this.runMap.get(runId);
|
|
184
|
+
if (!run || run?.run_type !== "chain") {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const agentRun = run;
|
|
188
|
+
agentRun.actions = agentRun.actions || [];
|
|
189
|
+
agentRun.actions.push(action);
|
|
190
|
+
await this.onAgentAction?.(run);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.BaseTracer = BaseTracer;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { AgentAction, BaseChatMessage, ChainValues, LLMResult, RunInputs, RunOutputs } from "../../schema/index.js";
|
|
2
|
+
import { BaseCallbackHandler } from "../base.js";
|
|
3
|
+
export type RunType = "llm" | "chain" | "tool";
|
|
4
|
+
export type Extra = Record<string, any>;
|
|
5
|
+
export interface BaseRun {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
start_time: number;
|
|
9
|
+
end_time: number;
|
|
10
|
+
extra?: Extra;
|
|
11
|
+
error?: string;
|
|
12
|
+
execution_order: number;
|
|
13
|
+
serialized: object;
|
|
14
|
+
inputs: RunInputs;
|
|
15
|
+
outputs?: RunOutputs;
|
|
16
|
+
reference_example_id?: string;
|
|
17
|
+
run_type: RunType;
|
|
18
|
+
}
|
|
19
|
+
export interface Run extends BaseRun {
|
|
20
|
+
child_runs: this[];
|
|
21
|
+
child_execution_order: number;
|
|
22
|
+
parent_run_id?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface AgentRun extends Run {
|
|
25
|
+
actions: AgentAction[];
|
|
26
|
+
}
|
|
27
|
+
export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
28
|
+
protected runMap: Map<string, Run>;
|
|
29
|
+
constructor();
|
|
30
|
+
copy(): this;
|
|
31
|
+
protected abstract persistRun(run: Run): Promise<void>;
|
|
32
|
+
protected _addChildRun(parentRun: Run, childRun: Run): void;
|
|
33
|
+
protected _startTrace(run: Run): void;
|
|
34
|
+
protected _endTrace(run: Run): Promise<void>;
|
|
35
|
+
protected _getExecutionOrder(parentRunId: string | undefined): number;
|
|
36
|
+
handleLLMStart(llm: {
|
|
37
|
+
name: string;
|
|
38
|
+
}, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>): Promise<void>;
|
|
39
|
+
handleChatModelStart(llm: {
|
|
40
|
+
name: string;
|
|
41
|
+
}, messages: BaseChatMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>): Promise<void>;
|
|
42
|
+
handleLLMEnd(output: LLMResult, runId: string): Promise<void>;
|
|
43
|
+
handleLLMError(error: Error, runId: string): Promise<void>;
|
|
44
|
+
handleChainStart(chain: {
|
|
45
|
+
name: string;
|
|
46
|
+
}, inputs: ChainValues, runId: string, parentRunId?: string): Promise<void>;
|
|
47
|
+
handleChainEnd(outputs: ChainValues, runId: string): Promise<void>;
|
|
48
|
+
handleChainError(error: Error, runId: string): Promise<void>;
|
|
49
|
+
handleToolStart(tool: {
|
|
50
|
+
name: string;
|
|
51
|
+
}, input: string, runId: string, parentRunId?: string): Promise<void>;
|
|
52
|
+
handleToolEnd(output: string, runId: string): Promise<void>;
|
|
53
|
+
handleToolError(error: Error, runId: string): Promise<void>;
|
|
54
|
+
handleAgentAction(action: AgentAction, runId: string): Promise<void>;
|
|
55
|
+
onLLMStart?(run: Run): void | Promise<void>;
|
|
56
|
+
onLLMEnd?(run: Run): void | Promise<void>;
|
|
57
|
+
onLLMError?(run: Run): void | Promise<void>;
|
|
58
|
+
onChainStart?(run: Run): void | Promise<void>;
|
|
59
|
+
onChainEnd?(run: Run): void | Promise<void>;
|
|
60
|
+
onChainError?(run: Run): void | Promise<void>;
|
|
61
|
+
onToolStart?(run: Run): void | Promise<void>;
|
|
62
|
+
onToolEnd?(run: Run): void | Promise<void>;
|
|
63
|
+
onToolError?(run: Run): void | Promise<void>;
|
|
64
|
+
onAgentAction?(run: Run): void | Promise<void>;
|
|
65
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { BaseCallbackHandler } from "../base.js";
|
|
2
|
+
export class BaseTracer extends BaseCallbackHandler {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
Object.defineProperty(this, "runMap", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: new Map()
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
copy() {
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
_addChildRun(parentRun, childRun) {
|
|
16
|
+
parentRun.child_runs.push(childRun);
|
|
17
|
+
}
|
|
18
|
+
_startTrace(run) {
|
|
19
|
+
if (run.parent_run_id !== undefined) {
|
|
20
|
+
const parentRun = this.runMap.get(run.parent_run_id);
|
|
21
|
+
if (parentRun) {
|
|
22
|
+
this._addChildRun(parentRun, run);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
this.runMap.set(run.id, run);
|
|
26
|
+
}
|
|
27
|
+
async _endTrace(run) {
|
|
28
|
+
const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
|
|
29
|
+
if (parentRun) {
|
|
30
|
+
parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
await this.persistRun(run);
|
|
34
|
+
}
|
|
35
|
+
this.runMap.delete(run.id);
|
|
36
|
+
}
|
|
37
|
+
_getExecutionOrder(parentRunId) {
|
|
38
|
+
const parentRun = parentRunId !== undefined && this.runMap.get(parentRunId);
|
|
39
|
+
// If a run has no parent then execution order is 1
|
|
40
|
+
if (!parentRun) {
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
return parentRun.child_execution_order + 1;
|
|
44
|
+
}
|
|
45
|
+
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams) {
|
|
46
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
47
|
+
const run = {
|
|
48
|
+
id: runId,
|
|
49
|
+
name: llm.name,
|
|
50
|
+
parent_run_id: parentRunId,
|
|
51
|
+
start_time: Date.now(),
|
|
52
|
+
end_time: 0,
|
|
53
|
+
serialized: llm,
|
|
54
|
+
inputs: { prompts },
|
|
55
|
+
execution_order,
|
|
56
|
+
child_runs: [],
|
|
57
|
+
child_execution_order: execution_order,
|
|
58
|
+
run_type: "llm",
|
|
59
|
+
extra: extraParams,
|
|
60
|
+
};
|
|
61
|
+
this._startTrace(run);
|
|
62
|
+
await this.onLLMStart?.(run);
|
|
63
|
+
}
|
|
64
|
+
async handleChatModelStart(llm, messages, runId, parentRunId, extraParams) {
|
|
65
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
66
|
+
const run = {
|
|
67
|
+
id: runId,
|
|
68
|
+
name: llm.name,
|
|
69
|
+
parent_run_id: parentRunId,
|
|
70
|
+
start_time: Date.now(),
|
|
71
|
+
end_time: 0,
|
|
72
|
+
serialized: llm,
|
|
73
|
+
inputs: { messages },
|
|
74
|
+
execution_order,
|
|
75
|
+
child_runs: [],
|
|
76
|
+
child_execution_order: execution_order,
|
|
77
|
+
run_type: "llm",
|
|
78
|
+
extra: extraParams,
|
|
79
|
+
};
|
|
80
|
+
this._startTrace(run);
|
|
81
|
+
await this.onLLMStart?.(run);
|
|
82
|
+
}
|
|
83
|
+
async handleLLMEnd(output, runId) {
|
|
84
|
+
const run = this.runMap.get(runId);
|
|
85
|
+
if (!run || run?.run_type !== "llm") {
|
|
86
|
+
throw new Error("No LLM run to end.");
|
|
87
|
+
}
|
|
88
|
+
run.end_time = Date.now();
|
|
89
|
+
run.outputs = output;
|
|
90
|
+
await this.onLLMEnd?.(run);
|
|
91
|
+
await this._endTrace(run);
|
|
92
|
+
}
|
|
93
|
+
async handleLLMError(error, runId) {
|
|
94
|
+
const run = this.runMap.get(runId);
|
|
95
|
+
if (!run || run?.run_type !== "llm") {
|
|
96
|
+
throw new Error("No LLM run to end.");
|
|
97
|
+
}
|
|
98
|
+
run.end_time = Date.now();
|
|
99
|
+
run.error = error.message;
|
|
100
|
+
await this.onLLMError?.(run);
|
|
101
|
+
await this._endTrace(run);
|
|
102
|
+
}
|
|
103
|
+
async handleChainStart(chain, inputs, runId, parentRunId) {
|
|
104
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
105
|
+
const run = {
|
|
106
|
+
id: runId,
|
|
107
|
+
name: chain.name,
|
|
108
|
+
parent_run_id: parentRunId,
|
|
109
|
+
start_time: Date.now(),
|
|
110
|
+
end_time: 0,
|
|
111
|
+
serialized: chain,
|
|
112
|
+
inputs,
|
|
113
|
+
execution_order,
|
|
114
|
+
child_execution_order: execution_order,
|
|
115
|
+
run_type: "chain",
|
|
116
|
+
child_runs: [],
|
|
117
|
+
};
|
|
118
|
+
this._startTrace(run);
|
|
119
|
+
await this.onChainStart?.(run);
|
|
120
|
+
}
|
|
121
|
+
async handleChainEnd(outputs, runId) {
|
|
122
|
+
const run = this.runMap.get(runId);
|
|
123
|
+
if (!run || run?.run_type !== "chain") {
|
|
124
|
+
throw new Error("No chain run to end.");
|
|
125
|
+
}
|
|
126
|
+
run.end_time = Date.now();
|
|
127
|
+
run.outputs = outputs;
|
|
128
|
+
await this.onChainEnd?.(run);
|
|
129
|
+
await this._endTrace(run);
|
|
130
|
+
}
|
|
131
|
+
async handleChainError(error, runId) {
|
|
132
|
+
const run = this.runMap.get(runId);
|
|
133
|
+
if (!run || run?.run_type !== "chain") {
|
|
134
|
+
throw new Error("No chain run to end.");
|
|
135
|
+
}
|
|
136
|
+
run.end_time = Date.now();
|
|
137
|
+
run.error = error.message;
|
|
138
|
+
await this.onChainError?.(run);
|
|
139
|
+
await this._endTrace(run);
|
|
140
|
+
}
|
|
141
|
+
async handleToolStart(tool, input, runId, parentRunId) {
|
|
142
|
+
const execution_order = this._getExecutionOrder(parentRunId);
|
|
143
|
+
const run = {
|
|
144
|
+
id: runId,
|
|
145
|
+
name: tool.name,
|
|
146
|
+
parent_run_id: parentRunId,
|
|
147
|
+
start_time: Date.now(),
|
|
148
|
+
end_time: 0,
|
|
149
|
+
serialized: tool,
|
|
150
|
+
inputs: { input },
|
|
151
|
+
execution_order,
|
|
152
|
+
child_execution_order: execution_order,
|
|
153
|
+
run_type: "tool",
|
|
154
|
+
child_runs: [],
|
|
155
|
+
};
|
|
156
|
+
this._startTrace(run);
|
|
157
|
+
await this.onToolStart?.(run);
|
|
158
|
+
}
|
|
159
|
+
async handleToolEnd(output, runId) {
|
|
160
|
+
const run = this.runMap.get(runId);
|
|
161
|
+
if (!run || run?.run_type !== "tool") {
|
|
162
|
+
throw new Error("No tool run to end");
|
|
163
|
+
}
|
|
164
|
+
run.end_time = Date.now();
|
|
165
|
+
run.outputs = { output };
|
|
166
|
+
await this.onToolEnd?.(run);
|
|
167
|
+
await this._endTrace(run);
|
|
168
|
+
}
|
|
169
|
+
async handleToolError(error, runId) {
|
|
170
|
+
const run = this.runMap.get(runId);
|
|
171
|
+
if (!run || run?.run_type !== "tool") {
|
|
172
|
+
throw new Error("No tool run to end");
|
|
173
|
+
}
|
|
174
|
+
run.end_time = Date.now();
|
|
175
|
+
run.error = error.message;
|
|
176
|
+
await this.onToolError?.(run);
|
|
177
|
+
await this._endTrace(run);
|
|
178
|
+
}
|
|
179
|
+
async handleAgentAction(action, runId) {
|
|
180
|
+
const run = this.runMap.get(runId);
|
|
181
|
+
if (!run || run?.run_type !== "chain") {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const agentRun = run;
|
|
185
|
+
agentRun.actions = agentRun.actions || [];
|
|
186
|
+
agentRun.actions.push(action);
|
|
187
|
+
await this.onAgentAction?.(run);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LangChainTracer = void 0;
|
|
4
|
+
const env_js_1 = require("../../util/env.cjs");
|
|
5
|
+
const tracer_js_1 = require("./tracer.cjs");
|
|
6
|
+
class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
7
|
+
constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
|
|
8
|
+
super();
|
|
9
|
+
Object.defineProperty(this, "name", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: "langchain_tracer"
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "endpoint", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: (typeof process !== "undefined"
|
|
20
|
+
? // eslint-disable-next-line no-process-env
|
|
21
|
+
process.env?.LANGCHAIN_ENDPOINT
|
|
22
|
+
: undefined) || "http://localhost:8000"
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "headers", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "sessionName", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "sessionExtra", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "session", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "exampleId", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "tenantId", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
// eslint-disable-next-line no-process-env
|
|
63
|
+
if (typeof process !== "undefined" && process.env?.LANGCHAIN_API_KEY) {
|
|
64
|
+
// eslint-disable-next-line no-process-env
|
|
65
|
+
this.headers["x-api-key"] = process.env?.LANGCHAIN_API_KEY;
|
|
66
|
+
}
|
|
67
|
+
this.tenantId =
|
|
68
|
+
tenantId ??
|
|
69
|
+
(typeof process !== "undefined"
|
|
70
|
+
? // eslint-disable-next-line no-process-env
|
|
71
|
+
process.env?.LANGCHAIN_TENANT_ID
|
|
72
|
+
: undefined);
|
|
73
|
+
this.sessionName =
|
|
74
|
+
sessionName ??
|
|
75
|
+
(typeof process !== "undefined"
|
|
76
|
+
? // eslint-disable-next-line no-process-env
|
|
77
|
+
process.env?.LANGCHAIN_SESSION
|
|
78
|
+
: undefined) ??
|
|
79
|
+
"default";
|
|
80
|
+
this.sessionExtra = sessionExtra;
|
|
81
|
+
this.exampleId = exampleId;
|
|
82
|
+
}
|
|
83
|
+
async ensureSession() {
|
|
84
|
+
if (this.session) {
|
|
85
|
+
return this.session;
|
|
86
|
+
}
|
|
87
|
+
const tenantId = await this.ensureTenantId();
|
|
88
|
+
const endpoint = `${this.endpoint}/sessions?upsert=true`;
|
|
89
|
+
const res = await fetch(endpoint, {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: this.headers,
|
|
92
|
+
body: JSON.stringify({
|
|
93
|
+
name: this.sessionName,
|
|
94
|
+
tenant_id: tenantId,
|
|
95
|
+
extra: this.sessionExtra,
|
|
96
|
+
}),
|
|
97
|
+
});
|
|
98
|
+
if (!res.ok) {
|
|
99
|
+
const body = await res.text();
|
|
100
|
+
throw new Error(`Failed to create session: ${res.status} ${res.statusText} ${body}`);
|
|
101
|
+
}
|
|
102
|
+
const session = await res.json();
|
|
103
|
+
this.session = session;
|
|
104
|
+
return session;
|
|
105
|
+
}
|
|
106
|
+
async ensureTenantId() {
|
|
107
|
+
if (this.tenantId) {
|
|
108
|
+
return this.tenantId;
|
|
109
|
+
}
|
|
110
|
+
const endpoint = `${this.endpoint}/tenants`;
|
|
111
|
+
const response = await fetch(endpoint, {
|
|
112
|
+
method: "GET",
|
|
113
|
+
headers: this.headers,
|
|
114
|
+
});
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
const body = await response.text();
|
|
117
|
+
throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText} ${body}`);
|
|
118
|
+
}
|
|
119
|
+
const tenants = await response.json();
|
|
120
|
+
if (!tenants || tenants.length === 0) {
|
|
121
|
+
throw new Error(`No tenants found for endpoint ${endpoint}`);
|
|
122
|
+
}
|
|
123
|
+
const tenantId = tenants[0].id;
|
|
124
|
+
this.tenantId = tenantId;
|
|
125
|
+
return tenantId;
|
|
126
|
+
}
|
|
127
|
+
async _convertToCreate(run, example_id = undefined) {
|
|
128
|
+
const session = await this.ensureSession();
|
|
129
|
+
const runExtra = run.extra ?? {};
|
|
130
|
+
runExtra.runtime = await (0, env_js_1.getRuntimeEnvironment)();
|
|
131
|
+
const persistedRun = {
|
|
132
|
+
id: run.id,
|
|
133
|
+
name: run.name,
|
|
134
|
+
start_time: run.start_time,
|
|
135
|
+
end_time: run.end_time,
|
|
136
|
+
run_type: run.run_type,
|
|
137
|
+
reference_example_id: example_id,
|
|
138
|
+
extra: runExtra,
|
|
139
|
+
execution_order: run.execution_order,
|
|
140
|
+
serialized: run.serialized,
|
|
141
|
+
error: run.error,
|
|
142
|
+
inputs: run.inputs,
|
|
143
|
+
outputs: run.outputs ?? {},
|
|
144
|
+
session_id: session.id,
|
|
145
|
+
child_runs: await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run))),
|
|
146
|
+
};
|
|
147
|
+
return persistedRun;
|
|
148
|
+
}
|
|
149
|
+
async persistRun(run) {
|
|
150
|
+
const persistedRun = await this._convertToCreate(run, this.exampleId);
|
|
151
|
+
const endpoint = `${this.endpoint}/runs`;
|
|
152
|
+
const response = await fetch(endpoint, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: this.headers,
|
|
155
|
+
body: JSON.stringify(persistedRun),
|
|
156
|
+
});
|
|
157
|
+
if (!response.ok) {
|
|
158
|
+
const body = await response.text();
|
|
159
|
+
throw new Error(`Failed to persist run: ${response.status} ${response.statusText} ${body}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
exports.LangChainTracer = LangChainTracer;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BaseTracer, Run, BaseRun } from "./tracer.js";
|
|
2
|
+
export interface RunCreate extends BaseRun {
|
|
3
|
+
child_runs: this[];
|
|
4
|
+
session_id: string;
|
|
5
|
+
}
|
|
6
|
+
export interface BaseTracerSession {
|
|
7
|
+
start_time: number;
|
|
8
|
+
name?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface BaseTracerSessionV2 extends BaseTracerSession {
|
|
11
|
+
tenant_id: string;
|
|
12
|
+
}
|
|
13
|
+
export interface TracerSessionCreateV2 extends BaseTracerSessionV2 {
|
|
14
|
+
id?: string;
|
|
15
|
+
}
|
|
16
|
+
export interface TracerSession extends BaseTracerSessionV2 {
|
|
17
|
+
id: string;
|
|
18
|
+
}
|
|
19
|
+
export interface LangChainTracerFields {
|
|
20
|
+
exampleId?: string;
|
|
21
|
+
tenantId?: string;
|
|
22
|
+
sessionName?: string;
|
|
23
|
+
sessionExtra?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
|
|
26
|
+
name: string;
|
|
27
|
+
protected endpoint: string;
|
|
28
|
+
protected headers: Record<string, string>;
|
|
29
|
+
sessionName: string;
|
|
30
|
+
sessionExtra?: LangChainTracerFields["sessionExtra"];
|
|
31
|
+
protected session: TracerSession;
|
|
32
|
+
exampleId?: string;
|
|
33
|
+
tenantId?: string;
|
|
34
|
+
constructor({ exampleId, tenantId, sessionName, sessionExtra, }?: LangChainTracerFields);
|
|
35
|
+
protected ensureSession(): Promise<TracerSession>;
|
|
36
|
+
protected ensureTenantId(): Promise<string>;
|
|
37
|
+
private _convertToCreate;
|
|
38
|
+
protected persistRun(run: Run): Promise<void>;
|
|
39
|
+
}
|