Setup OpenAI API Assistant In n8n w/ HTTP Requests & Thread ID To Maintain Context 🤖
Transcript
3,665 wordsWhat's going on everybody Josh pook here and in today's video I'm going to show you how you can use the open ai ai within your make.com or n8n application workflow scenarios or really just in anything all right if you're building out you know specific custom software or tools whatever how you can actually Leverage The Open AI API we're not going to cover everything but I'm going to show you the basics so you can message as an assistant maintain the thread context
And all that good stuff let's dive right into [Music] it all right so just to give you a little bit of background why I actually um you know came about this well first off um typically when I would build out my scenarios within make.com um what I would like to do let's say if I'm messaging an assistant here with Nate uh makes native uh C GPT module you know you can just message the assistant and show advanced settings um import the
Module as or model as well as the different tools file search File search resources Etc right your message goes here um you can add like user assistant Etc and here is the main thing that at least right now like I'm going to show you what I did and it's going to you're going to see kind of the workr I found if you know any other way you can let me know in the description down below but they have this thread ID
So essentially what in n8n if you're using n8n for example or really anything else maybe where it doesn't have that thread ID or whatever the case may be like um basically let's say if I go to open AI as this loads here I'll okay open AI so you have these specific actions you can create a assistant all this stuff message and assistant so let's say we go to message and assistant just like we did in make.com we can choose our assistant
Um and you can either take from previous nodes say if we're doing the chat one or Define below so I could just Define a um you know text right here now there's these different options base URL Max retries timeout preserve original tools so you can edit those you can edit settings obviously um but the main thing I notice within n8n and you know I'm still you know uh perfecting or like really working different you know building out different um open AI
Uh AI assistant you know ex uh scenarios or whatever you want to call them within NN so still learning a lot of course but one thing I noticed that's different than make.com is let's say if I have for example I have right here a message messaging of an assistant right and then if I wanted to let's just delete this delete this and for this example let's just drag this here so I wanted to have another message assistant well if I go
To this message assistant um take from previous noes so I can't really reference the the same thread right I have my thread ID right here but there's no way to put the thread ID opposed from in make.com I can just search put the thread ID so I could do another chat gbt message assistant and I could take the thread ID I get from this output put it here so it all maintains within the same message thread and sequence so it m
Maintains context within the whole conversation and theoretically I could just like um you know have a bunch of these going and they're all within the same message thread um it's a little bit different in n8n at least from what I've seen um and there are other like there's a lot more that NN has than make um in terms of like like you can connect memory here you can do these different options to add memory um you can also go up here
There is the advanced Ai and you can do the AI agent um where you can select the model um so you can do different models you can do let's see there is um there's literally so many different AI nodes there's memory so you can actually okay so that we cover the memory um there's embeddings so you can use different embeddings um and there's Vector stores so you can use different Vector stores so there's like pine cone and stuff so you can
Do it a lot of different ways um and I'm going to be showing you guys more of that as well but just for a very basic like if you wanted to and keep it within the same thread if you want to do a thread ID this is the way I found all right if you know a different way that's better let me know in the comments down below but basically just using HTTP requests for the open AI API so let's just
Say for example here's our example use case right here have a web hook air table and then this is where it all starts so we're messaging the assistant all right so here I just say just say text or test and we'll just do this so once that happens I already got the output for that so I'll just show you so I have the thread ID we're going to it really consists of three to four HTTP requests all right and like I
Said if you're building out a lot if you really want to leverage like you know memory and stuff um I'm going to show you more about using the AI agents with memory and embeddings and all that and Vector stores Etc um and I'll show you more of those in in later videos but for the purpose of this if you just want to like have it where you're messaging the same thread and you're getting that same thread response like I just showed
You in make.com this is one way to do it so you have the HTTP request and I'll show you I'll leave the API of open AI in the description down below of course you're going to need to first off make an open AI authorization connection um I'll show you that in just a second here okay so API is okay it's being kind of slow right now just because I'm recording but uh you can go all the way up uh to authentification
It's going to show you you what really what it is it's very simple it's same as pretty much any API you would connect to you're just doing an authorization and then bear and your open AI key all right you can easily get your open AI key from your open AI account you need to go to the pl platform um uh website all right so not going to like explain in depth like how to connect to open AI but basically if you
Go into make you create HTTP so like's say if I did this HTTP all right then you're going to see authentification you can do predefined credential type and you can just search open Ai and you'll see that right here then I already have uh credentials here but you would do new credentials and you would just type in uh your a actually you would just put your API key here and then you would um let me just make sure here okay so
I actually have the API key here I'm going to delete this of course um after this video but I don't even think you need this here let me just double check um let me see if I remove this header do we need the header so let's see remove test okay so yeah you actually don't need the header obviously once you do the credentials here so I'm going to delete that API key or that authorization so you only need that of course
If you're you know building it through whatever application and you don't already have the API key set up in here but um so yeah let me remove that and like I said I'm going to delete that API key afterwards but anyways so you yeah you have your predefined credential for open AI you would go here create your credential type you would just simply paste in your API key and that's it and you just click save and then you be connected right
It automatically does it so you don't need to add it in the headers all right so once you do that I'll delete that the four API well the three to four API endpoints it's really this is what I found um is the kind of flow uh because right here you already messaged an assistant so You' selected the assistant and yeah you already did that so you will already have a thread ID for that message so then we're going to use the
Endpoint of api. openen ai.com slv1 slth threads uh J uh and then this is the thread ID right here which we're just grabbing from this output right here and then we're doing forward slash messages all right so that's the first endpoint and we're doing a post all right this is basically because we're going to be sending a new message to um that same thread all right the the headers are content-type application Json and open ai- Beta assistance V2 and then we're
Going to send a body and the body content is going to be uh raw and I mean you could use others but this how we're just going to do it and then the Json that we're going to use looks something like this okay so roll user contact content please analyze ATT like you can use um like obviously this is just a prompt I was using but and and you don't even necessarily need these uh file attachments um I'm just showing you
What I have for mine and this is not the end product this is just me testing making sure everything works all right and I'll leave like this basic um code or the Json in the description down below but you can also get it of course in the API reference documents so just to show you what end points I basically used if this actually will load basically the first one is uh threads so you search on the left here for Threads doing
Thread and we're doing uh what is it it's actually yeah is it create a thread uh no not threads it's is it messages I think it's messages yeah create messages so we're creating a message in that specific thread ID SL messages all right you can see the path parameters which is the thread ID request body um you can see the possible different things so we have Ru content uh so you could do text content uh image URL image file if you're
You need to attachments this is where I had like the file ID and and you you can do tools such as uh file search and code interpreter uh metadata if needed and here's an example response we have ID uh object created at you you don't really need everything here like you'll see I just have this all right but you can use what you need all right from that point on let's go back here okay so once you have that you I
Like I did I'll do a test run here and you'll see this is our output so we got um yeah all of this is basically doing is just creating a message thread from that point on we're going to create another HTTP request so what you can do so you don't have to like redo everything you can just duplicate the last one so it has all the same authentification Etc and then you are going to do a post a a post again
All right and this end point is opening I this is for Threads and then thread ID and then forward slash runs so now we're basically running the assistant and that API endpoint I believe is right here uh threads threads ID let me double check threads yeah so it's under runs and we're creating a run all right so same thing has all the different you know stuff if you want to look in here you can like act you could do stuff like
Top p and Max prompt tokens and Max completion tokens and all this other stuff like there's a d bunch of different um you know things you can actually change here and customize if needed here's a example response um but basic basic what we're going to be doing is we're just going to be referencing our assistant um so yeah cuz we're just basically running it from that thread with our assistant so this the assistant I wanted to use um so all it
Really is is just this same thing we're just adding the body and we're just running the assistant ID all right you get your assistant ID you just go back into your platform and you just click on your assistant and it will show the assistant ID and it will look something like this ass store blah blah blah blah blah okay once you do that so let me just for example run the assistant I don't know if it will see if it works
Okay boom perfect it worked so from that point on there's really two to one one to two different options um for additional endpoint or for like the final endpoint um I'll show you so this is retrieving all the messages in that thread okay so this is so the last two are one retrieves all messages and this one retrieves the last response so you may or may not need this one cuz you could technically just reference the last response from here regardless
But this kind of just you know if you only want the last response you may want to use this too so I'll show you both um so retrieves all messages so this is the end point of believe threat messages so if we go to the platform or the API docks this loads here I'll refresh it oh here we go we got it okay so no we don't got it okay so I believe it would be under threads and we're going to
Get thread come on okay well while that's loading drink a sip of your coffee um make sure it's you know all black and then let's see here okay so we're using for slash threads then the thread ID and then for slash messages and we're doing a get response this time all right this is going to grab all the messages so you don't need to send any body it's like very simple you just do a get so you can delete the body
And it's going to give us all the messages all right you'll see the different messages are within this whole uh array of data so we see the different messages IDs Etc um right here you can see I got a decent amount of messages and let's go here let's see if it load Okay cool so I believe it would be under is it retrieve messages uh threads thread ID messages message ID no so let retrieve thread yeah so we're retrieving the thread
Message uh retrieve messages is it list I think it's assistant no it's list messages that's what we're doing I believe yep so we're listing the messages first the reason being is cuz well opening I just produced a new response to our what we asked it so if we wanted to reference any specific uh message ID we would need that message ID right um if you just pulled the message ID from your previous node that would be the message you sent so
We need to list the messages so we can actually see the most recent meage message which is this so you can see here this is the most recent um response from chat GPT um so we could just use that in our workflow or we could do one one last HTTP respon uh request same thing it's a get the only difference is at the end we're doing a forward slash and then we're referencing so it's the same thing as last uh last
Same basically the same mpoint like I said you we're listing them you can see the different end uh responses here as well and then we are actually retrieving a specific message which is this which is just SL message ID and you can see we're referencing the message ID of the last message right here which is in data zero right and all we're doing is yeah I don't need this anymore all we're doing is just a get response so by get we're
Testing and you can see here we got the last message ID in the same thread the assistant the thread ID the Run ID the assistant the content type or you know value right here right here so then from that point on we go ahead go out build out our flow and we could reference that specific message and if we wanted to do it like do this again we could simply just uh copy this paste it over here I just only pasted
Three but you would could paste all uh 1 2 3 4 5 or four whatever well four https and one open AI um you could also do stuff where you like create the variable of the different you know say if you wanted to change out the message assistant you could do set set variable um in a different node so it's easier to uh do a set field and you could just call this uh assistant ID I spelled assistant wrong and then
You could just put your assistant ID here and then if you do I I believe we only ref it's not like reference it a huge amount I think it's only once um but it's easier than just having to go into whatever specific one it is and find it and you could do specific variables like that for you know if there's any other areas that you want to variabe and then you could instead of having to change it out if you need
To change it within your workflow you could just you uh change it in the variable obviously you could change this to assistant ID and let see not going to figure out how to where to change it right now but probably will do this just because it's a little bit easier if I do want to change out the assistant for any reason okay and that's pretty much it guys uh like I said there are multiple different ways you can build AI agents
Or use AI with M and A M and we'll be covering some more of those I mean a little bit I mean this way it's not too advanced you're just using HTTP requests um you're doing it through the API some of these other ways using um memory Etc like you can get similar or like I mean arguably I don't know I'm not you know I'll let you know in those future videos but you know some different ways are probably better um
In terms of like depending on your use case um you know if you really need to like have really good context and memory or maybe you're buing like a specific AI agent or bot for your company may want to use memory or different um embeddings or vectors whatever and I'll show more about those in future videos so stay tuned for that if you're new to this channel make sure to subscribe you got value from this video let me know in the
Comments down below if you have any questions or suggestions for future videos if you're new here and you haven't already joined our stride community group link for that will be in the description down below it's 100% free free Facebook group free Discord Channel um we have about 750 to 800 members and both those combined so we're grow on that you can network with myself well as like-minded individuals AI enthusiasts automation experts business owners marketers uh marketing agencies coaches Consultants uh B2B
Founders SAS Founders whatever the case may be right I'm just naming them off here also too guys if you haven't already checked out my book client acquisition code it is super super valuable um it is over 500 600 Pages depending on the hard cover soft cover um so yeah it's extremely valuable very value packed not no fluff Etc check that out in the comments down or Link in the description below and then also too guys if you want help automating your
Business and uh generating a bunch of leads building AI appointment Setters that book leads automatically into your calendar scaling your business Etc book a call at executiv stride. Capp to speak to myself and we can see if it would make sense to partner up and then other than that guys I will see you in tomorrow's video we upload videos every single day here so make sure to subscribe to stay tuned for more value keep hustling keep grinding guys and of course
Guys accelerate your stride have a great weekend take care