/node-chatgpt-api-with-function-calls

This is a demonstration of how to integrate function calls with the OpenAI API (ChatGPT), NodeJS, RapidApi.com and WorldTimeAPI.org.

Primary LanguageJavaScript

node-chatgpt-api-with-function-calls

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.

  1. Clone this repository git clone https://github.com/normandmickey/node-chatgpt-api-with-function-calls.git.
  2. Change to the node-chatgpt-with-functon-calls directory.
  3. Install packages npm install.
  4. Copy env.sample to .env and update your OPENAI_API_KEY.
  5. Goto https://rapidapi.com/hub, subscribe to the WeatherAPI.com API (https://rapidapi.com/weatherapi/api/weatherapi-com).
  6. Add your X-RapidAPI-Key to your .env file.
  7. Register and obtain an API key from https://sendgrid.com
  8. Add your SendGrid API key to the .env file and update SENDER_EMAIL.
  9. 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.

  1. 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."

  2. WorldTimeAPI.org - When you ask "What time is it in Rio De Janeiro?", you should get the current time in Rio. (from worldtimeapi.org).

  3. 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.