In this exercise, we are going to explore the use of OpenAI functions, uh, in a long chain application,
and also the use of OpenAI functions out of long chain using directly the OpenAI API.
So, as you will see, using OpenAI function functions is extremely powerful.
It allows us a universe of possibilities and applications.
But in the current status of long chain, this approach may have glitches, may have, uh, situations
where uh, you break the application.
So it is possible that at the time you are watching this in this, this video, this glitch is fixed.
But by November 2023 it is still not so.
The long chain approach has a some areas where it is going to fail.
So here we are going to show you how it works, where it fails and how you can fix it using directly
the OpenAI uh API.
So in this, uh, chapter, we are going to design an LM application capable of receiving and managing
Chinese food orders.
So as you see in the code, this is a long exercise.
We are not going into the deep details, but we will go, uh, enough for you to see what is happening,
how it is working.
And with the working code, you will be able to experiment and try by yourself.
And then with the documentation, you can complete whatever doubt or question you may have.
But in this, uh, exercise, you are going to see that.
Connecting OpenAI functions to our LM applications.
I mean, the sky is the limit, as you will see.
So we will use for this to solve this, uh, request.
The ChatGPT capability to work with functions with OpenAI functions.
First, we will design a solution with long chain and study a use case where the solution may fail.
And then we will design a solution using the OpenAI API that solves the previous problem.
So as you can see, the process is long.
So let's go step by step.
First, as always, we connect with our dot m file to get the the credentials in order to communicate
with the OpenAI API.
And then so do you remember what we did when we were with agents in Lansing?
Uh, do you remember this agent that, uh, was, uh, deciding between different tools?
Right.
So if you remember a first, we, uh, imported the tools, and then instead of working directly with
the tools, we needed to provide a description of the tools to the LM.
So the LM was able to understand what.
Every tool does right what each tool does like.
Okay, this is a tool for searching Google.
This is a tool for math operations.
These are tools for this or that right.
So with the functions we need to uh take the same approach.
So we will have the function definition.
We will define functions in in Python are as as usual.
But we also have to provide an explanation or a description of what each of these function does to chat
GPT.
So this is what we do here.
So in this exercise in this application we are going to make use of two functions.
The first function is get item info.
This is a function that we are going to use to start the conversation with the customers.
Remember we are a Chinese restaurant, right?
Chinese food restaurant.
So the customers are going to start a conversation with us and they are going to ask us, okay, what
do you have in the menu?
Or how much is this, uh, Chinese rice, uh, this whatever.
Right.
So this is the function that is going to be in charge of that.
So we need to describe this function in this particular way for ChatGPT to be able to understand it,
the name of the function, the description, what the function does and then the parameters they, they,
they have.
And in this case you will see that the most important information regarding the parameters is the item
name.
So the product, the name of the product.
Right.
So we have the name of the product.
And then we have uh like like.
Like a like that.
The second function we have is going to manage a the orders.
So once you know the menu items and once you know how many of each of them you want, you are going
to make the order.
Right?
Okay I want to Chinese rice this whatever.
Right.
So this is the function that is going to tell you okay I got your order.
Your total is going to be $45.
And we are going to send the the order to your place in 30 minutes.
Right.
So you see that in the description of this, uh, second function we describe the function.
And then we, we, we, we say, okay, you have a number of required, uh, parameters item name.
So the name of the product, the quantity, how many of the product do you want and the address we are
going to send the the order.
Right.
So this is the first step.
We need to describe the functions in a way that ChatGPT can understand.
Right.
So just with this function description ChatGPT is able to start operating.
So if you don't have the function configuration, ChatGPT will not be able to proceed and execute the
function, but he will be able to identify which function is the right one for your particular question.
So just with these two function descriptions, ChatGPT is going to be able to say, okay, I need to
use this function, or I need to use this function depending of the question we make.
Let's see.
Let's see that.
So.
Let's see first how we use OpenAI functions with long chain.
In order to do that, we are going to create a chain.
That long chain has predefined for us.
In order to work with OpenAI functions, this is the chain create OpenAI function chain write.
This is the module we are going to use to create this chain.
So in this exercise we create a language model.
We create a prompt template.
And we create a chain with the language model and the prompt uh.
On the prompt template and see here that in this case, this create OpenAI function chain has a special
argument called functions.
So this argument functions is equal to.
These variable functions we have defined here.
We can call this variable whatever we want if we want.
If we call it functions we don't need to write here functions equal to functions because that is assumed.
But if we call this my functions or whatever I don't know like like my car.
Here we are going to say functions equal.
My car.
Okay.
You get it.
So.
When we execute this chain.
And we enter this user input.
Okay.
How much does the job say cost?
Okay.
Since we have here verbose true, we can see what is going on under the hood.
And you see that eh.
The the LM has here the prompt, you know, like okay, I am a chatbot having a conversation with a
human.
This is what the human is telling me.
And this is the conversation history.
And this is the response that right now ChatGPT can provide, which is okay.
For this question, I will need to use the function, get item info, and I will need to pass this argument
to this function.
So how?
Thus ChatGPT know it because it has the function description.
So when we make a question, a ChatGPT is going to say it's going to come here and say, okay, is this
function right for this or is this function right for this?
Right.
Do you remember how long chain operated when we were working with agents and different tools is the
same process, right.
So when we are when we were defining an agent with different tools, we make a question and then the
agent is able to identify which of the tools he needs to to use.
Because we have described the tools right, we have provided ChatGPT with the description of each tool.
So here is the same.
Since ChatGPT knows the function description, he knows what function is the right one to to to to create
a response and how.
He is going to configure this function.
So what are the parameters that he's going to use to execute that function.
Right.
So this is what a ChatGPT is doing here.
So if we continue our conversation and say, I want to order two chop suey to this address, right?
So once I have the information, I say, yeah, okay.
I like we don't have the information yet because the function wasn't run right.
But in case we had it and we follow the conversation with a second question like this, then ChatGPT
is going to identify that in order to respond to this second question, it needs to use the second function,
the function place order.
And he needs to execute this function with this argument.
So the item name is chop suey.
The quantity is two and the address is 321 street.
Okay, so see how powerful it is and the use of OpenAI functions.
What is the problem?
So don't worry, because now we are going to use this with the function definition.
So you are going to see this working.
But.
First of all, I wanted to show you what is the problem with long chain and OpenAI functions.
So if the user.
Behaves as expected if the user makes questions, in this case about Chinese food.
No problem, it is going to work.
But the problem comes when the user goes out of the script.
Right.
So let's say that the user asks how old did my grandfather get?
How well is my grandfather?
So.
In this case, the application is going to break and it's going to show an error message and it's going
to stop.
So this is a problem.
And if you have this application in production, your customer is going to have a bad customer experience.
You don't want a an application to break.
In this case you want to tell the user, I don't have this information or I only I am only allowed to
speak about Chinese food or whatever you don't want to break.
You don't want your application to break if a the user goes out out of script, right?
So how probably long chain is going to solve this.
But if long chain doesn't solve it, you have the option to use OpenAI API directly.
So here you you see how to create a chat model with the OpenAI API.
Right.
So here we are defining a chat model.
So a different a way a different chat a and we are including here the functions okay.
So this chat is aware of the function descriptions we have provide.
So if we make the same questions to this chat we are going to have the same answers.
Except if we go out of script.
This application is not going to break.
So this is a interesting.
And if we.
If we fill this application with the two elements that are a out, which is first the database.
So we need to know what are the items we have in the menu, what are the prices ingredients.
And second the function definition.
Here we are not going to use a real database.
We we don't want to connect with too much work for this exercise.
So instead of doing that we are going to fake a database with a Python dictionary.
Right.
But for the application is going to be like this is a database, right.
So once we have that we can create the functions.
And this functions a that are going to use the fake database as you, as you see here are going to uh
behave as we expect.
So the get item info function is going to give us information about a product and the place order is
going to, uh, manage, uh, the order.
Right.
So if we, uh, use these uh, functions with the new, uh, chat we have created with the OpenAI API,
we can see that we are able to manage the whole order process.
You can see that we can tell, okay, your order was placed successfully.
Your order ID is one, and your total price is $30.
Because you have asked for two jobs.
We and we know that chopsuey.
Is $15 each.
So since you have two units, it's going to be $30.
All right.
So here you have a working code for this, uh, application.
You can also improve this app making it dynamic so the LM can continue the conversation after the order
has been placed.
Right.
So here you have a little bit more detail a code in order to achieve that.
And then you can try.
And see that if a type of food is not in the menu, for example, the chatbot is going is going to respond
appropriately in an appropriate way.
And also, if you ask an off topic question, the chatbot is also going to act to respond in a in a
in a appropriate way as well.
Okay.
So just a stay with the main concepts.
Uh, very important to understand that.
The use of OpenAI functions with LM applications opens the door to a whole universe of possibilities.
So very important for you to understand right now.
What is the status of long chain about this?
What is the alternative of using directly the OpenAI API?
And as always, to understand that you can go to the launching documentation and in this case to the
OpenAI API documentation in order to explore more, to experiment more, and to know more about this
a depending of your particular necessities.
But this is absolutely one of the most interesting and powerful, uh, ways we have with LM applications.