Warning
JS Agent was an early library to test out ideas and is not actively developed any longer. Check out ModelFusion - a more general AI library for TypeScript that incorporates my learnings from JS Agent.
JS Agent is a composable and extensible framework for creating AI agents with JavaScript and TypeScript.
While creating an agent prototype is easy, increasing its reliability and robustness is complex and requires considerable experimentation. JS Agent provides robust building blocks and tooling to help you develop rock-solid agents faster.
Tutorial
An agent that has access to a wikipedia search engine and can read wikipedia articles. You can use it to answer questions about wikipedia content.
Used features: gpt-3.5-turbo
, custom tools (search wikipedia, read wikipedia article), generate next step loop, max steps run controller
An automated developer agent that works in a docker container. It can read files, write files and execute commands. You can adjust it for your project and use it to document code, write tests, update tests and features, etc.
Used features: gpt-4
, tool execution in Docker container, agent with fixed setup steps, multiple agent run properties, generate next step loop, tools (read file, write file, run, command, ask user), cost calculation after agent run
JS Agent implementation of BabyAGI.
Features used: HTTP Agent server, text completion model (text-davinci-003
), customized console output, update tasks planning loop
Takes a PDF and a topic and creates a Twitter thread with all content from the PDF that is relevant to the topic.
Features used: function composition (no agent), pdf loading, split-extract-rewrite
Splits a text into chunks and generates embeddings.
Features used: direct function calls (no agent), split text (gpt3-tokenizer), generate embeddings
text-davinci-003
etc.)gpt-4
, gpt-3.5-turbo
)text-embedding-ada-002
)Step
and AgentRun
).npm install js-agent
See the examples and documentation to learn how to create an agent.
import * as $ from "js-agent";
const openai = $.provider.openai;
export async function runWikipediaAgent({
wikipediaSearchKey,
wikipediaSearchCx,
openAiApiKey,
task,
}: {
openAiApiKey: string;
wikipediaSearchKey: string;
wikipediaSearchCx: string;
task: string;
}) {
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.",
inputExample: {
url: "https://en.wikipedia.org/wiki/Artificial_intelligence",
topic: "{query that you are answering}",
},
execute: $.tool.executeExtractInformationFromWebpage({
extract: $.text.extractRecursively.asExtractFunction({
split: $.text.splitRecursivelyAtToken.asSplitFunction({
tokenizer: openai.tokenizer.forModel({
model: "gpt-3.5-turbo",
}),
maxChunkSize: 2048, // needs to fit into a gpt-3.5-turbo prompt and leave room for the answer
}),
extract: $.text.generateText.asFunction({
prompt: $.prompt.extractChatPrompt(),
model: openai.chatModel({
apiKey: openAiApiKey,
model: "gpt-3.5-turbo",
}),
}),
}),
}),
});
return $.runAgent<{ task: string }>({
properties: { task },
agent: $.step.generateNextStepLoop({
actions: [searchWikipediaAction, readWikipediaArticleAction],
actionFormat: $.action.format.flexibleJson(),
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" }),
});
}