@langchain/core 0.1.7 → 0.1.8
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/language_models/base.d.ts +9 -0
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/runnables/branch.cjs +1 -1
- package/dist/runnables/branch.d.ts +1 -1
- package/dist/runnables/branch.js +1 -1
- package/dist/runnables/passthrough.cjs +1 -3
- package/dist/runnables/passthrough.d.ts +1 -1
- package/dist/runnables/passthrough.js +1 -3
- package/dist/tracers/initialize.cjs +22 -1
- package/dist/tracers/initialize.d.ts +11 -0
- package/dist/tracers/initialize.js +20 -0
- package/dist/tracers/tracer_langchain_v1.cjs +200 -0
- package/dist/tracers/tracer_langchain_v1.d.ts +59 -0
- package/dist/tracers/tracer_langchain_v1.js +196 -0
- package/package.json +9 -1
- package/tracers/tracer_langchain_v1.cjs +1 -0
- package/tracers/tracer_langchain_v1.d.ts +1 -0
- package/tracers/tracer_langchain_v1.js +1 -0
|
@@ -163,4 +163,13 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
163
163
|
*/
|
|
164
164
|
static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;
|
|
165
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Shared interface for token usage
|
|
168
|
+
* return type from LLM calls.
|
|
169
|
+
*/
|
|
170
|
+
export interface TokenUsage {
|
|
171
|
+
completionTokens?: number;
|
|
172
|
+
promptTokens?: number;
|
|
173
|
+
totalTokens?: number;
|
|
174
|
+
}
|
|
166
175
|
export {};
|
package/dist/load/import_map.cjs
CHANGED
|
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
return result;
|
|
25
25
|
};
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
-
exports.vectorstores = exports.utils__types = exports.utils__tiktoken = exports.utils__testing = exports.utils__stream = exports.utils__math = exports.utils__json_schema = exports.utils__json_patch = exports.utils__hash = exports.utils__env = exports.utils__chunk_array = exports.utils__async_caller = exports.tracers__tracer_langchain = exports.tracers__run_collector = exports.tracers__log_stream = exports.tracers__initialize = exports.tracers__console = exports.tracers__base = exports.tools = exports.stores = exports.retrievers = exports.runnables = exports.prompt_values = exports.prompts = exports.outputs = exports.output_parsers = exports.messages = exports.memory = exports.load__serializable = exports.language_models__llms = exports.language_models__chat_models = exports.language_models__base = exports.example_selectors = exports.embeddings = exports.documents = exports.chat_history = exports.callbacks__promises = exports.callbacks__manager = exports.callbacks__base = exports.caches = exports.agents = void 0;
|
|
27
|
+
exports.vectorstores = exports.utils__types = exports.utils__tiktoken = exports.utils__testing = exports.utils__stream = exports.utils__math = exports.utils__json_schema = exports.utils__json_patch = exports.utils__hash = exports.utils__env = exports.utils__chunk_array = exports.utils__async_caller = exports.tracers__tracer_langchain_v1 = exports.tracers__tracer_langchain = exports.tracers__run_collector = exports.tracers__log_stream = exports.tracers__initialize = exports.tracers__console = exports.tracers__base = exports.tools = exports.stores = exports.retrievers = exports.runnables = exports.prompt_values = exports.prompts = exports.outputs = exports.output_parsers = exports.messages = exports.memory = exports.load__serializable = exports.language_models__llms = exports.language_models__chat_models = exports.language_models__base = exports.example_selectors = exports.embeddings = exports.documents = exports.chat_history = exports.callbacks__promises = exports.callbacks__manager = exports.callbacks__base = exports.caches = exports.agents = void 0;
|
|
28
28
|
exports.agents = __importStar(require("../agents.cjs"));
|
|
29
29
|
exports.caches = __importStar(require("../caches.cjs"));
|
|
30
30
|
exports.callbacks__base = __importStar(require("../callbacks/base.cjs"));
|
|
@@ -54,6 +54,7 @@ exports.tracers__initialize = __importStar(require("../tracers/initialize.cjs"))
|
|
|
54
54
|
exports.tracers__log_stream = __importStar(require("../tracers/log_stream.cjs"));
|
|
55
55
|
exports.tracers__run_collector = __importStar(require("../tracers/run_collector.cjs"));
|
|
56
56
|
exports.tracers__tracer_langchain = __importStar(require("../tracers/tracer_langchain.cjs"));
|
|
57
|
+
exports.tracers__tracer_langchain_v1 = __importStar(require("../tracers/tracer_langchain_v1.cjs"));
|
|
57
58
|
exports.utils__async_caller = __importStar(require("../utils/async_caller.cjs"));
|
|
58
59
|
exports.utils__chunk_array = __importStar(require("../utils/chunk_array.cjs"));
|
|
59
60
|
exports.utils__env = __importStar(require("../utils/env.cjs"));
|
|
@@ -27,6 +27,7 @@ export * as tracers__initialize from "../tracers/initialize.js";
|
|
|
27
27
|
export * as tracers__log_stream from "../tracers/log_stream.js";
|
|
28
28
|
export * as tracers__run_collector from "../tracers/run_collector.js";
|
|
29
29
|
export * as tracers__tracer_langchain from "../tracers/tracer_langchain.js";
|
|
30
|
+
export * as tracers__tracer_langchain_v1 from "../tracers/tracer_langchain_v1.js";
|
|
30
31
|
export * as utils__async_caller from "../utils/async_caller.js";
|
|
31
32
|
export * as utils__chunk_array from "../utils/chunk_array.js";
|
|
32
33
|
export * as utils__env from "../utils/env.js";
|
package/dist/load/import_map.js
CHANGED
|
@@ -28,6 +28,7 @@ export * as tracers__initialize from "../tracers/initialize.js";
|
|
|
28
28
|
export * as tracers__log_stream from "../tracers/log_stream.js";
|
|
29
29
|
export * as tracers__run_collector from "../tracers/run_collector.js";
|
|
30
30
|
export * as tracers__tracer_langchain from "../tracers/tracer_langchain.js";
|
|
31
|
+
export * as tracers__tracer_langchain_v1 from "../tracers/tracer_langchain_v1.js";
|
|
31
32
|
export * as utils__async_caller from "../utils/async_caller.js";
|
|
32
33
|
export * as utils__chunk_array from "../utils/chunk_array.js";
|
|
33
34
|
export * as utils__env from "../utils/env.js";
|
|
@@ -84,7 +84,7 @@ class RunnableBranch extends base_js_1.Runnable {
|
|
|
84
84
|
*
|
|
85
85
|
* @example
|
|
86
86
|
* ```ts
|
|
87
|
-
* import { RunnableBranch } from "langchain/
|
|
87
|
+
* import { RunnableBranch } from "@langchain/core/runnables";
|
|
88
88
|
*
|
|
89
89
|
* const branch = RunnableBranch.from([
|
|
90
90
|
* [(x: number) => x > 0, (x: number) => x + 1],
|
|
@@ -73,7 +73,7 @@ export declare class RunnableBranch<RunInput = any, RunOutput = any> extends Run
|
|
|
73
73
|
*
|
|
74
74
|
* @example
|
|
75
75
|
* ```ts
|
|
76
|
-
* import { RunnableBranch } from "langchain/
|
|
76
|
+
* import { RunnableBranch } from "@langchain/core/runnables";
|
|
77
77
|
*
|
|
78
78
|
* const branch = RunnableBranch.from([
|
|
79
79
|
* [(x: number) => x > 0, (x: number) => x + 1],
|
package/dist/runnables/branch.js
CHANGED
|
@@ -81,7 +81,7 @@ export class RunnableBranch extends Runnable {
|
|
|
81
81
|
*
|
|
82
82
|
* @example
|
|
83
83
|
* ```ts
|
|
84
|
-
* import { RunnableBranch } from "langchain/
|
|
84
|
+
* import { RunnableBranch } from "@langchain/core/runnables";
|
|
85
85
|
*
|
|
86
86
|
* const branch = RunnableBranch.from([
|
|
87
87
|
* [(x: number) => x > 0, (x: number) => x + 1],
|
|
@@ -81,9 +81,7 @@ class RunnablePassthrough extends base_js_1.Runnable {
|
|
|
81
81
|
* });
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
|
-
static assign(
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
|
-
mapping) {
|
|
84
|
+
static assign(mapping) {
|
|
87
85
|
return new base_js_1.RunnableAssign(new base_js_1.RunnableMap({ steps: mapping }));
|
|
88
86
|
}
|
|
89
87
|
}
|
|
@@ -60,5 +60,5 @@ export declare class RunnablePassthrough<RunInput> extends Runnable<RunInput, Ru
|
|
|
60
60
|
* });
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
|
-
static assign
|
|
63
|
+
static assign<RunInput extends Record<string, unknown>, RunOutput extends Record<string, unknown>>(mapping: RunnableMapLike<RunInput, RunOutput>): RunnableAssign<RunInput, RunInput & RunOutput>;
|
|
64
64
|
}
|
|
@@ -78,9 +78,7 @@ export class RunnablePassthrough extends Runnable {
|
|
|
78
78
|
* });
|
|
79
79
|
* ```
|
|
80
80
|
*/
|
|
81
|
-
static assign(
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
-
mapping) {
|
|
81
|
+
static assign(mapping) {
|
|
84
82
|
return new RunnableAssign(new RunnableMap({ steps: mapping }));
|
|
85
83
|
}
|
|
86
84
|
}
|
|
@@ -1,7 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTracingV2CallbackHandler = void 0;
|
|
3
|
+
exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = void 0;
|
|
4
4
|
const tracer_langchain_js_1 = require("./tracer_langchain.cjs");
|
|
5
|
+
const tracer_langchain_v1_js_1 = require("./tracer_langchain_v1.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated Use the V2 handler instead.
|
|
8
|
+
*
|
|
9
|
+
* Function that returns an instance of `LangChainTracerV1`. If a session
|
|
10
|
+
* is provided, it loads that session into the tracer; otherwise, it loads
|
|
11
|
+
* a default session.
|
|
12
|
+
* @param session Optional session to load into the tracer.
|
|
13
|
+
* @returns An instance of `LangChainTracerV1`.
|
|
14
|
+
*/
|
|
15
|
+
async function getTracingCallbackHandler(session) {
|
|
16
|
+
const tracer = new tracer_langchain_v1_js_1.LangChainTracerV1();
|
|
17
|
+
if (session) {
|
|
18
|
+
await tracer.loadSession(session);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
await tracer.loadDefaultSession();
|
|
22
|
+
}
|
|
23
|
+
return tracer;
|
|
24
|
+
}
|
|
25
|
+
exports.getTracingCallbackHandler = getTracingCallbackHandler;
|
|
5
26
|
/**
|
|
6
27
|
* Function that returns an instance of `LangChainTracer`. It does not
|
|
7
28
|
* load any session data.
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { LangChainTracer } from "./tracer_langchain.js";
|
|
2
|
+
import { LangChainTracerV1 } from "./tracer_langchain_v1.js";
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use the V2 handler instead.
|
|
5
|
+
*
|
|
6
|
+
* Function that returns an instance of `LangChainTracerV1`. If a session
|
|
7
|
+
* is provided, it loads that session into the tracer; otherwise, it loads
|
|
8
|
+
* a default session.
|
|
9
|
+
* @param session Optional session to load into the tracer.
|
|
10
|
+
* @returns An instance of `LangChainTracerV1`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getTracingCallbackHandler(session?: string): Promise<LangChainTracerV1>;
|
|
2
13
|
/**
|
|
3
14
|
* Function that returns an instance of `LangChainTracer`. It does not
|
|
4
15
|
* load any session data.
|
|
@@ -1,4 +1,24 @@
|
|
|
1
1
|
import { LangChainTracer } from "./tracer_langchain.js";
|
|
2
|
+
import { LangChainTracerV1 } from "./tracer_langchain_v1.js";
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use the V2 handler instead.
|
|
5
|
+
*
|
|
6
|
+
* Function that returns an instance of `LangChainTracerV1`. If a session
|
|
7
|
+
* is provided, it loads that session into the tracer; otherwise, it loads
|
|
8
|
+
* a default session.
|
|
9
|
+
* @param session Optional session to load into the tracer.
|
|
10
|
+
* @returns An instance of `LangChainTracerV1`.
|
|
11
|
+
*/
|
|
12
|
+
export async function getTracingCallbackHandler(session) {
|
|
13
|
+
const tracer = new LangChainTracerV1();
|
|
14
|
+
if (session) {
|
|
15
|
+
await tracer.loadSession(session);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
await tracer.loadDefaultSession();
|
|
19
|
+
}
|
|
20
|
+
return tracer;
|
|
21
|
+
}
|
|
2
22
|
/**
|
|
3
23
|
* Function that returns an instance of `LangChainTracer`. It does not
|
|
4
24
|
* load any session data.
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LangChainTracerV1 = void 0;
|
|
4
|
+
const index_js_1 = require("../messages/index.cjs");
|
|
5
|
+
const env_js_1 = require("../utils/env.cjs");
|
|
6
|
+
const base_js_1 = require("./base.cjs");
|
|
7
|
+
/** @deprecated Use LangChainTracer instead. */
|
|
8
|
+
class LangChainTracerV1 extends base_js_1.BaseTracer {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
Object.defineProperty(this, "name", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: "langchain_tracer"
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(this, "endpoint", {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
configurable: true,
|
|
20
|
+
writable: true,
|
|
21
|
+
value: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(this, "headers", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: {
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "session", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
const apiKey = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
|
|
38
|
+
if (apiKey) {
|
|
39
|
+
this.headers["x-api-key"] = apiKey;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async newSession(sessionName) {
|
|
43
|
+
const sessionCreate = {
|
|
44
|
+
start_time: Date.now(),
|
|
45
|
+
name: sessionName,
|
|
46
|
+
};
|
|
47
|
+
const session = await this.persistSession(sessionCreate);
|
|
48
|
+
this.session = session;
|
|
49
|
+
return session;
|
|
50
|
+
}
|
|
51
|
+
async loadSession(sessionName) {
|
|
52
|
+
const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
|
|
53
|
+
return this._handleSessionResponse(endpoint);
|
|
54
|
+
}
|
|
55
|
+
async loadDefaultSession() {
|
|
56
|
+
const endpoint = `${this.endpoint}/sessions?name=default`;
|
|
57
|
+
return this._handleSessionResponse(endpoint);
|
|
58
|
+
}
|
|
59
|
+
async convertV2RunToRun(run) {
|
|
60
|
+
const session = this.session ?? (await this.loadDefaultSession());
|
|
61
|
+
const serialized = run.serialized;
|
|
62
|
+
let runResult;
|
|
63
|
+
if (run.run_type === "llm") {
|
|
64
|
+
const prompts = run.inputs.prompts
|
|
65
|
+
? run.inputs.prompts
|
|
66
|
+
: run.inputs.messages.map((x) => (0, index_js_1.getBufferString)(x));
|
|
67
|
+
const llmRun = {
|
|
68
|
+
uuid: run.id,
|
|
69
|
+
start_time: run.start_time,
|
|
70
|
+
end_time: run.end_time,
|
|
71
|
+
execution_order: run.execution_order,
|
|
72
|
+
child_execution_order: run.child_execution_order,
|
|
73
|
+
serialized,
|
|
74
|
+
type: run.run_type,
|
|
75
|
+
session_id: session.id,
|
|
76
|
+
prompts,
|
|
77
|
+
response: run.outputs,
|
|
78
|
+
};
|
|
79
|
+
runResult = llmRun;
|
|
80
|
+
}
|
|
81
|
+
else if (run.run_type === "chain") {
|
|
82
|
+
const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
|
|
83
|
+
const chainRun = {
|
|
84
|
+
uuid: run.id,
|
|
85
|
+
start_time: run.start_time,
|
|
86
|
+
end_time: run.end_time,
|
|
87
|
+
execution_order: run.execution_order,
|
|
88
|
+
child_execution_order: run.child_execution_order,
|
|
89
|
+
serialized,
|
|
90
|
+
type: run.run_type,
|
|
91
|
+
session_id: session.id,
|
|
92
|
+
inputs: run.inputs,
|
|
93
|
+
outputs: run.outputs,
|
|
94
|
+
child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
|
|
95
|
+
child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
|
|
96
|
+
child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
|
|
97
|
+
};
|
|
98
|
+
runResult = chainRun;
|
|
99
|
+
}
|
|
100
|
+
else if (run.run_type === "tool") {
|
|
101
|
+
const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
|
|
102
|
+
const toolRun = {
|
|
103
|
+
uuid: run.id,
|
|
104
|
+
start_time: run.start_time,
|
|
105
|
+
end_time: run.end_time,
|
|
106
|
+
execution_order: run.execution_order,
|
|
107
|
+
child_execution_order: run.child_execution_order,
|
|
108
|
+
serialized,
|
|
109
|
+
type: run.run_type,
|
|
110
|
+
session_id: session.id,
|
|
111
|
+
tool_input: run.inputs.input,
|
|
112
|
+
output: run.outputs?.output,
|
|
113
|
+
action: JSON.stringify(serialized),
|
|
114
|
+
child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
|
|
115
|
+
child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
|
|
116
|
+
child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
|
|
117
|
+
};
|
|
118
|
+
runResult = toolRun;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
throw new Error(`Unknown run type: ${run.run_type}`);
|
|
122
|
+
}
|
|
123
|
+
return runResult;
|
|
124
|
+
}
|
|
125
|
+
async persistRun(run) {
|
|
126
|
+
let endpoint;
|
|
127
|
+
let v1Run;
|
|
128
|
+
if (run.run_type !== undefined) {
|
|
129
|
+
v1Run = await this.convertV2RunToRun(run);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
v1Run = run;
|
|
133
|
+
}
|
|
134
|
+
if (v1Run.type === "llm") {
|
|
135
|
+
endpoint = `${this.endpoint}/llm-runs`;
|
|
136
|
+
}
|
|
137
|
+
else if (v1Run.type === "chain") {
|
|
138
|
+
endpoint = `${this.endpoint}/chain-runs`;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
endpoint = `${this.endpoint}/tool-runs`;
|
|
142
|
+
}
|
|
143
|
+
const response = await fetch(endpoint, {
|
|
144
|
+
method: "POST",
|
|
145
|
+
headers: this.headers,
|
|
146
|
+
body: JSON.stringify(v1Run),
|
|
147
|
+
});
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async persistSession(sessionCreate) {
|
|
153
|
+
const endpoint = `${this.endpoint}/sessions`;
|
|
154
|
+
const response = await fetch(endpoint, {
|
|
155
|
+
method: "POST",
|
|
156
|
+
headers: this.headers,
|
|
157
|
+
body: JSON.stringify(sessionCreate),
|
|
158
|
+
});
|
|
159
|
+
if (!response.ok) {
|
|
160
|
+
console.error(`Failed to persist session: ${response.status} ${response.statusText}, using default session.`);
|
|
161
|
+
return {
|
|
162
|
+
id: 1,
|
|
163
|
+
...sessionCreate,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
id: (await response.json()).id,
|
|
168
|
+
...sessionCreate,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async _handleSessionResponse(endpoint) {
|
|
172
|
+
const response = await fetch(endpoint, {
|
|
173
|
+
method: "GET",
|
|
174
|
+
headers: this.headers,
|
|
175
|
+
});
|
|
176
|
+
let tracerSession;
|
|
177
|
+
if (!response.ok) {
|
|
178
|
+
console.error(`Failed to load session: ${response.status} ${response.statusText}`);
|
|
179
|
+
tracerSession = {
|
|
180
|
+
id: 1,
|
|
181
|
+
start_time: Date.now(),
|
|
182
|
+
};
|
|
183
|
+
this.session = tracerSession;
|
|
184
|
+
return tracerSession;
|
|
185
|
+
}
|
|
186
|
+
const resp = (await response.json());
|
|
187
|
+
if (resp.length === 0) {
|
|
188
|
+
tracerSession = {
|
|
189
|
+
id: 1,
|
|
190
|
+
start_time: Date.now(),
|
|
191
|
+
};
|
|
192
|
+
this.session = tracerSession;
|
|
193
|
+
return tracerSession;
|
|
194
|
+
}
|
|
195
|
+
[tracerSession] = resp;
|
|
196
|
+
this.session = tracerSession;
|
|
197
|
+
return tracerSession;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
exports.LangChainTracerV1 = LangChainTracerV1;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ChainValues } from "../utils/types.js";
|
|
2
|
+
import type { LLMResult } from "../outputs.js";
|
|
3
|
+
import { BaseTracer, type RunType, type Run } from "./base.js";
|
|
4
|
+
export interface BaseRunV1 {
|
|
5
|
+
uuid: string;
|
|
6
|
+
parent_uuid?: string;
|
|
7
|
+
start_time: number;
|
|
8
|
+
end_time?: number;
|
|
9
|
+
execution_order: number;
|
|
10
|
+
child_execution_order: number;
|
|
11
|
+
serialized: {
|
|
12
|
+
name: string;
|
|
13
|
+
};
|
|
14
|
+
session_id: number;
|
|
15
|
+
error?: string;
|
|
16
|
+
type: RunType;
|
|
17
|
+
}
|
|
18
|
+
export interface LLMRun extends BaseRunV1 {
|
|
19
|
+
prompts: string[];
|
|
20
|
+
response?: LLMResult;
|
|
21
|
+
}
|
|
22
|
+
export interface ChainRun extends BaseRunV1 {
|
|
23
|
+
inputs: ChainValues;
|
|
24
|
+
outputs?: ChainValues;
|
|
25
|
+
child_llm_runs: LLMRun[];
|
|
26
|
+
child_chain_runs: ChainRun[];
|
|
27
|
+
child_tool_runs: ToolRun[];
|
|
28
|
+
}
|
|
29
|
+
export interface ToolRun extends BaseRunV1 {
|
|
30
|
+
tool_input: string;
|
|
31
|
+
output?: string;
|
|
32
|
+
action: string;
|
|
33
|
+
child_llm_runs: LLMRun[];
|
|
34
|
+
child_chain_runs: ChainRun[];
|
|
35
|
+
child_tool_runs: ToolRun[];
|
|
36
|
+
}
|
|
37
|
+
export interface BaseTracerSession {
|
|
38
|
+
start_time: number;
|
|
39
|
+
name?: string;
|
|
40
|
+
}
|
|
41
|
+
export type TracerSessionCreate = BaseTracerSession;
|
|
42
|
+
export interface TracerSessionV1 extends BaseTracerSession {
|
|
43
|
+
id: number;
|
|
44
|
+
}
|
|
45
|
+
/** @deprecated Use LangChainTracer instead. */
|
|
46
|
+
export declare class LangChainTracerV1 extends BaseTracer {
|
|
47
|
+
name: string;
|
|
48
|
+
protected endpoint: string;
|
|
49
|
+
protected headers: Record<string, string>;
|
|
50
|
+
protected session: TracerSessionV1;
|
|
51
|
+
constructor();
|
|
52
|
+
newSession(sessionName?: string): Promise<TracerSessionV1>;
|
|
53
|
+
loadSession(sessionName: string): Promise<TracerSessionV1>;
|
|
54
|
+
loadDefaultSession(): Promise<TracerSessionV1>;
|
|
55
|
+
protected convertV2RunToRun(run: Run): Promise<LLMRun | ChainRun | ToolRun>;
|
|
56
|
+
protected persistRun(run: Run | LLMRun | ChainRun | ToolRun): Promise<void>;
|
|
57
|
+
protected persistSession(sessionCreate: BaseTracerSession): Promise<TracerSessionV1>;
|
|
58
|
+
protected _handleSessionResponse(endpoint: string): Promise<TracerSessionV1>;
|
|
59
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { getBufferString } from "../messages/index.js";
|
|
2
|
+
import { getEnvironmentVariable } from "../utils/env.js";
|
|
3
|
+
import { BaseTracer } from "./base.js";
|
|
4
|
+
/** @deprecated Use LangChainTracer instead. */
|
|
5
|
+
export class LangChainTracerV1 extends BaseTracer {
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
Object.defineProperty(this, "name", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: "langchain_tracer"
|
|
13
|
+
});
|
|
14
|
+
Object.defineProperty(this, "endpoint", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: getEnvironmentVariable("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "headers", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: {
|
|
25
|
+
"Content-Type": "application/json",
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "session", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
|
|
35
|
+
if (apiKey) {
|
|
36
|
+
this.headers["x-api-key"] = apiKey;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async newSession(sessionName) {
|
|
40
|
+
const sessionCreate = {
|
|
41
|
+
start_time: Date.now(),
|
|
42
|
+
name: sessionName,
|
|
43
|
+
};
|
|
44
|
+
const session = await this.persistSession(sessionCreate);
|
|
45
|
+
this.session = session;
|
|
46
|
+
return session;
|
|
47
|
+
}
|
|
48
|
+
async loadSession(sessionName) {
|
|
49
|
+
const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
|
|
50
|
+
return this._handleSessionResponse(endpoint);
|
|
51
|
+
}
|
|
52
|
+
async loadDefaultSession() {
|
|
53
|
+
const endpoint = `${this.endpoint}/sessions?name=default`;
|
|
54
|
+
return this._handleSessionResponse(endpoint);
|
|
55
|
+
}
|
|
56
|
+
async convertV2RunToRun(run) {
|
|
57
|
+
const session = this.session ?? (await this.loadDefaultSession());
|
|
58
|
+
const serialized = run.serialized;
|
|
59
|
+
let runResult;
|
|
60
|
+
if (run.run_type === "llm") {
|
|
61
|
+
const prompts = run.inputs.prompts
|
|
62
|
+
? run.inputs.prompts
|
|
63
|
+
: run.inputs.messages.map((x) => getBufferString(x));
|
|
64
|
+
const llmRun = {
|
|
65
|
+
uuid: run.id,
|
|
66
|
+
start_time: run.start_time,
|
|
67
|
+
end_time: run.end_time,
|
|
68
|
+
execution_order: run.execution_order,
|
|
69
|
+
child_execution_order: run.child_execution_order,
|
|
70
|
+
serialized,
|
|
71
|
+
type: run.run_type,
|
|
72
|
+
session_id: session.id,
|
|
73
|
+
prompts,
|
|
74
|
+
response: run.outputs,
|
|
75
|
+
};
|
|
76
|
+
runResult = llmRun;
|
|
77
|
+
}
|
|
78
|
+
else if (run.run_type === "chain") {
|
|
79
|
+
const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
|
|
80
|
+
const chainRun = {
|
|
81
|
+
uuid: run.id,
|
|
82
|
+
start_time: run.start_time,
|
|
83
|
+
end_time: run.end_time,
|
|
84
|
+
execution_order: run.execution_order,
|
|
85
|
+
child_execution_order: run.child_execution_order,
|
|
86
|
+
serialized,
|
|
87
|
+
type: run.run_type,
|
|
88
|
+
session_id: session.id,
|
|
89
|
+
inputs: run.inputs,
|
|
90
|
+
outputs: run.outputs,
|
|
91
|
+
child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
|
|
92
|
+
child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
|
|
93
|
+
child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
|
|
94
|
+
};
|
|
95
|
+
runResult = chainRun;
|
|
96
|
+
}
|
|
97
|
+
else if (run.run_type === "tool") {
|
|
98
|
+
const child_runs = await Promise.all(run.child_runs.map((child_run) => this.convertV2RunToRun(child_run)));
|
|
99
|
+
const toolRun = {
|
|
100
|
+
uuid: run.id,
|
|
101
|
+
start_time: run.start_time,
|
|
102
|
+
end_time: run.end_time,
|
|
103
|
+
execution_order: run.execution_order,
|
|
104
|
+
child_execution_order: run.child_execution_order,
|
|
105
|
+
serialized,
|
|
106
|
+
type: run.run_type,
|
|
107
|
+
session_id: session.id,
|
|
108
|
+
tool_input: run.inputs.input,
|
|
109
|
+
output: run.outputs?.output,
|
|
110
|
+
action: JSON.stringify(serialized),
|
|
111
|
+
child_llm_runs: child_runs.filter((child_run) => child_run.type === "llm"),
|
|
112
|
+
child_chain_runs: child_runs.filter((child_run) => child_run.type === "chain"),
|
|
113
|
+
child_tool_runs: child_runs.filter((child_run) => child_run.type === "tool"),
|
|
114
|
+
};
|
|
115
|
+
runResult = toolRun;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
throw new Error(`Unknown run type: ${run.run_type}`);
|
|
119
|
+
}
|
|
120
|
+
return runResult;
|
|
121
|
+
}
|
|
122
|
+
async persistRun(run) {
|
|
123
|
+
let endpoint;
|
|
124
|
+
let v1Run;
|
|
125
|
+
if (run.run_type !== undefined) {
|
|
126
|
+
v1Run = await this.convertV2RunToRun(run);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
v1Run = run;
|
|
130
|
+
}
|
|
131
|
+
if (v1Run.type === "llm") {
|
|
132
|
+
endpoint = `${this.endpoint}/llm-runs`;
|
|
133
|
+
}
|
|
134
|
+
else if (v1Run.type === "chain") {
|
|
135
|
+
endpoint = `${this.endpoint}/chain-runs`;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
endpoint = `${this.endpoint}/tool-runs`;
|
|
139
|
+
}
|
|
140
|
+
const response = await fetch(endpoint, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
headers: this.headers,
|
|
143
|
+
body: JSON.stringify(v1Run),
|
|
144
|
+
});
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async persistSession(sessionCreate) {
|
|
150
|
+
const endpoint = `${this.endpoint}/sessions`;
|
|
151
|
+
const response = await fetch(endpoint, {
|
|
152
|
+
method: "POST",
|
|
153
|
+
headers: this.headers,
|
|
154
|
+
body: JSON.stringify(sessionCreate),
|
|
155
|
+
});
|
|
156
|
+
if (!response.ok) {
|
|
157
|
+
console.error(`Failed to persist session: ${response.status} ${response.statusText}, using default session.`);
|
|
158
|
+
return {
|
|
159
|
+
id: 1,
|
|
160
|
+
...sessionCreate,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
id: (await response.json()).id,
|
|
165
|
+
...sessionCreate,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
async _handleSessionResponse(endpoint) {
|
|
169
|
+
const response = await fetch(endpoint, {
|
|
170
|
+
method: "GET",
|
|
171
|
+
headers: this.headers,
|
|
172
|
+
});
|
|
173
|
+
let tracerSession;
|
|
174
|
+
if (!response.ok) {
|
|
175
|
+
console.error(`Failed to load session: ${response.status} ${response.statusText}`);
|
|
176
|
+
tracerSession = {
|
|
177
|
+
id: 1,
|
|
178
|
+
start_time: Date.now(),
|
|
179
|
+
};
|
|
180
|
+
this.session = tracerSession;
|
|
181
|
+
return tracerSession;
|
|
182
|
+
}
|
|
183
|
+
const resp = (await response.json());
|
|
184
|
+
if (resp.length === 0) {
|
|
185
|
+
tracerSession = {
|
|
186
|
+
id: 1,
|
|
187
|
+
start_time: Date.now(),
|
|
188
|
+
};
|
|
189
|
+
this.session = tracerSession;
|
|
190
|
+
return tracerSession;
|
|
191
|
+
}
|
|
192
|
+
[tracerSession] = resp;
|
|
193
|
+
this.session = tracerSession;
|
|
194
|
+
return tracerSession;
|
|
195
|
+
}
|
|
196
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -234,6 +234,11 @@
|
|
|
234
234
|
"import": "./tracers/tracer_langchain.js",
|
|
235
235
|
"require": "./tracers/tracer_langchain.cjs"
|
|
236
236
|
},
|
|
237
|
+
"./tracers/tracer_langchain_v1": {
|
|
238
|
+
"types": "./tracers/tracer_langchain_v1.d.ts",
|
|
239
|
+
"import": "./tracers/tracer_langchain_v1.js",
|
|
240
|
+
"require": "./tracers/tracer_langchain_v1.cjs"
|
|
241
|
+
},
|
|
237
242
|
"./utils/async_caller": {
|
|
238
243
|
"types": "./utils/async_caller.d.ts",
|
|
239
244
|
"import": "./utils/async_caller.js",
|
|
@@ -388,6 +393,9 @@
|
|
|
388
393
|
"tracers/tracer_langchain.cjs",
|
|
389
394
|
"tracers/tracer_langchain.js",
|
|
390
395
|
"tracers/tracer_langchain.d.ts",
|
|
396
|
+
"tracers/tracer_langchain_v1.cjs",
|
|
397
|
+
"tracers/tracer_langchain_v1.js",
|
|
398
|
+
"tracers/tracer_langchain_v1.d.ts",
|
|
391
399
|
"utils/async_caller.cjs",
|
|
392
400
|
"utils/async_caller.js",
|
|
393
401
|
"utils/async_caller.d.ts",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('../dist/tracers/tracer_langchain_v1.cjs');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/tracers/tracer_langchain_v1.js'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../dist/tracers/tracer_langchain_v1.js'
|