1.6.6 • Published 6 months ago

llm-agents v1.6.6

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

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
1.6.4

6 months ago

1.4.6

6 months ago

1.6.3

6 months ago

1.5.4

6 months ago

1.4.5

6 months ago

1.6.2

6 months ago

1.5.3

6 months ago

1.6.1

6 months ago

1.5.2

6 months ago

1.6.0

6 months ago

1.5.1

6 months ago

1.4.9

6 months ago

1.6.6

6 months ago

1.4.11

6 months ago

1.4.8

6 months ago

1.6.5

6 months ago

1.4.10

6 months ago

1.4.7

6 months ago

1.4.13

6 months ago

1.4.12

6 months ago

1.4.15

6 months ago

1.4.14

6 months ago

1.4.17

6 months ago

1.4.16

6 months ago

1.4.4

6 months ago

1.4.3

6 months ago

1.4.2

6 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.0

7 months ago

1.2.5

7 months ago

1.2.4

7 months ago

1.2.3

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago