DEV Community

Cover image for GPTAgent.js: Build your own AI Agents with TypeScript and JavaScript!
Lars Grammel
Lars Grammel

Posted on

GPTAgent.js: Build your own AI Agents with TypeScript and JavaScript!

As a JavaScript developer, having efficient tools and frameworks at our disposal is crucial, especially when creating AI agents that require experimentation and adaptability. That's why I'd like to introduce GPTAgent.js, a composable and extensible framework designed for building AI agents using TypeScript and JavaScript.

Please note that GPTAgent.js is currently in its early stages (v0.0.4), and more features and improvements will be added over time.

Here are some examples of what I've implemented with GPTAgent.js:

JavaScript/TypeScript Developer Agent

This agent runs in a Docker container and can read files, write files, and execute commands, allowing it to perform various development tasks. For instance, it can write, run, and fix unit tests.

Check out this tweet demonstrating how GPTAgent.js wrote the first unit test for the framework itself:

🦾 gptagent.js wrote the first unit test for gptagent.js

🧵Here's what it did: pic.twitter.com/b67Cl02qgX

— Lars Grammel (@lgrammel) April 10, 2023

Wikipedia Question-Answering Agent

This AI agent can search Wikipedia and read articles, providing answers based on the information found. The code snippet below demonstrates the configuration for the Wikipedia Question-Answering Agent:

import $, { ActionRegistry, Agent, runCLIAgent } from "@gptagent/agent";

const textGenerator = new $.ai.openai.Gpt4ChatTextGenerator({
  apiKey: process.env.OPENAI_API_KEY,
});

const searchWikipediaAction =
  new $.action.tool.ProgrammableGoogleSearchEngineAction({
    type: "tool.search-wikipedia",
    description:
      "Search wikipedia using a search term. Returns a list of pages.",
    executor: new $.action.tool.ProgrammableGoogleSearchEngineExecutor({
      key: process.env.WIKIPEDIA_SEARCH_KEY,
      cx: process.env.WIKIPEDIA_SEARCH_CX,
    }),
  });

const summarizeWebpageAction = new $.action.tool.SummarizeWebpageAction({
  type: "tool.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}",
  },
  executor: new $.action.tool.SummarizeWebpageExecutor({
    webpageTextExtractor:
      new $.component.webpageTextExtractor.BasicWebpageTextExtractor(),
    summarizer: new $.component.textSummarizer.SingleLevelSplitSummarizer({
      splitter: new $.component.splitter.RecursiveCharacterSplitter({
        maxCharactersByChunk: 4096 * 4,
      }),
      summarizer: new $.component.textSummarizer.ChatTextSummarizer({
        chatTextGenerator: textGenerator,
      }),
    }),
  }),
});

runCLIAgent({
  agent: new Agent({
    name: "Wikipedia QA",
    rootStep: new $.step.DynamicCompositeStep({
      nextStepGenerator: new $.step.BasicNextStepGenerator({
        role: `You are an knowledge worker that answers questions using Wikipedia content.`,
        constraints: `Make sure all facts for your answer are from Wikipedia articles that you have read.`,
        actionRegistry: new ActionRegistry({
          actions: [searchWikipediaAction, summarizeWebpageAction],
          format: new $.action.format.JsonActionFormat(),
        }),
        textGenerator,
      }),
    }),
  }),
});
Enter fullscreen mode Exit fullscreen mode

Give GPTAgent.js a try and share your experiences with the developer community. Your feedback is essential for the continuous improvement of the framework. Don't forget to follow the GPTAgent.js project for updates and news. Happy coding!

Top comments (1)

Collapse
 
dariel_vila_2d5ebae1c430f profile image
Dariel Vila • Edited

Great to see frameworks like GPTAgent.js advancing the possibilities of building AI agents with JavaScript! I’ve been working on KaibanJS, which also focuses on multi-agent systems but with an orchestration layer for complex workflows. Inspiring to see more tools emerging for JS devs in the AI space. Thanks for sharing