LangChain Crash Course - All You Need to Know to Build Powerful Apps with LLMs

Learn LangChain, a framework for building apps with large language models. The course covers model integrations like OpenAI and Anthropic, prompt templates, chains, agents with tools like Google Search, memory, document loaders, embeddings, and vector stores. A demo shows similarity search on State of the Union text.
Chapters
So it has a memory of our previous conversation. So that's why it will know that we are talking about the moon. Right? Welcome to this crash course on LangChain. It's a framework for developing applications powered by large language models. For example, you can create conversational AI systems based on your own data sets. You can create personal assistants, question answering chatbots, or your own docs, just like ChatGPT. It's a very powerful framework and there are multiple modules that you can use in your own projects. In this crash course, we are going to be looking at all these components. So for example, it supports multiple models, including models from OpenAI, hugging face and some Google models as well. Then there are prompts. You can define your own prompt templates to work with different large language models. It has support for memory. You can give memory to your chatbot so they will remember previous conversations. You can use indices to work with your own data sets. And with the help of chains, you can combine multiple models or different prompts to achieve different tasks. And then there are agents. So using agents you can perform multiple operations. For example, you can tell an agent to access Google Search or Wikipedia and perform different operations on the data. In this crash course, we're going to look at all these different components and then combine them together to create powerful applications. So let's get started. During this crash course we are going to be using this Google Colab notebook that I put together. And these are different topics that we are going to be covering during this course. First we need to install LangChain and you can do that using pip. Install LangChain. If you have Python installed on your local machine, you can run the same command to install LangChain on your local machine. We will be needing other packages, but I'm going to be installing them as we need them. LangChain provides a generic interface for different large language models. So here is a list of all the different models that are supported. For example, there are models from anthropic cohere, then gpt4all, there's more models from Hugging Face, you can also integrate those as well. And from OpenAI, let's look at a quick example of how you can use OpenAI models with it. So first you need to install the OpenAI package using pip install. In order to use OpenAI models, we will need the OpenAI module API key. For that, go to your OpenAI account and then under Users you have the API keys. Simply create a new API key, copy this and paste it in the notebook. Next, you simply import OpenAI from The LangChain library. And then here we are defining different parameters. So for example, we are setting the temperature 2.9. By default it's using DaVinci3.3 as the model. But you can change that if you have access to ChatGPT or GPT4S. Then we are providing a prompt to the large language model object that we just created and you will get a response. So for example, in this case I'm asking why the color of the sky is blue and the response is the color of the sky is blue because of the way sunlight interacts with the atmosphere. Now, OpenAI is not the only models that you can actually use With LangChain, there are different models and actually the great thing is if you click on any one of them, they have provided example of how you can use them. Right? So let's look at a model from Hugging Face Hub. Again, we first install the required package using pip. Next we need Hugging Face API key or token. For that you need to go to your Hugging Face account, click on your user icon, then click Settings and then go to Access tokens. So here you can generate a new token. You give it a name and then click Generate token and just copy that. After that simply come here, paste it. Next we import Hugging Face Hub from LangChain and here is how you can use actually different models. So in this case you need to provide the repo ID. For example, here we're using the Google Flan T5 model. Then if there are any arguments that you want to pass. So for example, in this case we are passing a temperature. Let me change it to let's say a temperature of 0.9 and then the maximum length of the response. So in this case I'm asking a question of why gravity is lower on moon compared to earth. So let's run this and it gives us response, the moon has a lower mass than Earth. The great thing about LangChain is it provides a very uniform interface that you can use with different large language models. Next we will look at prompt templates. So LangChain facilitates prompt management and optimization. And a prompt template refers to a reproducible way to generate prompts. So it's similar, very similar to F strings in Python. It essentially is a string which has different input variables that the user can adjust. So for example, here's a template, so, right, and there is an adjective, a poem about, and there is a subject, right? So we can use this as a template. To do this we will need to import this prompt template from LangChain. Next, in the Prompt template. You define what are different input variables. It's within a list. So we have these two input variable, ejector and subject. And then the template itself. You can simply pass on different values to your input variables using this format. For example, in this case the adjective is sad and the subject is ducks. So the prompt will become write sad poemo ducks. Now, in order to get a response, you simply pass this on to your LLM. So here is it. It is right? And we got a response. Actually, to make it well formatted, I can simply print this. Let's print it. Here is our sad poem regarding the duck. Okay, now let's look at a more practical example of prompt templates. So you can use prompt templates to provide instruction to the language model. Then a set of a few short examples to help the language model generate a better response, and then you can add a question at the end. So here is an example. In this case we are providing this template and we're telling the large language model that I want you to act as a naming consultant for new companies. Then we provided a few names for different industries and the instruction is the name should be short, catchy and easy to remember. Then what is a good name for a company that makes X product? In this case, the only input that we are requiring the user to provide is the product name, right? The product category, actually. So we create a create prompt template and the input variable is product. And then let's say in this case we are asking it the category to be news articles. Let's say if I run this next when we provide this to our LLM, so it came up with the name of newsbuzz. So which is pretty catchy. Next we will look at the concept of chains. So chains allow you to combine multiple components together to create a single coherent application. For example, you can create a chain that takes in user input formats it with a prompt template and then pass the formatted response to an LLM. Now in this case we have a prompt template, but unlike the previous case that we took an input from the user and then passed it on to the LLM, we can use LLM chain to actually combine the LLM call as well as the prompt template into a single call. And now whenever we run the user the new chain on a user query, we will get the response. So let's run this. So here's the response that we get. That's pretty nice. Rainbow night socks. Okay. You can build more complex chains by combining multiple chains together or by combining chains with other components. There are different chains that are available. We were looking at a simple LLM chain, but there are sequential chain, serialization, transformation chain and so on and so forth. Next we will look at agents and tools, which is a very powerful component of LangChain. Sometimes in order to complete an objective you need to perform multiple tasks. So the agents uses large language model to determine which actions to take and in what order. If you recall Auto GPT, it has multiple agents that are performing different tasks. Now in order to perform different tasks you need different tools, which is a function that performs a very specific duty. And this can be like simple Google search or a database lookup or calling other chain. LangChain has a number of tools available. Here is a list of these tools that you can use in your chains. So for example, you can perform Google search, you can look at Wikipedia articles and you can even do different tasks using Zapier. LangChain also has multiple different agents, for example a zero shot react description. So this agent uses the REACT framework to determine which tool to use based solely on the tool's description. Any number of tools can be provided and there are some other agents as well that you can use in your workflow based on your requirements. Let's look at an example to understand this. So let's say I have a prompt which has two different components. So what year did Messi joined Barcelona? So it's a simple lookup. Then the second component is what is his current age raised to the.43 power. Now, in order to perform this operation we need a mathematical tool. We are extracting information from Wikipedia. That's why you need to install the Wikipedia API. Next we are loading this tools package or function and then we're learning two different tools. One is Wikipedia that is going to be used for information retrieval. And the second one is the math module and you combine them with these large language model. Next we initialize the agent so we pass on different tools that it has access to the language model and then the type of agent that we want to define. So in this case we are going to be using zero shot react description agent and we set verbose to true so that we can look at the whole thought process. Lastly, we simply run this on our prompt and you see it actually performs different operations. So let's look at this. First the agent decides I need to find Leon Messi's age and year of joining Barcelona. So the action is access Wikipedia. So it looks up the article and creates a summary for that and it goes through the whole information retrieval and the final thought is now I know the final answer. The final answer is that he joined Barcelona in 2004 and his current age raised to the power 0.43 is around 28 years. Right? So you see that we combine different components, they are performing different operations and the agent decided when to use which one. Next, let's look at the concept of memory. So by default, chains and agents are stateless, meaning that they treat each incoming query independently. But in some applications such as chatbots, it is highly important to remember previous interactions, both at the short term and also at the long term level. So this concept of memory exists to accommodate that. Now, the way you do it is you create a conversation change. So we define our large language model using OpenAI. This is DaVinci text DaVinci3. Then you simply pass on that LLM to your conversational chain. So for example, first I'm asking it, hey there. Right? And this is set to verbose, so we can actually look at all the outputs. The human says hi there. And then they AI simply say hey there, it's nice to meet you, how can I help you today? Right, Next I'm asking it, let's talk about how physics work on the moon. And here is the response from the AI, right? So sure, physics on the moon is a fascinating topic, right? Blah, blah, blah. Right. Now the next is okay, why the gravitational field is lower, so it has a memory of our previous conversation. So that's why it will know that we are talking about the moon, right? So the moon's gravitational field is weaker than Hertz because it has a much smaller mass. Right now as you can see, memory is very critical and it's very important when you are trying to develop conversational AIs or chatbots. So far we only looked at the use cases of how to use LangChain to interact with already pre trained language models. However, the actual power of LangChain is its ability to have conversation with your data. Now in order to do that, you actually need to access your data and load them for that. LangChain has different document loaders. So for example, you can work with JSON files, you can work with CSVs, you can read emails or interact with Google Drive HTML files, Notion PDF. Right? So there are like a whole bunch of different file more formats that you can easily work with and this will help you read them. Now the documentation is very thorough. All you have to do is simply click on any one of them and it will tell you like how you can actually load them. Right? So for example, here's a Quick example script of how you can read PDF files in order to work with your own data, I said you really need to understand the concept of indices. Indices refers to ways to structure documents so that large language model can best interact with them. There are three different concepts that we need to understand regarding indices. So first is embeddings. An embedding is a numerical representation of piece of information. So for example, your text, documents or images, they are transformed from, let's say a string of characters into numerical representation that is going to be embeddings. And they are defined by the language model that you're using. Now, if you're dealing with very large files, then we need to divide our files into smaller chunks. That's where text splitters comes in. Sometimes when you are working with OpenAI models, you will see that the query exceeds the token limits, right? So that's why you need to split them into smaller chunks. And the last component that you need is vector stores. So it's a vector database store. It stores the index vector embeddings from your NLP model and you use these to do similarity search or look for information in your own document. I have a detailed video on how you can use indices with your own PDF files. So I'm going to put a link to that video. But in this case, let's go over a concrete example to understand what is going on. So here we are using the request library to download the text of the State of the Union address. We simply get that using the request library and then we write it to a file called stateoftheunion.txt. next we are using the document loader. So in this case text loader to actually load this document. If I simply run this, this is what the document looks like. Next we're using the character splitter to split this document into smaller chunks, So a chunk of thousand characters. And if we run this code, we'll see there are a total of 42 different chunks. So for this example we want to use embeddings from hugging face. So that's why I installed the sentence transformer library and then define the embedding variable using the hugging face embeddings. Now, in order to do search or information retrieval, we need some type of vector stores. LangChain supports different types. For example, there's Chroma Deep Lakes. We are going to be using this one, but depending on the application you could pick any other of those. For example Pycon or Redis. Using my vector stores, I simply transformed the different chunks or documents into the embedding space that we had from hugging face. Right. So that will become our database. And then the query is what did the President say about the Supreme Court? So then we simply run that query using similarity search and get the documents which will mention something regarding Supreme Court. So for example, if you print the results, here is a mention of Justice Stephen Breher. Right. And then there's another one. So Judge Katanji proud as well. Right. So it simply looks at the places where you have information regarding the Supreme Court because it's doing a similarity search on your document. So there you have it. It was a quick crash course on using LangChain and different important feature it has. Using the concepts covered in this quick tutorial, you can actually create really powerful applications. For example, we have this example of Open Assistant. So if you see they used LangChain to create a baby AGI as well. So the code is available here. You can go over this, but essentially you're simply combining the concept that we learned in this course. This was a quick overview of different components. I'm going to create more details video on each of these components. Hope you liked the video. If you did, please consider subscribing to the channel and commenting on the video. It really helps with the algorithm. Thanks for watching. See you in the next one. Thank you.










