UNPKG

@n8n/n8n-nodes-langchain

Version:

![Banner image](https://user-images.githubusercontent.com/10284570/173569848-c624317f-42b1-45a6-ab09-f0ea3c247648.png)

122 lines 6.25 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _N8nNonEstimatingTracing_parentRunIndex; Object.defineProperty(exports, "__esModule", { value: true }); exports.N8nNonEstimatingTracing = void 0; const base_1 = require("@langchain/core/callbacks/base"); const pick_1 = __importDefault(require("lodash/pick")); const n8n_workflow_1 = require("n8n-workflow"); const ai_utilities_1 = require("@n8n/ai-utilities"); class N8nNonEstimatingTracing extends base_1.BaseCallbackHandler { constructor(executionFunctions, options) { super(); this.executionFunctions = executionFunctions; this.name = 'N8nNonEstimatingTracing'; this.awaitHandlers = true; this.connectionType = n8n_workflow_1.NodeConnectionTypes.AiLanguageModel; _N8nNonEstimatingTracing_parentRunIndex.set(this, void 0); this.runsMap = {}; this.options = { errorDescriptionMapper: (error) => error.description, }; this.options = { ...this.options, ...options }; } async handleLLMEnd(output, runId) { const runDetails = this.runsMap[runId] ?? { index: Object.keys(this.runsMap).length }; output.generations = output.generations.map((gen) => gen.map((g) => (0, pick_1.default)(g, ['text', 'generationInfo']))); const tokenUsageEstimate = { completionTokens: 0, promptTokens: 0, totalTokens: 0, }; const response = { response: { generations: output.generations }, }; response.tokenUsageEstimate = tokenUsageEstimate; const parsedMessages = typeof runDetails.messages === 'string' ? runDetails.messages : runDetails.messages.map((message) => { if (typeof message === 'string') return message; if (typeof message?.toJSON === 'function') return message.toJSON(); return message; }); const sourceNodeRunIndex = __classPrivateFieldGet(this, _N8nNonEstimatingTracing_parentRunIndex, "f") !== undefined ? __classPrivateFieldGet(this, _N8nNonEstimatingTracing_parentRunIndex, "f") + runDetails.index : undefined; this.executionFunctions.addOutputData(this.connectionType, runDetails.index, [[{ json: { ...response } }]], undefined, sourceNodeRunIndex); (0, ai_utilities_1.logAiEvent)(this.executionFunctions, 'ai-llm-generated-output', { messages: parsedMessages, options: runDetails.options, response, }); } async handleLLMStart(llm, prompts, runId) { const estimatedTokens = 0; const sourceNodeRunIndex = __classPrivateFieldGet(this, _N8nNonEstimatingTracing_parentRunIndex, "f") !== undefined ? __classPrivateFieldGet(this, _N8nNonEstimatingTracing_parentRunIndex, "f") + this.executionFunctions.getNextRunIndex() : undefined; const options = llm.type === 'constructor' ? llm.kwargs : llm; const { index } = this.executionFunctions.addInputData(this.connectionType, [ [ { json: { messages: prompts, estimatedTokens, options, }, }, ], ], sourceNodeRunIndex); this.runsMap[runId] = { index, options, messages: prompts, }; } async handleLLMError(error, runId, parentRunId) { const runDetails = this.runsMap[runId] ?? { index: Object.keys(this.runsMap).length }; if (typeof error === 'object' && error?.hasOwnProperty('headers')) { const errorWithHeaders = error; Object.keys(errorWithHeaders.headers).forEach((key) => { if (!key.startsWith('x-')) { delete errorWithHeaders.headers[key]; } }); } if (error instanceof n8n_workflow_1.NodeError) { if (this.options.errorDescriptionMapper) { error.description = this.options.errorDescriptionMapper(error); } this.executionFunctions.addOutputData(this.connectionType, runDetails.index, error); } else { this.executionFunctions.addOutputData(this.connectionType, runDetails.index, new n8n_workflow_1.NodeOperationError(this.executionFunctions.getNode(), error, { functionality: 'configuration-node', })); } (0, ai_utilities_1.logAiEvent)(this.executionFunctions, 'ai-llm-errored', { error: Object.keys(error).length === 0 ? error.toString() : error, runId, parentRunId, }); } setParentRunIndex(runIndex) { __classPrivateFieldSet(this, _N8nNonEstimatingTracing_parentRunIndex, runIndex, "f"); } } exports.N8nNonEstimatingTracing = N8nNonEstimatingTracing; _N8nNonEstimatingTracing_parentRunIndex = new WeakMap(); //# sourceMappingURL=N8nNonEstimatingTracing.js.map