JS Agent: Building AI Agents with JavaScript and TypeScript
JS Agent is a groundbreaking framework designed for those interested in creating AI agents using JavaScript and TypeScript. It offers a sophisticated yet flexible toolkit to aid developers in building dependable AI systems more efficiently. While JS Agent is not actively developed anymore, it has laid the groundwork for more advanced technologies like ModelFusion. However, its existing tools and features continue to serve as an excellent starting point for those delving into AI agent development.
Overview of JS Agent Features
At its core, JS Agent is fully committed to providing developers with the essential tools required to define, execute, and observe AI agents. It ensures that each agent run is comprehensively monitored, enabling detailed analysis of all Large Language Model (LLM) interactions and associated costs. Developers can seamlessly transition between various LLM models to enhance the performance of their agents.
Key Functionalities
-
Agent Definition and Execution:
- Define configurable properties that improve prompt efficiency.
- Monitor agent performance through console outputs and web applications.
- Calculate the cost and log each call made to LLM during the agent run.
- Limit agent activities with criteria to ensure efficient run times.
-
Agent HTTP Server:
- Initiate, manage, and observe agent runs through an HTTP API.
- Facilitate hosting and managing multiple agents efficiently.
-
Supported LLM Models and APIs:
- Utilize OpenAI's text and chat completion models, such as
gpt-4
andtext-davinci-003
. - Employ OpenAI's embedding models for advanced functionalities.
- Utilize OpenAI's text and chat completion models, such as
-
Actions and Tools:
- Perform file manipulations, command executions, and interactive user inputs.
- Leverage programmable search engines to enhance data extraction and processing.
-
Agent Loops:
- Implement BabyAGI-style planning loops to streamline task execution.
- Generate logical next steps with built-in loop functionalities.
-
Prompt Templates and Text Functions:
- Use built-in templates for efficient data extraction and rewriting.
- Utilize functions to split and manage text effectively.
-
Data Handling and Conversion:
- Convert web pages and files into easily manageable data formats like HTML and ArrayBuffer.
- Transform HTML and PDF documents into text for seamless integration.
Design Principles
JS Agent adheres to a strong set of design principles that emphasize safety, discovery, and flexibility. It provides robust typing to improve user experience and ensure safety during development. The framework is designed to be composable and extensible, allowing users to integrate custom tools and actions effortlessly. By leveraging functional programming, JS Agent offers efficient immutability and seamless composition.
Enhancing AI Agent Development
JS Agent’s comprehensive design promotes a progressive refinement of agent specifications. It includes default settings that ease the entry into AI agent creation, while also offering the flexibility to override these settings with custom configurations and prompts. Its production-ready support for logging and cost tracking ensures that developers can deploy and manage AI applications effectively.
Getting Started
To start working with JS Agent, developers can easily install it via npm:
npm install js-agent
A wealth of examples and documentation is available to assist new users in creating their very first AI agents. By exploring these resources, developers can gain a deeper understanding of JS Agent's capabilities and how to harness them effectively.
Example Agent
Here is a simple illustration of an agent that uses a Wikipedia search engine to answer questions. It integrates multiple tools and models to extract and summarize information from Wikipedia:
import * as $ from "js-agent";
const openai = $.provider.openai;
export async function runWikipediaAgent({
wikipediaSearchKey,
wikipediaSearchCx,
openAiApiKey,
task,
}) {
const searchWikipediaAction = $.tool.programmableGoogleSearchEngineAction({
id: "search-wikipedia",
description: "Search wikipedia using a search term. Returns a list of pages.",
execute: $.tool.executeProgrammableGoogleSearchEngineAction({
key: wikipediaSearchKey,
cx: wikipediaSearchCx,
}),
});
const readWikipediaArticleAction = $.tool.extractInformationFromWebpage({
id: "read-wikipedia-article",
description: "Read a wikipedia article and summarize it considering the query.",
execute: $.tool.executeExtractInformationFromWebpage({
extract: $.text.extractRecursively.asExtractFunction({
split: $.text.splitRecursivelyAtToken.asSplitFunction({
tokenizer: openai.tokenizer.forModel({
model: "gpt-3.5-turbo",
}),
maxChunkSize: 2048,
}),
extract: $.text.generateText.asFunction({
prompt: $.prompt.extractChatPrompt(),
model: openai.chatModel({
apiKey: openAiApiKey,
model: "gpt-3.5-turbo",
}),
}),
}),
}),
});
return $.runAgent({
properties: { task },
agent: $.step.generateNextStepLoop({
actions: [searchWikipediaAction, readWikipediaArticleAction],
prompt: $.prompt.concatChatPrompts(
async ({ runState: { task } }) => [
{
role: "system",
content: `## ROLE
You are an knowledge worker that answers questions using Wikipedia content. You speak perfect JSON.
## CONSTRAINTS
All facts for your answer must be from Wikipedia articles that you have read.
## TASK
${task}`,
},
],
$.prompt.availableActionsChatPrompt(),
$.prompt.recentStepsChatPrompt({ maxSteps: 6 })
),
model: openai.chatModel({
apiKey: openAiApiKey,
model: "gpt-3.5-turbo",
}),
}),
controller: $.agent.controller.maxSteps(20),
observer: $.agent.observer.showRunInConsole({ name: "Wikipedia Agent" }),
});
}
JS Agent remains a remarkable resource for developers looking to innovate in the field of AI, offering a pragmatic and versatile approach to agent creation and deployment.