使用 Nodejs 创建 ReAct AI 代理(维基百科搜索)en
来源:dev.to
时间:2024-11-26 11:54:39 342浏览 收藏
在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是文章学习者,那么本文《使用 Nodejs 创建 ReAct AI 代理(维基百科搜索)en》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

介绍
我们将创建一个能够搜索维基百科并根据收集到的信息回答问题的人工智能代理。
该 react(推理和行动)代理使用 google generative ai api 来处理查询并生成响应。
我们的代理将能够:
- 在维基百科上搜索相关信息。
- 从维基百科页面中提取特定部分。
- 对收集到的信息进行分析并制定回复。
[2] 什么是react代理?
react agent 是一种遵循反射-操作循环的特定类型的代理。它根据可用信息和可以采取的操作来反映当前任务,然后决定采取什么操作或是否完成任务。
[3] 规划代理
3.1 所需工具
- node.js
- 用于 http 请求的 axios 库
- google 生成式 ai api (gemini-1.5-flash)
- 维基百科 api
3.2 代理结构
我们的 react agent 将具有三个主要状态:
- 思想(反思)
- 行动(执行)
- 回答(回复)
3.3 思想状态
思考状态是reactagent对收集到的信息进行反思并决定下一步应该做什么的时刻。
async thought() {
// ...
}
3.4 动作状态(action)
在动作状态下,代理根据先前的想法执行可用功能之一。
请注意,有行动(执行)和决定(哪个行动)。
async action() {
// chama a decisão
// executa a ação e retorna um actionresult
}
async decideaction() {
// chama o llm com base no pensamento (reflexão) para formatar e adequar a chamada de função.
// procure por um modo de função-ferramenta na [documentação da api do google](https://ai.google.dev/gemini-api/docs/function-calling)
}
[4] 实现代理
让我们逐步构建 react agent,突出显示每个状态。
4.1 初始配置
首先,配置项目并安装依赖项:
mkdir projeto-agente-react cd projeto-agente-react npm init -y npm install axios dotenv @google/generative-ai
在项目根目录创建一个.env文件:
google_ai_api_key=sua_chave_api_aqui
这里有免费的 api 密钥
4.2 角色声明
此文件是 node.js 将用来执行对维基百科的 api 调用的 javascript 文件。
我们在 functiondescription 中描述了该文件的内容。
使用以下内容创建 tools.js:
const axios = require("axios");
class tools {
static async wikipedia(q) {
try {
const response = await axios.get("https://pt.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://pt.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://pt.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://pt.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 创建reactagent.js文件
使用以下内容创建 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: 1.8,
});
}
async run() {
this.pushhistory(`**tarefa: ${this.query} **`);
try {
return await this.step();
} catch (e) {
console.error("erro durante a execução:", e);
return "desculpe, não consegui processar sua solicitação.";
}
}
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":
return await this.thought();
break;
case "action":
return await this.action();
break;
case "answer":
return await this.answer();
}
}
async thought() {
const funcoesdisponiveis = json.stringify(array.from(this.functions));
const contextohistorico = this.history.join("\n");
const prompt = `sua tarefa é ${this.consulta}
o contexto posui todas as reflexões que você fez até agora e os resultadoação que coletou.
açõesdisponíveis são funções que você pode chamar sempre que precisar de mais dados.
contexto: "${contextohistorico}" <<
açõesdisponíveis: "${funcoesdisponiveis}" <<
tarefa: "${this.consulta}" <<
reflita sobre sua tarefa usando o contexto, resultadoação e açõesdisponíveis para encontrar seu próximo_passo.
imprima seu próximo_passo com um pensamento ou finalize cumprindo sua tarefa caso tenha as informações disponíveis`;
const thought = await this.promptmodel(prompt);
this.pushhistory(`\n **${thought.trim()}**`);
if (
thought.tolowercase().includes("cumprida") ||
thought.tolowercase().includes("cumpra") ||
thought.tolowercase().includes("cumprindo") ||
thought.tolowercase().includes("finalizar") ||
thought.tolowercase().includes("finalizando") ||
thought.tolowercase().includes("finalize") ||
thought.tolowercase().includes("concluída")
) {
this.state = "answer";
} else {
this.state = "action";
}
return this.step();
}
async action() {
const action = await this.decideaction();
this.pushhistory(`** ação: ${action} **`);
const result = await this.executefunctioncall(action);
this.pushhistory(`** resultadoação: ${result} **`);
this.state = "thought";
return this.step();
}
async decideaction() {
const availablefunctions = json.stringify(array.from(this.functions));
const historycontext = this.history;
const prompt = `reflita sobre o pensamento, consulta e ações disponíveis
${historycontext[historycontext.length - 2]}
pensamento <<< ${historycontext[historycontext.length - 1]}
consulta: "${this.query}"
ações disponíveis: ${availablefunctions}
retorne apenas a função,parâmetros separados por vírgula. exemplo: "wikipedia,ronaldinho gaucho,1450"`;
const decision = await this.promptmodel(prompt);
return decision.replace(/`/g, "").trim();
}
async answer() {
const historycontext = this.history.join("\n");
const prompt = `com base no seguinte contexto, forneça uma resposta completa e detalhada para a tarefa: ${this.query}.
contexto:
${historycontext}
tarefa: "${this.query}"`;
const finalanswer = await this.promptmodel(prompt);
return finalanswer;
}
async promptmodel(prompt) {
const result = await this.model.generatecontent(prompt);
const response = await result.response;
return response.text();
}
async executefunctioncall(functioncall) {
const [functionname, ...args] = functioncall.split(",");
const func = tools[functionname.trim()];
if (func) {
return await func.call(null, ...args);
}
throw new error(`função ${functionname} não encontrada`);
}
pushhistory(value) {
this._history.push(value);
}
get history() {
return this._history;
}
}
module.exports = reactagent;
4.4 运行代理并解释可用工具 (index.js)
使用以下内容创建index.js:
const reactagent = require("./reactagentptbr.js");
async function main() {
const query = "que clubes ronaldinho gaúcho jogou para?";
// const query = "quais os bairros de joinville?";
// const query = "qual a capital da frança?";
const functions = [
[
"wikipedia",
"params: query",
"busca semântica na wikipedia api por pageid e sectionids >> \n ex: pontos turísticos de são paulo \n são paulo é uma cidade com muitos pontos turísticos, pageid, sections : []",
],
[
"wikipedia_with_pageid",
"params: pageid, sectionid",
"busca na wikipedia api usando pageid e sectionindex como parametros. \n ex: 1500,1234 \n informações sobre a seção blablalbal",
],
];
const agent = new reactagent(query, functions);
const result = await agent.run();
console.log("resultado do agente:", result);
}
main().catch(console.error);
角色描述
尝试添加新工具或功能时,请务必对其进行良好描述。
在我们的示例中,这已经完成并在调用新实例时添加到我们的 reactagent 类中。
const functions = [
[
"google", // nomeDaFuncao
"params: query", // NomeDoParâmetroLocal
"Pesquisa semântica na API da Wikipedia por snippets, pageIds e sectionIds >> \n ex: Quando o Brasil foi colonizado? \n O Brasil foi colonizado em 1500, pageId, sections : []", // breve explicação e exemplo (isso será encaminhado para o LLM)
]
];
[5] 维基百科部分如何运作
与维基百科的互动分两个主要步骤完成:
-
初始搜索(维基百科功能):
- 向维基百科搜索 api 发出请求。
- 最多返回 4 个与查询相关的结果。
- 对于每个结果,搜索页面的各个部分。
-
详细搜索(wikipedia_with_pageid函数):
- 使用页面 id 和分区 id 搜索特定内容。
- 返回请求部分的文本。
此过程允许代理首先获得与查询相关的主题的概述,然后根据需要深入到特定部分。
[6] 执行流程示例
- 用户提问。
- 智能体进入思考状态并反思问题。
- 他决定搜索维基百科并进入 action 状态。
- 运行维基百科函数并获取结果。
- 返回thought状态反思结果。
- 您可以决定寻找更多细节或不同的方法。
- 根据需要重复思想和行动循环。
- 当它有足够的信息时,它进入answer状态。
- 根据收集到的所有信息生成最终响应。
- 只要维基百科没有可收集的数据,就进入无限循环。用计时器解决这个问题=p
[7] 最后的考虑
- 模块化结构可以轻松添加新工具或 api。
- 实施错误处理和时间/迭代限制非常重要,以避免无限循环或过度的资源使用。
- 此示例使用温度 2。温度越低,代理在迭代过程中的创造力就越低。通过实验了解温度对 llm 的影响。
今天关于《使用 Nodejs 创建 ReAct AI 代理(维基百科搜索)en》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
220 收藏
-
102 收藏
-
420 收藏
-
498 收藏
-
278 收藏
-
156 收藏
-
225 收藏
-
250 收藏
-
446 收藏
-
228 收藏
-
360 收藏
-
165 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习