Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Created November 26, 2024 23:59
Show Gist options
  • Save kwhinnery/a6ca7f17f955c17e46f9ea48b1391e00 to your computer and use it in GitHub Desktop.
Save kwhinnery/a6ca7f17f955c17e46f9ea48b1391e00 to your computer and use it in GitHub Desktop.
Function calling in four acts. A simple but complete soup-to-nuts example of function calling with the OpenAI API.
import { OpenAI } from "openai";
const openai = new OpenAI();
// 1.) Define an array of functions the model could decide to call
const tools = [{
type: "function",
function: {
name: "generate_horoscope",
parameters: { type: "object", properties: { sign: { type: "string" } } }
}
}];
// 2.) Prompt the model, providing an array of available functions in the
// "tools" param (tools can theoretically have other items than just functions)
const messages = [
{ role: "user", content: "I'm a virgo, what's my horoscope for today?" },
];
const response1 = await openai.chat.completions.create({
model: "gpt-4o",
messages,
tools,
});
// 3.) If the model decides to call a function, parse function input from JSON
// and execute logic to implement the function however you need to.
const toolCall = response1.choices[0].message.tool_calls[0];
const args = JSON.parse(toolCall.function.arguments);
// Return a function call message to pass back in to the model
function generateHoroscope(fnArgs) {
return {
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify({
horoscope: `Oh, you're a ${fnArgs.sign}? Eventually you're gonna die.`
})
};
}
const functionResponse = generateHoroscope(args);
// 4.) Generate another response from the model, including both the assistant
// message from the first API response, and your new generated message from
// the function call. Now the model has all the data it needs to respond to the
// original prompt!
messages.push(response1.choices[0].message);
messages.push(functionResponse);
const response2 = await openai.chat.completions.create({
model: "gpt-4o",
messages,
tools,
});
console.log(response2.choices[0].message.content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment