GPTAgent.js: Build your own AI Agents with TypeScript and JavaScript!
Published: 2023-04-10 13:52:32

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](https://github.com/lgrammel/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](https://github.com/lgrammel/gptagent.js/tree/main/examples/javascript-developer)
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:
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">🦾 gptagent.js wrote the first unit test for gptagent.js<br><br>🧵Here&#39;s what it did: <a href="https://t.co/b67Cl02qgX">pic.twitter.com/b67Cl02qgX</a></p>&mdash; Lars Grammel (@lgrammel) <a href="https://twitter.com/lgrammel/status/1645396610220716035?ref_src=twsrc%5Etfw">April 10, 2023</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

### [Wikipedia Question-Answering Agent](https://github.com/lgrammel/gptagent.js/tree/main/examples/wikipedia-qa)
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:

```javascript
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,
      }),
    }),
  }),
});
```

Give **[GPTAgent.js](https://github.com/lgrammel/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!