Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AI Agent Node): Fix tools agent when using memory and Anthropic models #10513

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,59 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
/**
* Handles the agent text output and transforms it in case of multi-output.
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
*
* This method is necessary to handle different output formats from various language models.
* Specifically, it checks if the agent step is the final step (contains returnValues) and determines
* if the output is a simple string (e.g., from OpenAI models) or an array of outputs (e.g., from Anthropic models).
*
* If the output is an array of text outputs, this method will concatenate them into a single string,
* ensuring compatibility with downstream processes that expect a single output string.
*
* Examples:
* 1. Anthropic model output:
* {
* "output": [
* {
* "index": 0,
* "type": "text",
* "text": "The result of the calculation is approximately 1001.8166..."
* }
* ]
* }
*
* 2. OpenAI model output:
* {
* "output": "The result of the calculation is approximately 1001.82..."
* }
*
* This method ensures consistent handling of outputs regardless of the model used,
* providing a unified output format for further processing.
*
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
* This method is necessary to handle different output formats from various language models.
* Specifically, it checks if the agent step is the final step (contains returnValues) and determines
* if the output is a simple string (e.g., from OpenAI models) or an array of outputs (e.g., from Anthropic models).
*
* Examples:
* 1. Anthropic model output:
* ```json
* {
* "output": [
* {
* "index": 0,
* "type": "text",
* "text": "The result of the calculation is approximately 1001.8166..."
* }
* ]
* }
*```
* 2. OpenAI model output:
* ```json
* {
* "output": "The result of the calculation is approximately 1001.82..."
* }
* ```
*
* This method ensures consistent handling of outputs regardless of the model used.
*
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
* @param steps - The agent finish or agent action steps.
* @returns The modified agent finish steps or the original steps.
*/
Expand All @@ -108,13 +161,14 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE

if (agentFinishSteps.returnValues) {
const isMultiOutput = Array.isArray(agentFinishSteps.returnValues?.output);

if (isMultiOutput) {
// Define the type for each item in the multi-output array
type MultiOutputItem = { index: number; type: string; text: string };
const multiOutputSteps = agentFinishSteps.returnValues.output as MultiOutputItem[];

// Check if all items in the multi-output array are of type 'text'
const isTextOnly = (multiOutputSteps ?? []).every((output) => output.type === 'text');
const isTextOnly = (multiOutputSteps ?? []).every((output) => 'text' in output);

if (isTextOnly) {
// If all items are of type 'text', merge them into a single string
Expand All @@ -128,7 +182,7 @@ export async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeE
}

// If the steps do not contain multiple outputs, return them as is
return steps;
return agentFinishSteps;
}
async function agentStepsParser(
steps: AgentFinish | AgentAction[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import { OllamaEmbeddings } from '@langchain/ollama';
import { OllamaEmbeddings } from '@langchain/community/embeddings/ollama';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { ollamaDescription, ollamaModel } from '../../llms/LMOllama/description';
Expand Down
26 changes: 13 additions & 13 deletions packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@
"@google-cloud/resource-manager": "5.3.0",
"@google/generative-ai": "0.11.4",
"@huggingface/inference": "2.7.0",
"@langchain/anthropic": "0.2.15",
"@langchain/anthropic": "0.2.9",
"@langchain/cohere": "0.0.10",
"@langchain/community": "0.2.28",
"@langchain/core": "0.2.27",
"@langchain/google-genai": "0.0.26",
"@langchain/google-vertexai": "0.0.26",
"@langchain/groq": "0.0.17",
"@langchain/mistralai": "0.0.28",
"@langchain/ollama": "^0.0.4",
"@langchain/openai": "0.2.7",
"@langchain/pinecone": "0.0.9",
"@langchain/community": "0.2.20",
"@langchain/core": "0.2.18",
"@langchain/google-genai": "0.0.23",
"@langchain/google-vertexai": "0.0.21",
"@langchain/groq": "0.0.15",
"@langchain/mistralai": "0.0.27",
"@langchain/ollama": "^0.0.2",
"@langchain/openai": "0.2.5",
"@langchain/pinecone": "0.0.8",
"@langchain/qdrant": "^0.0.5",
"@langchain/redis": "0.0.5",
"@langchain/textsplitters": "0.0.3",
Expand All @@ -156,7 +156,7 @@
"@n8n/vm2": "3.9.25",
"@pinecone-database/pinecone": "3.0.0",
"@qdrant/js-client-rest": "1.9.0",
"@supabase/supabase-js": "2.45.1",
"@supabase/supabase-js": "2.43.4",
"@types/pg": "^8.11.6",
"@xata.io/client": "0.28.4",
"basic-auth": "catalog:",
Expand All @@ -169,12 +169,12 @@
"html-to-text": "9.0.5",
"jsdom": "^23.0.1",
"json-schema-to-zod": "2.1.0",
"langchain": "0.2.16",
"langchain": "0.2.11",
"lodash": "catalog:",
"mammoth": "1.7.2",
"n8n-nodes-base": "workspace:*",
"n8n-workflow": "workspace:*",
"openai": "4.56.0",
"openai": "4.53.0",
"pdf-parse": "1.1.1",
"pg": "8.12.0",
"redis": "4.6.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dist/**/*"
],
"devDependencies": {
"@langchain/core": "^0.2.27",
"@langchain/core": "^0.2.18",
"@types/deep-equal": "^1.0.1",
"@types/express": "catalog:",
"@types/jmespath": "^0.15.0",
Expand Down
Loading
Loading