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

agents-js

v0.0.5

Published

A versatile JavaScript library for designing and managing intelligent agents capable of interacting with AI-driven chat models, leveraging OpenAI's GPT for dynamic conversations and task automation.

Downloads

324

Readme

agents-js

agents-js is a JavaScript package designed to facilitate interaction with chat models, such as OpenAI's GPT models. It provides a structured way to define agents with specific instructions and functions, and it handles tool calls and context variables efficiently.

Installation

To install agents-js, you can use npm:

npm install agents-js

Usage

The primary classes provided by the agents-js package are Agent, AgentController, Result, and Response. Here, we'll walk through some basic usage examples.

Example 1: Creating an Agent

const { Agent } = require("agents-js");

// Define a simple agent with a name and instruction
const myAgent = new Agent({
  name: "MyAssistant",
  instructions: "You are a friendly assistant.",
  model: "gpt-4",
});

console.log(myAgent);

Example 2: Initializing OpenAI Client and Using AgentController

Before using the AgentController class to interact with chat models, you need to initialize the OpenAI client with your API key.

const OpenAI = require("openai");
const { AgentController, Agent } = require("agents-js");

// Initialize OpenAI client
const client = new OpenAI({
  apiKey: "sk-thekey", // Replace 'sk-thekey' with your actual API key
});

// Use the initialized client to create a AgentController instance
const agentController = new AgentController(client);

// Create an agent
const agent = new Agent({
  name: "Assistant",
  instructions: "You are a helpful assistant.",
  model: "gpt-4",
});

// Define a message history
const messages = [{ role: "user", content: "Hello, can you help me?" }];

// Fetch a chat completion
(async () => {
  const response = await agentController.run(agent, messages);
  console.log(response);
})();

If you want to use TuneStudio you can pass base url to the client like this:

const client = new OpenAI({
  apiKey: "sk-tune-key",
  baseURL: "https://proxy.tune.app",
});

### Example 3: Handling Tool Calls

You can define functions that the agent can call during its operation.

```javascript
const { AgentController, Agent, Result } = require("agents-js");

// Define a simple function that the agent can use
function add({ a, b }) {
  /**
   * @param {number} a - First number
   * @param {number} b - Second number
   * @description Adds two numbers
   */
  return new Result({ value: `${a + b}` });
}

// Initialize OpenAI client
const client = new OpenAI({
  apiKey: "sk-thekey", // Replace 'sk-thekey' with your actual API key
});

// Use the initialized client to create a AgentController instance
const agentController = new AgentController(client);

// Create an agent with the function
const agent = new Agent({
  name: "Calculator",
  instructions: "You can perform addition using the add function.",
  model: "gpt-4",
  functions: [add],
});

// Define a message history
const messages = [{ role: "user", content: "What is 2 + 3?" }];

// Fetch a chat completion
(async () => {
  const response = await agentController.run(agent, messages);
  console.log(response);
})();

Example 4: Using Context Variables

Context variables allow the agent to maintain state across interactions.

const { AgentController, Agent, Result } = require("agents-js");

// Define a function using context variables
function greet({ name }) {
  /**
   * @param {string} name - Name of the person to greet
   * @description Greets a person by name
   */
  return new Result({ value: `Hello, ${name}!` });
}

// Initialize OpenAI client
const client = new OpenAI({
  apiKey: "sk-thekey", // Replace 'sk-thekey' with your actual API key
});

// Use the initialized client to create a AgentController instance
const agentController = new AgentController(client);

// Create an agent with the function
const agent = new Agent({
  name: "Greeter",
  instructions: "You can greet people using the greet function.",
  model: "gpt-4",
  functions: [greet],
});

// Define a message history and context variables
const messages = [{ role: "user", content: "Greet John for me." }];
const contextVariables = { name: "John" };

// Fetch a chat completion
(async () => {
  const response = await agentController.run(agent, messages, contextVariables);
  console.log(response);
})();

Conclusion

agents-js provides a flexible framework for building conversational applications that can leverage external functions and maintain context across interactions. By defining agents and their capabilities, you can create powerful and dynamic chat applications.


Make sure to replace "sk-thekey" with your actual API key when using the OpenAI client in your application.