Package Exports
- ai-function-chain
- ai-function-chain/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ai-function-chain) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Demo
YouTube link: FunctionChain: OpenAI Function Calling Simplified in Node.js
Installation
- Install the package using npm or yarn:
npm install ai-function-chain
# or
yarn add ai-function-chain
# or
pnpm install ai-function-chain- Create a file named
.envat the root of your project. Obtain your OpenAI API Key from here, and add it to the.envfile:
OPENAI_API_KEY=your_openai_api_keySetup
To setup FunctionChain:
- Create an
index.jsfile in the root of your project. - Import the
FunctionChainclass fromai-function-chainand instantiate it. - Call the
callmethod with a message. Optionally, you can specify a set of functions to execute.
import { FunctionChain } from "ai-function-chain";
const functionChain = new FunctionChain();
async function main() {
const res1 = await functionChain.call("Get me the latest price of Bitcoin");
const res2 = await functionChain.call("Open the calculator on my computer");
const res3 = await functionChain.call("Get me the latest price of Ethereum", {
functionArray: ["latestPrices"] // Optionally specify which functions to use
});
console.log(res1, res2, res3);
}
main();Customization
You can customize FunctionChain instance by specifying different OpenAI model and a custom directory for your function modules:
const initOptions = {
openaiOptions: {
model: "gpt-3.5-turbo-0613", // specify a different model if needed
},
functionsDirectory: "./myFunctions", // specify a custom directory if you have one
};
const functionChain = new FunctionChain(initOptions);Adding Custom Functions
- Create a new JavaScript file for your function in the
functionsDirectoryspecified while creating theFunctionChaininstance. - Follow the following pattern to define your function:
import { exec } from 'child_process';
export const execute = (options) => {
const { appName } = options;
return new Promise((resolve, reject) => {
exec(`open -a "${appName}"`, (error, stdout, stderr) => {
if (error) {
console.warn(error);
reject(`Error opening ${appName}: ${error.message}`);
}
resolve(`${appName} opened successfully.`);
});
});
}
export const details = {
name: "openApp",
description: "Opens a specified application on your computer",
parameters: {
type: "object",
properties: {
appName: {
type: "string",
description: "The name of the application to open"
},
},
required: ["appName"],
},
example: "Open the 'Calculator' application"
};Running Your Project
After setting up index.js and adding your functions, run your project using:
npm run dev