DEV Community

Creating a ReAct Agent from the scratch with nodeJS ( wikipedia search )

In this tutorial, we will create an AI agent capable of searching Wikipedia and answering questions based on the information it finds. This ReAct (Reason and Act) Agent uses the Google Generative AI API to process queries and generate responses. Our agent will be able to search Wikipedia for relevant information, extract specific sections from Wikipedia pages, and reason about the information gathered to formulate answers. To start, we need to set up the project and install necessary dependencies. We will use Node.js, Axios library for HTTP requests, Google Generative AI API, and Wikipedia API. After setting up the project, we will create a .env file with our Google AI API key. Next, we will create a Tools.js file that contains functions for interacting with Wikipedia. These functions include searching Wikipedia for snippets, page IDs, and section IDs, as well as fetching specific content from Wikipedia using a page ID and section ID. We will then create a ReactAgent.js file that defines the ReActAgent class. This class has three main states: THOUGHT (Reflection), ACTION (Execution), and ANSWER (Response). The agent uses these states to process queries and generate responses. Finally, we will create an index.js file to run the agent. This file will define an async main function that initializes the ReActAgent with a query and a list of available functions, then runs the agent to get a response. The Wikipedia part of the agent works in two main steps: initial search and detailed search. The initial search function makes a request to the Wikipedia search API and returns up to 4 relevant results for the query. For each result, it fetches the sections of the page. The detailed search function uses the page ID and section ID to fetch specific content and returns the text of the requested section. The execution flow of the agent involves entering the THOUGHT state to reflect on the question, deciding to search Wikipedia and entering the ACTION state, executing the wikipedia function and obtaining results, returning to the THOUGHT state to reflect on the results, and repeating the THOUGHT and ACTION cycle as necessary. When it has sufficient information, it enters the ANSWER state and generates a final answer based on all the information collected. The modular structure of the agent allows for easy addition of new tools or APIs. It's important to implement error handling and time/iteration limits to avoid infinite loops or excessive resource use.
favicon
dev.to
dev.to
Create attached notes ...