#OpenAI functions

1 messages · Page 1 of 1 (latest)

polar mulch
#

How do I use OpenAI functions when calling the api?

sly depotBOT
#

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

#

OpenAI functions allow your application to take actions based on user inputs. However, it's important to note that OpenAI doesn't execute any code for you. It just tells your app which functions it should use in a given scenario, and then leaves it up to your app to invoke them.

For example, in an API call, you can describe functions and have the model intelligently choose to output a JSON object containing arguments to call one or many functions. The Chat Completions API does not call the function; instead, the model generates JSON that you can use to call the function in your code. The latest models (gpt-3.5-turbo-1106 and gpt-4-1106-preview) have been trained to both detect when a function should be called (depending on the input) and to respond with JSON that adheres to the function signature more closely than previous models (source (https://platform.openai.com/docs/guides/function-calling)).

Here's an example of how you might use the OpenAI API with functions in a Node.js application:

import OpenAI from "openai";
 
const openai = new OpenAI({
 apiKey: process.env.OPENAI_API_KEY,
 dangerouslyAllowBrowser: true,
});

In this example, you're importing the OpenAI SDK and authenticating with your API key. The dangerouslyAllowBrowser: true option is used to confirm that you understand the risks involved with client-side API requests. In a production app, you should move these requests over to a Node server (source (https://cookbook.openai.com/examples/how_to_build_an_agent_with_the_node_sdk)).