Introduction & Initial Setup
Hello crew and welcome to the very first LangChain tutorial where we go over the quick start guide that is held within the documentation. Now if you want to see more explicit information about this, I suggest you head over to their documentation and you can check out the examples that we're going to run through and you can see some code samples and get a lot more information here. My goal for with this Python notebook is to give a bit more color commentary for the top of it and to explain a little bit more about what's happening underneath the hood so that you're able to understand and run through some examples together.
Now the very first things that you're going to want to do is pip install langchain and pip install openai. I already have both of these and so I won't do that again. And then the next thing that is extremely important, you're going to want to set your OpenAI API key as an environment variable. Now, why is this? Well, because we're going to be making some API calls to the OpenAI API and in order to do that, you need an API key. And the way that you would do that is you'd head over to openai.com, log in, register, and under your name, you'll see that there is an API key section. In fact, let me just show you just so you can have it. You click on API, and you're going to log in. I'm going to log in with my Google again. And all of a sudden, I get my dashboard, and then underneath your personal, there's a View API keys, and you can go view all of your API keys that you want.
Once you have that, you just need to set it as an environment variable. And in order to do that, in order to do that, let's see, let's get some video back. Beautiful. And in order to do that, what you need to do is you can either set this within your terminal and make an environment key or environment variable that way. Or what I like to do is I just like to import OS, and then I like to do os.environ["OPENAI_API_KEY"] and then insert your key here. And then you can just go ahead and run that command, and then it would set it for your environment here. I'm not going to do that because I've already done it.
Your First LLM Call
Okay, now let's get into the really cool part now that we've gotten past the setup. So, building a large language model application. What we're going to do here is we're going to run through about four or five examples of some pretty cool use cases using LangChain. And if you're not going to see anything else in this tutorial series, I think this is a cool one because it might start to get your ideas started to start going.
So the first thing that we're going to do is we're going to import from LangChain's large language model support, and in particular, we're going to import the OpenAI one. Now, with this OpenAI one, we're going to create a variable that's going to be our language model: OpenAI, and then we're going to have the temperature at 0.9. Now, if you're not familiar with temperature, this is the amount of randomness that is going to be in your model. And if you were to set this to zero, then this would be a deterministic model, meaning you get the same result over and over and over again. But because we want a little bit of variability within our data outputs here, I'm going to put the temperature at 0.9. So we'll go ahead and run this. Okay, now this model has been initialized.
And so what we can do is we can start to feed it some text, and we're going to go ahead and query that exact model within the OpenAI API. And so, what are the five vacation destinations for someone who likes to eat pasta? I'm going to go ahead and I'm going to store this prompt in a text variable, and I'm going to feed this text variable to this language model and let's see what happens here. And you'll notice that there's a delay, and that's because it's actually going and querying that model right now. And then of course, we got some really nice answers. If you like pasta, you should go to Rome, basically anywhere in Italy. Cool. Wonderful.
Let's move on to the next example. So this is around prompt templates. Now, prompt templates are going to be an easy way for prompt management. And what I mean by prompt management is you're able to more easily maneuver and scale the different prompts that you're using. So in this case, LangChain has a PromptTemplate object, and you can see that we're importing it here. And so with this template, we're giving it some input variables, and you notice that this is a list, which means we're going to be able to do more of these or more input variables later. But in this case, I'm just passing one, which is the food variable. And the template is going to be "what are the five vacation destinations for someone who likes to eat food?"
So the same thing that we had above, but instead of pasta, I want to turn this into a variable, which is what I'm going to create a template around. So let me go ahead and import both of those. Okay, cool. And then what I'm going to do is I'm going to print the prompt of where food equals dessert. So if I go ahead and print this: "What are the five vacation destinations for someone who likes to eat dessert?"
And then I'm going to take this prompt that it just made. I can store this in a variable if I wanted to, but I'm just going to create this one and I'm going to feed that into the large language model. Let's go ahead and print that out and run that. And all of a sudden you get some cool dessert destinations. And so New York City, fabulous. Tokyo, haven't been. Paris, I would love to. And I enjoy seeing San Francisco on there. Sometime about 10 minutes north of San Francisco. Cool. So that's around prompts and prompt management.
Multi-Step Workflows with Chains
So a next really cool one is we're going to start to do some chaining. Now, what chaining is, is when you're going to have multi-step workflows. So right now we've been asking one question and getting one answer back. But what if your prompt needs multiple questions and multiple answers in order to complete them? And so let's go ahead and let's see what we can do here.
So we're going to have our prompt templates, we're going to have OpenAI, and we're going to have a chain. And so then all of a sudden we're going to have our... this is basically what we did beforehand. We have our prompt templates, we're going to have a chain which we're going to create. And then I'm going to see what is something for where somebody likes to eat for food. Cool. So now we have these chains that are coming over from here.
Now, if you want more information on chains, of course, please head over to their website and go check it out. But in this one, we feed both the language model and the prompt. We didn't have to feed the prompt into the language model, so the chain took care of that for us. Cool.
Now we want to look at agents. And agents are going to be extremely cool because this is where you start to be able to have others do work on your behalf. Now, in this case, what we're going to do is we're going to go check out Google Search results. So then what's really cool is all of a sudden we're connecting OpenAI with Google Search, which is something that we could not do with the raw engine specifically. So in order to do this, you need to import or install google-search-results. I already did this and so I won't do this again.
Then I'm going to import load_tools, initialize_agent, and OpenAI. So very first thing is we're going to instantiate our language model. And then we're going to use a tool called serpapi, and this is where you can go and scrape basically Google searches, and I've already imported my API key, so I won't bore you here with that.
Now, the other thing that we're going to do is we're also going to import llm-math, and this is going to be another library or another package that LangChain supports, and this is going to help us do some really cool math. So all of a sudden we're going to load some tools, and we're going to load in the large language model that comes with it, and that is because llm-math needs that language model. Cool, we got that.
And then we're going to initialize these agents, initialize this single agent. So we're going to initialize the agent, and we're going to pass in the tools that have been loaded up above here. We're going to pass in our language model, and we're going to pass in an agent type. Now, this may look like gibberish: zero-shot-react-description. However, if you head over to the agent documentation, there's other agent types that you can use depending on your use case. And for this one, I want to do verbose=True. And the reason why we do verbose=True is because it is going to print out all the many steps and all the information that it's thinking about in the first place. This helps us debug what's going on and really see underneath the hood.
So what's cool about this one is I can do a longer prompt, and not only a longer prompt, but this prompt needs multiple steps in order to answer. So, who is the current leader of Japan? What is the largest prime number that is smaller than their age? So it's going to go grab that leader of Japan and do the smallest prime number below them. So let's go ahead and let's run this. And then all of a sudden it says, "Oh, I'm doing a new executor chain." And this is what's really sweet, is you can see what is it thinking about what it needs to do in order to do your request. So in this case, they need to find out who the largest—prime minister is of Japan, and then the prime number that's smaller than the age. So it goes and finds the current prime minister of Japan, has the right answer. And then it needs to find their age. So it goes and searches that, and it finds his age. Sweet. And then it needs to find the largest prime number smaller than that. So it goes and gets the calculator, finds 65, knows the final answer. And it says the current leader of Japan is this dude and has the largest prime number that is smaller than his age of 61, because he's 65. That's cool.
Creating Stateful Applications with Memory
And then finally, let's move on to memory. So this is the concept of—let me see if I can make this a little smaller... No, let me zoom out just a little bit. Oh, that's annoying. Okay, well, this is the concept of having memory. So this means that your agent can remember things that you've done in the past.
And so for example, we have OpenAI and we are going to import our ConversationChain. So we have a large language model and we have our conversation chain that we're going to get instantiated here. And again, we did verbose=True so it prints out all that really cool information for us.
So now what I'm going to do is we're going to start to have a conversation with this chatbot. So in this case, I'm going to say, "Hi there!" and you can see here that it says, "Hi there," and it went and grabbed the response from the AI. And in this case, it's the OpenAI API. And it says, "Hi there, it's nice to meet you. How can I help you today?" And I said, "I'm doing well, just having a conversation with an AI." So there's my first text to it. I get a response. Here's my second text. It's thinking. And then all of a sudden it says, "Great, what do you want to talk about?"
I'm saying I want to double check that the memory actually works. And so in this prompt I'm going to say, "What is the first thing that I said to you?" So that I can see that it goes back and remembers that. It says, "You said, 'Hi there!'" Nice.
"What is an alternative phrase for the first thing that I said there?" And so I'm making sure that it has to actually go understand what was the first thing that I said and understanding what is an alternative phrase for it? "An alternative phrase for the first thing you said to me is 'Greetings.'"
Pretty cool how it can go back and see the memory of what we chatted about.
Conclusion & What's Next
Now that is a quick start guide for what LangChain is and how you can use some interesting examples there. What we're going to do is we're going to go a lot deeper into some technical details and some more very actionable use cases for you. So enjoy and follow us on the next video.