LLM Chains using GPT 3.5 and other LLMs — LangChain #3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to take a look at chains in the lung chain Library as you might have guessed chains are a pretty big component of line chain and naturally being able to use chains is a essential skill for anyone wanting to use the line chain library now today is going to be a little bit different we have Francisco who is one of the contributors to the line chain library and later on he's going to dive into some of the details behind chains and actually show you how to use them so that'll be pretty interesting but for now let's just introduce chains I'll give you a quick overview before handing it over now we can think of chains as thin wrappers around different components in the line chain library and more specifically Primitives within the library so Primitives cover a few different things they can be the prompts that we use they can be large zones models utilities within the library and even other chains now the simplest of these chains you might have seen it before is called the large language model chain and what it is is actually very simple so up here we have our prompt template now to actually build our problem template we'd actually use a prompt template object as well let's just pretend I've done that here now what a user is going to import into this large language model chain is going to be a query okay now the query we actually have here okay so the first part of this chain is taking the input from the user and putting it into first primitive of the chain which is this prompt template here and then the next step is this prompt template with the query included inside it will now be passed across to our large language model okay and the large language model will of course output some generation based on our prompt that is the llm chain and yeah I mean you've seen it it's super simple and and naturally it's very easy to create we can actually see the code here so we're just run this we would put in our openai API key here we'd initialize our large volumes model create our prompt template as I just described and then we just use this so if we run that we should be able to get an output so we're going to run this here llm chain dot run and we just ask a question and it's going to return as a bullet point list because in this prompt template that we use here we asked for a bullet point list and there we get it so despite only running this single function we've actually created a chain of in this case two functions now as I said this is one of the simplest chains inline chain they get far more complicated than this and we can actually see that in the Dots here so if we go over to the left we have chains have two guys we can see that there's these three different types of chains generic chains combine document chains and utility chains and we can describe each of these chains starting with the generic chains which is as you might have guessed from name very generic they are more or less used to build other chains in the library you wouldn't necessarily use them by themselves because they're not built for any particular purpose but nonetheless you might find yourself using them as I just demonstrated with the large language model chain we use it just to generate some text but typically we're going to be using these as parts of other chains we have the combined document chains a wish is super useful when we are doing anything while we're working with other documents like we might do in question answering summarization or any retrieval augmented use case and then the final one is utility chains and these are more specific chains that consists of usually a large language model chain alongside a another specific line chain utility now the combined document chains they are very use case specific and I feel need a bit more of a deep dive to understand how to use them properly so we're actually just going to focus on two of the types of chains at the moment which are the generic chains and the utility chains now what I'm going to do is leave you there and I'm going to let Francisco take you through these chains and take a look at one of the utility chains and how we use it and also have a look at what the generic chainsaw and give a couple of cool examples on actually using them which I think rarely gets across what they are useful for so without any further Ado let me hand over to Francisco hi there my name is Francisco as James said I am a contributor to Lang chain I think Lang chain is definitely one of the most powerful libraries out there and to really leverage Lang chain for Lee we need to understand what its fundamental building blocks are so we know how we can use them for our specific use case so let's get right into the code for chains we will be using the openai llm as the llm in this notebook so you will need to set your openai API key right here I have already set it up so I can initialize my llm there and we will be using this count tokens function which will really let us know how many tokens we are using when we run our llm and this is important because open AI charges us by the number of tokens we use so we really want to keep track of these tokens when we are making many experiments with blank chain so we will be diving into one example for the utility chains to really understand what the utility chain is under the hood and the example chain we'll be using is the llm math chain the LM mathchain as you can see here will help us use llms to do complex math so let's say that we want to know what 13 is raised to the 0.3432 power and if we run this we will be running the llm math chain with that query and we will get an answer for that and it will be the correct answer this is not so easy to do with a vanilla llm because the llm doesn't know how to do complex math so we need to help it to be able to return accurate results so let's see what's going on here the chain received a question and we are changing that question we are Computing the question through the llm and we are getting some python code in return so now we need to ask ourselves what is going on under the hood here and we can see that python code is being generated in an intermediate step so why is this python code being generated and who is generating its python code here is where prompts come in and prompts are really important for utility chains because utility chains serve a very specific purpose and the prompt we use for the utility chain will tell the chain how exactly it needs to perform the utility purpose that we have for that chain so we will be sending this prompt to the llm and the llm will know how to behave because this prompt will give it very precise instructions and this is a concept that is repeated across all utility chains so let's take a look as we can see here we can always print a chains prompt by accessing The Prompt attribute and then the template attribute from The Prompt so here we can see that we're telling the llm that it can do basic math but it should not try to do complex math and if anyone asks it a complex question it should just output python code so that's why we're getting python code as an intermediate step the llm is returning python code because it knows that it cannot do really complex math and what if we try to send the llm just a random complex query and see if it can perform give us an accurate result on its own if we do as we have here we will send it the same question but without any context we will not ask it to generate python code just give us an answer so if we run this we will see that the answer is 2.907 which is different and wrong from our right answer which was 2.41 so if we don't have this prompt and we don't do this intermediate python code we wouldn't be getting a right answer so here the chain is really enabling us to use llms to do math whereas if we didn't have this chain the llm would be doing the math wrongly so here is an insight for utility chains by using prompts intelligently we can force the llm to avoid common pitfalls and this can be done by explicitly programming it to behave in a certain way and this is really important because that is what is done again and again in different utility chains so let's see the llm math call method to see how this python code is being used then once the python code is generated we can see here that if the llm returned python code right T here is the output of the llm we will be able to run that code and then give that code as an answer right right and if not we will just output that answer directly so here it's interesting that we can handle both cases we can receive python code and run it or we can just return the straight answer and this is it for the llm Mac chain as we can see there are several other chains that do very different things there's the SQL chain which computes SQL commands and can build SQL commands for a natural language queries there's also the API chain which helps us make correct API calls to a specific API by giving the documentation for that API and also there's the bash commands chain which helps us create batch commands on the fly but there's many more and we really encourage you to check them out on your own and play with the example notebooks which are in the documentation which you can check out in this link and with that documentation and those notebooks you will be able to understand which utility chains you might need for your specific application awesome so now we are ready to go deep into generic chains and generic chains are a bit different than utility chains in that they're not thought of as chains that you will be using on their own but more as building blocks for building other chains and there are three types of generic chains which we will cover and we will cover them all in the same example so we can really understand the power of combining These Chains to build custom solutions for applications so let's start by the first of these chains which is the transform chain and let's say we need to clean and input text from Extra Spaces because Extra Spaces are being charged for us as tokens and we don't want to spend extra with dirty text and also it's just not really neat so we have this function as we can see here and we will initialize our Chain by saying this is the input and this is the output and this is a transform function so basically a transform chain just gets some inputs applies a function to those inputs and Returns the outputs it's very important here to notice that the transform chain does not have an llm so not all chains are lln dependent so we here we will be building our clean Extra Spaces chain and then we will run it we don't need the count tokens here function because we don't have any llms so as we can see here the random text with a lot of irregular spacing will be cleaned and will have our clean text as an output so let's say now that we want to use this chain to clean some text and then send it to an LM and we want to send it to an llam that will change the style of what what we have given as an input to another style so say to write it in the style of a poet or a policeman so to do that we will need to build our prompt template for our llm chain and we will say a paraphrase this text in the style of a specific style here and this is our prompt and here is our style paraphrase chain with this llm chain which is the second generic chain that we will be seeing and the llm chain is just a chain that computes an llm call with a prompt template James already covered this in detail so as we can see here we will have our style paraphrase chain and now we want to combine these two and here comes our third generic chain so how can we combine two chains well we can do that with a sequential chain so the sequential chain just receives a few chains as an input and we want to tell it which are the input variables we will give in the sequential chain and now which output variables we are expecting so we will initialize that and now we can give our final chain an input which will be cleaned from extra spacing and then will be written in the style of some desired style we want so let's say we have this definition of change from the Lang chain documentation which has as you can see lots of extra spacing that we don't really want but we also want it to be written in the style of let's say a 90s wrapper just because we can try that and if we just run it here we will see that here we get our answer let's change let us link up multiple pieces to pieces to make one dope app like we can take user input style it with style it up with a prompt template then pass it to an LM we can get even more creative by combining multiple chains or mixing chains with our components so that's quite creative and and accurate in the style of a 90s rapper so finally just a small note on Langton Hub Langton Hub is where chains prompts and agents are serialized and we can load them from launching Hub to use them for our own purposes and it's going to be the place where these components will ultimately live so it's really important that we learn how to use launching Hub to load these components and then use them so it's really really easy to load chains from langtune Hub you can just import the load chain function set the path for the chain you want to import this path you can find it in the Lang chain Hub repository you can find the appropriate path for the chain you want to load and here you can overwrite any of the default parameters that you want to change so if for example if you want to have the chain not be verbose when it's being run you can just set it to false and then you can see here that it's not verbose anymore so it's really easy to change any parameters that you might want so that's all for chains okay so that's it for the code portion again massive thank you to Francisco for taking us through that and sharing some of his insights as one of the contributors of the library if there's one thing I'd like to point out is that just now we only saw a couple of chains this really just acts as an introduction to chains in line chain but there are a huge number of these that we haven't even spoken about or even mentioned yet and okay we've covered generic change relatively well combined documents change we haven't covered whatsoever and utility chains we've covered a little bit but even within utility chains we've had a look at llm math but we haven't had a look at any of the other ones so there are a ton of these chains that we can use in the library and we will make sure to go through at least a few of these in the future but the point being that there is a lot more potential for building really cool tools yet than what we've just shown you so for now we're going to leave it there I hope this is all been useful and interesting so thank you very much for watching and I will see you again in the next one bye
Info
Channel: James Briggs
Views: 18,866
Rating: undefined out of 5
Keywords: python, machine learning, artificial intelligence, natural language processing, nlp, Huggingface, semantic search, similarity search, vector similarity search, vector search, langchain, langchain ai, langchain in python, james briggs, gpt 3.5, gpt 3, gpt 4, gpt 3 open source, openai tutorial, llm course, large language model, llm, gpt index, gpt 3 chatbot, gpt 3 tutorial, langchain tutorial, langchain course, gpt 3.5 explained, gpt 3.5 python, langchain chains, llm chain
Id: S8j9Tk0lZHU
Channel Id: undefined
Length: 16min 38sec (998 seconds)
Published: Tue Feb 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.