Building Chatbots with Long-Term Memory - FlowiseAI Tutorial #5

15k views2233 WordsCopy TextShare
Leon van Zyl
#flowiseai #flowise #openai #langchain We can use Conversation Chains to create power, interactive...
Video Transcript:
<b>In this video I will show you how to</b> <b>create a chatbot similar</b> <b>to chatgpt without writing</b> <b>a single line of code using Flowwise AI. </b> <b>This means that you will be able to have</b> <b>back and forth</b> <b>conversations with the chatbot and</b> <b>we will also have a look at storing and</b> <b>retrieving multiple</b> <b>conversations just like chatgpt. </b> <b>Let's get started by</b> <b>creating a new chat flow.
</b> <b>And let's save our flow</b> <b>and let's give it a name. </b> <b>So in the previous videos we spent a lot</b> <b>of time looking at</b> <b>these LLM chains and we</b> <b>learned that with these LLM chains we</b> <b>could provide a model</b> <b>and a prompt template and</b> <b>these chains are perfect for one time</b> <b>uses where we wanted to</b> <b>generate a recipe or a</b> <b>joke etc. </b> <b>But in this video we'll actually move on</b> <b>to a different type of</b> <b>chain and that is the</b> <b>conversation chain.
</b> <b>So let's go to add nodes and let's add</b> <b>this conversation chain to the canvas. </b> <b>And we can see that this chain is</b> <b>different to the LLM chain in a few ways. </b> <b>It still takes in a chat model as input</b> <b>but we can now see that</b> <b>the prompt template is</b> <b>actually optional and we now have a</b> <b>memory property which is mandatory.
</b> <b>And this memory property will be used to</b> <b>store the conversation history. </b> <b>And that is one of the major differences</b> <b>between a standard LLM</b> <b>chain and a conversation</b> <b>chain. </b> <b>The conversation chain is able to</b> <b>remember our conversation</b> <b>history and it is therefore</b> <b>able to recall information.
</b> <b>Let's start by adding our model. </b> <b>So under add nodes let's go to chat</b> <b>models and let's add our</b> <b>chat open AI model and then</b> <b>let's connect this model to our chain. </b> <b>Let's add our credentials.
</b> <b>I'll simply leave the model name as GPT</b> <b>3. 5 turbo and let's set</b> <b>the temperature to something</b> <b>like 0. 7.
</b> <b>Now let's have a look</b> <b>at this memory node. </b> <b>Let's go to add nodes then let's go down</b> <b>to memory and here we</b> <b>have a couple of options</b> <b>to select from. </b> <b>But let's start by adding a buffer memory</b> <b>node and let's add</b> <b>this to the chain as well.
</b> <b>Believe it or not that's all we need to</b> <b>build a chatbot in Flowwise. </b> <b>But during this video I am going to show</b> <b>you how we can greatly</b> <b>improve this chatbot. </b> <b>Let's test this by saving this chat flow.
</b> <b>Let's open chat and let's</b> <b>check if this is working. </b> <b>I'll start off by saying hello and we do</b> <b>get our response back. </b> <b>Now let's test out the memory.
</b> <b>Also something like my name is Leon and</b> <b>now if I ask it what is</b> <b>my name we can see that</b> <b>the chatbot is able to answer this</b> <b>question because of the</b> <b>memory that we assigned to</b> <b>it. </b> <b>Let's dive a bit deeper. </b> <b>We can view past conversations with our</b> <b>chatbots by going to</b> <b>settings and then clicking on</b> <b>view messages.
</b> <b>Here you are able to filter based on date</b> <b>and source and at the moment we only have</b> <b>one conversation and if we click on it we</b> <b>can view the entire</b> <b>conversation transcript. </b> <b>This is useful for when we deploy our</b> <b>chatbots to production</b> <b>for instance to applications</b> <b>or websites and here we can view all the</b> <b>conversations that</b> <b>our users have had with</b> <b>our chatbot and we can use this to fine</b> <b>tune and improve our chatbots. </b> <b>The prompt template node is optional but</b> <b>it does have a very</b> <b>important use case and we</b> <b>will have a look at it in a second.
</b> <b>But if we wanted to prime this model we</b> <b>could simply click on</b> <b>additional parameters and</b> <b>here we can see the system message that</b> <b>is assigned to this model at the moment. </b> <b>We can of course change this to whatever</b> <b>we want and let's say something like "You</b> <b>are a pirate called John". </b> <b>Let's save this and then</b> <b>let's test out this chat.
</b> <b>I'm actually going to clear this previous</b> <b>chat and let's try this again. </b> <b>Let's say hello and we can see exactly</b> <b>how the system message</b> <b>is affecting the behavior</b> <b>of our chatbot. </b> <b>But we can also add a chat prompt</b> <b>template and I will show</b> <b>you one possible use case</b> <b>for why you might want to do that.
</b> <b>Let's click on add nodes, let's go to</b> <b>prompts and let's select</b> <b>this chat prompt template. </b> <b>Then let's also connect</b> <b>this template to our chain. </b> <b>Now with the chat prompt template we can</b> <b>do a couple of things.
</b> <b>We can enter a system message similar to</b> <b>what we had in this</b> <b>additional parameters and we</b> <b>can then manipulate the human message or</b> <b>simply grab the</b> <b>message from the chat box. </b> <b>We can of course also click on Lang chain</b> <b>hub and this will</b> <b>give us access to a whole</b> <b>bunch of pre-created templates. </b> <b>So I could just select on model and let's</b> <b>select open AI GPT 3.
5 turbo. </b> <b>Then under use case we could select</b> <b>something like chatbots</b> <b>and let's click on search and</b> <b>here we can find a whole bunch of</b> <b>pre-created templates that we could use. </b> <b>But I'm not going to do that here.
</b> <b>Instead let's add a system message like</b> <b>your name is hope rhyme every sentence. </b> <b>Then in the human message let's simply</b> <b>add a placeholder for</b> <b>our input and let's click</b> <b>on format prompt values. </b> <b>Then for input let's click on edit and</b> <b>when we click on this</b> <b>field we can simply select</b> <b>the question from the chat box and let's</b> <b>go ahead and save this</b> <b>flow and let's test it.
</b> <b>I'm actually going to clear this previous</b> <b>chat and let's enter</b> <b>something like how are</b> <b>you and now we get a response like I am</b> <b>doing swell thank you</b> <b>for asking hope I can say</b> <b>the same for you in every tasking. </b> <b>So this is indeed rhyming every sentence. </b> <b>Another reason why you might want to use</b> <b>a prompt template is to</b> <b>include prompt chaining</b> <b>in your application.
</b> <b>We had a look at prompt chaining in the</b> <b>previous video so I am</b> <b>going to go through this quite</b> <b>quickly but let's say we wanted to</b> <b>include an allyl mchain</b> <b>and let's give this chain</b> <b>a model as well. </b> <b>So under allyl m's let's select open AI</b> <b>and let's add it to the canvas. </b> <b>Let's attach our model to the chain.
</b> <b>Let's select our credentials. </b> <b>I'll just leave it on GPT 3. 5 Turbo</b> <b>instruct and let's also add</b> <b>a prompt by adding a prompt</b> <b>template.
</b> <b>Let's attach the prompt template to this</b> <b>chain and for the prompt</b> <b>template I'm simply going</b> <b>to enter select a random holiday only</b> <b>return the name of the holiday. </b> <b>I'll give this chain a name</b> <b>as well like holiday chain. </b> <b>Let's change the output to output</b> <b>prediction and now we can</b> <b>actually pass the output of</b> <b>this chain into the chat prompt template</b> <b>and we can include that</b> <b>value in this chat prompt</b> <b>template by adding a placeholder by using</b> <b>these curly braces and let's just call it</b> <b>holiday.
</b> <b>Let's change this text to something like</b> <b>answer the user's</b> <b>questions in the context</b> <b>of a holiday and then in format prompt</b> <b>values let's add holiday</b> <b>let's edit this value and</b> <b>when we click on this field we can now</b> <b>select the output of this holiday chain. </b> <b>Let's close this and let's save this flow</b> <b>and now when we chat</b> <b>to this application it</b> <b>should be in the theme of some holiday. </b> <b>What are you celebrating and saying that</b> <b>it's celebrating Independence Day and if</b> <b>we have a look in the terminal we can</b> <b>actually see that</b> <b>Independence Day was generated by</b> <b>the LLM chain.
</b> <b>Alright so I'm actually going to remove</b> <b>these nodes but that is</b> <b>one possible use case for</b> <b>why you would want to use prompt</b> <b>templates but I'm</b> <b>actually going to remove this and</b> <b>simply stick with the system message</b> <b>within this additional parameters. </b> <b>Let's have a look at input moderation. </b> <b>We can use input moderation to block any</b> <b>harmful or unwanted</b> <b>messages from the user.
</b> <b>So let's open add nodes and let's open</b> <b>this moderation node and</b> <b>at the moment there are</b> <b>two possible nodes to select from. </b> <b>Let's start with the basic one and that</b> <b>is the simple prompt moderation node. </b> <b>Let's connect this node to our chain and</b> <b>what we can do with</b> <b>this node is censor certain</b> <b>words or phrases.
</b> <b>So if I wanted to block certain phrases I</b> <b>could say something like frogs are ugly. </b> <b>Of course in a real application you would</b> <b>use not safe for work</b> <b>which I can't include</b> <b>on YouTube but let's say this was a not</b> <b>safe for work phrase. </b> <b>Let's save this flow then in the chat</b> <b>window let's type frogs</b> <b>are ugly and we instantly</b> <b>get this message back.
</b> <b>We can also change the</b> <b>error message in this block. </b> <b>Let's remove this node and let's have a</b> <b>look at the other type</b> <b>of moderation node and</b> <b>that is the open AI moderation node. </b> <b>This node is useful when you are using</b> <b>open AI models as open</b> <b>AI themselves have a list</b> <b>of blocked phrases and words.
</b> <b>And to use this is very simple simply</b> <b>attach it to the chain then</b> <b>select your open AI credentials</b> <b>and that's all you have to do. </b> <b>Now if we enter something that violates</b> <b>the open AI policies we</b> <b>will get a rejection back. </b> <b>Now lastly let's have a look at long term</b> <b>memory within these applications.
</b> <b>At the moment we can only have a single</b> <b>conversation with our bot and</b> <b>if we clear this conversation</b> <b>it's actually completely</b> <b>removed from the database as well. </b> <b>But you might want to do something</b> <b>similar to chatgpt where</b> <b>you can simply select a pass</b> <b>conversation and simply</b> <b>continue with that conversation. </b> <b>This is really easy to implement and for</b> <b>this we'll actually use</b> <b>a different memory node.
</b> <b>So let's delete this buffer memory node</b> <b>then let's click on add</b> <b>nodes and within memory</b> <b>let's select the upstache Redis backed</b> <b>chat memory node and</b> <b>let's connect this to our</b> <b>chain. </b> <b>So how this is going to work is the</b> <b>conversation history is</b> <b>actually going to be stored against</b> <b>a database and we will assign a unique</b> <b>session ID per conversation. </b> <b>We can then use that ID to</b> <b>retrieve pass conversations.
</b> <b>In order to set up this node we need to</b> <b>set up upstache credentials. </b> <b>We can do that by clicking on this arrow</b> <b>then click on create new. </b> <b>We can give our credential a name I'll</b> <b>call mine upstache Redis</b> <b>and now we need to provide</b> <b>an upstache Redis REST token.
</b> <b>In order to get your token go to</b> <b>upstache. com and then click on log in. </b> <b>After logging in ensure that you have</b> <b>Redis selected and then</b> <b>click on create database.
</b> <b>Let's give our database a name I'll call</b> <b>mine Flowwise AI select</b> <b>a region and then click</b> <b>on create. </b> <b>Then scroll down to this REST API section</b> <b>and here you will find</b> <b>two buttons for copying</b> <b>the URL and the REST token. </b> <b>Let's start by copying the token and</b> <b>let's paste it into this</b> <b>field and let's click on</b> <b>add.
</b> <b>Now we also need to provide our REST URL. </b> <b>Let's grab that as well by clicking on</b> <b>copy URL and then it's</b> <b>pasted into this field. </b> <b>Now as I mentioned earlier we do have to</b> <b>specify a unique</b> <b>identifier for our conversations.
</b> <b>We can do that by clicking on additional</b> <b>parameters and here we</b> <b>can specify a session ID. </b> <b>I'll just call mine chat1 and we can also</b> <b>set a session timeout and this means that</b> <b>the session will automatically be deleted</b> <b>from the database</b> <b>after a certain period of</b> <b>time. </b> <b>You can simply leave this blank to ensure</b> <b>that the conversation never gets deleted.
</b> <b>It's close to spot pop. </b> <b>Let's save this flow</b> <b>and let's click on chat. </b> <b>Let's just type something like what is</b> <b>your name and we do get a response.
</b> <b>Let's also say something like my name is</b> <b>Leon and let's ask it what is my name and</b> <b>it was able to remember my name but the</b> <b>big difference between</b> <b>this node and the buffer</b> <b>memory node is that this conversation is</b> <b>now being recorded in</b> <b>a persisted database. </b> <b>So if I go back to upstash and let's</b> <b>click on data browser you</b> <b>will now notice this entry</b> <b>called chat1 and within this we can see</b> <b>the conversation history being recorded. </b> <b>We can also change this to a different</b> <b>session ID so let's start</b> <b>a new conversation called</b> <b>chat2.
</b> <b>Let's save this and let's enter something</b> <b>like the passphrase</b> <b>is flow wise is awesome</b> <b>and let's ask it what is the passphrase</b> <b>and it's able to recall</b> <b>that passphrase and if</b> <b>we go back to upstash and refresh this we</b> <b>can now see that</b> <b>second conversation being</b> <b>recorded as well. </b> <b>So now we can simply swap between the two</b> <b>conversations for</b> <b>instance if I change this</b> <b>back to chat1 and save this we can now</b> <b>ask this what is the</b> <b>passphrase and it actually</b> <b>won't know because the passphrase was</b> <b>part of chat2 but if I</b> <b>ask it what is my name it</b> <b>is able to recall that information</b> <b>because that is</b> <b>something we mentioned as part of</b> <b>this conversation and that my friends is</b> <b>how we build a chatbot using flow wise.
Copyright © 2024. Made with ♥ in London by YTScribe.com