OpenAI's advanced models, such as gpt-3.5-turbo-0613
and gpt-4-0613
, feature "function calling." This empowers developers to use functions within the model's responses, retrieving structured data and performing various tasks. This article examines ChatGPT functions, their uses, and offers examples of their potential applications.
ChatGPT Functions: An Overview
Functions embedded in the system prompt guide the model to produce a JSON object with function arguments that integrate with your code. The Chat Completions API doesn't execute these functions, but generates the JSON to enable function calls. This control and flexibility are all yours.
Recent ChatGPT models are adept at discerning when to call functions from input and respond with suitable JSON matching the function's signature.
ChatGPT Functions Use Cases
Consider these use cases:
Advanced Chatbots: Energize chatbots with functions that interact with external APIs, enabling them to answer questions and provide information from various services. Functions such as
send_email(to: string, body: string)
orget_current_weather(location: string, unit: 'celsius' | 'fahrenheit')
can send emails or fetch weather information.From Natural Language to API Calls: Transform user queries into powerful API calls. Functions can convert questions like "Who are my top customers?" into API calls such as
get_customers(min_revenue: int, created_before: string, limit: int)
, easily retrieving customer data.Extracting Structured Data: Extract structured data from unstructured text by defining functions such as
extract_data(name: string, birthday: string)
orsql_query(query: string)
. This turns unstructured data into valuable insights.
For instance, in my Playwright Job Board, I use functions to extract company names from job descriptions and pull data from Crunchbase.
These examples demonstrate the potential of ChatGPT functions to integrate AI conversational agents with external services, automate workflows, and mine unstructured data.
Harnessing ChatGPT Functions
Let's look at how to use ChatGPT functions with the completions
JavaScript package:
1. Install the completions
package via npm:
npm install completions
2. Import the required modules and create a chat instance:
import { createChat, CancelledCompletionError } from "completions";
const chat = createChat({
apiKey: OPENAI_API_KEY,
model: "gpt-3.5-turbo-0613",
});
3. Define your functions within the createChat
configuration:
const chat = createChat({
apiKey: OPENAI_API_KEY,
model: "gpt-3.5-turbo-0613",
functions: [
{
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"],
},
function: async ({ location }) => {
return {
location: "Albuquerque",
temperature: "72",
unit: "fahrenheit",
forecast: ["sunny", "windy"],
};
},
},
],
});
4. Send a message to the chat:
const response = await chat.sendMessage("What is the weather in Albuquerque?");
console.log(response.content);
// Output: "The weather in Albuquerque is 72 degrees Fahrenheit, sunny with a light breeze."
You can also specifically request a function call:
const response = await chat.sendMessage("What is the weather in Albuquerque?", {
functionCall: {
name: "get_current_weather"
},
});
console.log(response.content);
// Output: "The weather in Albuquerque is 72 degrees fahrenheit, sunny with a light breeze."
This is helpful if you have functions with similar descriptions. Otherwise, ChatGPT effectively selects the right function to call automatically.
Conclusion
ChatGPT functions let you create dynamic conversational AI experiences by integrating external services, automating workflows, and extracting valuable insights. The potential with ChatGPT is limitless. Embrace the examples provided, explore the world of ChatGPT functions, and start developing intelligent, interactive, and extraordinary applications.