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

llm-agents

v1.6.6

Published

LLM Agent framework for Node.js

Downloads

9,885

Readme

LLM Agents

A library to create LLM Agents with Node.js

Usage

$ bun install llm-agents

# or the legacy way

$ npm install llm-agents

Actions

Extends the LLMAction class to define an action with:

  • name
  • usage
  • parameters
import { execSync } from 'child_process';

import { Action, ActionFeedback } from 'llm-agents';

type ExecuteShellCommandActionParametersNames = 'command';

export class ExecuteShellCommandAction extends LLMAction<ExecuteShellCommandActionParametersNames> {
  public name = 'executeShellCommand';
  public usage = 'execute a shell command';
  public parameters = [
    {
      name: 'command' as const,
      usage: 'command to execute',
    },
  ];

  protected async executeAction(
    parameters: Record<ExecuteShellCommandActionParametersNames, string>
  ): Promise<ActionFeedback> {
    const { command } = parameters;

    try {
      const result = execSync(command);

      return {
        message: `$ ${command}\n${result.toString()}`,
        type: 'success',
      };
    } catch (error) {
      return {
        message: `$ ${command}\n${error.message}`,
        type: 'error',
      };
    }
  }
}

Examples:

Agents

Extends the LLMAgent class to define an Agent with:

  • template content
  • template formating
  • actions (optionnal)
import { PromptTemplate } from 'langchain/prompts';

import { LLMAction, FileCache, LLMAgent } from 'llm-agents';

import {
  CreateDirectoryAction,
  CopyFileAction,
  ListFilesAction,
} from './actions';

export class BackupAgent extends LLMAgent {
  private source: string;
  private destination: string;

  protected template = new PromptTemplate({
    template: `You are a backup agent capable of moving files from one place to another.

You task is to backup the files containing the typescript code of a project.
You need to copy the files containing the code into a backup directory.
You can only move one file after another.

You can use the following actions:
# BEGIN ACTIONS DEFINITION
{actions}
# END ACTIONS DEFINITION

The last action result was:
# BEGIN LAST ACTION RESULT
{feedback}
# END LAST ACTION RESULT

Ensure you are copying every files in every directories from the source.
The source directory is {source} and the destination directory is {destination}.
Skip node_modules directories.

Start by a sentence summarizing the current state of your task according to the last action result.
Then, answer with the actions you want to execute.
`,
    inputVariables: ['source', 'destination', 'actions', 'feedback'],
  });

  constructor({
    source,
    destination,
  }: {
    source: string;
    destination: string;
  }) {
    super({
      actions: [
        new ListFilesAction(),
        new CopyFileAction(),
        new CreateDirectoryAction(),
      ],
      cacheEngine: new FileCache(),
    });

    this.source = source;
    this.destination = destination;
  }

  protected async formatPrompt({
    actions,
    feedbackSteps,
  }: {
    actions: string;
    feedbackSteps: string[];
  }) {
    return this.template.format({
      source: this.source,
      destination: this.destination,
      actions,
      feedback: feedbackSteps.join('\n\n'),
    });
  }
}

Examples:

Cache

During development phase, it's advised to use the filesystem cache.

It will save both prompt and answer for each step.

The cache key is a hash of the prompt so if the prompt does not change, the cached answer will be used directly.

You can also debug your prompts and answers in the .cache/ folder. In development mode, they will be prefixed by the step number (e.g. 0-92957a2b27-answer.txt)

Tests

Unit tests can be run with Bun: bun test

See tests/unit

Integration tests consist in a benchmark of LLMs agents:

  • BackupAgent: bun tests/prompt-engineering/backup-agent/run-backup-agent-test.ts