GPT-4’s Secret Weapon: Function Calling for Custom AI Applications

Kian M.
3 min readJun 14, 2023

GPT-4, the latest and most powerful language model from OpenAI, has a new trick up its sleeve: function calling. This feature allows developers to access GPT-4 via code and describe functions to the model, which can then output arguments to call those functions. This opens up a whole new level of customization and integration for AI applications.

Function calling is enabled by the new chat completions API endpoint, which takes a list of messages as input and returns a model-generated message as output. The messages can be instructions or examples of how to complete a task using GPT-4. The model can then decide whether to output a text response or a JSON object with the function name and arguments.

For example, a developer can use the chat completions API to ask GPT-4 to generate an email subject line for a newsletter, given some keywords and a tone. The model can then output a JSON object with the function name “send_email” and the arguments “subject”, “to”, and “from”, which the developer can use to send the email.

# Import openai package
import openai
# Set your API key
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Define your messages
messages = [
{"role": "system", "content": "You are an email assistant."},
{"role": "user", "content": "Generate an email subject line…

--

--