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

llmaochain

v1.0.3

Published

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

Downloads

6

Readme

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

Install

npm install llmaochain

Example

Start by defining your links. Each link is either a ModelLink or a FunctionLink. ModelLinks are configuration objects that define how to call a model. FunctionLinks are just regular functions.

Templates are a way to build prompts for your model. You can use the results of previous links in your templates with the {{variable}} notation.

// 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.9,
  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: "auto",
};

Then, execute the chain. The chain will execute each link in order, passing the results of previous links to the next link. The chain will return the results of the last link.

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

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,
    }
  );