Build a Simple AI Agent with n8n

In the last article, I explained what AI Agents are and what they are used for. In this article, I will show you an example of an AI Agent that is not very specific but can help you understand the concept. We will focus on the general structure and concepts.

This is the visual representation of the AI Agent we will discuss.

n8n as a powerful AI Agent tool

The most widely used tool to implement AI Agents is n8n. It allows you to drag and drop nodes that perform different tasks and connect them. That is how you set up the AI Agent to assist you in your daily tasks. Adding different nodes and connecting them the right way gives your assistant its unique set of features, capabilities, and personality.

In n8n, you can not only position and connect functional nodes, but you can also use colored rectangles and text. This can be used to designate sections of the node tree so that it is easier to read and extend later. Next, I will go through all the different sections of my Agent and explain what the sections are used for and what the nodes inside mean. After reading this article, you could start using n8n and try to recreate a simple AI Agent, too.

1. Get chatInput

In order to communicate with our assistant, we need to connect it to a chat service. For my assistant, I chose Telegram because it is the easiest to set up. You need to create a Telegram Bot inside your Telegram application and use the API Key to connect the newly created bot to your AI Agent. One by one, we will go through the different nodes in the image.

2. AI Agent

This section is the brain of our AI Agent. It contains the AI Agent node that connects the most important parts. The node has three connections that can be configured by attaching nodes to the sockets.

The AI Agent node starts to act whenever it is triggered from a path entering from the left side. Since this is the brain of the AI Agent, it does not run in a linear way. The node uses the LLM to understand the task and to create responses. It then uses the memory and the attached tools to fulfill the objective. It performs many iterations to create the best result. For example, the LLM understands the objective and uses a tool, but then the LLM thinks the result is not good enough, so the tool is used again with different input. Later on, the memory is considered, and so on. The iterations continue until the AI Agent is convinced that the output is satisfactory. You can configure how many iterations it should use at most.

But we also need to give the AI Agent context so that it acts in the way we want it to. You can configure the node to act according to a given context. For this simple assistant, I added the following context: “You are a helpful assistant named Amy.” Here, you could explain in detail what personality your assistant should have. As a joke, you could tell the assistant to always act like Sheldon Cooper, with all the disadvantages that would come with that.

When adding this to my assistant, it acted like this:

Who are you? ___ Bazinga! I am Amy, a helpful assistant. However, I am not the Amy Farrah Fowler who is a neurobiologist with a Ph.D. and a member of my social group. I am merely a digital construct, designed to assist you with a variety of tasks, much like a highly efficient, yet slightly less sarcastic, supercomputer.

3. Tools

These nodes are connected to the tools socket of the AI Agent node. The connection to the third-party system is established the same as for the AI models, through Credentials that are managed in n8n.

4. Output

This section is responsible for sending a response to the user who initiated the process. The second node is just the default Telegram node that sends a response. While the second node would technically be enough, I noticed that sometimes the Google Gemini LLM creates text containing certain symbols that the Telegram node does not support. Therefore, I placed a Custom JavaScript Code node in front to replace these specific symbols.

JS
// Convert double asterisks to single asterisk for bold
$input.first().json.output = $input.first().json.output.replace(/\*\*(.*?)\*\*/g, '*$1*');
// Replace list asterisks at line start with dash
$input.first().json.output = $input.first().json.output.replace(/^\s*\*\s+/gm, '\\- ');
// Escape parentheses and dots and dashes
$input.first().json.output = $input.first().json.output.replace(/([()])/g, '\\$1');
$input.first().json.output = $input.first().json.output.replace(/(?<!\\)\./g, '\\.');
$input.first().json.output = $input.first().json.output.replace(/(?<!\\)-/g, '\\-');
$input.first().json.output = $input.first().json.output.replace(/(?<!\\)#/g, '\\#');
$input.first().json.output = $input.first().json.output.replace(/(?<!\\)!/g, '\\!');
return $input.first();
Click to expand and view more

Result

Now you have seen an example for a simple AI Agent implemented using n8n. We did not went into detail. I will do it in the next articles. But you could already start to use n8n and test what you already learned on your own.

Comments

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut