fix(AI Agent Node): Fix tool-usage with fallback mechanism (#16898)

This commit is contained in:
Benjamin Schroth
2025-07-03 09:48:44 +02:00
committed by GitHub
parent 29bf4a46bd
commit 58fd1ec325

View File

@@ -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,
]); ]);