Skip to content

RafalMCichon/openai-function-calling-nodejs-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI ChatGPT Function Calling Node.js Example with the Weather API

This Node.js code example showcases the powerful new feature of OpenAI - Function Calling. Function Calling enables developers to interact more reliably with external tools and APIs, including weather information APIs. By describing functions, the model can intelligently output a JSON object containing the necessary arguments for the function call.

Prerequisites

Before running this code, make sure you have the following prerequisites:

Setup

  1. Clone the repository and navigate to the project directory.

  2. Install dependencies by running the following command:

   npm install
  1. Create a .env file in the project root directory and add the following environment variables:
OPENAI_API_KEY=your_openai_api_key
WEATHER_API_KEY=your_weather_api_key

Replace your_openai_api_key with your actual OpenAI API key and your_weather_api_key with your actual weather API key.

Usage

To use the code, you can run the following command:

npm start

This will execute the run_conversation function and retrieve weather information using OpenAI GPT-3.5-turbo model and the weather API.

The code starts by calling the OpenAI API with a user query and a set of defined functions. If the model decides to call a function, it invokes the get_current_weather function, which makes a request to the weather API to retrieve weather data for the specified location. The weather information is then returned as a JSON string.

The code uses the response from the OpenAI API to extract the weather information and prints it to the console.

Customization

If you want to customize the code for your specific use case, you can modify the following parts:

Weather API: In the get_current_weather function, you can replace the API URL and response parsing logic with your actual weather API implementation.

License

This code is released under the MIT License.

About

OpenAI Function Calling Node.js Example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published