Transcription
Hello everyone, how are you?
In this video, I'm going to guide you what exactly is LangChain, how to install LangChain in your own system, and how to create a very simple AI-powered agent using LangChain.
Now, LangChain is a framework which can be used to create AI-powered applications, or I would say AI-powered agents. And these agents are actually powered by AI, and these AIs are nothing but the large language models. So, I would say LangChain is a framework which is used to create AI-powered apps or agents using LLMs, the large language models.
Now, there are various large language models, such as we have ChatGPT, Gemini model, we have Claude by Anthropic. Gemini is by Google, ChatGPT is by OpenAI, and there are many other model providers and many other LLMs in the market. LangChain is a framework which can provide you a way for all these models' integration. You can actually code using LangChain, and behind the scenes, all these models can actually work for you.
Ideally, all these models have their own framework. Gemini has its own framework. We have something called as Google GenAI framework for this. We have something for Claude. We have something for ChatGPT as well. All of these model providers provide their own set of APIs. But LangChain actually provides you an integration. You can work with all these models in your own application using LangChain.
Now, the best part of this is, you might use Gemini and Claude for your work, for your application, using LangChain. And tomorrow, if you want to change Gemini to ChatGPT, you don't need to change anything in the code. You just need to change your model. So now your application can use ChatGPT and Claude together using the LangChain framework because the LangChain framework is like the integration. It's the integration point of your application. You are not actually coding using these model providers' framework. You are coding using LangChain. So the code is going to be common.
Now, how to install this LangChain? Basically, we are going to see the installation for the Python programming language. We have LangChain for other programming languages like JavaScript and TypeScript as well. But we are going to see how to install LangChain for the Python programming environment.
Now, you should have Python installed in your system. Once you have Python installed in your system, let's go to a terminal. I would say let's go to VS Code, but you can use your terminal as well. In the VS Code, I have opened my terminal, and in the terminal, you need to type the command: `pip install langchain`. Hit enter. Wait for some time. See, for me, it is already installed. So, for me, it is saying "Requirement already satisfied." But for you, after you run this command, the command, let me show you the command again. It is `pip install langchain`. The LangChain framework will be installed in your own system.
Now, once the installation is done, let me actually clear the console. How to actually use this LangChain framework to create your own AI-powered application is, you should know that what are the different LLMs you are going to use.
Now, once you see the installation guide for LangChain, what you will observe is, you have to install the LangChain package. And now, to use those LLMs, we have different LLMs, right? We have all these LLMs. And now, and we have LangChain. So, once LangChain is installed, you actually need to install which particular LLM model you need to use. So, let's say you want to actually use, let me open that page. We have this integrations page. In this integrations page, you need to decide what's the LLM model you want to use. Let's say I want to use the Google GenAI model, which is basically used for the Gemini model. So, I have to install this particular package: `langchain-google-genai`. So, just copy the name of this package. I will, I'll just provide you this link in the description of this video. So, you can directly go to this link and see which are the integrations available for different LLMs, and you can install those integration packages.
So, let me copy this `langchain-google-genai`. Go to the terminal and type the command: `pip install` and paste the name, right? So, you just need to run this command. For me, it is already installed. For you, this package will be installed.
Now, once this is installed, you can use LangChain to create AI-powered applications using the Google GenAI model, Google Gemini model, I would say. If you want to use, let us say, the ChatGPT model, which is provided by OpenAI, or you want to use the, you know, Anthropic's model, which is the Claude model, which is provided by Anthropic, so you will have to install that particular package.
Now, let us not worry about all these packages. Just let us worry about installing LangChain and installing the `langchain-google-genai`. Once it is installed, let's start creating our code.
Now, before we actually write our code, whatever LLM model you want to use, you should have the access to that model as well. For example, if I want to use a Gemini model, I should have a Gemini API key.
Now, I have already created a playlist on the Google GenAI Python package or framework, which is used to interact with the Gemini model. That playlist is called "Gemini AI with Python." Here, I have told everything of how to use the Google GenAI package to interact with the Gemini model using your Python code. In the first video, I have told how to get the API key. But let me tell you in short that how to get the API key for the Gemini model.
You will actually have to go to a link, which is `studio.google.com`, what you can see on the screen. Go to this link. The link you can find in the description as well. Once you go to this link, you will see something called as "Get API key." Now, this is a Google environment. So, you should actually log in using your Google account, or the Gmail account, I would say. Then, you will have to go to this particular page, "Get API key," and in this "Get API key," click on "Create API key."
Once you click on "Create API key," you'll have to just provide a simple name to your API key. Let's say, "test-lang." And then, you'll have to select a cloud project. If you have a cloud project, just choose that. If you do not have it, you can just click on "Create project." Name your project like "LangChain." Create project. And then the project will be created. So, now you have the name and the project. Click on "Create key." A key will be created. You can use this key and provide this key to your LangChain environment, and then your LangChain will be able to interact with the Google GenAI model and provide you a way to create AI agents.
So, you can copy the API key using this particular button. Just copy this. And then, what you have to do is, just go to whatever IDE you prefer. I prefer VS Code. Just go to any environment where you can do your coding. In that environment, create a folder named as `langchain-demo`, and in that folder, create two files: a file will be `.env` file, and another file will be `first.py`. You can name this as anything, but for this particular file, just name it as `.env`. Here, we are going to provide our actual Gemini API key, or the Google API key. So, you have to type `GOOGLE_API_KEY=` and then the value. This is the API key which you just got. This particular API key, you can just copy it from here and paste it here, right? So, your `.env` file will look something like this.
Once it is ready, now just close this and go to your Python file, the `first.py` file. And in this Python file, let's actually create an agent which can help us to get the weather details of a particular city.
Now, this is just going to be a demonstration. I'm not going to create a full code for this which will be a full working code, but a good demonstration on how to create your AI agent using LangChain. This is not just one video. I'm actually going to create many videos on LangChain. This is like a series. So, later on, you will understand each and everything about LangChain and how to create a fully functional AI agent using LangChain.
For now, let's say from `langchain.agents` you have to import something called as `create_agent`. Now, using this particular function, you will actually create your agent. So, let us create the agent and call it as `create_agent`. This is our agent. `agent = create_agent(...)`. In this function, you'll have to provide some arguments. You have to provide which model do you want to use.
Now, let me go back to the diagram. When you are creating an AI-powered application using LangChain, you are actually in the background using any model, right? Any large language model, right? Now, we have planned for Gemini. So, what we have to do is, we have to provide the agent a model. We are going to create a model. Apart from the model, you will have to provide your agent with some set of tools. If you want to provide your agent with some set of tools, you can provide it. If you do not have any external tools, it is completely fine.
Apart from this, let's also write a system prompt, an initial prompt which is going to tell what is this agent all about. So, we can say, "You are a weather assistant agent and also let's say answer in 50 words," right? So that our response is not too big. So, that's our initial prompt. We have to provide with some set of tools. But let's first create the model.
To create our model, we have to say `from langchain_google_genai import ChatGoogleGenerativeAI`. Now, this is the Google GenAI module which we just installed. After we installed the LangChain, right? We also installed `langchain-google-genai`, something like that. Let me make you check it again. It was `langchain-google-genai`. This was our package. So, from that package, we are actually getting this particular import. So, from `langchain_google_genai`, we have to import something called as `ChatGoogleGenerativeAI`. This is the class which is going to create our model.
So, let's create our model. And let's say `gemini_model = ChatGoogleGenerativeAI(...)`. This is our model. And in this model, we have to say which Google Gemini model we want to use. So, we want to use the `gemini-2.5-flash` model, right? So, now our model is ready. This is our model, `gemini_model`. So, we have to provide the model here: `gemini_model`. And we can also provide with some set of tools.
Now, we are actually creating a weather agent, right? See, the thing is, these models don't know what's the weather at New York at this particular current time. It, it doesn't know, right? The model is not getting trained live, right? It's getting trained, or it might have trained on a previous set of data. So, you'll have to provide a tool which can provide the details to the model that what is the, uh, temperature in a city at a particular time, or what's the weather in a city at a particular time.
So, let's create a simple tool, and that will be a function in Python. So, the name will be `get_weather`. Now, the thing is, guys, when you are creating these tools, make sure that you're giving a good name. The name should define that what this particular agent, or what this particular function is going to do, so that your model can identify that this particular tool is used to get the weather details. Let's provide an argument that for which city we want to get the weather, and `city` will be a string, and the response of this will be a string as well.
Now, before we proceed writing the code, let's write a small markup. This markup is going to help our model to understand in detail that what is this tool going to do. The `get_weather` function, or the tool, what's what it is going to do. "Get weather for a given city." Right?
Now, let's write our implementation. So, for now, we do not have a way to actually get the weather details. So, for now, let's just return a simple message. Return, "It's always let's say sunny in in the whatever the city is provided." So, let's say, `city`, and this should actually be an f-string. So, anytime if somebody will call this particular function with, with the city name, it will always return, "It's always sunny in this particular city."
We just need to provide this agent with this tool. So, our tool name is `get_weather`. So, now our agent is ready, our tool is ready, our model is ready. But the thing is, this model should be provided with the API key. So, you don't actually need to provide the API key, and that's why we did this setup of `.env` file. In this `.env` file, we already provided an environment variable. By default, this Gemini model will actually search for, I would say, search for an environment variable with this name `GOOGLE_API_KEY`. So, we have created our environment variable. We just need to load this environment variable.
So, for this, we need to say `from dotenv import load_dotenv`. This particular `dotenv` is a third-party module. It's not a Python built-in Python internal module. So, you'll have to install this. To install it, you have to say `pip`. Go to a terminal and type the command: `pip install python-dotenv`. Hit enter. It will get installed for you. For me, it is already installed.
So, now, once it is installed, you actually can write this particular code. And just call this function. This is a function. And just call this function, and that's it. This function will automatically load all the environment variables which are there in the `.env` file. This `.env` file should be in the same folder in which this `first.py`, or whatever file you are writing the code in.
Now, once this is done, we have our environment variable ready. We have our model, we have our model's tool, and we have our agent. Now, we just need to invoke our agent, right? So, let's invoke our agent and we can say, `response = agent.invoke(...)`. We have to invoke the agent and let's provide us initial message to the agent. Uh, we can provide a message using a dictionary. The key should be `messages`, and the value should be a list. In the list, we actually need to provide a dictionary. Again, in this dictionary, there should be two keys: one should be `role`, should have a value, and it should also have something called as the `content`, the content of the message. So, let's say the role is actually `user`, because we are providing this message. So, the role of this particular message is going to be `user`, and the content of this should be, "What is the weather in, let's say, New York?"
Save this. And now, if you want to print a response of the, uh, agent, you have to say `print(response["choices"][0]["message"]["content"])`. The response will actually be a dictionary in which there will be a key `choices`. In this `choices`, you will have multiple messages. I believe we are going to go in detail of how this response will look like. But for now, let me directly print a message. It should be in the last data. This should be a list, and in the list, the last data should be your latest message, and then you have to say `.content`.
Save this. And now, run the code. To run the code, you can say `python first.py`. Hit enter and wait for the response. We are definitely going to get a response.
So, it says that "The weather in New York is always sunny. Enjoy the bright skies and warm weather." This message is actually coming from the agent, right? But in reality, in the background, the agent is getting this message from the LLM model.
What's happening is, uh, you created an agent, right? This is your agent. You told that, "Hey agent, you have a model, you have your set of tools, and you have a system prompt." Now, I told that I'm invoking this agent with this particular initial message. The message says that the user has given a message, "What is the weather in New York?" So, now the agent will understand that the, the user is asking for me the weather details. So, in the response, the tool, the agent will try to, you know, invoke the tool. Why? Because it doesn't know the current weather in New York, right? It, it don't know about it. But it knows about a tool which can get it the weather details of a particular city. So, it just asks that particular function that, "Hey, `get_weather`, tell me what is the weather in New York." It got the response as, "It's always sunny in New York." So, it says, "The weather in New York is always sunny." That's the message coming from the LLM because now the LLM knows that in New York, the weather is sunny. And also, it added one more line.
So, this is how you can just create a very beautiful agent using LangChain. And now, the best part of it is, let's say tomorrow you do not want to use a Gemini model, the Google Gemini model. So, do you do not need to completely change your code? You just need to change your code at one place, which is the model. You can create a ChatGPT model and provide the ChatGPT model. Other part of your code will remain the same. That's because LangChain actually provides you a framework for integration. LangChain doesn't provide you a large language model. It just provides an integration between different large language models. So, that's what this particular diagram is all about.
So, guys, that's it for this video. In the next video, we are going to cover more concepts of LangChain, and we are actually working on a series, and a lot of videos on LangChain is about to come. So, make sure that you hit that subscribe button to stay updated with all those videos, and make sure that you like this video as well.
If you need to, uh, you know, get the answer of anything, if you are facing some issues, or if you need some kind of assistant in some other part of LangChain, just make sure that you put it in the comment box. If you need any help, then put it in the comment box.
For now, I think that's it for this video. So, I will see you guys in the next one. Thank you for watching this one.