So im using this demo code to try to learn functions in gpt API, and data doesnt seem to exist on completion. Every tutorial I follow shows this. Is there anyway to get this to work?
// IMPORTANT! Set the runtime to edge
export const runtime = "edge";
export async function POST(req: Request) {
const { messages } = await req.json();
async function lookupTime(location: any) {
try {
const response = await fetch(
http://worldtimeapi.org/api/timezone/${location}
); // Make a GET request to the World Time API with the location parameter as the timezone.
const { datetime } = await response.json(); // Extract the datetime property from the data in the response.
const dateTime = moment(datetime, location).format("h:mmA"); // Use moment-timezone to create the Date object in the specified timezone.
console.log(The current time in ${location} is ${dateTime}.); // Log the formatted time to the console.
} catch (error) {
console.error(error); // Log any errors that occur to the console.
}
}
// Ask OpenAI for a streaming chat completion given the prompt
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
stream: true,
messages,
functions: [
{
name: "lookupTime",
description: "get the current time in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description:
"The location, e.g. Beijing, China. But it should be written in a timezone name like Asia/Shanghai",
},
},
required: ["location"], // specify that the location parameter is required
},
},
],
function_call: "auto",
});
// Extract the generated completion from the OpenAI API response.
const completionResponse = completion.data.choices[0].message;
console.log(completionResponse);