1- // import '@dotenvx/dotenvx/config';
1+ import '@dotenvx/dotenvx/config' ;
22import './langfuse-instrumentation' ;
33
44import { HumanMessage } from '@langchain/core/messages' ;
55import { ChatOpenAI } from '@langchain/openai' ;
6+ import { CallbackHandler } from '@langfuse/langchain' ;
67
7- import { startActiveObservation , startObservation } from '@langfuse/tracing ' ;
8+ import { langfuseSpanProcessor } from './langfuse-instrumentation ' ;
89
9- // import { NodeSDK } from "@opentelemetry/sdk-node";
10- // import { LangfuseSpanProcessor } from "@langfuse/otel";
10+ // Initialize the Langfuse callback handler with tracing configuration
11+ const langfuseHandler = new CallbackHandler ( {
12+ sessionId : 'user-session-123' , // Track user session
13+ userId : 'user-abc' , // Track user identity
14+ tags : [ 'langchain-test' ] , // Add searchable tags
15+ } ) ;
1116
1217const model = new ChatOpenAI ( {
1318 model : 'qwen/qwen3-vl-4b' ,
@@ -18,60 +23,17 @@ const model = new ChatOpenAI({
1823 // temperature: 0.5,
1924} ) ;
2025
21- // import { CallbackHandler } from "@langfuse/langchain";
22- // const langfuseHandler = new CallbackHandler();
23-
2426console . log ( ';; langfuse ' , process . env [ 'LANGFUSE_PUBLIC_KEY' ] ) ;
2527
26- // const sdk = new NodeSDK({
27- // spanProcessors: [new LangfuseSpanProcessor()],
28- // });
29-
30- // sdk.start();
31-
32- // await startActiveObservation("user-request", async (span) => {
33- // span.update({
34- // input: { query: "What is the capital of France?" },
35- // });
36-
37- // // This generation will automatically be a child of "user-request"
38- // const generation = startObservation(
39- // "llm-call",
40- // {
41- // model: "gpt-4",
42- // input: [{ role: "user", content: "What is the capital of France?" }],
43- // },
44- // { asType: "generation" },
45- // );
46-
47- // // ... LLM call logic ...
48- // // refers to https://github.com/calcajack3/auto-tool-eval/blob/main/src/utils/utils.ts
28+ const messages = [
29+ new HumanMessage ( 'give an brief intro to codemirror in less than 80 words ' ) ,
30+ ] ;
4931
50- // const messages = [
51- // new HumanMessage('give an brief intro to codemirror in less than 80 words '),
52- // ];
53-
54- // const res = await model.invoke(messages, {
55- // // callbacks: [langfuseHandler]
56- // });
57- // console.log('res');
58-
59- // generation
60- // .update({
61- // output: { content: "The capital of France is Paris." },
62- // })
63- // .end();
64-
65- // span.update({ output: "Successfully answered." });
66- // });
67-
68- async function main ( ) {
69- await startActiveObservation ( 'my-first-trace' , async ( span ) => {
70- span . update ( {
71- input : 'Hello, Langfuse!' ,
72- output : 'This is my first trace!' ,
73- } ) ;
74- } ) ;
75- }
32+ const res = await model . invoke ( messages , {
33+ callbacks : [ langfuseHandler ] ,
34+ // Name for the trace (if no active span)
35+ runName : 'joke-generator' ,
36+ } ) ;
37+ console . log ( ';; res ' , res . content ) ;
7638
77- main ( ) ;
39+ await langfuseSpanProcessor . forceFlush ( ) ;
0 commit comments