@blaxel/langgraph 0.2.10-dev.86 → 0.2.10-dev1
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/README.md +1 -1
- package/dist/model.js +1 -0
- package/dist/telemetry/intrumentation.d.ts +41 -0
- package/dist/telemetry/intrumentation.js +208 -0
- package/dist/tools.d.ts +4 -4
- package/dist/tools.js +4 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Blaxel Typescript SDK
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
-
<img src="https://blaxel.ai/logo.png" alt="Blaxel"/>
|
|
4
|
+
<img src="https://blaxel.ai/logo-bg.png" alt="Blaxel"/>
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
**Blaxel is a computing platform for AI agent builders, with all the services and infrastructure to build and deploy agents efficiently.** This repository contains the TypeScript SDK to interact with Blaxel resources using LangChain/LangGraph format.
|
package/dist/model.js
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { InstrumentationConfig } from "@opentelemetry/instrumentation";
|
|
2
|
+
export interface LangChainInstrumentationConfig extends InstrumentationConfig {
|
|
3
|
+
/**
|
|
4
|
+
* Whether to log prompts, completions and embeddings on traces.
|
|
5
|
+
* @default true
|
|
6
|
+
*/
|
|
7
|
+
traceContent?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* A custom logger to log any exceptions that happen during span creation.
|
|
10
|
+
*/
|
|
11
|
+
exceptionLogger?: (e: Error) => void;
|
|
12
|
+
}
|
|
13
|
+
import { InstrumentationBase, InstrumentationModuleDefinition } from "@opentelemetry/instrumentation";
|
|
14
|
+
export declare class LangChainInstrumentation extends InstrumentationBase {
|
|
15
|
+
protected _config: LangChainInstrumentationConfig;
|
|
16
|
+
constructor(config?: LangChainInstrumentationConfig);
|
|
17
|
+
manuallyInstrument({ chainsModule, agentsModule, toolsModule, vectorStoreModule, runnablesModule, }: {
|
|
18
|
+
chainsModule?: any;
|
|
19
|
+
agentsModule?: any;
|
|
20
|
+
toolsModule?: any;
|
|
21
|
+
vectorStoreModule?: any;
|
|
22
|
+
runnablesModule?: any;
|
|
23
|
+
}): void;
|
|
24
|
+
protected init(): InstrumentationModuleDefinition[];
|
|
25
|
+
private patchChainModule;
|
|
26
|
+
private patchAgentModule;
|
|
27
|
+
private patchToolsModule;
|
|
28
|
+
private patchVectorStoreModule;
|
|
29
|
+
private patchRunnablesModule;
|
|
30
|
+
private unpatchChainModule;
|
|
31
|
+
private unpatchAgentModule;
|
|
32
|
+
private unpatchToolsModule;
|
|
33
|
+
private unpatchVectorStoreModule;
|
|
34
|
+
private unpatchRunnablesModule;
|
|
35
|
+
private _shouldSendPrompts;
|
|
36
|
+
}
|
|
37
|
+
import { Tracer } from "@opentelemetry/api";
|
|
38
|
+
import { TraceloopSpanKindValues } from "@traceloop/ai-semantic-conventions";
|
|
39
|
+
export declare function genericWrapper(tracer: () => Tracer, shouldSendPrompts: boolean, spanKind: TraceloopSpanKindValues, spanName?: string): (original: Function) => (this: any, ...args: unknown[]) => any;
|
|
40
|
+
export declare function taskWrapper(tracer: () => Tracer, shouldSendPrompts: boolean, spanName?: string): (original: Function) => (this: any, ...args: unknown[]) => any;
|
|
41
|
+
export declare function workflowWrapper(tracer: () => Tracer, shouldSendPrompts: boolean, spanName: string): (original: Function) => (this: any, ...args: unknown[]) => any;
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LangChainInstrumentation = void 0;
|
|
4
|
+
exports.genericWrapper = genericWrapper;
|
|
5
|
+
exports.taskWrapper = taskWrapper;
|
|
6
|
+
exports.workflowWrapper = workflowWrapper;
|
|
7
|
+
const api_1 = require("@opentelemetry/api");
|
|
8
|
+
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
9
|
+
const ai_semantic_conventions_1 = require("@traceloop/ai-semantic-conventions");
|
|
10
|
+
class LangChainInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
11
|
+
constructor(config = {}) {
|
|
12
|
+
super("@traceloop/instrumentation-langchain", '0.0.0', config);
|
|
13
|
+
}
|
|
14
|
+
manuallyInstrument({ chainsModule, agentsModule, toolsModule, vectorStoreModule, runnablesModule, }) {
|
|
15
|
+
console.log("Manually instrumenting langchain");
|
|
16
|
+
if (chainsModule) {
|
|
17
|
+
this._diag.debug("Manually instrumenting langchain chains");
|
|
18
|
+
this.patchChainModule(chainsModule);
|
|
19
|
+
}
|
|
20
|
+
if (agentsModule) {
|
|
21
|
+
this._diag.debug("Manually instrumenting langchain agents");
|
|
22
|
+
this.patchAgentModule(agentsModule);
|
|
23
|
+
}
|
|
24
|
+
if (toolsModule) {
|
|
25
|
+
this._diag.debug("Manually instrumenting langchain tools");
|
|
26
|
+
this.patchToolsModule(toolsModule);
|
|
27
|
+
}
|
|
28
|
+
if (vectorStoreModule) {
|
|
29
|
+
this._diag.debug("Manually instrumenting langchain vector stores");
|
|
30
|
+
this.patchVectorStoreModule(vectorStoreModule);
|
|
31
|
+
}
|
|
32
|
+
if (runnablesModule) {
|
|
33
|
+
this._diag.debug("Manually instrumenting @langchain/core/runnables");
|
|
34
|
+
this.patchRunnablesModule(runnablesModule);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
init() {
|
|
38
|
+
console.log("Initializing langchain instrumentation");
|
|
39
|
+
const chainModule = new instrumentation_1.InstrumentationNodeModuleDefinition("langchain/chains.cjs", [">=0.3.0"], this.patchChainModule.bind(this), this.unpatchChainModule.bind(this));
|
|
40
|
+
const agentModule = new instrumentation_1.InstrumentationNodeModuleDefinition("langchain/agents.cjs", [">=0.3.0"], this.patchAgentModule.bind(this), this.unpatchAgentModule.bind(this));
|
|
41
|
+
const toolsModule = new instrumentation_1.InstrumentationNodeModuleDefinition("langchain/tools.cjs", [">=0.3.0"], this.patchToolsModule.bind(this), this.unpatchToolsModule.bind(this));
|
|
42
|
+
const vectorStoreModule = new instrumentation_1.InstrumentationNodeModuleDefinition("langchain/core/vectorstores.cjs", [">=0.3.0"], this.patchVectorStoreModule.bind(this), this.unpatchVectorStoreModule.bind(this));
|
|
43
|
+
const runnablesModule = new instrumentation_1.InstrumentationNodeModuleDefinition("@langchain/core/runnables.cjs", [">=0.3.0"], this.patchRunnablesModule.bind(this), this.unpatchRunnablesModule.bind(this));
|
|
44
|
+
return [
|
|
45
|
+
chainModule,
|
|
46
|
+
agentModule,
|
|
47
|
+
toolsModule,
|
|
48
|
+
vectorStoreModule,
|
|
49
|
+
runnablesModule,
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
patchChainModule(moduleExports, moduleVersion) {
|
|
53
|
+
console.log("Patching langchain/chains.cjs");
|
|
54
|
+
this._diag.debug(`Patching langchain/chains.cjs@${moduleVersion}`);
|
|
55
|
+
this._wrap(moduleExports.RetrievalQAChain.prototype, "_call", workflowWrapper(() => this.tracer, this._shouldSendPrompts(), "retrieval_qa.workflow"));
|
|
56
|
+
this._wrap(moduleExports.BaseChain.prototype, "call", taskWrapper(() => this.tracer, this._shouldSendPrompts()));
|
|
57
|
+
return moduleExports;
|
|
58
|
+
}
|
|
59
|
+
patchAgentModule(moduleExports, moduleVersion) {
|
|
60
|
+
console.log("Patching langchain/agents.cjs");
|
|
61
|
+
this._diag.debug(`Patching langchain/agents.cjs@${moduleVersion}`);
|
|
62
|
+
this._wrap(moduleExports.AgentExecutor.prototype, "_call", workflowWrapper(() => this.tracer, this._shouldSendPrompts(), "langchain.agent"));
|
|
63
|
+
return moduleExports;
|
|
64
|
+
}
|
|
65
|
+
patchToolsModule(moduleExports, moduleVersion) {
|
|
66
|
+
console.log("Patching langchain/tools.cjs");
|
|
67
|
+
this._diag.debug(`Patching langchain/tools.cjs@${moduleVersion}`);
|
|
68
|
+
this._wrap(moduleExports.Tool.prototype, "call", taskWrapper(() => this.tracer, this._shouldSendPrompts()));
|
|
69
|
+
return moduleExports;
|
|
70
|
+
}
|
|
71
|
+
patchVectorStoreModule(moduleExports, moduleVersion) {
|
|
72
|
+
console.log("Patching langchain/vectorstores.cjs");
|
|
73
|
+
this._diag.debug(`Patching langchain/vectorstores.cjs@${moduleVersion}`);
|
|
74
|
+
this._wrap(moduleExports.VectorStoreRetriever.prototype, "_getRelevantDocuments", taskWrapper(() => this.tracer, this._shouldSendPrompts()));
|
|
75
|
+
return moduleExports;
|
|
76
|
+
}
|
|
77
|
+
patchRunnablesModule(moduleExports, moduleVersion) {
|
|
78
|
+
console.log("Patching @langchain/core/runnables.cjs");
|
|
79
|
+
this._diag.debug(`Patching @langchain/core/runnables@${moduleVersion}`);
|
|
80
|
+
this._wrap(moduleExports.RunnableSequence.prototype, "invoke", taskWrapper(() => this.tracer, this._shouldSendPrompts()));
|
|
81
|
+
return moduleExports;
|
|
82
|
+
}
|
|
83
|
+
unpatchChainModule(moduleExports, moduleVersion) {
|
|
84
|
+
console.log("Unpatching langchain/chains.cjs");
|
|
85
|
+
this._diag.debug(`Unpatching langchain/chains.cjs@${moduleVersion}`);
|
|
86
|
+
this._unwrap(moduleExports.RetrievalQAChain.prototype, "_call");
|
|
87
|
+
this._unwrap(moduleExports.BaseChain.prototype, "call");
|
|
88
|
+
return moduleExports;
|
|
89
|
+
}
|
|
90
|
+
unpatchAgentModule(moduleExports, moduleVersion) {
|
|
91
|
+
console.log("Unpatching langchain/agents.cjs");
|
|
92
|
+
this._diag.debug(`Unpatching langchain/agents.cjs@${moduleVersion}`);
|
|
93
|
+
this._unwrap(moduleExports.AgentExecutor.prototype, "_call");
|
|
94
|
+
return moduleExports;
|
|
95
|
+
}
|
|
96
|
+
unpatchToolsModule(moduleExports) {
|
|
97
|
+
console.log("Unpatching langchain/tools.cjs");
|
|
98
|
+
this._diag.debug(`Unpatching langchain/tools.cjs`);
|
|
99
|
+
this._unwrap(moduleExports.Tool.prototype, "call");
|
|
100
|
+
return moduleExports;
|
|
101
|
+
}
|
|
102
|
+
unpatchVectorStoreModule(moduleExports) {
|
|
103
|
+
console.log("Unpatching langchain/vectorstores.cjs");
|
|
104
|
+
this._diag.debug(`Unpatching langchain/vectorstores.cjs`);
|
|
105
|
+
this._unwrap(moduleExports.VectorStoreRetriever.prototype, "_getRelevantDocuments");
|
|
106
|
+
return moduleExports;
|
|
107
|
+
}
|
|
108
|
+
unpatchRunnablesModule(moduleExports) {
|
|
109
|
+
console.log("Unpatching @langchain/core/runnables.cjs");
|
|
110
|
+
this._diag.debug(`Unpatching @langchain/core/runnables.cjs`);
|
|
111
|
+
this._unwrap(moduleExports.Runnable.prototype, "invoke");
|
|
112
|
+
return moduleExports;
|
|
113
|
+
}
|
|
114
|
+
_shouldSendPrompts() {
|
|
115
|
+
console.log("Should send prompts");
|
|
116
|
+
const contextShouldSendPrompts = api_1.context
|
|
117
|
+
.active()
|
|
118
|
+
.getValue(ai_semantic_conventions_1.CONTEXT_KEY_ALLOW_TRACE_CONTENT);
|
|
119
|
+
if (contextShouldSendPrompts !== undefined) {
|
|
120
|
+
return !!contextShouldSendPrompts;
|
|
121
|
+
}
|
|
122
|
+
return this._config.traceContent !== undefined
|
|
123
|
+
? this._config.traceContent
|
|
124
|
+
: true;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.LangChainInstrumentation = LangChainInstrumentation;
|
|
128
|
+
const api_2 = require("@opentelemetry/api");
|
|
129
|
+
const instrumentation_2 = require("@opentelemetry/instrumentation");
|
|
130
|
+
const ai_semantic_conventions_2 = require("@traceloop/ai-semantic-conventions");
|
|
131
|
+
function genericWrapper(tracer, shouldSendPrompts, spanKind, spanName) {
|
|
132
|
+
console.log("Generic wrapper");
|
|
133
|
+
// eslint-disable-next-line
|
|
134
|
+
return (original) => {
|
|
135
|
+
return function method(...args) {
|
|
136
|
+
const span = tracer().startSpan(spanName || `${this.constructor.name}.${spanKind}`);
|
|
137
|
+
span.setAttribute(ai_semantic_conventions_2.SpanAttributes.TRACELOOP_SPAN_KIND, spanKind);
|
|
138
|
+
if (shouldSendPrompts) {
|
|
139
|
+
try {
|
|
140
|
+
if (args.length === 1 &&
|
|
141
|
+
typeof args[0] === "object" &&
|
|
142
|
+
!(args[0] instanceof Map)) {
|
|
143
|
+
span.setAttribute(ai_semantic_conventions_2.SpanAttributes.TRACELOOP_ENTITY_INPUT, JSON.stringify({ args: [], kwargs: args[0] }));
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
span.setAttribute(ai_semantic_conventions_2.SpanAttributes.TRACELOOP_ENTITY_INPUT, JSON.stringify({
|
|
147
|
+
args: args.map((arg) => arg instanceof Map ? Array.from(arg.entries()) : arg),
|
|
148
|
+
kwargs: {},
|
|
149
|
+
}));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
this._diag.debug(e);
|
|
154
|
+
this._config.exceptionLogger?.(e);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const execContext = api_2.trace.setSpan(api_1.context.active(), span);
|
|
158
|
+
const execPromise = (0, instrumentation_2.safeExecuteInTheMiddle)(() => {
|
|
159
|
+
return api_1.context.with(execContext, () => {
|
|
160
|
+
return original.apply(this, args);
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
164
|
+
() => { });
|
|
165
|
+
const wrappedPromise = execPromise
|
|
166
|
+
.then((result) => {
|
|
167
|
+
return new Promise((resolve) => {
|
|
168
|
+
span.setStatus({ code: api_2.SpanStatusCode.OK });
|
|
169
|
+
try {
|
|
170
|
+
if (shouldSendPrompts) {
|
|
171
|
+
if (result instanceof Map) {
|
|
172
|
+
span.setAttribute(ai_semantic_conventions_2.SpanAttributes.TRACELOOP_ENTITY_OUTPUT, JSON.stringify(Array.from(result.entries())));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
span.setAttribute(ai_semantic_conventions_2.SpanAttributes.TRACELOOP_ENTITY_OUTPUT, JSON.stringify(result));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
this._diag.debug(e);
|
|
181
|
+
this._config.exceptionLogger?.(e);
|
|
182
|
+
}
|
|
183
|
+
finally {
|
|
184
|
+
span.end();
|
|
185
|
+
resolve(result);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
})
|
|
189
|
+
.catch((error) => {
|
|
190
|
+
return new Promise((_, reject) => {
|
|
191
|
+
span.setStatus({
|
|
192
|
+
code: api_2.SpanStatusCode.ERROR,
|
|
193
|
+
message: error.message,
|
|
194
|
+
});
|
|
195
|
+
span.end();
|
|
196
|
+
reject(error);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
return api_1.context.bind(execContext, wrappedPromise);
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function taskWrapper(tracer, shouldSendPrompts, spanName) {
|
|
204
|
+
return genericWrapper(tracer, shouldSendPrompts, ai_semantic_conventions_2.TraceloopSpanKindValues.TASK, spanName);
|
|
205
|
+
}
|
|
206
|
+
function workflowWrapper(tracer, shouldSendPrompts, spanName) {
|
|
207
|
+
return genericWrapper(tracer, shouldSendPrompts, ai_semantic_conventions_2.TraceloopSpanKindValues.WORKFLOW, spanName);
|
|
208
|
+
}
|
package/dist/tools.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
export declare function blTool(name: string): Promise<import("@langchain/core/tools").DynamicStructuredTool<import("zod"
|
|
1
|
+
export declare function blTool(name: string, ms?: number): Promise<import("@langchain/core/tools").DynamicStructuredTool<import("zod").ZodObject<any, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
|
|
2
2
|
[x: string]: any;
|
|
3
3
|
}, {
|
|
4
4
|
[x: string]: any;
|
|
5
5
|
}>, unknown, {
|
|
6
6
|
[x: string]: any;
|
|
7
|
-
}
|
|
8
|
-
export declare function blTools(names: string[]): Promise<import("@langchain/core/tools").DynamicStructuredTool<import("zod"
|
|
7
|
+
}>[]>;
|
|
8
|
+
export declare function blTools(names: string[], ms?: number): Promise<import("@langchain/core/tools").DynamicStructuredTool<import("zod").ZodObject<any, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
|
|
9
9
|
[x: string]: any;
|
|
10
10
|
}, {
|
|
11
11
|
[x: string]: any;
|
|
12
12
|
}>, unknown, {
|
|
13
13
|
[x: string]: any;
|
|
14
|
-
}
|
|
14
|
+
}>[]>;
|
package/dist/tools.js
CHANGED
|
@@ -4,9 +4,9 @@ exports.blTool = blTool;
|
|
|
4
4
|
exports.blTools = blTools;
|
|
5
5
|
const core_1 = require("@blaxel/core");
|
|
6
6
|
const tools_1 = require("@langchain/core/tools");
|
|
7
|
-
async function blTool(name) {
|
|
7
|
+
async function blTool(name, ms) {
|
|
8
8
|
try {
|
|
9
|
-
const blaxelTool = await (0, core_1.getTool)(name);
|
|
9
|
+
const blaxelTool = await (0, core_1.getTool)(name, ms);
|
|
10
10
|
return blaxelTool.map((t) => (0, tools_1.tool)(t.call.bind(t), {
|
|
11
11
|
name: t.name,
|
|
12
12
|
description: t.description,
|
|
@@ -18,7 +18,7 @@ async function blTool(name) {
|
|
|
18
18
|
throw err;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
async function blTools(names) {
|
|
22
|
-
const toolArrays = await Promise.all(names.map(blTool));
|
|
21
|
+
async function blTools(names, ms) {
|
|
22
|
+
const toolArrays = await Promise.all(names.map((n) => blTool(n, ms)));
|
|
23
23
|
return toolArrays.flat();
|
|
24
24
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaxel/langgraph",
|
|
3
|
-
"version": "0.2.10-
|
|
3
|
+
"version": "0.2.10-dev1",
|
|
4
4
|
"description": "Blaxel SDK for TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Blaxel, INC (https://blaxel.ai)",
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
"langchain": "^0.3.24",
|
|
66
66
|
"zod": "^3.24.3",
|
|
67
67
|
"zod-to-json-schema": "^3.24.5",
|
|
68
|
-
"@blaxel/core": "0.2.10-
|
|
68
|
+
"@blaxel/core": "0.2.10-dev1"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"@eslint/js": "^9.26.0",
|