mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(AI Agent Node): Fix tool-usage with fallback mechanism (#16898)
This commit is contained in:
@@ -1,7 +1,11 @@
|
|||||||
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
||||||
import type { ChatPromptTemplate } from '@langchain/core/prompts';
|
import type { ChatPromptTemplate } from '@langchain/core/prompts';
|
||||||
import { RunnableSequence } from '@langchain/core/runnables';
|
import { RunnableSequence } from '@langchain/core/runnables';
|
||||||
import { AgentExecutor, createToolCallingAgent } from 'langchain/agents';
|
import {
|
||||||
|
AgentExecutor,
|
||||||
|
type AgentRunnableSequence,
|
||||||
|
createToolCallingAgent,
|
||||||
|
} from 'langchain/agents';
|
||||||
import type { BaseChatMemory } from 'langchain/memory';
|
import type { BaseChatMemory } from 'langchain/memory';
|
||||||
import type { DynamicStructuredTool, Tool } from 'langchain/tools';
|
import type { DynamicStructuredTool, Tool } from 'langchain/tools';
|
||||||
import omit from 'lodash/omit';
|
import omit from 'lodash/omit';
|
||||||
@@ -38,16 +42,24 @@ function createAgentExecutor(
|
|||||||
memory?: BaseChatMemory,
|
memory?: BaseChatMemory,
|
||||||
fallbackModel?: BaseChatModel | null,
|
fallbackModel?: BaseChatModel | null,
|
||||||
) {
|
) {
|
||||||
const modelWithFallback = fallbackModel ? model.withFallbacks([fallbackModel]) : model;
|
|
||||||
const agent = createToolCallingAgent({
|
const agent = createToolCallingAgent({
|
||||||
llm: modelWithFallback,
|
llm: model,
|
||||||
tools,
|
tools,
|
||||||
prompt,
|
prompt,
|
||||||
streamRunnable: false,
|
streamRunnable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let fallbackAgent: AgentRunnableSequence | undefined;
|
||||||
|
if (fallbackModel) {
|
||||||
|
fallbackAgent = createToolCallingAgent({
|
||||||
|
llm: fallbackModel,
|
||||||
|
tools,
|
||||||
|
prompt,
|
||||||
|
streamRunnable: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
const runnableAgent = RunnableSequence.from([
|
const runnableAgent = RunnableSequence.from([
|
||||||
agent,
|
fallbackAgent ? agent.withFallbacks([fallbackAgent]) : agent,
|
||||||
getAgentStepsParser(outputParser, memory),
|
getAgentStepsParser(outputParser, memory),
|
||||||
fixEmptyContentMessage,
|
fixEmptyContentMessage,
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user