拥抱Face的Smolagents:一个简化的Python图书馆,用于构建AI代理
这篇博客文章介绍了Smolagents,这是一个来自拥抱面孔的新python库,简化了AI代理开发。我们将探索它的好处并浏览演示项目。
什么是Smolagents?
拥抱的面孔将Smolagents描述为一个简单的库,使语言模型代理。 但是为什么要创建代理需要专门的库?
>代理通过使用定义的工具包与环境,计划和执行操作来动态求解任务。 虽然从头开始构建这些代理并非不可能,但它需要开发许多组件以确保有效的资源使用(避免过度的API调用和执行时间)。 代理框架简化了此过程。对AI代理框架的常见批评包括过度的抽象层(导致僵化和调试困难)以及专注于刚性工作流而不是动态的协作。 Smolagents解决了这些问题:
>最小抽象层。
每日论文:跟上最近的研究的宝贵资源。
设置Smolagents
安装很简单:
需要一个拥抱的脸代币。
构建自定义工具
> Smolagents提供内置工具(例如Duckduckgosearchtool),而创建自定义工具同样简单。 我们的演示使用四个工具:
pip install smolagents
>
:使用其标题获得纸张ID。
get_hugging_face_top_daily_paper
:读取已下载的PDF文件。get_paper_id_by_title
有效的工具设计对于代理成功至关重要。 确保清晰度:>工具示例:get_hugging_face_top_daily_paper
pip install smolagents
>,get_paper_id_by_title
,download_paper_by_id
)是类似定义的(分别使用read_pdf_file
,huggingface_hub
和arxiv
),遵循相同的最佳实践。
pypdf
运行代理
我们将使用QWEN2.5-CODER-32B-INSTRUCT模型(免费使用):
代理商的分步输出演示了其工具使用情况。 (此处将包括第0、1、2和3步骤中代理输出的屏幕截图,显示代理的过程和最终摘要)。
from smolagents import tool import requests from bs4 import BeautifulSoup import json @tool def get_hugging_face_top_daily_paper() -> str: """ Retrieves the most upvoted paper from Hugging Face daily papers. Returns the paper's title. """ try: url = "<https:>" # URL to Hugging Face Daily Papers response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.content, "html.parser") containers = soup.find_all('div', class_='SVELTE_HYDRATER contents') top_paper = "" for container in containers: data_props = container.get('data-props', '') if data_props: try: json_data = json.loads(data_props.replace('"', '"')) if 'dailyPapers' in json_data: top_paper = json_data['dailyPapers'][0]['title'] except json.JSONDecodeError: continue return top_paper except requests.exceptions.RequestException as e: print(f"Error fetching HTML: {e}") return None</https:>
结论
Smolagents以上是拥抱面孔的smolagents:指南的详细内容。更多信息请关注PHP中文网其他相关文章!