OpenAI Function Calling Tools
The OpenAI Function Calling Tools project is designed to simplify the process of building models that can call functions using the OpenAI API. This repository offers a collection of pre-built tools, making it easier for developers to integrate various functionalities into their OpenAI-powered applications.
Overview
The project provides a set of useful tools wrapped in a simple package available via npm. These tools are particularly helpful for developers who need to add function-calling capabilities to their applications using OpenAI's API.
πͺ Tools Provided
Here is a list of the tools included in the repository:
- ShowPoisOnMap: Displays points of interest on a map.
- ReverseGeocode: Converts geographic coordinates into a readable address.
- Clock: Tells the current time.
- Calculator: Performs basic arithmetic calculations.
- GoogleCustomSearch: Interfaces with Google's Custom Search API to answer questions about current events.
- BingCustomSearch: Provides a similar functionality using Bing's Custom Search API.
- SerperCustomSearch: Utilizes SerpAPI for current event queries.
- SerperImagesSearch: Searches for images using SerpAPI.
- fs Tools: Includes WriteFileTool and ReadFileTool for filesystem access.
- Web Browser: Opens websites given a URL.
- SQL Executor: Takes detailed SQL queries and returns results from a database.
- JavaScript Interpreter: Executes JavaScript code strings to perform operations.
Each of these tools is ready-to-use with the { Tool }
factory function. More examples can be found in the /tools
directory of the project.
π¦ Quick Installation
To start using the OpenAI Function Calling Tools, install the package via npm:
npm install openai-function-calling-tools
π Usage Examples
Example 1: Basic Arithmetic with JavaScriptInterpreter
This example demonstrates using the JavaScriptInterpreter tool to solve arithmetic problems like 0.1 + 0.2
.
import { Configuration, OpenAIApi } from "openai";
import { createCalculator } from "openai-function-calling-tools"
// Set up OpenAI API configuration
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Define and ask the question
const QUESTION = "What is 100*2?";
const messages = [{ role: "user", content: QUESTION }];
// Create and use the Calculator tool
const [calculator, calculatorSchema] = createCalculator();
const functions = { calculator };
// Function to get computed results from OpenAI
const getCompletion = async (messages) => {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
functions: [calculatorSchema],
temperature: 0,
});
return response;
};
// Process the response and call the function
let response = await getCompletion(messages);
if (response.data.choices[0].finish_reason === "function_call") {
const fnName = response.data.choices[0].message.function_call.name;
const args = response.data.choices[0].message.function_call.arguments;
const fn = functions[fnName];
const result = fn(JSON.parse(args));
messages.push({ role: "assistant", content: null, function_call: { name: fnName, arguments: args } });
messages.push({ role: "function", name: fnName, content: JSON.stringify({ result: result }) });
response = await getCompletion(messages);
console.log(response.data.choices[0].message.content);
}
Example 2: Using Google Custom Search
Performing a search using Google Custom Search, this requires an API key and search engine ID from Google.
const { Configuration, OpenAIApi } = require("openai");
const { createGoogleCustomSearch } = require("openai-function-calling-tools");
const main = async () => {
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY });
const openai = new OpenAIApi(configuration);
const QUESTION = "How many tesla model 3 sold in 2022?";
const messages = [{ role: "user", content: QUESTION }];
// Initialize Google Custom Search tool
const [googleCustomSearch, googleCustomSearchSchema] = createGoogleCustomSearch({
apiKey: process.env.GOOGLE_API_KEY,
googleCSEId: process.env.GOOGLE_CSE_ID
});
const functions = { googleCustomSearch };
const getCompletion = async (messages) => {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages,
functions: [googleCustomSearchSchema],
temperature: 0,
});
return response;
};
let response;
while (true) {
response = await getCompletion(messages);
if (response.data.choices[0].finish_reason === "stop") {
console.log(response.data.choices[0].message.content);
break;
} else if (response.data.choices[0].finish_reason === "function_call") {
const fnName = response.data.choices[0].message.function_call.name;
const args = response.data.choices[0].message.function_call.arguments;
const fn = functions[fnName];
const result = await fn(JSON.parse(args));
messages.push({ role: "assistant", content: "", function_call: { name: fnName, arguments: args }});
messages.push({ role: "function", name: fnName, content: JSON.stringify({result: result}) });
}
}
};
main();
Example 3: Extracting a Tree Structure from a Function Call
This example shows how to extract a tree structure schema from a function call.
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY });
const openai = new OpenAIApi(configuration);
const getCompletion = async (messages) => {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
messages: [{
role: "user",
content: `root
βββ folder1
β βββ file1.txt
β βββ file2.txt
βββ folder2
βββ file3.txt
βββ subfolder1
βββ file4.txt`
}],
functions: [{
"name": "buildTree",
"description": "build a tree structure",
"parameters": {
"type": "object",
"properties": {
"name": { "type": "string", "description": "The name of the node" },
"children": { "type": "array", "description": "The tree nodes", "items": { "$ref": "#" }},
"type": { "type": "string", "description": "The type of the node", "enum": ["file", "folder"] }
},
"required": ["name", "children", "type"]
}
}],
temperature: 0,
});
return response;
};
let response = await getCompletion();
if (response.data.choices[0].finish_reason === "function_call") {
const args = response.data.choices[0].message.function_call.arguments;
console.log(args);
}
π» Supported Environments
The tools are designed to be compatible with several environments, including:
- Node.js (version 16 or higher)
- Cloudflare Workers
- Vercel / Next.js (Backend, Serverless, and Edge functions)
- Supabase Edge Functions
- Web Browsers (in development)
π‘οΈ Production Safety
The project has been assessed for security to ensure safety in production environments.
π Inspirational Source
This project takes inspiration from LangChainAI, reflecting its innovative approach to integrating multiple functionalities seamlessly.
For developers looking to enhance their applications with dynamic and context-aware tools, the OpenAI Function Calling Tools repository provides a robust and versatile framework.