@turingpulse/sdk-langchain 1.0.1
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/package.json +16 -0
- package/src/index.ts +7 -0
- package/src/wrapper.ts +51 -0
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@turingpulse/sdk-langchain",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
5
|
+
"description": "TuringPulse SDK integration for LangChain.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@turingpulse/sdk": ">=1.0.0",
|
|
11
|
+
"@langchain/core": ">=1.1.24"
|
|
12
|
+
},
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"typescript": ">=5.0.0"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/index.ts
ADDED
package/src/wrapper.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { withInstrumentation, GovernanceDirective, currentContext } from '@turingpulse/sdk';
|
|
2
|
+
|
|
3
|
+
interface LangchainInstrumentOptions {
|
|
4
|
+
name: string;
|
|
5
|
+
governance?: GovernanceDirective;
|
|
6
|
+
model?: string;
|
|
7
|
+
provider?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Wrap a LangChain Runnable for TuringPulse observability.
|
|
12
|
+
*/
|
|
13
|
+
export function instrumentLangchain(
|
|
14
|
+
runnable: { invoke: (input: unknown, config?: unknown) => Promise<unknown> },
|
|
15
|
+
options: LangchainInstrumentOptions,
|
|
16
|
+
): (input: unknown, config?: unknown) => Promise<unknown> {
|
|
17
|
+
const { name, governance, model, provider = 'openai' } = options;
|
|
18
|
+
|
|
19
|
+
const wrappedFn = withInstrumentation(
|
|
20
|
+
async function langchainInvoke(input: unknown, config?: unknown): Promise<unknown> {
|
|
21
|
+
const result = await runnable.invoke(input, config);
|
|
22
|
+
|
|
23
|
+
const ctx = currentContext();
|
|
24
|
+
if (ctx) {
|
|
25
|
+
ctx.framework = 'langchain';
|
|
26
|
+
ctx.nodeType = 'llm';
|
|
27
|
+
if (model) ctx.setModel(model, provider);
|
|
28
|
+
|
|
29
|
+
const resp = result as Record<string, unknown> | null;
|
|
30
|
+
const content = typeof resp?.content === 'string' ? resp.content : '';
|
|
31
|
+
|
|
32
|
+
const responseMeta = resp?.response_metadata as Record<string, unknown> | undefined;
|
|
33
|
+
const tokenUsage = responseMeta?.tokenUsage as { promptTokens?: number; completionTokens?: number } | undefined;
|
|
34
|
+
if (tokenUsage) {
|
|
35
|
+
ctx.setTokens(tokenUsage.promptTokens ?? 0, tokenUsage.completionTokens ?? 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const detectedModel = (responseMeta?.model_name as string) ?? model;
|
|
39
|
+
if (detectedModel) ctx.setModel(detectedModel, provider);
|
|
40
|
+
|
|
41
|
+
const inputStr = typeof input === 'string' ? input : JSON.stringify(input);
|
|
42
|
+
ctx.setIO(inputStr, content || JSON.stringify(result));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return result;
|
|
46
|
+
},
|
|
47
|
+
{ name, governance },
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return wrappedFn as (input: unknown, config?: unknown) => Promise<unknown>;
|
|
51
|
+
}
|