Merge pull request #1532 from FlowiseAI/feature/Lagnfuse-Session-Id

Feature/SessionId Tracking
This commit is contained in:
Henry Heng 2024-01-15 19:12:57 +00:00 committed by GitHub
commit 24d227598b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 13 deletions

View File

@ -57,7 +57,7 @@
"ioredis": "^5.3.2", "ioredis": "^5.3.2",
"langchain": "^0.0.214", "langchain": "^0.0.214",
"langfuse": "2.0.2", "langfuse": "2.0.2",
"langfuse-langchain": "2.0.2", "langfuse-langchain": "2.3.3",
"langsmith": "0.0.53", "langsmith": "0.0.53",
"linkifyjs": "^4.1.1", "linkifyjs": "^4.1.1",
"llmonitor": "^0.5.5", "llmonitor": "^0.5.5",

View File

@ -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 { AgentAction, ChainValues } from 'langchain/schema'
import { Logger } from 'winston' import { Logger } from 'winston'
import { Server } from 'socket.io' import { Server } from 'socket.io'
import { Client } from 'langsmith' import { Client } from 'langsmith'
import { LangChainTracer } from 'langchain/callbacks' import { LLMonitorHandler, LLMonitorHandlerFields } from 'langchain/callbacks/handlers/llmonitor'
import { LLMonitorHandler } from 'langchain/callbacks/handlers/llmonitor'
import { getCredentialData, getCredentialParam } from './utils' import { getCredentialData, getCredentialParam } from './utils'
import { ICommonObject, INodeData } from './Interface' import { ICommonObject, INodeData } from './Interface'
import CallbackHandler from 'langfuse-langchain' import CallbackHandler from 'langfuse-langchain'
import { LangChainTracerFields } from '@langchain/core/tracers/tracer_langchain'
import { RunTree, RunTreeConfig, Client as LangsmithClient } from 'langsmith' import { RunTree, RunTreeConfig, Client as LangsmithClient } from 'langsmith'
import { Langfuse, LangfuseTraceClient, LangfuseSpanClient, LangfuseGenerationClient } from 'langfuse' import { Langfuse, LangfuseTraceClient, LangfuseSpanClient, LangfuseGenerationClient } from 'langfuse'
import monitor from 'llmonitor' import monitor from 'llmonitor'
@ -235,11 +235,17 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
apiKey: langSmithApiKey apiKey: langSmithApiKey
}) })
const tracer = new LangChainTracer({ let langSmithField: LangChainTracerFields = {
projectName: langSmithProject ?? 'default', projectName: langSmithProject ?? 'default',
//@ts-ignore //@ts-ignore
client client
}) }
if (nodeData?.inputs?.analytics?.langSmith) {
langSmithField = { ...langSmithField, ...nodeData?.inputs?.analytics?.langSmith }
}
const tracer = new LangChainTracer(langSmithField)
callbacks.push(tracer) callbacks.push(tracer)
} else if (provider === 'langFuse') { } else if (provider === 'langFuse') {
const release = analytic[provider].release as string const release = analytic[provider].release as string
@ -248,13 +254,17 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
const langFusePublicKey = getCredentialParam('langFusePublicKey', credentialData, nodeData) const langFusePublicKey = getCredentialParam('langFusePublicKey', credentialData, nodeData)
const langFuseEndpoint = getCredentialParam('langFuseEndpoint', credentialData, nodeData) const langFuseEndpoint = getCredentialParam('langFuseEndpoint', credentialData, nodeData)
const langFuseOptions: any = { let langFuseOptions: any = {
secretKey: langFuseSecretKey, secretKey: langFuseSecretKey,
publicKey: langFusePublicKey, publicKey: langFusePublicKey,
baseUrl: langFuseEndpoint ?? 'https://cloud.langfuse.com' baseUrl: langFuseEndpoint ?? 'https://cloud.langfuse.com'
} }
if (release) langFuseOptions.release = release 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) const handler = new CallbackHandler(langFuseOptions)
callbacks.push(handler) callbacks.push(handler)
@ -262,11 +272,15 @@ export const additionalCallbacks = async (nodeData: INodeData, options: ICommonO
const llmonitorAppId = getCredentialParam('llmonitorAppId', credentialData, nodeData) const llmonitorAppId = getCredentialParam('llmonitorAppId', credentialData, nodeData)
const llmonitorEndpoint = getCredentialParam('llmonitorEndpoint', credentialData, nodeData) const llmonitorEndpoint = getCredentialParam('llmonitorEndpoint', credentialData, nodeData)
const llmonitorFields: ICommonObject = { let llmonitorFields: LLMonitorHandlerFields = {
appId: llmonitorAppId, appId: llmonitorAppId,
apiUrl: llmonitorEndpoint ?? 'https://app.llmonitor.com' apiUrl: llmonitorEndpoint ?? 'https://app.llmonitor.com'
} }
if (nodeData?.inputs?.analytics?.llmonitor) {
llmonitorFields = { ...llmonitorFields, ...nodeData?.inputs?.analytics?.llmonitor }
}
const handler = new LLMonitorHandler(llmonitorFields) const handler = new LLMonitorHandler(llmonitorFields)
callbacks.push(handler) callbacks.push(handler)
} }
@ -360,7 +374,8 @@ export class AnalyticHandler {
}, },
serialized: {}, serialized: {},
project_name: this.handlers['langSmith'].langSmithProject, 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) const parentRun = new RunTree(parentRunConfig)
await parentRun.postRun() await parentRun.postRun()
@ -390,8 +405,9 @@ export class AnalyticHandler {
const langfuse: Langfuse = this.handlers['langFuse'].client const langfuse: Langfuse = this.handlers['langFuse'].client
langfuseTraceClient = langfuse.trace({ langfuseTraceClient = langfuse.trace({
name, name,
userId: this.options.chatId, sessionId: this.options.chatId,
metadata: { tags: ['openai-assistant'] } metadata: { tags: ['openai-assistant'] },
...this.nodeData?.inputs?.analytics?.langFuse
}) })
} else { } else {
langfuseTraceClient = this.handlers['langFuse'].trace[parentIds['langFuse']] langfuseTraceClient = this.handlers['langFuse'].trace[parentIds['langFuse']]
@ -420,7 +436,8 @@ export class AnalyticHandler {
runId, runId,
name, name,
userId: this.options.chatId, userId: this.options.chatId,
input input,
...this.nodeData?.inputs?.analytics?.llmonitor
}) })
this.handlers['llmonitor'].chainEvent = { [runId]: runId } this.handlers['llmonitor'].chainEvent = { [runId]: runId }
returnIds['llmonitor'].chainEvent = runId returnIds['llmonitor'].chainEvent = runId