构建强大的ai代理的关键在于其管理和执行工具(函数调用)的能力。这些工具赋予代理执行各种任务的能力,例如数据抓取、内容总结,甚至复杂的流程自动化。然而,随着ai代理规模和功能的扩张,管理和维护众多工具的难度也随之增加。
本教程将使用Toolhouse SDK演示如何利用预构建工具,并追踪每个工具调用的过程。我们将构建一个简单的界面,用户输入URL和提示,AI代理则使用工具抓取网页并处理数据。
工具是AI代理的核心,如同其四肢一般。每个工具都代表着AI执行特定任务的专业技能或功能。
面向用户的AI代理需要胜任各种任务。从零开始编写AI工具以实现API集成或网页抓取逻辑,无异于重复造轮子,并且需要开发团队进行长期维护。
Toolhouse平台有效解决了这些问题。它能够:
这些功能简化了工具管理,让您专注于构建更智能的AI代理,而无需费心于工具的构建和维护。
让我们构建一个AI驱动的网络爬虫。它是一个单页面应用,用户输入待抓取的URL和可选提示,AI代理将抓取网页并处理数据。
所需资源:
我们将使用React创建一个简单的前端来管理工具调用。确保已安装create-react-app,我们将用它初始化新的React应用。如果未安装,请运行以下命令:
<code class="bash">npm install -g create-react-app</code>
在终端输入以下命令:
<code class="bash">npx create-react-app ai-scraper</code>
进入项目目录:
<code class="bash">cd ai-scraper</code>
项目文件夹结构应如下所示:

启动服务器:
<code class="bash">npm start</code>
应用应在localhost:3000启动:

这些SDK将使我们的应用与Toolhouse平台和OpenAI模型交互:
<code class="bash">npm install @toolhouseai/sdk openai</code>
在ai-scraper项目文件夹中创建一个.env文件,并添加以下API密钥:
<code>REACT_APP_TOOLHOUSE_API_KEY=your_toolhouse_api_key REACT_APP_OPENAI_API_KEY=your_openai_api_key</code>
您可以在platform.openai.com/api-keys找到OpenAI API密钥。将your_openai_api_key替换为您的实际密钥。
要获取Toolhouse API密钥,请先在toolhouse.ai注册一个帐户。

注册后,访问API密钥页面:

点击眼睛图标显示您的API密钥,复制并粘贴到.env文件的your_toolhouse_api_key处。
您的Toolhouse仪表板应如下所示:

点击左侧菜单中的“Bundles”创建新的Bundle。Bundle用于组织AI工具。

创建后,您可以添加不同的预制工具到Bundle中:

启用Tavily Web Search工具将其添加到您的Bundle:

App.js组件在src文件夹中的App.js文件(或App.ts)中替换代码:
<code class="javascript">import React, { useState } from "react";
import { Toolhouse } from "@toolhouseai/sdk";
import OpenAI from "openai";
import "./App.css";
const model = "gpt-4o-mini";
function App() {
const [url, setUrl] = useState("");
const [prompt, setPrompt] = useState("");
const [result, setResult] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const handleSubmit = async (e) => {
e.preventDefault();
setIsLoading(true);
setError("");
setResult("");
try {
const toolhouse = new Toolhouse({
apiKey: process.env.REACT_APP_TOOLHOUSE_API_KEY,
metadata: {
id: "user_id",
timezone: "0",
},
});
const client = new OpenAI({
apiKey: process.env.REACT_APP_OPENAI_API_KEY,
dangerouslyAllowBrowser: true,
});
const messages = [
{
role: "user",
content: `get the contents of ${url} and ${prompt}`,
},
];
const tools = await toolhouse.getTools();
const chatCompletion = await client.chat.completions.create({
messages,
model: model,
tools,
});
const openaiMessage = await toolhouse.runTools(chatCompletion);
const newMessages = [...messages, ...openaiMessage];
const chatCompleted = await client.chat.completions.create({
messages: newMessages,
model: model,
tools,
});
setResult(chatCompleted?.choices[0]?.message?.content);
} catch (err) {
console.error("Error occurred:", err);
setError(
`An error occurred while processing your request. ${
err.message || JSON.stringify(err)
}`
);
} finally {
setIsLoading(false);
}
};
return (
<div className="container">
<h1>AI Scraper with Toolhouse</h1>
<form onSubmit={handleSubmit}>
<div className="form-group">
<label htmlFor="url">URL</label>
<input
type="text"
id="url"
value={url}
onChange={(e) => setUrl(e.target.value)}
placeholder="https://example.com"
required
/>
</div>
<div className="form-group">
<label htmlFor="prompt">Prompt</label>
<textarea
id="prompt"
value={prompt}
onChange={(e) => setPrompt(e.target.value)}
placeholder="Summarize the content..."
required
/>
</div>
<button type="submit" disabled={isLoading}>
{isLoading ? "Processing..." : "Scrape and Process"}
</button>
</form>
{error && <div className="error">{error}</div>}
{result && (
<div className="result">
<h2>Result:</h2>
{result}
</div>
)}
</div>
);
}
export default App;</code>在src文件夹内的App.css文件中添加样式:
<code class="css">/* (CSS styles remain the same as in the original input) */</code>
停止已运行的React服务器(ctrl + c),然后运行以下命令重新启动服务器:
<code class="bash">npm start</code>
应用界面如下所示:

输入URL和提示,AI代理将抓取并总结网页内容。请注意,某些网站可能不允许抓取,导致工具调用失败。
示例:


Toolhouse执行日志可以监控每个工具调用的情况,帮助您评估工具调用数量并优化调用以节省资源。

日志记录了每个工具调用的时间和输出。
本教程演示了如何使用Toolhouse SDK构建一个简单的AI驱动的网络爬虫。Toolhouse简化了工具管理,让您可以专注于构建更强大的AI代理。 请记住,API密钥需要替换为您的实际密钥。
以上就是使用 Toolhouse SDK 管理用于函数调用的 AI 工具的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号