npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openai-function-tree

v1.0.3

Published

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

Downloads

5

Readme

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.