How to Add Conversational Memory to an LLM Using LangChain | Vector Database Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is the main difference between a conversation with you and a conversation with an llm conversations with you with people involve conversational context and conversational memory that lasts outside of the immediate moment so in llms you can pass in some context with the context window of course but how do you keep track of the contacts that you need for your conversation over a long period of time that's where these things called Converse that's where this thing called conversational memory comes in and Link Chain offers a way to work with conversational memory using conversation chains and in this tutorial we're going to look at how you can provide persistent storage to your conversation chains in Lane chain using a vector store like novice so for this example we're just going to install these libraries these are the libraries that I installed for this example Lane chain millvis pymovis python.m openai so python.n by install for loading and managing my very environment variables open AI is for access to gbt you know Lane chain also has access to gbt but I put in my API key through open Ai and milvis is the you know the server for the vector database Pi Memphis is the client typically um you know in this situation what I use it for is managing utility if I need to so we're going to start and I'm just going to import I have the code pre-written here so I'm just going to import some of this code for you and we'll take a look at it and understand why we're importing it right so open AI embeddings these are the vector embeddings that we're going to use to represent our text open AI itself is how we're going to access GPT from Lane chain right these are the five Lane chain Imports that we're going to use we're going to use a vector store retriever memory so this is how we turn milvis as a vector store into you know the retriever how we retrieve it basically then we're going to use conversation chain this is what I was talking about earlier this is the framework or the object or the Primitive whatever you want to call it that powers having this conversational memory then we have prompt template which is the basic way we interact with prompts in link chain um the next thing we're going to do is we're going to use uh load.m which is from you know python.if and we're going to use open Ai and we're going to basically put our API key into there and so you can see that I called my AI key open aai API key uh you can call yours whatever you would like uh now we're going to do some typing here and I'm going to import the default server from Novus and I'm going to start the oops I'm going to start the default server for notice oh man okay so now that we have milvis started we can move to kind of the rest of this so I'm also going to import Novus from lanechain Lane chain Vector store so I I light a little bit up there we're actually going to have six Imports and this is the sixth one and then we're going to set our embeddings equal to the open AI embeddings and um this part is uh for what I was saying earlier is I'm actually going to drop the existing Lane chain collection that I have going on right now in milvis and I'm going to use the pi Midwest utility to drop this if you don't have a collection this may not be something that's necessary for you okay so now this is the important part that you should probably pay a little bit of attention to so we're going to put our Vector DB we're going to instantiate our Vector DB in um link chain so Mill this right Dot from documents so Vector database is a link chain are instantiated from documents or from there's something else you can saturate them from what is it from text but in this case we're going to use documents and we're just going to give it an empty doc store and then we're going to pass in the embeddings as our you know the opening eye bettings and then we're going to give it the connection arguments connection cards so the host is going to be localhost 127.0.0.1 and the port is going to be default server.list import so this should spin up milvis as a vector database now we're going to spin it up as a retriever the vector database as the retriever so Retriever and this is basically how we're you know retrieving memory uh Mill bit oops equals as retriever Vector GB search keyword args equals k equals one so this just means we're searching for the top one result memory we'll set the vector store retriever memory with the retriever equal to the retriever we just created cool so that's basically how we set this up now we're going to set up this About Me section where we're going to tell you know link chain let's pretend like I'm having an introductory conversation with you know somebody uh and this somebody is link chain and I'm just going to copy and paste this um basically what you want is you want to give a list of dictionaries in the form of input blank output blank and the input is what you say to the llm and the output is what the llm sends back to you now obviously this example that I've given is not really how an llm would talk with you um but uh it's also not how you would talk with an llm but this is to illustrate the example of conversational memory right this is to illustrate that the llm can understand and remember the conversation so for example I'm going to tell it my favorite snack is chocolate which is pretty true um if you consider chocolate a snack I just like eating chocolate Okay and then my favorite sport is swimming I was a year-round swimmer my favorite beer is Guinness um it's just good my favorite dessert is cheesecake I love cheesecake and my favorite musician is Taylor Swift once I have this list of dictionaries of input outputs I simply save this into the memory the vector store retriever memory and then you save context to save this information as context now what we're going to do here is we're just going to do a little brief query to show that it understands what's going on so we're going to have it print load memory variables and I'm going to give it a prompt B There we go prompt uh who is my favorite musician um there we go and we're going to check history and it should say that my favorite musicians Taylor Swift oh there it is okay so it found the answer and it found where is my favorite who's my favorite musician and it says the input that matches this query which is broken down using embeddings is my is Taylor Swift okay so now let's get our model out so let's actually access openai the llm that we're going to access open Ai and we're going to give it a temperature of zero we don't want it to make anything up I don't want it to make anything up about me uh and then we're gonna give a template so this is just default templates what okay so obviously when I put in these quotes it thinks the first thing is going to be an arithmetic error um so I'm going to say this is a or here we go the following Exchange is a conversation is a friendly conversation between a human and an AI the AI is let's see what I say the AI was talkative and provides lots of specific details from its context the AI is talkative and provides a lot of specific details from its context if the AI does not know the answer if the AI does not know the answer into a question it will truthfully say it does not know question it will truthfully say it does not know relevant pieces of previous conversation and we're going to pass this in as a history as a variable and then current conversation and human here's the inputs Ai and this is where we asked them the element to generate and then our prompt itself is going to be uh because we're going to you know make a template from this and put variables and we know that that's going to be history and input and then the template that we're using is going to be this default template here okay and now we're going to create a conversation with summary so that you know we can say this is the kind of summary of the conversation so far this is how conversation chain would handle this and this is because we may have a longer chain than we can actually fit into the context window and we want to be able to summarize that chain if we need to so let's call it conversation chain or I'm just going to call it conversation chain in the tutorial that I originally wrote I called it conversation uh with summary uh and I will drop that below so you can read that as well LM equals LM prompt equals prompt oops yep uh memory equals memory um okay and then verbose equals true true oh wow this is a tough one okay so that'll show us what the album is thinking as we use it and now we're gonna go now we're gonna ask a question we're gonna predict and give an input oops input and the input is going to be pi my name is uh well last time I used the name Gary was the name of the main antagonist in Pokemon uh today we're going to use the name of the main let's use the name of the main protagonist in this isn't I mean the name of the main protagonist in Yu-Gi-Oh my name is yuhimoto hi yukimoto it's nice to meet you what do you like to do in your free time well I'm not going to say anything here but I'm going to ask you what my favorite memory is or who my favorite musician is let's see it should be able to get this uh and this should say conversation chain there we go now obviously I'm not very friendly here I'm being kind of rude I'm ignoring the question entirely of what do you like to do in your free time um but you know we're just testing this you said your favorite musician Taylor Swift it knows so it knows what's going on all right let's see if it knows about my favorite dessert right I've shown you the Swift thing twice let's see what else it knows Ah that's because I am copying and pasting okay here we go you said your favorite desserts cheesecake now let's see if it remembers one more thing if it remembers my name which I said earlier remember today we are yuhimoto what is my name oh man I should have made Beyonce or something is that one of our songs what's my name no whatever I'm gonna find out who it is and the next time we're gonna use that person as an example your name is yukimata okay cool so this is basically it this is what uh you know this is how you add a conversational memory memory chain this is how you add a conversational memory chain to link chain so that your llm has contacts of your conversations so far and now I'm just going to clean up and stop my novice server um because you should do this well at every time you you run something in your notebook that's temporary okay cool thanks for watching and um I hope you learned something if you have any questions please feel free to reach out to me
Info
Channel: Zilliz
Views: 2,258
Rating: undefined out of 5
Keywords:
Id: m3WwttSBDvo
Channel Id: undefined
Length: 13min 6sec (786 seconds)
Published: Fri Aug 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.