
我们将创建一个能够搜索维基百科并根据找到的信息回答问题的人工智能代理。该 react(理性与行动)代理使用 google generative ai api 来处理查询并生成响应。我们的代理将能够:
react agent 是一种遵循反射-操作循环的特定类型的代理。它根据可用信息和它可以执行的操作反映当前任务,然后决定采取哪个操作或是否结束任务。
我们的 react agent 将具有三个主要状态:
让我们逐步构建 react agent,突出显示每个状态。
首先,设置项目并安装依赖项:
mkdir react-agent-project cd react-agent-project npm init -y npm install axios dotenv @google/generative-ai
在项目根目录创建一个 .env 文件:
google_ai_api_key=your_api_key_here
使用以下内容创建 tools.js:
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;
使用以下内容创建 reactagent.js:
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;
使用以下内容创建index.js:
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);
与维基百科的交互主要分为两个步骤:
初始搜索(维基百科功能):
详细搜索(wikipedia_with_pageid函数):
此过程允许代理首先获得与查询相关的主题的概述,然后根据需要深入研究特定部分。
以上就是使用 nodeJS 从头开始创建 ReAct Agent(维基百科搜索)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号