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

@raphtlw/hyperfunc

v0.0.7

Published

Streamlined Function Calling for LLMs

Downloads

1

Readme

hyperfunc

Node.js library for defining function calls as inline functions.

Installation

To use the HyperFunction library in your project, you can install it via pnpm:

pnpm add @raphtlw/hyperfunc

Don't forget to install zod for schema declarations.

pnpm add zod

Usage

z stands for zod

Import Zod at the top of every file for function calling parameter declarations.

import { z } from "zod"

Zod tells the LLM what the type of your function arguments have to be.

Defining a LLM function

You can define a new HyperFunction using the hyper function provided by the library. A HyperFunction consists of a description, input schema (defined using Zod), and a handler function.

const addTwo = hyper({
  description: "Add two numbers",
  args: {
    a: z.number().describe("First number"),
    b: z.number().describe("Second number"),
  },
  handler: async ({ a, b }) => {
    return a + b
  },
})

This addTwo object now has a bunch of metadata associated with it.

Defining a group of functions

export const functions = hyperStore({
  parse_date: hyper({
    description: "Parse a date from an ISO string",
    args: {
      date: z
        .string()
        .describe(
          "Date to parse, in ISO 8601 calendar date extended format",
        ),
    },
    handler({ date }) {
      dateObj = parseISO(date);
    },
  }),
  add_hours: hyper({
    description: "Add hours to date",
    args: {
      hours: z.number().describe("Hours to add"),
    },
    handler({ hours }) {
      dateObj = addHours(dateObj, hours);
    },
  }),
  add_days: hyper({
    description: "Add days to date",
    args: {
      days: z.number().describe("Days to add"),
    },
    handler({ days }) {
      dateObj = addDays(dateObj, days);
    },
  }),
  get_date: hyper({
    description: "Format processed date in localized format",
    args: {
      timezone: z
        .string()
        .describe("Timezone to convert date to before formatting."),
    },
    handler() {
      return intlFormat(dateObj, {
        dateStyle: "full",
        timeStyle: "full",
        timeZone: "Asia/Singapore",
      });
    },
  }),
});

Modifying existing function group

Imperatively define a new function on the existing group:

functions.set("get_place_details", hyper({
  description: "Get details of place using the Google Maps API.",
  args: {
    place_id: z
      .string()
      .describe(
        "A textual identifier that uniquely identifies a place, returned from a Place Search.",
      ),
  },
  async handler({ place_id }) {
    const client = new GoogleMapsClient();
    const response = await client.placeDetails({
      params: {
        place_id,
        key: Env.GOOGLE_MAPS_API_KEY,
      },
    });

    return response.data;
  },
}));

Converting HyperFunctions to Tools

You can convert HyperFunctions to OpenAI Chat Completion Tools using the asTools method. This is useful for integrating HyperFunctions with OpenAI's tools.

const completion = await openai.chat.completions.create({
  max_tokens: 4096,
  messages: [...history.get(), ...current.get()],
  model,
  tool_choice: "auto",
  tools: functions.asTools(),
});

Running HyperFunctions

You can run functions after getting LLM outputs as such:

if (chosen.message.tool_calls) {
  for (const toolCall of chosen.message.tool_calls) {
    const response = await functions.callTool(toolCall, context);
    console.log(response) // output of function call (type: unknown)
  }
}

Contributing

We welcome contributions to the HyperFunction library to improve its functionality, fix bugs, and add new features. Before contributing, please take a moment to review the guidelines below to ensure a smooth and collaborative development process.

Reporting Issues

If you encounter any bugs, issues, or have feature requests, please open an issue on the GitHub repository. When reporting an issue, please provide as much detail as possible, including:

  • Description of the issue
  • Steps to reproduce the issue
  • Expected behavior
  • Any relevant code snippets or error messages
  • Environment details (operating system, Node.js version, etc.)

Code Contribution

Fork and Clone
  1. Fork the HyperFunction repository to your GitHub account.
  2. Clone the forked repository to your local machine.
git clone https://github.com/your-username/hyperfunction.git
  1. Navigate to the cloned repository.
cd hyperfunction
Branching

Create a new branch for your contributions.

git checkout -b <username>/<feature-branch>

Conclusion

The HyperFunction library simplifies the creation, storage, and usage of hyperfunctions in your projects. Explore the provided functions and methods to harness the full power of HyperFunctions in your applications.