mirror of
https://github.com/james-m-jordan/openai-cookbook.git
synced 2025-05-09 19:32:38 +00:00
Update title and reformat with prettier
This commit is contained in:
parent
ec414ee67b
commit
6d3db1374b
@ -1,4 +1,4 @@
|
|||||||
# 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.
|
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.
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ import OpenAI from "openai";
|
|||||||
|
|
||||||
const openai = new OpenAI({
|
const openai = new OpenAI({
|
||||||
apiKey: process.env.OPENAI_API_KEY,
|
apiKey: process.env.OPENAI_API_KEY,
|
||||||
dangerouslyAllowBrowser: true
|
dangerouslyAllowBrowser: true,
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ user.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
async function getLocation() {
|
async function getLocation() {
|
||||||
const response = await fetch('https://ipapi.co/json/');
|
const response = await fetch("https://ipapi.co/json/");
|
||||||
const locationData = await response.json();
|
const locationData = await response.json();
|
||||||
return locationData;
|
return locationData;
|
||||||
}
|
}
|
||||||
@ -87,20 +87,20 @@ const functionDefinitions = [
|
|||||||
},
|
},
|
||||||
latitude: {
|
latitude: {
|
||||||
type: "string",
|
type: "string",
|
||||||
}
|
|
||||||
},
|
},
|
||||||
required: ["longitude", "latitude"]
|
},
|
||||||
}
|
required: ["longitude", "latitude"],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "getLocation",
|
name: "getLocation",
|
||||||
description: "Get the user's location based on their IP address",
|
description: "Get the user's location based on their IP address",
|
||||||
parameters: {
|
parameters: {
|
||||||
type: "object",
|
type: "object",
|
||||||
properties: {}
|
properties: {},
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
]
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
## Setting up the messages array
|
## Setting up the messages array
|
||||||
@ -108,15 +108,17 @@ const functionDefinitions = [
|
|||||||
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.
|
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
|
```js
|
||||||
const messages = [{
|
const messages = [
|
||||||
|
{
|
||||||
role: "system",
|
role: "system",
|
||||||
content: "You are a helpful assistant. Only use the functions you have been provided with."
|
content:
|
||||||
}];
|
"You are a helpful assistant. Only use the functions you have been provided with.",
|
||||||
|
},
|
||||||
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Creating the agent function
|
## Creating the agent function
|
||||||
|
|
||||||
We are now ready to build the logic of our app, which lives in the
|
We are now ready to build the logic of our app, which lives in the
|
||||||
@ -127,14 +129,16 @@ We start by pushing the `userInput` to the messages array. This time, we set the
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
async function agent(userInput) {
|
async function agent(userInput) {
|
||||||
messages.push([{
|
messages.push([
|
||||||
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content: userInput,
|
content: userInput,
|
||||||
}]);
|
},
|
||||||
|
]);
|
||||||
const response = await openai.chat.completions.create({
|
const response = await openai.chat.completions.create({
|
||||||
model: "gpt-4",
|
model: "gpt-4",
|
||||||
messages: messages,
|
messages: messages,
|
||||||
functions: functionDefinitions
|
functions: functionDefinitions,
|
||||||
});
|
});
|
||||||
console.log(response);
|
console.log(response);
|
||||||
}
|
}
|
||||||
@ -153,7 +157,6 @@ properties:
|
|||||||
Here, we'll we use the `functionDefinitions` array we created
|
Here, we'll we use the `functionDefinitions` array we created
|
||||||
earlier.
|
earlier.
|
||||||
|
|
||||||
|
|
||||||
## Running our app with a simple input
|
## Running our app with a simple input
|
||||||
|
|
||||||
Let's try to run the `agent` with an input that requires a function call to give a suitable reply.
|
Let's try to run the `agent` with an input that requires a function call to give a suitable reply.
|
||||||
@ -206,7 +209,7 @@ both of our functions in an object called `availableFunctions`:
|
|||||||
```js
|
```js
|
||||||
const availableFunctions = {
|
const availableFunctions = {
|
||||||
getCurrentWeather,
|
getCurrentWeather,
|
||||||
getLocation
|
getLocation,
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -215,7 +218,6 @@ via bracket notation and the string we got back from OpenAI, like this:
|
|||||||
`availableFunctions["getLocation"]`.
|
`availableFunctions["getLocation"]`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
const { finish_reason, message } = response.choices[0];
|
const { finish_reason, message } = response.choices[0];
|
||||||
|
|
||||||
if (finish_reason === "function_call") {
|
if (finish_reason === "function_call") {
|
||||||
@ -232,7 +234,6 @@ We're also grabbing ahold of any arguments OpenAI wants us to pass into
|
|||||||
the function: `message.function_call.arguments`.
|
the function: `message.function_call.arguments`.
|
||||||
However, we won't need any arguments for this first function call.
|
However, we won't need any arguments for this first function call.
|
||||||
|
|
||||||
|
|
||||||
If we run the code again with the same input
|
If we run the code again with the same input
|
||||||
(`"Where am I located right now?"`), we'll see that `functionResponse`
|
(`"Where am I located right now?"`), we'll see that `functionResponse`
|
||||||
is an object filled with location about where the user is located right
|
is an object filled with location about where the user is located right
|
||||||
@ -249,8 +250,10 @@ specify the name of the function we called.
|
|||||||
messages.push({
|
messages.push({
|
||||||
role: "function",
|
role: "function",
|
||||||
name: functionName,
|
name: functionName,
|
||||||
content: `The result of the last function was this: ${JSON.stringify(functionResponse)}
|
content: `The result of the last function was this: ${JSON.stringify(
|
||||||
`
|
functionResponse
|
||||||
|
)}
|
||||||
|
`,
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -280,12 +283,11 @@ If we get `finish_reason: "stop"` back, then GPT has found a suitable
|
|||||||
answer, so we'll return the function and cancel the loop.
|
answer, so we'll return the function and cancel the loop.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
||||||
for (let i = 0; i < 5; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
const response = await openai.chat.completions.create({
|
const response = await openai.chat.completions.create({
|
||||||
model: "gpt-4",
|
model: "gpt-4",
|
||||||
messages: messages,
|
messages: messages,
|
||||||
functions: functionDefinitions
|
functions: functionDefinitions,
|
||||||
});
|
});
|
||||||
const { finish_reason, message } = response.choices[0];
|
const { finish_reason, message } = response.choices[0];
|
||||||
|
|
||||||
@ -300,8 +302,10 @@ for (let i = 0; i < 5; i++) {
|
|||||||
role: "function",
|
role: "function",
|
||||||
name: functionName,
|
name: functionName,
|
||||||
content: `
|
content: `
|
||||||
The result of the last function was this: ${JSON.stringify(functionResponse)}
|
The result of the last function was this: ${JSON.stringify(
|
||||||
`
|
functionResponse
|
||||||
|
)}
|
||||||
|
`,
|
||||||
});
|
});
|
||||||
} else if (finish_reason === "stop") {
|
} else if (finish_reason === "stop") {
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
@ -320,7 +324,9 @@ At this point, we are ready to try our app! I'll ask the agent to
|
|||||||
suggest some activities based on my location and the current weather.
|
suggest some activities based on my location and the current weather.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const response = await agent("Please suggest some activities based on my location and the current weather.");
|
const response = await agent(
|
||||||
|
"Please suggest some activities based on my location and the current weather."
|
||||||
|
);
|
||||||
console.log(response);
|
console.log(response);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1029,13 +1029,14 @@
|
|||||||
- tiktoken
|
- tiktoken
|
||||||
- completions
|
- completions
|
||||||
|
|
||||||
- title: How to build an agent with the Node.js SDK and function-calling
|
- title: How to build an agent with the Node.js SDK
|
||||||
path: examples/How_to_build_an_agent_with_the_node_sdk.md
|
path: examples/How_to_build_an_agent_with_the_node_sdk.md
|
||||||
date: 2023-10-05
|
date: 2023-10-05
|
||||||
authors:
|
authors:
|
||||||
- perborgen
|
- perborgen
|
||||||
tags:
|
tags:
|
||||||
- completions
|
- completions
|
||||||
|
- functions
|
||||||
|
|
||||||
- title: What makes documentation good
|
- title: What makes documentation good
|
||||||
path: articles/what_makes_documentation_good.md
|
path: articles/what_makes_documentation_good.md
|
||||||
|
Loading…
x
Reference in New Issue
Block a user