New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

openai-function-tree

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openai-function-tree

Easily categorizing OpenAI tools & function calling to save token consumption.

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
6
Maintainers
1
Weekly downloads
 
Created
Source

openai-function-tree

npm: https://www.npmjs.com/package/openai-function-tree
GitHub: https://github.com/yugoka/openai-function-tree

Overview

Function Callingのトークン使用量を大幅節約!😎
Significantly save on token usage by categorizing Function Calling! 😎

openai-function-tree is a library designed for OpenAI Assistant Tools, specifically for Function Calling. It categorizes tools in a tree structure, enabling recursive searching. This approach helps save token length in tool descriptions and other related texts. Initially created for personal use, it is now available for everyone. Please note, this is a Work in Progress.

Installation

To install the package, run the following command:

npm install openai-function-tree

Usage

Defining a Function Tree Category

Start by defining your FunctionTreeCategory. Here's an example:

import { FunctionTreeCategory } from "../src/types/functionTreeCategory";

export const exampleFunctionTree: FunctionTreeCategory = {
  type: "category",
  name: "root",
  description: "root",
  modelName: "gpt-4-1106-preview",
  prompt: "Please choose a tool",
  children: [
    {
      type: "category",
      name: "control",
      description: "Operate electronic devices and web services",
      children: [
        {
          type: "category",
          name: "control-iot-device",
          description: "Operate electronic device",
          children: [
            {
              type: "tool",
              tool: {
                // Based on OpenAI assistant tools
                // Include tool descriptions, parameters, etc.
                // See https://platform.openai.com/docs/assistants/tools
              },
              // This args is based on the parameters defined above.
              function: (args) =>
                `First floor air conditioner turned on. deviceName: ${args.device_name}`,
            },
          ],
        },
        // Further nested categories and tools...
      ],
    },
    // Additional categories and tools...
  ],
};

Initializing and Running FunctionTreeAgent

Initialize and run FunctionTreeAgent as follows:

import { FunctionTreeAgent } from 'openai-function-tree';

const functionTreeAgent = new FunctionTreeAgent({
  apiKey: process.env.OPENAI_API_KEY,
  functionTree: exampleFunctionTree,
  options: { verbose: true }
});

const result = await functionTreeAgent.run([
  {
    role: "user",
    content: "Turn on the air conditioner on the first floor.",
  },
]);

console.log(result);
// result example: '"control" => "control-iot-device" => "1st-floor-cooler-control" => "First floor air conditioner turned on"'

Additionally, ResponderTreeAgent can be used as a wrapper that recursively executes FunctionTreeAgent until the user's command is fully accomplished.

import { ResponderAgent } from 'openai-function-tree';

const agent = new ResponderAgent({
  apiKey: process.env.OPENAI_API_KEY,
  functionTree: exampleFunctionTree,
  functionTreeAgentOptions: {
    verbose: true,
  },
  options: {
    maxSteps: 3,
    enableFallbackMessage: true,
  },
});

// Use the agent as needed
const result = await agent.run([
  {
    role: "user",
    content: `Check the temperature in Fujisawa City and set the air conditioner to the same temperature.`,
  },
]);

Contributing

Contributions are welcome! Please feel free to submit pull requests, report issues, or suggest enhancements.

License

This project is licensed under MIT License, see the LICENSE file for details.

FAQs

Package last updated on 20 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts