Maison  >  Article  >  interface Web  >  Créer un agent ReAct à partir de zéro avec nodeJS (recherche wikipedia)

Créer un agent ReAct à partir de zéro avec nodeJS (recherche wikipedia)

DDD
DDDoriginal
2024-09-24 10:30:10755parcourir

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

Introduction

Nous allons créer un agent IA capable de rechercher sur Wikipédia et de répondre aux questions en fonction des informations qu'il trouve. Cet agent ReAct (Reason and Act) utilise l'API Google Generative AI pour traiter les requêtes et générer des réponses. Notre agent pourra :

  1. Recherchez sur Wikipédia des informations pertinentes.
  2. Extraire des sections spécifiques des pages Wikipédia.
  3. Raisonnez sur les informations recueillies et formulez des réponses.

[2] Qu'est-ce qu'un agent ReAct ?

Un Agent ReAct est un type spécifique d'agent qui suit un cycle Réflexion-Action. Il réfléchit à la tâche en cours, sur la base des informations disponibles et des actions qu'il peut effectuer, puis décide quelle action entreprendre ou s'il faut conclure la tâche.

[3] Planification de l'agent

3.1 Outils requis

  • Node.js
  • Bibliothèque Axios pour les requêtes HTTP
  • API Google IA générative (gemini-1.5-flash)
  • API Wikipédia

3.2 Structure des agents

Notre agent ReAct aura trois états principaux :

  1. PENSÉE (Réflexion)
  2. ACTION (Exécution)
  3. RÉPONSE (Réponse)

[4] Implémentation de l'agent

Construisons l'agent ReAct étape par étape, en mettant en évidence chaque état.

4.1 Configuration initiale

Tout d'abord, configurez le projet et installez les dépendances :

mkdir react-agent-project
cd react-agent-project
npm init -y
npm install axios dotenv @google/generative-ai

Créez un fichier .env à la racine du projet :

GOOGLE_AI_API_KEY=your_api_key_here

4.2 Création du fichier Tools.js

Créez Tools.js avec le contenu suivant :

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;

4.3 Création du fichier ReactAgent.js

Créez ReactAgent.js avec le contenu suivant :

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. ><";
      }
      return "Unable to process your request, please try again? ><";
    }
  }

  async step() {
    const colors = {
      reset: "\x1b[0m",
      yellow: "\x1b[33m",
      red: "\x1b[31m",
      cyan: "\x1b[36m",
    };

    console.log("====================================");
    console.log(
      `Next Movement: ${
        this.state === "THOUGHT"
          ? colors.yellow
          : this.state === "ACTION"
            ? colors.red
            : this.state === "ANSWER"
              ? colors.cyan
              : colors.reset
      }${this.state}${colors.reset}`,
    );
    console.log(`Last Movement: ${this.history[this.history.length - 1]}`);
    console.log("====================================");
    switch (this.state) {
      case "THOUGHT":
        await this.thought();
        break;
      case "ACTION":
        await this.action();
        break;
      case "ANSWER":
        await this.answer();
        break;
    }
  }

  async promptModel(prompt) {
    const result = await this.model.generateContent(prompt);
    const response = await result.response;
    return response.text();
  }

  async thought() {
    const availableFunctions = JSON.stringify(Array.from(this.functions));
    const historyContext = this.history.join("\n");
    const prompt = `Your task to FullFill ${this.query}.
Context contains all the reflection you made so far and the ActionResult you collected.
AvailableActions are functions you can call whenever you need more data.

Context: "${historyContext}" <<

AvailableActions: "${availableFunctions}" <<

Task: "${this.query}" <<

Reflect uppon Your Task using Context, ActionResult and AvailableActions to find your next_step.
print your next_step with a Thought or FullFill Your Task `;

    const thought = await this.promptModel(prompt);
    this.pushHistory(`\n **${thought.trim()}**`);
    if (
      thought.toLowerCase().includes("fullfill") ||
      thought.toLowerCase().includes("fulfill")
    ) {
      this.state = "ANSWER";
      return await this.step();
    }
    this.state = "ACTION";
    return await this.step();
  }

  async action() {
    const action = await this.decideAction();
    this.pushHistory(`** Action: ${action} **`);
    const result = await this.executeFunctionCall(action);
    this.pushHistory(`** ActionResult: ${result} **`);
    this.state = "THOUGHT";
    return await this.step();
  }

  async decideAction() {
    const availableFunctions = JSON.stringify(Array.from(this.functions));
    const historyContext = this.history;
    const prompt = `Reflect uppon the Thought, Query and AvailableActions

    ${historyContext[historyContext.length - 2]}

    Thought <<< ${historyContext[historyContext.length - 1]}

    Query: "${this.query}"

    AvailableActions: ${availableFunctions}

    output only the function,parametervalues separated by a comma. For example: "wikipedia,ronaldinho gaucho, 1450"`;

    const decision = await this.promptModel(prompt);
    return `${decision.replace(/`/g, "").trim()}`;
  }

  async executeFunctionCall(functionCall) {
    const [functionName, ...args] = functionCall.split(",");
    const func = Tools[functionName.trim()];
    if (func) {
      return await func.call(null, ...args);
    }
    throw new Error(`Function ${functionName} not found`);
  }

  async answer() {
    const historyContext = this.history;
    const prompt = `Based on the following context, provide a complete, detailed and descriptive formated answer for the Following Task: ${this.query} .

Context:
${historyContext}

Task: "${this.query}"`;

    const finalAnswer = await this.promptModel(prompt);
    this.history.push(`Answer: ${this.finalAnswer}`);
    console.log("WE WILL ANSWER >>>>>>>", finalAnswer);
    return finalAnswer;
  }
}

module.exports = ReActAgent;

4.4 Exécution de l'agent (index.js)

Créez index.js avec le contenu suivant :

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);

[5] Comment fonctionne la partie Wikipédia

L'interaction avec Wikipédia se fait en deux étapes principales :

  1. Recherche initiale (fonction wikipedia) :

    • Fait une requête à l'API de recherche Wikipédia.
    • Renvoie jusqu'à 4 résultats pertinents pour la requête.
    • Pour chaque résultat, il récupère les sections de la page.
  2. Recherche détaillée (fonction wikipedia_with_pageId) :

    • Utilise l'ID de page et l'ID de section pour récupérer un contenu spécifique.
    • Renvoie le texte de la section demandée.

Ce processus permet à l'agent d'abord d'obtenir un aperçu des sujets liés à la requête, puis d'approfondir des sections spécifiques si nécessaire.

[6] Exemple de flux d'exécution

  1. L'utilisateur pose une question.
  2. L'agent entre dans l'état de PENSÉE et réfléchit à la question.
  3. Il décide de rechercher Wikipédia et entre dans l'état ACTION.
  4. Exécute la fonction wikipedia et obtient les résultats.
  5. Revient à l'état de PENSÉE pour réfléchir aux résultats.
  6. Peut décider de rechercher plus de détails ou une approche différente.
  7. Répète le cycle de PENSÉE et d'ACTION si nécessaire.
  8. Lorsqu'il dispose de suffisamment d'informations, il entre dans l'état RÉPONSE.
  9. Génère une réponse finale basée sur toutes les informations collectées.
  10. Entre en boucle infinie chaque fois que Wikipédia n'a pas les données à collecter. Réparez-le avec une minuterie =P

[7] Considérations finales

  • La structure modulaire permet d'ajouter facilement de nouveaux outils ou API.
  • Il est important d'implémenter la gestion des erreurs et les limites de temps/itération pour éviter les boucles infinies ou l'utilisation excessive des ressources.
  • Température d'utilisation : 99999 mdr

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn