# Building AI Agents: Chat Trigger, Memory, and System/User Messages Explained [Part 1]

## Метаданные

- **Канал:** n8n
- **YouTube:** https://www.youtube.com/watch?v=yzvLfHb0nqE
- **Дата:** 29.11.2024
- **Длительность:** 20:16
- **Просмотры:** 304,255

## Описание

Watch Part 2: https://www.youtube.com/watch?v=lkudvrC3AOU

Welcome to Part 1 of the "Building AI Agents" tutorial series! In this video, Max walks you through everything you need to know to get started with AI Agents in n8n, the powerful workflow automation tool.

Here's what you'll learn:
✨ What is an AI Agent? Understand the difference between AI Agents and Large Language Models (LLMs).
💡 Building Your First Agent: Create an interactive chat-based AI Agent using n8n's Chat Trigger and AI nodes.
🧠 Memory and Context: Configure memory to maintain context across multiple messages in a conversation.
🛠️ Prompt Engineering Basics: Best practices for crafting User and System messages to optimize your AI Agent's responses.
🚀 Publishing Your Agent: Learn how to activate and share your AI Agent via n8n's chat UI.

Chapters
00:00
01:27 - Key concepts: AI Agents vs. LLMs
02:25 - What is n8n
03:30 - Build an AI Agent
05:55 - Add chat model
07:16 - Run the AI Agent
08:10 - AI Agent settings
09:54 - System messages
14:19 - Adding memory
17:16 - Activating workflow, Chat trigger
18:38 - Iterating & troubleshooting once live
19:30 - Wrap up

💬 What kind of AI Agents would you like to create? Share your ideas in the comments!

🌟 See you in Part 2, where we'll dive into integrating tools and services with your AI Agents to supercharge their capabilities!

🔗 Links and Resources:
- Sign up at n8n.io and get 50% off for 12 months with coupon code MAX50 (apply the code after your free trial)!
- docs.n8n.io for documentation
- community.n8n.io for help whilst building

## Содержание

### [1:27](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=87s) Key concepts: AI Agents vs. LLMs

days to frame what we're doing let's first Define what an AI agent is and to do that let's quickly Define what a large language model is right so generates text based on input text it's basically trying to guess what the next word is based on what you input so it's a function right input comes in output comes out an AI agent on the other hand Builds on top of what an llm can do by adding goal oriented functionality that's that task completion that we were talking about it also has the ability to leverage tools and other such dependencies in short an llm model just take some input text and output text versus an agent can take your input text feed that into an llm make multiple thoughts under the hood those thoughts can decide to use a tool it could use that tool it can ingest the output of that tool back into the llm and decide what that next step is and then finally decide in those many steps once it has the final answer once it has completed your task and output that now

### [2:25](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=145s) What is n8n

there's dozens and probably at this point hundreds of different tool tools and Frameworks and systems that help you build AI agents we're going to focus on using NN to build our AI agents and in short NN is a workflow automation tool where you can build workflows with multiple steps we call them nodes and some of those steps can be AI agents now the really neat thing about NN workflows is not every step of a use case needs to be solved by an AI agent if the first step in your workflow is to receive an email and check if it's valid there's probably a really simple Le reject function you could run to check if it meets a certain set of rules relatively speaking it's going to be a lot more expensive and laggy to try and use an AI agent for that job so the nice thing about using a Kul like NN is we have the flexibility to combine these deterministic steps the things we do with traditional programming with the power of LMS to process things like natural language and also multimodal use cases right so images audio video that sort of thing now that we've got the definitions out of way let's open up n ATN and build our first AI agent so I'm

### [3:30](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=210s) Build an AI Agent

here in my n account and I'm currently inside a project but if you're in your home section this view is going to look very similar what we're going to want to do is to create a new workflow so we'll do that now in my blank workflow there's this add First Step action that I'm going to click and this is going to open up what we call the nodes panel is where you access all the different nodes in NM that you can use to build your workflow so think of a node as a step in your flow the first option here that we need to decide on is how we trigger the work for how it runs for this example we're going to build a classic chat assistant so the user is going to type something in the AI agent's going to take that perform its task on that message and send back a response so to do that let's pick this on chat message option which is going to add the chat trigger to the niden canvas this is just one way to start an AI agentic workflow you could use a web hawk or hundreds of different triggers but this is a great way to get started because this chat trigger is integrated with nit 's chat UI so we can actually test it in line and once it's ready to go we can also publish that and then once we're ready to activate this workflow we'll be able to access a public version of this UI so basically creating your own chat GPT this trigger node's job is essentially to collect the user input and send it along to the next step so let's click this plus button here and again we're going to open up the nodes panel for most of this tutorial series we're going to be hanging out in the advanced AI section but there's hundreds of other nodes for example if you want to fetch something from an app before the AI step or do various conditional logic and whatnot and it then has all those sort of Key Parts ready for you when you need to build that out so let's go into the advanced AI section and we're going to want to add an AI agent there's various other options here for really specific use cases for example if you need to extract information from a certain input or analyze sentiment most of these you could achieve with an AI agent these are just more opinionated if you're doing that specific use case so let's go ahead and click the AI agent to add it to my workflow once I add the AI agent there's going to be a few things that we need to set up before we can run and test this so the way AI agent nodes work in N ATN is they have one or more dependencies that they need to run the chat model is always required this is the llm that's going to use under the hood to process semantic text and to think so to speak and then there's various other options that we can also add since the chat model is required let's set that up first but throughout this series we'll get into all the different dependencies and options available for AI agents so

### [5:55](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=355s) Add chat model

to add a chat model let's click this plus here now there's various different models that you can connect if you'd like to self-host llms olama is going to be a great way to go and it then has a self-hosted AI State you could leverage for that and that's going to let you leverage dozens of Open Source models that you can run on your own Hardware or on your own bare metal since I already have an open AI API key I'm going to go ahead and add the open AI chat model because I'd like to use some of their models so let's click on that the first thing you're going to want to do is to add your credential now in N credentials are separated from workflow so you can have credentials in one spot for example here I've got my open AI account that I'm leveraging in each one of my workflows that needs this chat model but if you're opening this up for the first time when you click this drop down there's going to be the create new credential button you're going to click that in here you're going to have to add your open AI API key that's out of scope of this video but if you're not sure how to do that a quick Google search uh should help you there and you only need an organization ID if you belong to various organizations so for most folks you won't have to fill this in so once you have that API key hit save and you should be good to go and then most chat model nodes you will select the specific model that you want to use so here we could use GPT 40 for example and then there's also options here now I'm not going to go into every single option but if you're ever reading an article on how to tweak some settings in an llm to get the results that you want those are

### [7:16](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=436s) Run the AI Agent

usually going to be found here in the options so now that we have our chat model set up we can test our AI agent using the chat button because we're using a chat trigger here so if I click this let's just write hello and we can see it's running and let's have a look at these logs so what's happened is we can see we got a response from our AI agent hello how can I assist you today if we open up my AI agent what we can see is we've got some input data this is from our chat trigger here we can see it sent along a session ID that's going to be great in future when we're setting up some memory the action so this was a send message and then the chat input that's the message that we just wrote and then we have some of the settings for our AI agent here that we're going to configure in a sec and we can see the output this is the message that it outputed there so inside of as you can see we've got the input data that comes into a node we can set up the settings of that node to configure how we want it to complete its step you can reference input data to do that to give it context and then it

### [8:10](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=490s) AI Agent settings

outputs something so let's have a look at the important parameters when setting up the AI agent so there's the agent type this is the first option here its default is the tools agent and we're going to focus on the tools agent throughout this tutorial series because it's the most advanced and comprehensive NN AI agent available today there are a few others but with the tools agent you can basically achieve a lot of what these other ones are doing I recommend for most folks unless you really know what you're doing just stick to this as your default the first option here is the prompt Source you can think of this as the user message and if you don't know what that is we'll explain in a moment but this is basically the task that we're asking the AI agent to complete so AI agents take a task a question then they try to answer they try to complete the thing they were asked to do in here the default is take from previous node automatically so what this does is it expects in its input some input data with the key of chat input so if I go over to Json view here we can see this is actually how the data that's coming into this node is represented so we've got one item of data coming in and it's got this chat input top level text variable here that's set to hello so as we can see that's what it's referencing here now I could overwrite that I could click on prompt source and change it to Define below and then here I could type out static text so I could say here is my Tusk right and now if I run this of course please go ahead and let me know what task you'd like assistance with it's responding to this text here in short be it using the chat trigger and automatically piping that in through the taken from previous nodes automatically option or through defined below it's in this text or this text from previous node where you're defining the task the thing that you want your AI agent to accomplish now there's a few other parameters that you can use to control how your AI agent completes your

### [9:54](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=594s) System messages

task but one of the most important ways to control the results and the output and basically the efficacy of an AI agentic workflow is with what is called the system message which you can add from the options in here so if I click to add that you can see it does have a default message which is you are a helpful assistant so we're going to cover a few best practices for system messages but I could do a whole multi-part tutorial series on system messages themselves so know that this is General guidance and it will depend on your specific use case nonetheless there are some best practices that you can abide by before we get into those you can see already we've got two basically places where we're providing text to our AI agent to modulate or to inform how it's going to complete the task right so we have the prompt Source or the user message as it's called in some of the docs you might read for AI models and then we have the system message so what's the difference between the two so in the user message goes the actual task the instruction for the thing that you want done and then the system message defines the context behavior and rules for how that task should be completed think of the system message as an instruction manual on the tone perspective and functionality of how basically your AI agent completes the tasks it's asked to do crafting your user messages and system messages are what's collectively known as prompt engineering now again that could be a whole tutorial series and in fact it is an entire profession so let's cover a few Basics right now to get you started but we will do more of a deep dive throughout this tutorial Series so my bare bones checklist when I'm creating system messages is it should define the role of your AI this can be a direct voice like you are a helpful assistant for example this is the default message here it's defining the role of the assistant the next thing I typically like to provide is the style so this is the tone a great example might be concise and avoid jargon that's telling it how the output should sound what words it should gravitate towards what length of the text it should be outputting and then the last part of my simple checklist is to make sure that you're adding boundaries to the task now obviously that can look different from task to task but a great example of boundaries would be don't hallucinate that's something if you've been in the AI space for a little bit you've definitely heard these prompting hacks like don't hallucinate which improve results of these L agents a more tangible example of that could be something like ask clarifying questions if you're not sure what the user wants to ask or don't make assumptions the system message is a great place also to include static context especially with these newer AI models that have very large context windows so an example of a static context could be the time right now the GPT model that we're using right now does not know what time it is right now in the system message this is a great place where we could add that kind of static context static in the sense of it's not something AI agents using as a tool at runtime so to add this context we can actually use inid in Expressions so I'll switch this to an expression and what this is going to let me do is basically combine JavaScript with my static text very similar to how a mail merge might work in a email marketing program to do that I'll hit open parenthesis and we see Ed 's auto complete here and I can use the now variable here and I'll add that and this is including the date time stamp here now we can see it's including this date time here I could write something like today is and now we have the date and the time and even the time zone so if we run this again what date is it today we can see that today is November 26th and that is correct that's when I'm shooting this video so that's a very simple example but it shows how we can add context as a system message how we can modulate its response another great example could be output in German so now when you ask what date is it today is of November my German's not that good and as you saw I'm just writing these in sentences here grouping this by sections and different syntax around prompt writing is definitely helpful here's a quick example template you can borrow already today but we'll get into the details in future videos here we've added a chat model we're sending in the user message and we're also feeding in context in the system prompt and modulating the tone or the output so that it's in German now

### [14:19](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=859s) Adding memory

that we've got the basics working let's have a look at these other dependencies and see how we can leverage those in our AI workflows so memory gives your AI agents state fness so the way a chat model works today is if I send in a message its job is to predict how to complete the output it's a function right I send in some text it will output some text it does not remember memory in context and when you use tools for example like chat gbt it's actually using a memory functionality under the hood and remembering your messages together and then when it sends it to their jpt model it sends a stack of those messages so that it has the extra context that's what n atn's memory feature does as well here so you don't have to m manage that stack to highlight this let's try to have it remember some stuff between these executions and we'll see that it's not able to do that so let's change this back to English so I can communicate with it and let's give it something that it has to remember so the number is three what is the number so it should reply with three okay the number is three now if I ask what is the number it's not going to know what I'm talking about because it does not know that this happened so let's change that by adding some memory I'll click the plus here we have a few different memory options so the easiest one as it's labeled here is the window buffer memory let's click now how memory works at a high level is it expects a session ID every time you reference the same session ID it finds that session and it'll append that latest message to that session creating basically an array of messages or a stack of messages the way it works in N ATN if you're using the chat trigger is there's really nothing to set up by default it's taking the session ID from the prev node where Auto piping in that session ID you could however Define that below but let's keep it on the default now and then the default context window length is five this means it's only going to get the last five messages which for now is fine but if you're doing a chat based use case you're probably going to want to bump that up so now that we've attached some memory let's try that example again so let's say the number is five what is the number we can see we've run the memory as well so we check that okay the number is five great what is the number is five so now that we've run something with multiple dependencies we can see how the logs could be useful here we see the order in which the AI called its various dependencies so we can see at first it used the window buffer memory and added this message to the buffer under the hood it also checked if there was any messages previously there wasn't because we had just added it right and then made the call to the open aai chat model and then to the window buffer again now we have an AI agent that can respond to your task using a chat model and it has statefulness which means it can remember a chat session and multiple messages so

### [17:16](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=1036s) Activating workflow, Chat trigger

has that context as well let's say we've tested my AI agent and it's ready to go admittedly this is not the most sophisticated example but let's say we built it out and we want to publish it we want to use it in prod so to speak what we could is double click on the chat trigger and make the chat publicly available and we can copy this URL and then what we're going to want to do is save the workflow and activate it this is going to make sure that public URL we just copied is live so now if we go to a new tab and enter that URL we can see this chat UI which is being served by my edn workflow and so here I could ask what is the time right now and because we're feeding that in as context we can see it's giving the right time now there's some defaults in here like the text and whatnot those can be customized in the chat trigger you can also attach some authentication at top so you can ask someone to have to enter a username and password before they use this form which is super useful if your AI agent can interact with your proprietary systems which we'll cover in the next video when we get into the tools that the AI agents can do and Ned's chat trigger can also handle various options like allowing the US to upload files and whatnot so there's options for all those sorts of things as well these aren't complicated settings and there's documentation for this stuff as well so just go check that out if you're curious on any of those

### [18:38](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=1118s) Iterating & troubleshooting once live

details now that the workflow is active if we go over to the executions tab here we can see the various executions of this workflow if there's a beaker icon next to it means it was a test execution which means that we ran it in this editor otherwise if there isn't one it means it was a production execution which in this case means it was run through this uh web U right here there's a super useful feature that I'm going to call out when you're inspecting your production runs and you want to perhaps iterate on your AI agent or troubleshoot an edge case when you have your execution open this is true for an error execution or successful execution is you can copy the data of this workflow run to the editor so if I copy to editor I can now open up my AI agent and I can see the state of its data when it ran I can see the logs from when it ran as well it's useful for troubleshooting and iterating once you published your AI

### [19:30](https://www.youtube.com/watch?v=yzvLfHb0nqE&t=1170s) Wrap up

agent that's all for part one of the intro to AI agents tutorial Series in part two we'll explore setting up tools for your AI agents so they can interact with apps Services databases and even perform mathematical operations this series is part of my work at NN Studio where we build Ai and automation projects in public so make sure to subscribe to catch part two of the series when it comes out and for updates on future projects I'm Max catch you in part two now

---
*Источник: https://ekstraktznaniy.ru/video/15535*