Skip to content

Commit

Permalink
Merge pull request #1532 from FlowiseAI/feature/Lagnfuse-Session-Id
Browse files Browse the repository at this point in the history
Feature/SessionId Tracking
  • Loading branch information
HenryHengZJ authored Jan 15, 2024
2 parents b3d2d7c + b3ab852 commit 24d2275
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"ioredis": "^5.3.2",
"langchain": "^0.0.214",
"langfuse": "2.0.2",
"langfuse-langchain": "2.0.2",
"langfuse-langchain": "2.3.3",
"langsmith": "0.0.53",
"linkifyjs": "^4.1.1",
"llmonitor": "^0.5.5",
Expand Down
41 changes: 29 additions & 12 deletions packages/components/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BaseTracer, Run, BaseCallbackHandler } from 'langchain/callbacks'
import { BaseTracer, Run, BaseCallbackHandler, LangChainTracer } from 'langchain/callbacks'
import { AgentAction, ChainValues } from 'langchain/schema'
import { Logger } from 'winston'
import { Server } from 'socket.io'
import { Client } from 'langsmith'
import { LangChainTracer } from 'langchain/callbacks'
import { LLMonitorHandler } from 'langchain/callbacks/handlers/llmonitor'
import { LLMonitorHandler, LLMonitorHandlerFields } from 'langchain/callbacks/handlers/llmonitor'
import { getCredentialData, getCredentialParam } from './utils'
import { ICommonObject, INodeData } from './Interface'
import CallbackHandler from 'langfuse-langchain'
import { LangChainTracerFields } from '@langchain/core/tracers/tracer_langchain'
import { RunTree, RunTreeConfig, Client as LangsmithClient } from 'langsmith'
import { Langfuse, LangfuseTraceClient, LangfuseSpanClient, LangfuseGenerationClient } from 'langfuse'
import monitor from 'llmonitor'
Expand Down Expand Up @@ -235,11 +235,17 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
apiKey: langSmithApiKey
})

const tracer = new LangChainTracer({
let langSmithField: LangChainTracerFields = {
projectName: langSmithProject ?? 'default',
//@ts-ignore
client
})
}

if (nodeData?.inputs?.analytics?.langSmith) {
langSmithField = { ...langSmithField, ...nodeData?.inputs?.analytics?.langSmith }
}

const tracer = new LangChainTracer(langSmithField)
callbacks.push(tracer)
} else if (provider === 'langFuse') {
const release = analytic[provider].release as string
Expand All @@ -248,25 +254,33 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
const langFusePublicKey = getCredentialParam('langFusePublicKey', credentialData, nodeData)
const langFuseEndpoint = getCredentialParam('langFuseEndpoint', credentialData, nodeData)

const langFuseOptions: any = {
let langFuseOptions: any = {
secretKey: langFuseSecretKey,
publicKey: langFusePublicKey,
baseUrl: langFuseEndpoint ?? 'https://cloud.langfuse.com'
}
if (release) langFuseOptions.release = release
if (options.chatId) langFuseOptions.userId = options.chatId
if (options.chatId) langFuseOptions.sessionId = options.chatId

if (nodeData?.inputs?.analytics?.langFuse) {
langFuseOptions = { ...langFuseOptions, ...nodeData?.inputs?.analytics?.langFuse }
}

const handler = new CallbackHandler(langFuseOptions)
callbacks.push(handler)
} else if (provider === 'llmonitor') {
const llmonitorAppId = getCredentialParam('llmonitorAppId', credentialData, nodeData)
const llmonitorEndpoint = getCredentialParam('llmonitorEndpoint', credentialData, nodeData)

const llmonitorFields: ICommonObject = {
let llmonitorFields: LLMonitorHandlerFields = {
appId: llmonitorAppId,
apiUrl: llmonitorEndpoint ?? 'https://app.llmonitor.com'
}

if (nodeData?.inputs?.analytics?.llmonitor) {
llmonitorFields = { ...llmonitorFields, ...nodeData?.inputs?.analytics?.llmonitor }
}

const handler = new LLMonitorHandler(llmonitorFields)
callbacks.push(handler)
}
Expand Down Expand Up @@ -360,7 +374,8 @@ export class AnalyticHandler {
},
serialized: {},
project_name: this.handlers['langSmith'].langSmithProject,
client: this.handlers['langSmith'].client
client: this.handlers['langSmith'].client,
...this.nodeData?.inputs?.analytics?.langSmith
}
const parentRun = new RunTree(parentRunConfig)
await parentRun.postRun()
Expand Down Expand Up @@ -390,8 +405,9 @@ export class AnalyticHandler {
const langfuse: Langfuse = this.handlers['langFuse'].client
langfuseTraceClient = langfuse.trace({
name,
userId: this.options.chatId,
metadata: { tags: ['openai-assistant'] }
sessionId: this.options.chatId,
metadata: { tags: ['openai-assistant'] },
...this.nodeData?.inputs?.analytics?.langFuse
})
} else {
langfuseTraceClient = this.handlers['langFuse'].trace[parentIds['langFuse']]
Expand Down Expand Up @@ -420,7 +436,8 @@ export class AnalyticHandler {
runId,
name,
userId: this.options.chatId,
input
input,
...this.nodeData?.inputs?.analytics?.llmonitor
})
this.handlers['llmonitor'].chainEvent = { [runId]: runId }
returnIds['llmonitor'].chainEvent = runId
Expand Down

0 comments on commit 24d2275

Please sign in to comment.