#ChatGPT Functions

1 messages · Page 1 of 1 (latest)

oak rapids
#

In this code how do I define the get_weather_function so that it is actually called and returns the static response?

export async function POST({ request }) {
const { messages } = await request.json();
console.log(messages, 'request')

const tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
];

const initialMessage = {
role: "system",
content: " Your name is Tina, please keep your responses succint when possible."
};

const updatedMessages = [initialMessage, ...messages];
// Ask OpenAI for a streaming chat completion given the prompt
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo', //gpt-4-1106-preview gpt-3.5-turbo gpt-4-0125-preview // does not work gpt-4-1106-vision-preview
stream: true,
messages:updatedMessages,
tools: tools,
tool_choice: "auto",

});

// Convert the response into a friendly text-stream
const stream = OpenAIStream(response);
// Respond with the stream
return new StreamingTextResponse(stream);
}

craggy joltBOT
#

Hi @oak rapids. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The get_current_weather function is defined as a tool in your code, but the actual implementation of the function is not provided in the code snippet you've shared.

To define the get_current_weather function, you need to create a function that takes in the parameters defined in the tool (in this case, location and unit) and returns the current weather for the given location. The function should return a response in the format expected by the OpenAI API.

Here's a simple example of how you might define the get_current_weather function:

async function get_current_weather(location, unit) {
  // Here you would typically call a weather API to get the current weather
  // For simplicity, let's return a static response
  return {
    "weather": "sunny",
    "temperature": 21,
    "unit": unit
  };
}

Please note that in a real-world application, you would likely use a weather API to get the current weather for the given location. The function above is a simplified example and returns a static response.

Once you've defined the get_current_weather function, you need to map the function name received from the OpenAI API to the actual function. This is typically done using a dictionary or an object in JavaScript. Here's an example:

const available_functions = {
  "get_current_weather": get_current_weather,
};

Then, in the part of your code where you handle the tool calls from the OpenAI API, you would look up the function in the available_functions object and call it with the provided arguments.

Please note that the code snippets provided above are based on the general principles of defining and calling functions in JavaScript and the information provided in the knowledge sources. The actual implementation may vary depending on the specifics of your application and the weather API you are using.

#

@oak rapids kapa.ai is still learning and improving, please let us know how it did by reacting below