Pretty new to ChatGPT function calling.
I'm trying to create a cloud function which takes in a message (user's activity idea), location (where the activity will be), and day (when the activity will take place).
This info would be passed to ChatGPT which would do a function call to get the weather information and then respond about the activity based on the activity and weather info for the location on that day.
This is what I've tried so far:
const OPENAI_API_KEY = 'OPENAI_API_KEY';
const WEATHER_API_KEY = 'WEATHER_API_KEY';
const axios = require('axios');
const OpenAI = require('openai');
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
});
exports.activityRecommender = functions.https.onRequest(async (req, res) => {
try {
// Get the message, location, and day from the body header
const { message, location, day } = req.body;
// Get the weather of a given location on a particular day
async function getWeatherInfo(location, day) {
const weatherResponse = await axios.get(
`https://api.weatherapi.com/v1/forecast.json?key=${WEATHER_API_KEY}&q=${location}&days=10&aqi=no&alerts=no`
// day must be in YYYY-MM-DD format
);
// Find the forecast for the requested day
const forecast = weatherResponse.data.forecast.forecastday.find(forecastday =>
forecastday.date === day
);
if (!forecast) {
return res.status(404).send('Forecast not found or invalid day.');
}
// Extract relevant weather details from the forecast
const temperature = forecast.day.avgtemp_c;
const humidity = forecast.day.avghumidity;
const weatherDescription = forecast.day.condition.text;
return "The weather in " + location + " on " + day + " is expected to be " + weatherDescription.toLowerCase() + " with an average temperature of " + temperature + "°C and humidity of " + humidity + "%."
}
// Define ChatGPT Function
async function callChatGPTWithFunctions(appendString){
let messages = [{
role: "system",
content: "Perform function requests for the user",
},
{
role: "user",
content: "Hello, I am a user, I would like to give activity idea suggestions based on the weather.",
}];
// Step 1: Call ChatGPT with the function name
let chat = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
functions: [{
name: "getWeatherInfo",
description: "Gets the weather information of a location on a day",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The location to get the weather"
},
day: {
type: "integer",
description: "The day to check the weather"
},
},
require: ["location", "day"],
}
},
// more function defenitions here
],
function_call: "auto",
})
let wantsToUseFunction = chat.data.choices[0].finish_reason == "function_call"
let content = message
// Step 2: Check if ChatGPT wants to use a function
if (wantsToUseFunction) {
// Step 3: Use ChatGPT arguments to call your function
if (chat.data.choices[0].message.function_call.name == "getWeatherInfo"){
content = getWeatherInfo(location, day)
messages.push(chat.data.choices[0].message)
messages.push({
role: "function",
name: "getWeatherInfo",
content,
})
}
}
// Step 4: Call ChatGPT again with the function response
let finalResponse = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
});
//console.log(finalResponse.data.choices[0])
return finalResponse
}
//callChatGPTWithFunctions(message, location, day)
res.status(200).send(callChatGPTWithFunctions(message, location, day));
}
catch (error) {
res.status(500).json({ error: error.message });
}
});
When I try sending a POST request as: https:cloudfunctions.net/activityRecommender?message="walking on the beach"&location=london&day=2023-10-25
the response received is {}
Before uploading, I checked to make sure there were no errors and used --debug
flag for any errors. I did try renaming the variable to see if it would return the correct response. Using the console it returns the correct response, but not when called as a POST request. I've also asked chatgpt but was changing the code to be a non cloud function code.
Not really sure what to do. I know it has to be something with the correct response not being sent, but not sure why as the variable is defined.