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

ai-fun-chain

v1.0.7

Published

Chain requests to OpenAI models and regular functions together super easily. Function calling supported!

Downloads

122

Readme

ai-fun-chain

Break complex tasks up into small pieces that are easier for LLMs to handle into a chain. Link them with non-ai functions to augment, parse, or validate responses. Function calling supported.

  • Aggregates token usage throughout chain
  • Automatic retries
  • Fully configurable, at both chain and link level

Currently only works with Open AI. May support other models in the future.

Install

Set OPENAI_API_KEY in your environment variables.

npm install ai-fun-chain

Usage

import { FunctionLink, ModelLink, executeChain } from "ai-fun-chain";

ai-fun-chain works by defining a chain of links. Each link is either a ModelLink or a FunctionLink. ModelLinks are configuration objects that define the request made to the LLM. FunctionLinks are just regular functions. ai-fun-chain makes it easy to pass results from previous links into prompts for later links.

  const result = await executeChain([
    getRandomletter, // Function Link
    getRandomName, // Model Link
    getGender, // Model Link (function calling)
    addKunOrChanToName, // Function Link
  ]);

Defining a Model Link

ModelLink is an extension of Open AI's CreateChatCompletionRequest, so you can pass in any of the properties defined in the Open AI docs.

You can pass in a normal messages property, or you can use templates. templates is an array of MessageTemplate. A MessageTemplate is similar to the normal message you would pass to Open AI, but instead of content it has contentBlocks.

contentBlocks are useful for programtically including blocks of text in the prompt. Each content block is composed of a template and an include property. If include is true, the template will be included in the prompt. If include is false, the template will not be included in the prompt. This is useful for conditionally including text in the prompt based on the result of a previous link.

The modelLink also has extra parameters to control cleanup functions. autoNewLineContent defaults to true. If set to false, it will use a space. You do not need to add leading/trailing spaces between contentBlocks. removeDoubleSpaces defaults to true. If set to true, it will remove double spaces.


const nameShouldBeFourLetters = true;

const getRandomName: ModelLink = {
name: "getRandomName",
temparature: 0,
autoNewLineContent: false, // defaults to true. If set to false, it will use a space. You do not need to add leading/trailing spaces between contentBlocks.
removeDoubleSpaces: true, // defaults to true.
contentBlocks: [
{
template: `Come up with one first name that start with the letter {{getRandomletter}}`,
include: true,
},
{
template: `The name should be four letters long`,
include: nameShouldBeFourLetters,
},
],
};

Full Example


    // Define links
    const getRandomletter: FunctionLink = () => {
      const letters = "abcdefghijklmnopqrstuvwxyz";
      const randomLetter = letters[Math.floor(Math.random() * letters.length)];
      return randomLetter;
    };

    const getRandomName: ModelLink = {
      name: "getRandomName",
      model: "gpt-3.5-turbo-0613",
      temperature: 0.9,
      template: [
        {
          content: `Come up with one first name that start with the letter {{getRandomletter}}`,
          include: true,
        },
      ],
    };

    const getGender: ModelLink = {
      name: "getGender",
      retries: 2,
      model: "gpt-3.5-turbo-0613",
      temperature: 0,
      template: [
        {
          content: `What is the gender of {{getRandomName}}`,
          include: true,
        },
      ],
      functions: [
        {
          name: "getGender",
          description: "Gets the gender of a name",
          parameters: {
            type: "object",
            properties: {
              gender: {
                type: "string",
                description: "Either boy or girl",
                enum: ["boy", "girl"],
              },
              name: {
                type: "string",
                description: "The name",
              },
            },
          },
        },
      ],
      function_call: {
        name: "getGender",
      },
    };

    const addKunOrChanToName = ({
      gender,
      name,
    }: {
      gender: "boy" | "girl";
      name: string;
    }) => {
      if (gender === "boy") {
        return `${name}-kun`;
      } else {
        return `${name}-chan`;
      }
    };

    // Execute the chain
    const example = async () => {
      const result = await executeChain([
        getRandomletter, // Function Link
        getRandomName, // Model Link
        getGender, // Model Link (function calling)
        addKunOrChanToName, // Function Link
      ]);
      console.log(result);
      return result;
    };
    example()

Output

executeChain outputs the following object, so you can easily access the final result and aggregated usage across each link, as well as the results of each link.


{
finalResult: 'Ivan-chan',
totalTokens: 108,
totalPromptTokens: 90,
totalCompletionTokens: 18,
chatCompletionResponses: {
getRandomName: {
id: 'chatcmpl-7TNbk54yIUba6wvtPoT0z5HTNSbMC',
object: 'chat.completion',
created: 1687236616,
model: 'gpt-3.5-turbo-0613',
choices: [Array],
usage: [Object]
},
getGender: {
id: 'chatcmpl-7TNbkKZ9n5wmprfpXQL8YNW35CDv0',
object: 'chat.completion',
created: 1687236616,
model: 'gpt-3.5-turbo-0613',
choices: [Array],
usage: [Object]
}
},
linkResults: {
getRandomletter: 'i',
getRandomName: 'Ivan',
getGender: { name: 'Ivan' },
addKunOrChanToName: 'Ivan-chan'
}
}

Pass results from previous links to a template in a later link

For prompt templates, you can just refer to the name of the previous link in the template. For example, if you want to use the result of the getRandomName link in the template for the getGender link, you can just use the name of the link in the template.


{{getRandomName}}

If one of your model links outputs JSON through function calling, you can reference a specific value with dot notation


{{getGender.gender}}

If you are passing the result of a function call directly into a Function Link, you simply define the arguments in that function to match the expected result of the previous link


const addKunOrChanToName = ({ gender, name }: {
gender: "boy" | "girl";
name: string;
}) => {
...
}

Retries

You can pass retries in at each link, or you can pass in a global retry quantity in the executeChain config.


const result = await executeChain(
[
getRandomName, // ModelLink
determineKunOrChan, // FunctionLink
addKunOrChanToName, // FunctionLink
writeStoryAboutName, // ModelLink
],
{
retries: 2,
}
);