Skip to content

Commit

Permalink
feat: Add option to returnIntermediateSteps for AI agents (#8113)
Browse files Browse the repository at this point in the history
## Summary
![CleanShot 2023-12-21 at 08 30
16](https://github.com/n8n-io/n8n/assets/12657221/66b0de47-80cd-41f9-940e-6cacc2c940a9)

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
  • Loading branch information
OlegIvaniv committed Dec 21, 2023
1 parent 6580b92 commit 7806a65
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export const conversationalAgentProperties: INodeProperties[] = [
default: 10,
description: 'The maximum number of iterations the agent will run before stopping',
},
{
displayName: 'Return Intermediate Steps',
name: 'returnIntermediateSteps',
type: 'boolean',
default: false,
description: 'Whether or not the output should include intermediate steps the agent took',
},
],
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export async function conversationalAgentExecute(
systemMessage?: string;
humanMessage?: string;
maxIterations?: number;
returnIntermediateSteps?: boolean;
};

const agentExecutor = await initializeAgentExecutorWithOptions(tools, model, {
Expand All @@ -50,6 +51,7 @@ export async function conversationalAgentExecute(
// memory option, but the memoryKey set on it must be "chat_history".
agentType: 'chat-conversational-react-description',
memory,
returnIntermediateSteps: options?.returnIntermediateSteps === true,
maxIterations: options.maxIterations ?? 10,
agentArgs: {
systemMessage: options.systemMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export const openAiFunctionsAgentProperties: INodeProperties[] = [
default: 10,
description: 'The maximum number of iterations the agent will run before stopping',
},
{
displayName: 'Return Intermediate Steps',
name: 'returnIntermediateSteps',
type: 'boolean',
default: false,
description: 'Whether or not the output should include intermediate steps the agent took',
},
],
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export async function openAiFunctionsAgentExecute(
const options = this.getNodeParameter('options', 0, {}) as {
systemMessage?: string;
maxIterations?: number;
returnIntermediateSteps?: boolean;
};

const agentConfig: AgentExecutorInput = {
Expand All @@ -49,6 +50,7 @@ export async function openAiFunctionsAgentExecute(
}),
tools,
maxIterations: options.maxIterations ?? 10,
returnIntermediateSteps: options?.returnIntermediateSteps === true,
memory:
memory ??
new BufferMemory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export const reActAgentAgentProperties: INodeProperties[] = [
rows: 6,
},
},
{
displayName: 'Return Intermediate Steps',
name: 'returnIntermediateSteps',
type: 'boolean',
default: false,
description: 'Whether or not the output should include intermediate steps the agent took',
},
],
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function reActAgentAgentExecute(
suffix?: string;
suffixChat?: string;
humanMessageTemplate?: string;
returnIntermediateSteps?: boolean;
};
let agent: ChatAgent | ZeroShotAgent;

Expand All @@ -50,7 +51,11 @@ export async function reActAgentAgentExecute(
});
}

const agentExecutor = AgentExecutor.fromAgentAndTools({ agent, tools });
const agentExecutor = AgentExecutor.fromAgentAndTools({
agent,
tools,
returnIntermediateSteps: options?.returnIntermediateSteps === true,
});

const returnData: INodeExecutionData[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ export class ToolWikipedia implements INodeType {
};

async supplyData(this: IExecuteFunctions): Promise<SupplyData> {
const WikiTool = new WikipediaQueryRun();

WikiTool.description =
'A tool for interacting with and fetching data from the Wikipedia API. The input should always be a string query.';

return {
response: logWrapper(new WikipediaQueryRun(), this),
response: logWrapper(WikiTool, this),
};
}
}

0 comments on commit 7806a65

Please sign in to comment.