This is a demonstration to show how to integrate function calls with the OpenAI API (ChatGPT) and NodeJS using RapidApi.com, WorldTimeAPI.org and SendGrid.
- Clone this repository
git clone https://github.com/normandmickey/node-chatgpt-api-with-function-calls.git
. - Change to the node-chatgpt-with-functon-calls directory.
- Install packages
npm install
. - Copy env.sample to .env and update your OPENAI_API_KEY.
- Goto https://rapidapi.com/hub, subscribe to the WeatherAPI.com API (https://rapidapi.com/weatherapi/api/weatherapi-com).
- Add your X-RapidAPI-Key to your .env file.
- Register and obtain an API key from https://sendgrid.com
- Add your SendGrid API key to the .env file and update SENDER_EMAIL.
- Run the following command
node node-chatgpt-js
The script will return normal responses to your prompts using OpenAI's Chat Completion endpoint but also has access to three API's.
-
WeatherAPI.com - If you ask a weather related question about a specific location, ChatGPT will create the function call to lookup the current temperature using the WeatherAPI.com's API. This time the response is generated by the API call and then passed back through ChatGPT for summarization. If you ask "Whats the weather forecast for Las Vegas Nevada?" ChatGPT should return something like "The current temp is 71.1."
-
WorldTimeAPI.org - When you ask "What time is it in Rio De Janeiro?", you should get the current time in Rio. (from worldtimeapi.org).
-
SendGrid - You can also ask ChatGPT to send an email, for example "Send an email to me@mydomain.com asking when will the report on global climate change be ready?"
As you can see in the sample code the OpenAI API is used to generate API calls if necessary. Howerver, it's still up to you to run them, parse the responses and either return the response directly to the use or use it's data in another function. For example, instead of immediately returning data from the API to the user, you can also append the API response to a second call to ChatGPT giving it context to answer the original question.