# How to build an agent with the Node.js SDK and function-calling
# How to build an agent with the Node.js SDK
OpenAI functions enable your app to take action based on user inputs. This means that it can, e.g., search the web, send emails, or book tickets on behalf of your users, making it more powerful than a regular chatbot.
@ -13,7 +13,7 @@ If you prefer watching screencasts over reading, then you can check out [this sc
Our app is a simple agent that helps you find activities in your area.
It has access to two functions, `getLocation()` and `getCurrentWeather()`,
which means it can figure out where you’re located and what the weather
is at the moment.
is at the moment.
At this point, it's important to understand that
OpenAI doesn't execute any code for you. It just tells your app which
@ -27,12 +27,12 @@ internal knowledge to suggest suitable local activities for you.
We start by importing the OpenAI SDK at the top of our JavaScript file and authenticate with our API key, which we have stored as an environment variable.
```js
```js
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
dangerouslyAllowBrowser: true
apiKey: process.env.OPENAI_API_KEY,
dangerouslyAllowBrowser: true,
});
```
@ -44,11 +44,11 @@ Next, we'll create the two functions. The first one - `getLocation` -
uses the [IP API](https://ipapi.co/) to get the location of the
@ -74,49 +74,51 @@ describe them using a specific schema. We'll create an array called
`functionDefinitions` that contains one object per function. Each object
will have three keys: `name`, `description`, and `parameters`.
```js
```js
const functionDefinitions = [
{
name: "getCurrentWeather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
longitude: {
type: "string",
},
latitude: {
type: "string",
}
},
required: ["longitude", "latitude"]
}
{
name: "getCurrentWeather",
description: "Get the current weather in a given location",
parameters: {
type: "object",
properties: {
longitude: {
type: "string",
},
latitude: {
type: "string",
},
},
required: ["longitude", "latitude"],
},
{
name: "getLocation",
description: "Get the user's location based on their IP address",
parameters: {
type: "object",
properties: {}
}
}
]
},
{
name: "getLocation",
description: "Get the user's location based on their IP address",
parameters: {
type: "object",
properties: {},
},
},
];
```
## Setting up the messages array
We also need to define a `messages` array. This will keep track of all of the messages back and forth between our app and OpenAI.
We also need to define a `messages` array. This will keep track of all of the messages back and forth between our app and OpenAI.
The first object in the array should always have the `role` property set to `"system"`, which tells OpenAI that this is how we want it to behave.
``` js
const messages = [{
role: "system",
content: "You are a helpful assistant. Only use the functions you have been provided with."
}];
```js
const messages = [
{
role: "system",
content:
"You are a helpful assistant. Only use the functions you have been provided with.",
},
];
```
## Creating the agent function
We are now ready to build the logic of our app, which lives in the
@ -125,18 +127,20 @@ We are now ready to build the logic of our app, which lives in the
We start by pushing the `userInput` to the messages array. This time, we set the `role` to `"user"`, so that OpenAI knows that this is the input from the user.