Okay.
So.
In this chapter we are going to talk about a basic long chain operations.
So the goal of this chapter is not to go into details, is just show you how easy it is to work with
a long chain.
So a.
From now on you are going to see the screen in these two sides.
In this side we are going to follow the presentation in order to to have, you know, the main concepts
we are talking about and in this other side is where we are going to have the code of our application.
As you can see, this is a Jupyter notebook.
And in the Jupyter notebook you see a couple of things.
You have markdown and you have code.
So a Jupyter notebook.
Let's move.
This Jupyter notebook is something you.
And you create, you will see that we will have the terminal open.
As you see, you can see that I have a virtual environment here.
And we are also having a ChatGPT here as an assistant for us.
So for example you can come here, you can use GPT 3.5.
This is the free version or GPT four which is the plus uh, subscription of OpenAI.
And this allows you for a very, very, uh, convenient price, uh, to use different, uh, functionality.
The default is extremely good because it knows about coding is really amazing.
It's like GPT 3.5 on steroids.
But you also have, you know, different possibilities.
Like to browse with Bing.
And this is where you can find answers to, uh, current matters, you know, things that happened yesterday
or whatever.
Right.
So for example, if you enter here how to install Pyenv.
You are going to see that immediately.
GPT four is going to, uh, give you the detailed instructions for doing so.
So super easy companion to have a in the terminal.
You know that I have several windows of the terminal open in one of the, uh, one of the windows.
I, I entered Jupyter notebook.
If you click here, you are going to see a screen like this.
So here we have a lot of Jupyter notebooks already created.
You can create a new Jupyter notebook doing this.
Notebook new.
And you will see something like this.
We want this with Python three.
You will see that the Jupyter notebook is a is formed by cells.
This is a cell.
In the cell you can have, you know, code.
In order to execute this code.
Once you are here, you, uh, press shift and enter, and then you execute the code.
And here, if you want to have markdown instead of code, you can have a.
Things like that.
Right.
So this is not going to be executed.
This is just a markdown like like text.
So if you go to ChatGPT and ask about Jupyter Notebook markdown, you will have a complete guide or
super easy to to use.
And this is very, very useful to have markdown with your code because it's going to allow you to understand
the code you're writing, and especially if you are learning or teaching, it's a super useful tool.
And a in the code you can come here and for example comment out this.
Now you do shift enter.
This is not executed because it's commented out.
If you come back and you remove the pound sign and shift enter again, you will execute.
You come here and click in.
Plus you create a new cell.
You click here.
You are removed.
This is super easy.
This is super easy to use Jupyter notebook.
So in this, uh, notebook we have five operations in in long chain to show you how easy it is.
The first thing you see here is how we are invoking the dot env file.
So if you remember in the previous chapter we talk about this hidden file where we have our passwords,
you know and private information.
So this dot m file is something like this.
We have a file that is called dot env.
And we have inside of this file which we can create this file with whatever editor like the typical
notes editor you have in your computer.
Important thing is that this dot m file is in the same directory as the directory you have here.
So here in this folder I have the dot m.
But since it's a hidden file uh, you you cannot see it here.
So in this file I have a variable called OpenAI API key in capital letters equal to, you know, and
here I copy and paste my, you know, OpenAI key.
I close this file, okay.
And uh, and I invoke it from the Jupyter notebook.
So what I'm doing here is import a module called OS.
And from dot m the module dot m import these two uh components.
So this thing the module dot m, uh, if you remember in a previous chapter, the way we import this
module is actually, uh, in importing Python.
A m instead of a m by itself.
So is pip python a dot m okay.
So once you have that imported, if you don't have it, you will see an error message.
Then you go to terminal and you import.
It is like pip install python this dot m.
Okay.
So.
I say pit instead of pip is pip okay?
Then you execute it and you will install.
I have it installed so I'm not going to do it again.
Okay, so once we have that now this Jupyter notebook has my OpenAI API key save in this variable.
Okay.
This is the name that OpenAI require us to have a in order to, uh, use the OpenAI API key.
So first operation we are going to do.
How to connect with an LLM.
In this case, we are going to connect with OpenAI with ChatGPT.
Okay.
We are going to ask a question to ChatGPT from our application.
So the first thing we do is we call the module OpenAI inside of lang chain.
And with this module we are going to create a new instance of an LLM.
In this case we are going to call it my creative LLM.
And as you will see, we are going to include a temperature of 0.9.
Why?
Because uh, we want this instance of LLM to be creative, very creative.
The creativity or the temperature of the OpenAI ChatGPT is from 0 to 1.
So 0.9 is a very high creativity and zero is a very low.
We are going to use low creativity when we want to have very accurate information, and we are going
to use high creativity when we want.
We don't matter about it if if it is a fake content or a hallucination or whatever, we don't care as
long as it is interesting or beautiful for us.
Right?
So we are creating a brand name or we are creating a story for our children or whatever.
We want to have a high temperature, high creativity, or if we want to have factual data, we want
to have low creativity, low temperature.
So this is the question we want to, uh, to send to the.
Chat gpt lm.
So we save this question in a variable.
And then the only thing we do is we.
Send this question to the instance of the LM we have created and print the response.
So this is the response that ChatGPT sent to our application.
As you can see here, all this code is written for you.
I don't want to waste time writing, writing and executing these cells.
This is a pre-written, uh, for you so we can go a little bit faster.
So this is the response that the GPT, OpenAI LM has sent back to us.
So to the question, what are the five best things to do in life?
ChatGPT says, live life of purpose and meaning, blah, blah, blah.
Excellent.
Okay.
So you see how easy it is to connect our application with ChatGPT.
And we can do that with any other foundational.
So we will do the same when we are connecting with llama too, or anthropic or whatever Elm foundation
elm we want to use.
Second thing, I want to show you how to wrap the user prompt with a prompt template.
So we are going to do is we are going to create the prompt template with a variable to include the user's
prompt.
Second, we are going to get the user's prompt.
Then combine the user's prompt with the prompt template, pass the combined prompt to the LM and finally
get the answer.
So what we have done here is we have this question.
Is the prompt okay.
The prompt is what we tell the LM.
So in the second example we are going to do what we are most frequently going to do in our application,
which is in the application.
The LM engineer is going to create a prompt template.
The prompt template is not the full prompt, it's just a part of the prompt that has some parts that
are going to be filled by the user.
So as you can see here, we are using the prompt template module to create my prompt template with this
content.
And in this content we are going to include a variable.
So the template is going to be what are the three best things to do in a destination.
But the destination is not provided here.
It's just a variable.
And this variable will be provided by the user.
So this user can provide this variable from a form or a mobile or an email or whatever.
But in this case we are going to enter it manually.
We say this input variable is Barcelona.
So the prompt we are going to send the LM is going to be what are the three best things to do in Barcelona
okay.
Or Barcelona.
So what we are doing here is we are telling okay.
Enter this prompt template.
With this variable.
And send it to the creative LM.
You have created and print the response.
So the LM ChatGPT in this case is going to send back this response.
So what are the three best things to do in Barcelona.
First, explore the Sagrada Familia.
Second, wondering La Ramblas.
Third, take a day trip to the park.
Blah blah.
Excellent.
Good answer.
Third thing, I want to show you how to combine instructions in chains.
So long chain as its name because of it is very good doing chains.
We will see that chains are just a collection or a sequence of instructions we group together.
So what we are going to do here is we are going to do the first two things we previously did.
So initialize an instance of the foundation lm.
Create the prompt template with a variable to include the user's prompt.
Get the user's prompt.
All this we have already made.
Now we are going to add the fourth step, which is combine the user's prompt with the prompt template.
We will do.
Uh, we will create a chain combining the LM and the Prom template, and we will run the chain.
So this we already did it.
We create we have an instance of the LM, we create a Prom template.
And, uh, in this case, the template is going to be what are the three most remarkable achievements
of a politician?
We don't know which politician is going to be the variable that the user is going to include.
And here what we are saying is create a chain instead of a printing.
Here, all the things we were printing in the previous exercise.
Here we are printing the chain.
We are printing the result of running the chain with this variable.
So the politician that the user is giving us is Winston Churchill.
And here we have created the chain with the component LM chain we have imported.
So here what we are saying is okay, use this component to create a chain that is going to use the LM
you have created and the prompt template you have created.
And here send this chain to the foundation LM to ChatGPT in this case and print the response.
So in this case the prompt we are sending is what are the three most remarkable achievements of Churchill
Downs is going to be first one leading Britain through the World War two.
Second, establishing the special relationship between the United Kingdom and the United States.
And third, winning the Nobel Prize in Literature.
Okay, very good answer.
The next thing I want to review, just to see how easy it is to do things with long chain, is to create
an agent.
So agents are one of the most interesting things with LM applications.
In this case you will see how fast we can create an agent.
But we are going to do is to load external tools, initialize an agent over a foundation LM in this
case OpenAI, and set a goal for the agent and let it decide what tools to use, uh, to achieve it.
Okay.
This is the purpose of the agent.
The agent is going to reason by itself.
So we are going to to in this case, we are going to create an agent.
And we are going to tell him, okay, here you have a tool, uh, to search on Google.
And here you have a math calculator.
Use whatever you need.
You decide.
So if we ask a mathematical question like, okay, you know, calculate the square root of whatever
this agent is going to say, okay, I'm going to use the math calculator for this.
And if we make a question which is I don't know how old are the pyramids in Egypt, the agent is going
to say, okay, for this question, I'm going to use the Google search in order to find the results.
Okay.
So.
We achieve this functionality with these six lines.
So what we are doing here and this, all these six lines are commented out in order to comment it out.
All the code in a in a cell in Mac you use command plus design.
Okay, so if you use this, all the code you have in in the cell is commented out.
What we are saying in this code why?
Why have we commented out this code?
Because as I say here, operations with agents are more expensive in tokens.
So if you know the previous operations have a cost of $0.0003 for you, this one can cost you ten times,
uh, more.
So it's still very low.
But I just wanted to tell you this is more expensive.
How can you control.
How much are you spending?
So in your OpenAI account, you have this usage, uh, tab where you can see how much have you spent
per day.
And you can do a lot of operations and still spend, you know, less than $1 per day.
So it's good for you, you know, to control on a daily basis at the at the beginning, what is your
cost and your expenses in ChatGPT?
Okay.
This is is not exactly real time, but is it can have a delay of one minute in order to to update the
cost that you are, uh, you are having.
So in this case, what we are saying is load the tools.
Initially this is a module two load module to load the tools, a module to initialize the agent, and
a module to initialize the LM.
And these three lines of code make it all.
The first thing we do is load two tools.
Three tools, actually the Google search, the calculator and the LM.
Then we are initializing an agent with the previous tools and the LM.
And the type of agent we are going to use is this type you have here you have several types of agents.
We will see it.
And then with this line of code agent dot run, we are executing the agent with this particular question.
So here as you can see in this prompt we have two questions.
Who is the current leader of Japan and what is the largest prime number blah blah blah.
So for the first question the agent is going to use the Google search.
And for the second question it is going to use the math calculator.
And this decision is made by the agent itself.
That's the powerful thing of agents.
And the last thing I want to cover is the memory how to add memory to the LM.
So in this case we are going to use a chain, a predefined chain called conversation chain to record
the conversation in the memory of the LM instance.
So with just these lines we well we are importing a couple of modules OpenAI and conversation chain.
With the first one we are creating a LM instance.
We call it my no creative LM because the temperature, the creativity level is very low and we are going
to create a chain.
With that LM.
This chain is created with the component conversation chain.
And because of that it is going to have a very interesting functionality already included.
So this is one of the predefined chains that long chain has created for us.
And this is going to save us a lot of work.
Okay.
So with this chain that we call my conversation we can start a conversation with the LM.
So hi there.
The LM is going to answer first showing us the prompt, the default prompt that the conversation chain
has created.
And then it's going to tell us okay, this is what you are saying.
Hi there.
And this is what I respond.
Hi there.
How can I help you?
Next thing we tell we tell the LM.
My name is Julio.
What is your name response my name is Ivan.
It's nice to meet you, Julio.
Third thing.
Do you remember my name?
Yes, I remember your name is Julio.
This is just a very basic example to show you how easy it is to create an LM application with memory.
And the last thing you have here is we are printing my conversations.
We are printing these.
Chain you have created.
We are printing just to see what is inside there.
You see that inside here, this variable, my conversation.
There are a lot of things.
There are memory.
There are things like human message, I message, etc. etc. we will see what is this about in the next
chapter.
So the purpose of this initial chapter regarding long chain is just to show you how easy it is to work
with this framework and how powerful it is.
What we have covered here are super simple things we haven't gone through details you know, into details,
but you see how easy it is, uh, to communicate with, uh, the, the long chain code, to use the
long chain code and to integrate, you know, different tools and different functionality.
We will go into details in the next chapters.