|
| 1 | +import { BaseCallbackHandler } from "@langchain/core/callbacks/base"; |
| 2 | +import type { Serialized } from "@langchain/core/load/serializable"; |
| 3 | +import type { BaseMessage } from "@langchain/core/messages"; |
| 4 | +import type { LLMResult } from "@langchain/core/outputs"; |
| 5 | +import { |
| 6 | + type ISupplyDataFunctions, |
| 7 | + type IExecuteFunctions, |
| 8 | + type IDataObject, |
| 9 | + NodeOperationError, |
| 10 | + type JsonObject, |
| 11 | +} from "n8n-workflow"; |
| 12 | +import { logAiEvent } from "./helpers"; |
| 13 | + |
| 14 | +type RunDetail = { |
| 15 | + index: number; |
| 16 | + messages: BaseMessage[] | string[] | string; |
| 17 | + options: any; |
| 18 | +}; |
| 19 | + |
| 20 | +export class N8nLlmTracing extends BaseCallbackHandler { |
| 21 | + name = "N8nLlmTracing"; |
| 22 | + awaitHandlers = true; |
| 23 | + |
| 24 | + connectionType = 'ai_languageModel'; |
| 25 | + #parentRunIndex?: number; |
| 26 | + runsMap: Record<string, RunDetail> = {}; |
| 27 | + |
| 28 | + constructor(private executionFunctions: ISupplyDataFunctions | IExecuteFunctions) { |
| 29 | + super(); |
| 30 | + } |
| 31 | + |
| 32 | + async handleLLMStart(llm: Serialized, prompts: string[], runId: string) { |
| 33 | + const sourceNodeRunIndex = |
| 34 | + this.#parentRunIndex !== undefined |
| 35 | + ? this.#parentRunIndex + (this.executionFunctions as any).getNextRunIndex?.() |
| 36 | + : undefined; |
| 37 | + |
| 38 | + const options = llm.type === "constructor" ? llm.kwargs : llm; |
| 39 | + const { index } = (this.executionFunctions as any).addInputData( |
| 40 | + this.connectionType, |
| 41 | + [[{ json: { messages: prompts, options } }]], |
| 42 | + sourceNodeRunIndex, |
| 43 | + ); |
| 44 | + |
| 45 | + this.runsMap[runId] = { |
| 46 | + index, |
| 47 | + options, |
| 48 | + messages: prompts, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + async handleLLMEnd(output: LLMResult, runId: string) { |
| 53 | + const runDetails = this.runsMap[runId] ?? { index: 0, options: {}, messages: [] }; |
| 54 | + |
| 55 | + const response = { response: { generations: output.generations } }; |
| 56 | + |
| 57 | + const sourceNodeRunIndex = |
| 58 | + this.#parentRunIndex !== undefined ? this.#parentRunIndex + runDetails.index : undefined; |
| 59 | + |
| 60 | + (this.executionFunctions as any).addOutputData( |
| 61 | + this.connectionType, |
| 62 | + runDetails.index, |
| 63 | + [[{ json: response }]], |
| 64 | + undefined, |
| 65 | + sourceNodeRunIndex, |
| 66 | + ); |
| 67 | + |
| 68 | + logAiEvent(this.executionFunctions, "ai-llm-generated-output", { |
| 69 | + messages: runDetails.messages, |
| 70 | + options: runDetails.options, |
| 71 | + response, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + async handleLLMError(error: IDataObject | Error, runId: string, parentRunId?: string) { |
| 76 | + const runDetails = this.runsMap[runId] ?? { index: 0, options: {}, messages: [] }; |
| 77 | + |
| 78 | + (this.executionFunctions as any).addOutputData( |
| 79 | + this.connectionType, |
| 80 | + runDetails.index, |
| 81 | + new NodeOperationError((this.executionFunctions as any).getNode(), error as JsonObject, { |
| 82 | + functionality: "configuration-node", |
| 83 | + }), |
| 84 | + ); |
| 85 | + |
| 86 | + logAiEvent(this.executionFunctions, "ai-llm-errored", { |
| 87 | + error: (error as any)?.message ?? String(error), |
| 88 | + runId, |
| 89 | + parentRunId, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + setParentRunIndex(runIndex: number) { |
| 94 | + this.#parentRunIndex = runIndex; |
| 95 | + } |
| 96 | +} |
0 commit comments