feat: Add model selector node (#16371)

This commit is contained in:
Benjamin Schroth
2025-06-20 15:30:33 +02:00
committed by GitHub
parent a9688b101f
commit 79650ea55a
20 changed files with 1321 additions and 113 deletions

View File

@@ -41,6 +41,8 @@ export class N8nLlmTracing extends BaseCallbackHandler {
completionTokensEstimate = 0;
#parentRunIndex?: number;
/**
* A map to associate LLM run IDs to run details.
* Key: Unique identifier for each LLM run (run ID)
@@ -141,9 +143,16 @@ export class N8nLlmTracing extends BaseCallbackHandler {
return message;
});
this.executionFunctions.addOutputData(this.connectionType, runDetails.index, [
[{ json: { ...response } }],
]);
const sourceNodeRunIndex =
this.#parentRunIndex !== undefined ? this.#parentRunIndex + runDetails.index : undefined;
this.executionFunctions.addOutputData(
this.connectionType,
runDetails.index,
[[{ json: { ...response } }]],
undefined,
sourceNodeRunIndex,
);
logAiEvent(this.executionFunctions, 'ai-llm-generated-output', {
messages: parsedMessages,
@@ -154,19 +163,27 @@ export class N8nLlmTracing extends BaseCallbackHandler {
async handleLLMStart(llm: Serialized, prompts: string[], runId: string) {
const estimatedTokens = await this.estimateTokensFromStringList(prompts);
const sourceNodeRunIndex =
this.#parentRunIndex !== undefined
? this.#parentRunIndex + this.executionFunctions.getNextRunIndex()
: undefined;
const options = llm.type === 'constructor' ? llm.kwargs : llm;
const { index } = this.executionFunctions.addInputData(this.connectionType, [
const { index } = this.executionFunctions.addInputData(
this.connectionType,
[
{
json: {
messages: prompts,
estimatedTokens,
options,
[
{
json: {
messages: prompts,
estimatedTokens,
options,
},
},
},
],
],
]);
sourceNodeRunIndex,
);
// Save the run details for later use when processing `handleLLMEnd` event
this.runsMap[runId] = {
@@ -218,4 +235,9 @@ export class N8nLlmTracing extends BaseCallbackHandler {
parentRunId,
});
}
// Used to associate subsequent runs with the correct parent run in subnodes of subnodes
setParentRunIndex(runIndex: number) {
this.#parentRunIndex = runIndex;
}
}