We'll 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:
A ReAct Agent is a specific type of agent that follows a Reflection-Action cycle. It reflects on the current task, based on available information and actions it can perform, and then decides which action to take or whether to conclude the task.
Our ReAct Agent will have three main states:
Let's build the ReAct Agent step by step, highlighting each state.
First, set up the project and install dependencies:
mkdir react-agent-project cd react-agent-project npm init -y npm install axios dotenv @google/generative-ai
Create a .env file at the project's root:
GOOGLE_AI_API_KEY=your_api_key_here
Create Tools.js with the following content:
const axios = require("axios"); class Tools { static async wikipedia(q) { try { const response = await axios.get("https://en.wikipedia.org/w/api.php", { params: { action: "query", list: "search", srsearch: q, srwhat: "text", format: "json", srlimit: 4, }, }); const results = await Promise.all( response.data.query.search.map(async (searchResult) => { const sectionResponse = await axios.get( "https://en.wikipedia.org/w/api.php", { params: { action: "parse", pageid: searchResult.pageid, prop: "sections", format: "json", }, }, ); const sections = Object.values( sectionResponse.data.parse.sections, ).map((section) => `${section.index}, ${section.line}`); return { pageTitle: searchResult.title, snippet: searchResult.snippet, pageId: searchResult.pageid, sections: sections, }; }), ); return results .map( (result) => `Snippet: ${result.snippet}\nPageId: ${result.pageId}\nSections: ${JSON.stringify(result.sections)}`, ) .join("\n\n"); } catch (error) { console.error("Error fetching from Wikipedia:", error); return "Error fetching data from Wikipedia"; } } static async wikipedia_with_pageId(pageId, sectionId) { if (sectionId) { const response = await axios.get("https://en.wikipedia.org/w/api.php", { params: { action: "parse", format: "json", pageid: parseInt(pageId), prop: "wikitext", section: parseInt(sectionId), disabletoc: 1, }, }); return Object.values(response.data.parse?.wikitext ?? {})[0]?.substring( 0, 25000, ); } else { const response = await axios.get("https://en.wikipedia.org/w/api.php", { params: { action: "query", pageids: parseInt(pageId), prop: "extracts", exintro: true, explaintext: true, format: "json", }, }); return Object.values(response.data?.query.pages)[0]?.extract; } } } module.exports = Tools;
Create ReactAgent.js with the following content:
require("dotenv").config(); const { GoogleGenerativeAI } = require("@google/generative-ai"); const Tools = require("./Tools"); const genAI = new GoogleGenerativeAI(process.env.GOOGLE_AI_API_KEY); class ReActAgent { constructor(query, functions) { this.query = query; this.functions = new Set(functions); this.state = "THOUGHT"; this._history = []; this.model = genAI.getGenerativeModel({ model: "gemini-1.5-flash", temperature: 2, }); } get history() { return this._history; } pushHistory(value) { this._history.push(`\n ${value}`); } async run() { this.pushHistory(`**Task: ${this.query} **`); try { return await this.step(); } catch (e) { if (e.message.includes("exhausted")) { return "Sorry, I'm exhausted, I can't process your request anymore. >>>>>>>", finalAnswer); return finalAnswer; } } module.exports = ReActAgent;
Create index.js with the following content:
const ReActAgent = require("./ReactAgent.js"); async function main() { const query = "What does England border with?"; const functions = [ [ "wikipedia", "params: query", "Semantic Search Wikipedia API for snippets, pageIds and sectionIds >> \n ex: Date brazil has been colonized? \n Brazil was colonized at 1500, pageId, sections : []", ], [ "wikipedia_with_pageId", "params : pageId, sectionId", "Search Wikipedia API for data using a pageId and a sectionIndex as params. \n ex: 1500, 1234 \n Section information about blablalbal", ], ]; const agent = new ReActAgent(query, functions); try { const result = await agent.run(); console.log("THE AGENT RETURN THE FOLLOWING >>>", result); } catch (e) { console.log("FAILED TO RUN T.T", e); } } main().catch(console.error);
The interaction with Wikipedia is done in two main steps:
Initial search (wikipedia function):
Detailed search (wikipedia_with_pageId function):
This process allows the agent to first get an overview of topics related to the query and then dive deeper into specific sections as needed.
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3