Skip to content

Commit fd7188a

Browse files
committed
feat: 提升 LangChain 回调处理的健壮性并为 Langfuse 跟踪添加会话和用户 ID。
1 parent 4ae9312 commit fd7188a

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

backend/app/services/ai_provider_service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class AiProviderService {
7171
const rawTranscriptionApiKey = transcriptionApiKeySetting?.value
7272
const transcriptionApiKey
7373
= rawTranscriptionApiKey && rawTranscriptionApiKey !== 'null'
74-
? CryptoHelper.tryDecrypt(rawTranscriptionApiKey)
74+
? CryptoHelper.tryDecrypt(rawTranscriptionApiKey) ?? undefined
7575
: undefined
7676
const transcriptionModel = transcriptionModelSetting?.value
7777

@@ -92,7 +92,14 @@ export default class AiProviderService {
9292
/**
9393
* Get Langfuse Callback Handler for tracing
9494
*/
95-
public getLangfuseHandler(options: { tags?: string[], metadata?: Record<string, any> } = {}) {
95+
public getLangfuseHandler(
96+
options: {
97+
tags?: string[]
98+
metadata?: Record<string, any>
99+
sessionId?: string
100+
userId?: string
101+
} = {},
102+
) {
96103
if (env.get('LANGFUSE_PUBLIC_KEY')) {
97104
return new CallbackHandler(options)
98105
}

backend/app/services/evaluation_service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ export default class EvaluationService {
8484
dataSourceId: 9999, // 虚拟 ID,映射到当前的评测 SQLite 文件
8585
}
8686

87-
const result = await graph.invoke(inputs, { callbacks: [traceHandler] })
87+
const callbacks = [traceHandler].filter((h): h is NonNullable<typeof h> => !!h)
88+
const result = await graph.invoke(inputs, { callbacks })
8889
generatedSql = (result.sql || '').trim()
8990
errorMsg = result.error || ''
9091

backend/app/services/lang_chain_service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export default class LangChainService {
9191
tags: ['general_chat'],
9292
})
9393

94-
const { llm: model } = await this.getModel(false, undefined, [traceHandler])
94+
const callbacks = [traceHandler].filter((h): h is NonNullable<typeof h> => !!h)
95+
const { llm: model } = await this.getModel(false, undefined, callbacks)
9596
const messages: BaseMessage[] = [new SystemMessage(systemPrompt)]
9697
for (const m of context.history || []) {
9798
if (m.role === 'user')
@@ -180,10 +181,11 @@ export default class LangChainService {
180181

181182
try {
182183
// Use streamEvents to capture tokens and tool events
184+
const callbacks = [traceHandler].filter((h): h is NonNullable<typeof h> => !!h)
183185
const stream = app.streamEvents(inputs, {
184186
version: 'v2',
185187
recursionLimit: 10,
186-
callbacks: [traceHandler],
188+
callbacks,
187189
})
188190

189191
const streamState: StreamState = {
@@ -367,7 +369,8 @@ export default class LangChainService {
367369

368370
const config = await aiProvider.getConfig()
369371
const modelName = config.chatModel
370-
const { llm } = await this.getModel(false, undefined, [traceHandler])
372+
const callbacks = [traceHandler].filter((h): h is NonNullable<typeof h> => !!h)
373+
const { llm } = await this.getModel(false, undefined, callbacks)
371374
const dbType = context.dbType || 'mysql'
372375

373376
const prompt = SQL_OPTIMIZATION_PROMPT_TEMPLATE(

0 commit comments

Comments
 (0)