韩国网站设计风格,常州网站快速排名优化,淘宝运营培训学校,做网站需要哪些技术人员这篇文章锁定官网教程中的 Tools-in-depth-guide 章节#xff0c;主要介绍了如何详细构造自己的Tools#xff0c;在之前的博文 smolagents学习笔记系列#xff08;二#xff09;Agents - Guided tour 中我初步介绍了下如何将一个函数或一个类声明成 smolagents 的工具…这篇文章锁定官网教程中的 Tools-in-depth-guide 章节主要介绍了如何详细构造自己的Tools在之前的博文 smolagents学习笔记系列二Agents - Guided tour 中我初步介绍了下如何将一个函数或一个类声明成 smolagents 的工具那么这篇文章将对这部分内容进一步深入。
官网链接https://huggingface.co/docs/smolagents/v1.9.2/en/tutorials/tools What is a tool, and how to build one?
Tool是Agent系统在LLM中使用最广泛的东西根据smolagents框架的要求想要使用tool必须对以下内容进行定义
name工具名最好是能直接表述工具功能的名字description功能描述对这个工具功能的详细表述input types and descriptions输入类型与描述output type输出类型
官网提供了一个将类包装成工具的示例如果是想通过类的方式定义工具的话需要继承smolagents Tool并且要重写 forward 这个函数目前可以粗略地认为这个函数就是最终执行的函数。该工具的作用是找到 HuggingFace 仓库中以 downloads 为分类指定 task 功能的模型返回最受欢迎的模型信息。
但是直接运行看不到任何结果需要在后面加一句输出内容这里我让这个工具对象执行了 text-classification
from smolagents import Toolclass HFModelDOwnloadsTool(Tool):name model_download_counterdescription This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub.It returns the name of the checkpoint.inputs {task: {type: string,description: the task category (such as text-classification, depth-estimation, etc),}}output_type stringdef forward(self, task:str):from huggingface_hub import list_modelsmodel next(iter(list_models(filtertask, sortdownloads, direction-1)))return model.idmodel_download_tool HFModelDOwnloadsTool()print(model_download_tool.forward(text-classification))运行
$ (LLM) ~/Desktop/LLM $ python demo.py
cross-encoder/ms-marco-MiniLM-L-6-v2如果你在运行时报错下面的错
huggingface_hub.errors.HfHubHTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/api/models?filtertext-classificationsortdownloadsdirection-1需要在bash中设置环境变量
$ export HF_TOKEN你之前的token码Import a Space as a tool
smolagents提供了另一种方式允许你通过 Tool.from_space 这个函数从HuggingFace Hub中直接导入开源的工具官网的示例用了 FLUX.1-dev 作为示例如果你直接运行仍然是无法运行的这个时候需要通过你的 HuggingFace 账户登录 FLUX.1-dev 的仓库https://huggingface.co/black-forest-labs/FLUX.1-dev
【注意】如果你没有登录账号下图中红框是不会出现的。
登录之后会有这么一个页面点击 Agree and access repository 按钮授权使用。
对示例代码进行修改以显示生成的图像并且需要将 FLUX.1-schnell 改为 FLUX.1-dev
from smolagents import Tool
from PIL import Image
import numpy as np
import cv2image_generation_tool Tool.from_space(black-forest-labs/FLUX.1-dev,nameimage_generator,descriptionGenerate an image from a prompt,api_name/infer
)response image_generation_tool(A sunny beach)
print(response)image Image.open(response)
image np.array(image)
image cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # PIL 是 RGBOpenCV 需要 BGRcv2.imshow(Generated Image, image)
cv2.waitKey(0)
cv2.destroyAllWindows()上面代码中的 response 返回的是你 本地计算机的一个路径比如我的是下面内容从 /private/var 开始的一长串内容。
运行
$ python demo
Loaded as API: https://black-forest-labs-flux-1-dev.hf.space ✔
/private/var/folders/zk/vtzyjvmx7f16zrstmmd3t27w0000gn/T/gradio/9026dfa2d826f1dd7a9c5089e051f2bf775bd3e972b298c9d1801a57f2b68981/image.webp【注意】这个可能需要多运行几次因为涉及到远程GPU资源如果申请GPU超时了就会返回错误但是如果你运行次数太多则会报下面的错误提示你GPU使用次数超限需要你升级到Pro账户。
gradio_client.exceptions.AppError: The upstream Gradio app has raised an exception: You have exceeded your free GPU quota (75s requested vs. 73s left). a stylewhite-space: nowrap;text-underline-offset: 2px;color: var(--body-text-color) hrefhttps://huggingface.co/settings/billing/subscriptionSubscribe to Pro/a to get 5x more daily usage quota.【Tips】如果你想直接使用这个图像生成工具可以在浏览器中直接访问网站https://black-forest-labs-flux-1-dev.hf.space这个是 FLUX.1 提供的一个网页在输入你的prompt后点击 run 即可根据你的提示词生成图像这个过程是逐步得到的因此刚开始你会看到一篇马赛克稍微等待一小会儿即可得到最终结果。 之后官网提到了可以将这个工具作为Agent的输入先让 Qwen2.5-Coder 对提示词进行改进然后再调用 FLUX.1 生成图像。
from smolagents import CodeAgent, HfApiModel
from smolagents import Toolimage_generation_tool Tool.from_space(black-forest-labs/FLUX.1-schnell,nameimage_generator,descriptionGenerate an image from a prompt
)model HfApiModel(Qwen/Qwen2.5-Coder-32B-Instruct)
agent CodeAgent(tools[image_generation_tool], modelmodel)response agent.run(Improve this prompt, then generate an image of it., additional_args{user_prompt: A rabbit wearing a space suit}
)print(response)【注】因为我当天的GPU资源用完了后续恢复后我会补上相应的内容。 Use LangChain tools
在使用 LangChain 这个工具之前需要安装一些依赖
$ pip install -U langchain-community
$ pip install langchain google-search-results -q但是很遗憾 官网的demo仍然无法直接运行因为你需要申请一个 Serp API Key
登录Serp官网https://serpapi.com/users/welcome绑定/注册账号、在邮箱中验证、绑定中国区手机号并获得验证码白嫖一个免费的API 在获得 Serp API Key 之后需要在环境变量中注册这个Key值
$ export SERPAPI_API_KEY你的Serp API Key对示例代码进行稍微修改添加model对象后运行代码
from smolagents import Tool, CodeAgent, HfApiModel
from langchain.agents import load_toolsmodel HfApiModel()search_tool Tool.from_langchain(load_tools([serpapi])[0])agent CodeAgent(tools[search_tool], modelmodel)agent.run(How many more blocks (also denoted as layers) are in BERT base encoder compared to the encoder from the architecture proposed in Attention is All You Need?)运行结果和之前文章中提到的一样在默认状态下调用的是 Qwen/Qwen2.5-Coder-32B-Instruct 模型 Manage your agent’s toolbox
官方提供了一个基于 HuggingFace Hub 的工具箱管理管理功能但这个功能需要在你已经将工具上传到 Hub 的 Spaces 前提下使用对于大多数人而言不需要将自己写的工具上传上去因此这里就放上示例我也没有运行测试
from smolagents import HfApiModel
from smolagents import CodeAgent, Tool, load_toolmodel_download_tool load_tool({your_username}/hf-model-downloads,trust_remote_codeTrue
)model HfApiModel(Qwen/Qwen2.5-Coder-32B-Instruct)agent CodeAgent(tools[], modelmodel, add_base_toolsTrue)
agent.tools[model_download_tool.name] model_download_tool如果你对大家开源的 Tools 感兴趣可以进入 Spaces 网页 https://huggingface.co/spaces 去查看有哪些可用的工具网页向下拉就可以看到上面我们使用的 FLUX.1-dev 这个工具不同的工具有不同的授权和使用方式需要自己去阅读感兴趣的工具如何使用。 官方demo中其实还介绍了使用 slug 和 mcp 的方式调用工具但这两种方式我没有用过如果后续需要的话我会进行补充。