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

@sammyl/ai-agents

v1.1.1

Published

Extends the capabilities of openai by creating agents that work together.

Downloads

68

Readme

@sammyl/ai-agents

AI Agents is a TypeScript library that extends the capabilities of OpenAI’s Node.js API by creating and orchestrating multiple agents (with their own "tools") working together to achieve a given goal.

Features

  1. Intuitive Agent Builder: Easily construct agents with custom names, descriptions, and capabilities.
  2. Predefined & Customizable Tools: Leverage built-in tools or build your own to provide agents with specialized capabilities.
  3. Agent Orchestration: Coordinate multiple agents and their tools to accomplish complex tasks.
  4. Logging & Observability: Add optional logging to track agent messages and orchestrator updates.

Installation

Prerequisites:

  • Node.js: Ensure you have Node.js v22.2.0 or higher installed.

Install the Package:

npm install @sammyl/ai-agents

Quick Start Example

Below is a minimal example showing how to create two agents—a writer and a historian—and orchestrate them to solve a prompt.

import {
	AgentBuilderFactory,
	CompletionResult,
	IOrchestrator,
	ORCHESTRATOR_COMPLETED_EVENT,
	OrchestratorBuilder,
} from '@sammyl/ai-agents';
import OpenAI from 'openai';

// Initialize OpenAI with your API key
const openai = new OpenAI({
	apiKey: process.env.OPENAI_API_KEY,
});

// Create agents
const agentFactory = new AgentBuilderFactory(openai);
const writer = agentFactory
	.getBuilder()
	.setName('Writer Agent')
	.setDescription('A clever writer that can write engaging stories.')
	.build();

const historian = agentFactory
	.getBuilder()
	.setName('Historian Agent')
	.setDescription('A historian with a vast knowledge base.')
	.build();

// Create orchestrator and add agents
const orchestrator: IOrchestrator = new OrchestratorBuilder()
	.setOpenAIClient(openai)
	.addAgent(writer)
	.addAgent(historian)
	.build();

// Listen for completion
orchestrator.on(ORCHESTRATOR_COMPLETED_EVENT, (result: CompletionResult) => {
	console.log(result.summary);
});

// Run a task
orchestrator.run(
	'Write a short story about why and how the Roman Empire fell.',
);

Using Tools

Tools provide agents with enhanced capabilities. For example, you can enable file reading/writing, web searching, or any other custom function by integrating tools.

Prebuilt Tools

FileAccessTools:
Allows agents to read and write files in a designated outputs directory. Useful for scenarios like saving generated content for later use or record-keeping.

Adding a Prebuilt Tool to an Agent

import { AgentBuilder, FileAccessTools } from '@sammyl/ai-agents';
import OpenAI from 'openai';

const openai = new OpenAI();
const fileAccessTools = new FileAccessTools(); // Interacts with `outputs` directory

const writerAgent = new AgentBuilder()
	.addOpenAIClient(openai)
	.addTools(fileAccessTools) // Add file access capabilities
	.setName('Blog writer')
	.setDescription('Writes a blog on how to utilize AI and saves it to a file.')
	.build();

Creating Your Own Tool

You can define custom tools by providing a function specification and a corresponding request handler. For details, see the OpenAI Function Calling documentation.

Example:

import { AgentBuilder, MessageToolCall, ToolBuilder } from '@sammyl/ai-agents';

// A fake web search API
const fakeSearchApi = (query: string) => `Processing query: ${query}...`;

const searchTool = new ToolBuilder()
	.setToolDefinition({
		type: 'function',
		function: {
			name: 'search_web',
			description: 'Search the web for a given query',
			parameters: {
				type: 'object',
				properties: {
					query: {
						type: 'string',
						description: 'What to search for.',
					},
				},
				required: ['query'],
				additionalProperties: false,
			},
		},
	})
	.setToolRequestHandler(async (request: MessageToolCall) => {
		const {
			id,
			function: { arguments: parameters },
		} = request;
		const { query } = JSON.parse(parameters) as { query: string };

		return {
			tool_call_id: id,
			role: 'tool',
			content: fakeSearchApi(query),
		};
	})
	// .setIsGlobal(true) // Uncomment to make this tool available to all agents
	.build();

const webSurferAgent = new AgentBuilder()
	.setName('Web Surfer')
	.setDescription('Searches the web for interesting content.')
	.addTool(searchTool)
	.build();

Orchestration with File Access Tool Example

Below is a more complex example. Two agents (a "Reviewer" and a "Storyteller") cooperate under an Orchestrator. The Orchestrator and Agents use FileAccessTools to log and store the generated content. Logging is enabled to observe the conversation’s progression.

import {
	AgentBuilderFactory,
	CompletionResult,
	FileAccessTools,
	IOrchestrator,
	JSONLinesFileLogger,
	ORCHESTRATOR_COMPLETED_EVENT,
	ORCHESTRATOR_UPDATE_EVENT,
	OrchestratorBuilder,
} from '@sammyl/ai-agents';
import OpenAI from 'openai';

const openai = new OpenAI({
	apiKey: process.env.OPENAI_API_KEY,
});

const factory = new AgentBuilderFactory(openai);

const reviewer = factory
	.getBuilder()
	.setName('Analytical Reviewer')
	.setDescription(
		'Provides feedback on feasibility and logical validity of stories.',
	)
	.setAgentMessageLogger(new JSONLinesFileLogger('reviewer.jsonl', 'logs')) // Log messages
	.build();

const storyteller = factory
	.getBuilder()
	.setName('Story Teller')
	.setDescription('Crafts brilliant, joyful, and positive stories.')
	.setAgentMessageLogger(new JSONLinesFileLogger('storyteller.jsonl', 'logs')) // Log messages
	.build();

const orchestrator: IOrchestrator = new OrchestratorBuilder()
	.setOpenAIClient(openai)
	.addAgent(reviewer)
	.addAgent(storyteller)
	.addTools(new FileAccessTools()) // Enable file access
	.setMessageLogger(new JSONLinesFileLogger('orchestrator-log.jsonl', 'logs')) // Orchestrator logging
	.build();

orchestrator.on(ORCHESTRATOR_UPDATE_EVENT, (update) => {
	console.log(update);
});

orchestrator.on(
	ORCHESTRATOR_COMPLETED_EVENT,
	async (result: CompletionResult) => {
		console.log(result.summary);
		console.log('DONE');
	},
);

orchestrator.run(
	'Craft an engaging story about learning to program and save it to a markdown file.',
);

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check out the issues page.

License

This project is licensed under the MIT License.