# AI LangChain Workshop Highlight Video

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

- **Канал:** n8n
- **YouTube:** https://www.youtube.com/watch?v=6-4yruDaXdA
- **Дата:** 23.10.2023
- **Длительность:** 33:28
- **Просмотры:** 10,343
- **Источник:** https://ekstraktznaniy.ru/video/15694

## Описание

Join Nikki & Oleg as they showcase the incredible new AI LangChain nodes now available at n8n.io

For more details visit: https://n8n.io/langchain/
For details of our $1000 AI LangChain Contest, visit : https://bit.ly/n8nLangChainContest

Timestamps

00:32 - Intro
03:15 - AI Chat Agent 
17:43 - AI Summarisation
28:24 - Vector Refinement
33:09 - Links & Contest

## Транскрипт

### Intro [0:32]

chain myself so it's been a c Journey for those of you who don't know what a lang chain is um what nen has done is create a series of features and workflow nodes that can make this more accessible to you um you most people will probably be familiar what AI is more specifically you have ai chat Bots and AI um sources that can actually process information for you and use it in interpret it into a in a way that's usable um but o would you like to kind of dive a little deeper into what we have been working on here at na we heard voices from the community about uh integrating the L chain that people are and we've seen people using L chain uh a lot to build AI applications because uh we thought it integrate it would integrate so well with all the data streams and U options that you have with nent with how you can work with your data get your data into anent process it and then pass it into the these AI workflows uh we started building this uh sets of noes and generally the features around it to be able to create these AI workflows there's several ways for you to get data in N1 so I mentioned we have slack email uh from getting it from web hook or just requesting it from somewhere and then you can wrap it all up pass it to the agent or chain and do some uh decisions on top of it it's super interesting how you can use these language models as sort of a back end and let it make decisions like in your app that you would otherwise have to program for uh so like you don't have to handle every single use case you can just give the language model an input and it will be able to figure out based on your promp what it what you want it to do and give you uh output that makes sense for it uh yeah this is definitely where your data plays a role uh it also decreases the amount of hallucinations this model makes if you give some data and you ask it to only work on top of that data or to only provide you answer on top of that data um not a good aspect and you mention that you can start very quickly and we're going to see it in a bit I think where you can literally go into the templates click on a template provide your credentials and that said you have a chat B that you can connect your a to student we're going to create three workflows today one of them is going to be sort of a main workflow which will have an agent uh that will be communicating with via chat it's going to have a memory and then we're going to create two workflows to give this agent some uh tools so some things that it can do except for answering the questions um yeah let's get right into it because as you said it might take some time I'm going to go into

### AI Chat Agent [3:15]

templates and click on the advanced AI uh as you mentioned there's a bunch of templates that you can use for whatever use case you might have this start as a building for your workflow but um uh specifically interested in the slack chat box Byer by AI click on the use workflow and it gives me these notes already set up it gives me the stickies that explain what these notes do uh I just need to create set up the credentials so let me do that now what I done I've opened the chat open AI which is the language model node that connects uh to an agent and I'm going to set up the credentials first so I'll set the open AI credentials I select my model which is gbd 3. 5 turbo 16k in this case and I'll just leave the default sampling temperature for now um I also need to set up the web hook to listen for the slack messages and what I've already done is I've set up the credentials in slack so when you're setting up a new web hook in slack you would have to go through this setting up step where you basically respond to a vbook challenge you respond back with that challenge it's very simple to do but I just uh do not lose time now so I've already done that so you see that I have to explain what I'm doing here in the B Hook where just waiting on this address uh for a call and that call is going to come from slack so my path is guil foil minus web hook because that's the name of the bot that I'm using for testing and this is the URL and then once you set your workflow to active here uh your app or the slack would have to use the production URL that you could see here uh but let's stick with the test URL because that's what we have set up uh so we have the thatb hook ready uh we can test that it's actually doing something by sending a message to Gil poil here and you see that the execution or the listening stopped we got an output here and we got the data about the message uh some headers uh this is the body which contains the um message see here text hello and it contains bunch of other information that we're going to use throughout the workflow to yeah build this SL bot uh next step is we filtered out the nonuser messages because the uh slack would fire U fire a vetbook request also for your Bot so we have to make sure that uh we don't process these messages uh because we only care about what user says to the agent we don't want it to talk back to itself uh then we have our agent uh it's a bit spicy so it we tell it that it's in Gil foil from Silicon W TV show amplify your bluntness and cynicism dollar rating zero incompetence be yeah you can um so these connections that are coming here from the bottom and these are what we call configuration ND uh so they provide some functionality to like this root node uh so we see that this root node agent is utilizing model that one is required so we connected chat open AI uh and with a conversational uh agent you can only use chat models so you see that uh when I click here on the model I click on the post connection uh here there are only chat models uh then we have a memory which is not required so your agent put on completely fine without it but it just wouldn't be able to uh remember things and we'll go into the setup of the memory in a bit uh we have some tools that we give to the agent that it can use here we have Vicky and ser API uh we won't be needing that so just keep Vicky for now and uh finally there's an out parser that allows you to provide it as uh either pars it in list or provided with the schema or Json schema to what you expect your Json output to be of uh this node uh but this is fine for let's check how the memory is set up because that's an important aspect to how this SL Bo is going to work so if I open the memory uh this is the configuration for it and we can click here on mapping so uh we have two modes in the input we have mapping and debugging would show you the current execution input to this note while with mapping you can map execution data from the previous notes sort of from the parent of your root node so we check the that hook note and we are going to set a session key so what how memory we're using window buffer memory which stores your conversation based on the session key that you provided the session key is includes the workflow ID and it includes whatever you write here so we want this agent to be able to remember our user so we're going to key it uh the session based on the user ID or we could also use the thread ID we would just want it to have a memory within a threat or we can do combinations of any of those or any other attributes uh but let's use user for now so we see that the slack uh response uh this is the VB hook here it gives us the user which is what we're going to use so let me just get rid of this to make it empty uh you see here that I click to expression to be able to write the expressions and I'll just drag it from the left panel user to here uh we get undefined because we haven't executed it yet uh but uh it's going to be there we also configure here the window uh context window length which is how many messages in history it's going to store or it's going to remember so that's memory setup we also have Wikipedia we give it Wikipedia as one of the tools that it can use for now um in a bit we going to add it more tools but let's just keep Wikipedia for now for testing and uh then we have a slack note that is going to respond the uh to the message so let's see how that is set up uh again I need to set up my credential and here we can see that it's uh it's getting the user we need to fill in which user is going to send the message to so we do it by cting sent user by ID here again we need to provide it the ID which we can drag uh same as in the previous step so that would be this thing drag it in we got it and uh we want to give it a message so the message would be the output of an agent um and that's it we just basically changed the credentials to set it up for our SL credentials and now it should uh work if we go execute workflow MH that will start theb hook test listener so now I should be ready to say hello um and you see that it triggered the event uh and it provided a response here very polite now let's uh so now if I would say something again nothing would happen because that web Hook is not listening anymore because in the test mode like when you click on the execute workl and you're not you don't have it active uh it's only listen once then it's going to terminate The Listener so you would have to click on the execute workflow again uh to get any response that's a good reminder to show you the debugging so uh i' I've opened this agent uh agent note and here it has two tabs it has an output which is what we are sending to the slack to send back to the user so here and here we're using a Chas output and you see that these are now green so you are able to see what exact L they resol to so here we have our message and the user ID and in the agent uh we also have logs so when I click on logs you can see well you can see logs for this configuration note and what were there input and output so it starts with the window buffer memory where uh we in inserted a new message hello how are you so that was the initial message and we see there's no output so there's no messages uh in that memory and that was passed to the chat uh open AI so to the language model uh here we can see again the input here is the system promt that it gives uh to explain the agent what it is and what it can do here at the bottom you see the user's input hello how are you and this is a simplified view so we are doing some parsing on that output to show you this view uh but you can also see it as a raw Json of what EX exactly is happening uh so this will be the object that we uh that we get MH um there's another way you can view the log as well uh so theoretically if you wanted to test whether your setup is correct you could temporarily add a manual chat uh directly in the canvas 100% let's do that quickly uh but for that we'll have to disconnect the slack node or just disable it for now because it won't know where to set SL messages to uh let add the chat manual Trier is what it's called I connected to my agent the only thing is that I will have to change the text so just do that copy from here remove it so I'll just change the expression to use the Json do input for the text because that's what the chat is going to uh send uh now that I have a chat connected oh I also need to change the window memory uh session key because we no longer have this data as we're going to be doing the debugging so let's just it's all fine I just copy it we'll be super quick uh and now we can open the chat uh so we say and it's execute the workflow oops here it's waiting for the web hook so oh we have to disable the web hook U and yeah here's a response again and you can see the same log but just for this specific response uh you see that the user input here is just a single message again because I changed the session key so it doesn't have access anymore to that same memory MH uh and this is how quick way how you can debu your workflows one thing we haven't mentioned is the abil the ability or where you can go to look to see how many tokens you've been using because obviously one of the things that I've learned very quickly is if you are using the API for um uh for example GPT you are you're going to be using quite a few tokens quite quickly so it's important to be able see where that is that's a good point so uh let's Qui show that if I add a b I'm you model open a and I execute this I can I access so if you go to the logs uh you see in this uh language model step open AI chat model it shows you the amount of token and consumed and you can hover over this icon and it shows you uh the exact amount of prom tokens and completion tokens uh because those we have sometimes different prices um yeah so you can see it at least there's somewhere you can actually see how many you're using so you can refine your workflow depending on your usage Wantage or your usage limits what to do if you want to have your uh agent uh exposed like on the website for example how so uh we have this L chat package looks like this it's an npm package that you can either include in your uhb application but more importantly you can also use an embet version of it uh so we're just going to copy that and uh it contains a example workflow how to set this up I'm just going to open it and copy I'm pasting that workflow um and okay so that will be that workflow let me show you a version that is annotated so here we have an incoming chat so this is a web hook that listens on this chat agent URL uh it transforms the body so to uh to convert it uh from string to Json and it uses uh switch to uh to one of the actions either send message or load previous questions and uh so this would take care of the back end of the agent sort of so you can see that here is the agent that we have connected it's using the section ID from the uh request body as a memory key so it will be able to remember based on a session and has access to Wikipedia and response to that with the web hook and now we also can use the Ed version of that n chat module here uh so I've just pasted it here uh basically and uh you can see that it's create chats web hook URL is just going to be origin web hook chat agent uh so this would be the webook from here but the production one chat agent and so we have two web books one of them is to handle the chat uh connection and the other one is to serve the page that contains the snippet to renter that chat so if I now save this and I set it to active uh and I go to production chat click this is my custom n 10 chat so this could be whatever you can include it on whatever page you want uh but here more importantly we have this chat window hi my name is Nathan how can I assist you today and nice to meet you all I put is my name and it's should remember my name I please dare you praying of course it was going to remember your name you said it up correctly think it so we have that set up I just change the name here is

### AI Summarisation [17:43]

summarize s thre and uh yeah we can go ahead to start actually building this workflow so open a new page at workflow and uh what it's going to do first it's going to accept so it's going to be called when executed or when called by another workflow and we're going to expect these uh parameters that we set here so we're going to expect them to be Set uh mainly Channel and uh time stamp M so let's just mock them for now uh channel so this is channel that so essentially what you're setting up is whenever an action is performed in the other workflow it will trigger the actions that we're now instructing it to do in this workflow uh so whenever that tool is called uh this workflow will be called with the yeah with the inputs for that to so we're just marking for now we got Channel and the threat uh so let's what so what we have to do uh is get all the messages from this threat uh get all the conversation to summarize we have to check that the uh we have to filter out the messages so we don't include the B messages in case there's already summary and then we have to pass that to the chain uh to summarize that uh let's do that and for that we're actually going to need one more thing and that is the bot user ID to know what to filter for so we're going user copy this expression here and what we're looking for is authorization DOT first zero let me copy it from here and uh yeah now we can start building so as I said the first step is we need to get uh this conversation so we use our slack note uh and we're interested in Channel so we're interested get a thread of messages posted to a channel because that's where it's going the yeah there's where messages select our credential uh we get channel replies that's what we want so now we have to select the channel I click here by ID because that's uh that's what I know and I drag it here and that's populated now it's asking me for message time stamp this TS which is what identifies the bread uh put it here and return all limit 50 we'll just leave it for 50 and try executing it and we got that message uh which is great because that works uh but there's nothing to summarize here so we uh we need to use different CH or a different message uh let's do that quickly so we're just interested in listening for test event in the web hook and I'm going to P it now you have to think of uh more complicated have you prepped something yeah I'll have this conversation about Brett versus uh croan so and by the way that this is one of the beauty of using these agents I think because you can just say something like some distat please and it will know what to do like you don't have to program for specific string to be included in your mage message let that to uh the agents to figure it out uh but anyway we got this response here I'm just going to pin it so that is uh so I don't have to execute this test or uh again from slack but I can just always have access to that data and uh we're going to copy the channel uh here and PR DS which is this one I'm going to set it here in our mock kiding uh so yeah those are the messages uh right now we're in schema view so this is an example of one of the those messages look like so what we get is uh text we get users thread and we also get blocks those we won't be using so we'll just stick to uh text user uh I think that's it that's all what we need uh you can also switch to table view if you want to like go over these uh these messages or go over your data so what I've just done here is I set it to send message to channel by ID Direct in my ID channel ID the message text is still the same and I've added an option to reply to a message so it imply replies in that threat I dragged the thread time stamp and uh I didn't check in the reply to thread so now we can execute it me execute it you see it went through MH and the message is now sent uh we didn't have to start the VB hook again because now it already remembers the data from the previous execution so now when I'm in the note and I click on the execute note it's just going to execute that single note yeah so uh yeah we have a response here that's cool so now if I execute this summarization uh slack note again we have eight items because some of the uh some of them are the new ones and bot messages like for examp Le this one so and we see this is the we see an sorry we see an ID here of the user who sent the message and we see that it matches with our B user so we have to filter it out so it doesn't sumarize its own messages we can do that very quickly by using filter note uh we add a condition here that condition is going to be string I believe because yeah that user ID is a string and we'll set or we say um user of the message uh doesn't equal not equal value uh so that b user that's coming from the as input here MH and we run it uh we see that it discarded one item this one which is the bot message and it kept these other seven messages all right so that gives us the messages that we're interested in uh now we just uh we just need to join all these messages into one object because right now there's seven uh there's seven items so if I would add a chain uh to summarize it uh it would be run for each of these items so we want run just once so we add it as a uh we merge it using the item list and uh connect now we can add our chain story spacing ANM chain and I'm going to insert my prompt here uh so you are summarization engine based on the messages below provide a short title description and concise summary if context requires it mention sender ID they will be later replaced with actual names provide your summary below um so here we are iterating over the messages uh you see it's red so we have to just fix that because now the data is in Json data and now we can map over it so we're mapping over these messages and only including like the relevant information for the LM it doesn't need to know about all the IDS of Channel and so on it's only interested about the sender and the message and this is what it's going to summarize didn't connect the model I was gonna say so he hasn't got a brain yet you need to give it its brain it does let's do that also and uh yeah now we can run the chain see what we get okay it responded with the well the summary we can see how it happened well here it's pretty simple because it's just a chain with an input uh and it sends it to LM but uh this is the output and you see that we asked it to uh provide it in uh provide pre properties title description and uh summary uh and it provided it but it's like an a free form text so uh now have to parse it but what we can do is connect an output parser here uh that would tell the LM what format we expect the input and what format or what structure it should output in uh so structured output for uh here we need to provide this adjacent schema of the output that we want so let's say that we want title which is a string and we want a summary which is also a string so pretty straightforward and we want it wrapped in an object mhm now disconnected let's execute it again see how it change and now you see that we got an Json response with title and summary so uh we can already use it further in our uh app and it's par as we want if you want to be like have even better control or better performance of this uh output paring what you can do is you can connect AO fixing output parser and this is a note that again except the output parser but it also has a model uh connection uh to which you can connect a model uh and now it would IO correct itself so if the message from the LM wouldn't adhere to this to the Jason schema that we are asking for it to Output data in it would tell the LM and it would be using this model it would tell it to try to self-correct and providing an example of a good schema and as it because it would know where exactly it made um the mistake so it would try to correct it last thing we need to do here and we have it done is outputting this data so what the tool here expect here this summarize select threat it expects a single response object or whatever you specify your uh response object as here you see response so we have to make sure that our workflow aderes to that so we have

### Vector Refinement [28:24]

the following workflow and uh it do two things so first it's inserting data into the vector store so when I click execute it's going to get all files in docs uh in a Google Docs folder the folder name is frontend oik and I've put some uh some PDFs from our knowledge base in there uh so I could ask questions on top of it so I got the file IDs and I got the names next I need to download these files to get it as in binary representation into the na1 uh so we run this for each of those items I get an output here in a moment okay four items uh so I got these files what's cool is that you can view these files uh you see that yeah there it uh and now that I have them downloaded I want to insert them into my pine cone index so I have pine cone index set up uh called Vector data I'm going to use Nam space from then docs 1 2 3 4 1 125 I'm always going to clear the name space before inserting new data uh to make sure it's on there multiple times I'm I've connected the binary input loader which takes care of parsing this data so I tell it where the binary data resides how it can access it and uh I can tell it to split the pages of the PDFs maybe let's turn this off here all my data is PDF so I don't have to decide if I want to use different loaders uh I also have a token splitter which is going to split these files into chunks let's give it a bit larger so there's not as many of them 3,000 chunk or La zero and I have an embedding so that's what these chunks are going to we're going to use the Open Eye embeddings to convert these chunks into mathematical representation of the data then that's going to be put into the vector store that we're going to later search for if I execute it see that this data is getting chunked it's getting embedded and it's finally been set to the vector store and here's the page contact if you would be interested what's getting into there you always have these logs so you can inspect what exactly got chunked and even the embeddings if you're into that so now we have it in the vector store uh so this is a tool so it has execute workflow trigger uh we're not going to use it for now so let's disconnect it uh now we want to ask questions uh on top of that data and that could be done using retrieval QA chain um all these chains you can find here Advanced UI chains uh there's some other nodes for the one for the vexer store is this insert but we already have it here so what's happening here is we've connected the vector store retriever uh we tell it to retrieve four matching documents based on that embedding uh eding of the prompt the query uh how so I'm going to ask how can I test workflow demo mode uh because it contains some of our questing guidelines uh some of our testing guidelines um uh the data that we just uploaded I have a chat model connected and I have the pine con load node uh which is using that index and now it should be ready to answer that question so if I go execute you see again that it's doing all this retrieve the data uh pass it to the LM and here's an uh response and that makes perfect sense and we also wrap it again andent there's a response to make sure it's available to use as a tool I also would create a new tool so the workflow tool but this one is going to be named answer knowledge based question call this tool to get an answer based on company knowledge base the input should be concise question uh and the workflow ID grops and now hopefully if we save this and we click on execute we should be able to ask a GU foil that's was doing something having nice so it did exactly what you said pretty much yeah and that's that that's it for the slack uh bot pretty much thanks for watching the

### Links & Contest [33:09]

highlight video from our Workshop if you'd like to learn more about n's new AI Lang chain features visit nn. i/l chain don't forget to take a look at our AI Lang chain contest you can find more details in the link in the description below it's running until November 1st good luck everyone
