Check out a free preview of the full Build AI-Powered Apps with OpenAI and Node.js course
The "Creating a Function" Lesson is part of the full, Build AI-Powered Apps with OpenAI and Node.js course featured in this preview video. Here's what you'd learn in this lesson:
Scott creates the application for demonstrating AI function calling. The advanced-math NPM package is installed and a calculate function is created which will call the math.evaluate method inside the package.
Transcript from the "Creating a Function" Lesson
[00:00:00]
>> That is not okay. Let's do this. So function calling. Let's do that. So let's set up our code. So first thing is I'll make a file called functions.js, okay? And we're gonna import from openai, the file that I created. And we're gonna get openai. Cool, and we're gonna do all this thing called Advanced Calculator.
[00:00:23]
In this demo, I'm just gonna walk through how we can teach GPT how to do advanced math. It actually is not that good at math. It seems like it is, but if you ask it to do some quadratic form or something like that, it'll just live straight through its teeth.
[00:00:43]
So you probably wanna help it and like teach to do math, but we can actually write a function to teach you anything. And if I got time, I'll write a function that talks to another AI and have it do something. And then you can see just how crazy it gets cuz it's unexpected.
[00:00:57]
Cool, okay. So we got that. I'm gonna download that thing. Events calculator, what is it? It's pretty cool. It takes a mathematical expression as a string and then it evaluates it, which is exactly what I would want. So it could take something like this, right? And then evaluate it.
[00:01:20]
Who made this? Who sat down and was like I'm gonna do this. Good for them. But this is great cuz the AI will probably respond with something like this and we just wanna feed that to a function. And this is the function. So it's great. It works out for us.
[00:01:37]
So we're gonna teach the AI how to evaluate advanced math. But we can teach it anything. We can teach it anything a function can do. Just think about that. Any function that you can write ever in your life, you can have the AI do that. So there's literally an infinite amount of possibilities.
[00:01:55]
You can have this thing, you have a function that sends back an entire React app, it can do that, [LAUGH] right? There's nothing it can't do. So let's do that. So we'll import that, important math from that thing. And then we'll make a question. Oops. There we go.
[00:02:23]
And same thing, process.argv2, or just, some default thing, doesn't really matter. We have that. And then what we wanna do is, first, we're just going to, so, similar to the way we did chat, we need a loop here. But we're not gonna create a chat interface, we're just gonna have it run a function.
[00:02:43]
So we don't need an in-and-out thing, we just needed to run on a loop because it might need to make multiple calls. So, we'll do like a wild true here. But first, let's just create our messages here. So, the first one is just gonna be I just skip the system message.
[00:02:59]
You can add a system message if you want to, it doesn't really matter. I just get the system message and I put our first message in here. It's gonna be whatever question got typed in All right, so that's the beginning of the message history is a question from us.
[00:03:15]
And then from there, I need to create an object that is gonna act like a router for our functions. Because, you'll see in a minute, the way we know what function to run is because we towed GPT what is the name of the function to run, and then we need to map that to actual functions in our code.
[00:03:33]
So, the best way to map things is with a map, an object. So let's do that. I'm gonna do that. So in this case, I'm gonna have one function. You can call it whatever you want. I'm just gonna call it calculate. And calculate is basically, I don't know why I have async here.
[00:03:51]
You don't need a async here. It's going to take, this is totally up to us. I'm later on I'm gonna tell Open AI that when you call calculate, you need to pass me an object with a property on it called expression. It must be in a property called expression.
[00:04:09]
And it has to be like a string. I'm going to I'm going to give GPT a schema on how to pass an argument to this function if it wants to call it. And it's up to you. It's a design choice. I'm choosing to accept an object with a property on it called expression that's always gonna be a string because that's what this function wants.
[00:04:29]
This math that evaluate takes in a string. So it's just easy for me to get that from GPT. If I got back a number, then I have to figure out how to translate that number to a string and then feed it here. It's too much work. Okay, and then I'm just gonna return math.evaluates, or what is it?
[00:04:47]
Yeah, math.evaluate, and then pass it that expression. And this is what I'm gonna return back to ChatGPT. Whatever the result of that is. So we got our functions and eventually you can add more functions here. Yeah, can add whatever functions you want. It's just a function.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops