smolagents:Hugging Face 開源的Agent框架,用代碼驅(qū)動(dòng) Agent 的新思路 精華
近日,Hugging Face 最近開源的一個(gè)Agent項(xiàng)目:smolagents。相較于其它框架,它的理念和實(shí)現(xiàn)都比較簡單?;趐ython開發(fā),核心設(shè)計(jì)理念是 “少即是多”。相比市面上動(dòng)輒幾萬行代碼的 Agent 框架,它保持了極簡的風(fēng)格,核心代碼僅有數(shù)千行,但功能卻毫不遜色。Hugging Face 團(tuán)隊(duì)希望通過這種方式,降低 Agent 開發(fā)的門檻,讓更多開發(fā)者能夠快速上手。
設(shè)計(jì)亮點(diǎn)
smolagents 的最大亮點(diǎn)在于其對(duì)“代碼 Agent” 的原生支持。這意味著 Agent 的行為將直接通過 Python 代碼來表達(dá),而非傳統(tǒng)的 JSON 或者文本指令。這種設(shè)計(jì)思路有以下幾點(diǎn)優(yōu)勢(shì):
- 執(zhí)行效率更高:代碼 Agent 可以減少 LLM 的調(diào)用次數(shù),從而提高整體的執(zhí)行效率。
- 表達(dá)能力更強(qiáng):代碼本身就是一種清晰、簡潔的表達(dá)方式,更適合描述復(fù)雜的 Agent 行為。
- 復(fù)用性更好:代碼的模塊化設(shè)計(jì),能夠更好地復(fù)用和擴(kuò)展 Agent 的功能。
- 避免 JSON 的復(fù)雜性:省去了 JSON 結(jié)構(gòu)定義、解析等環(huán)節(jié),讓 Agent 開發(fā)回歸本質(zhì)。
關(guān)鍵特性解讀:
- 極簡架構(gòu):核心代碼簡潔,易于理解和定制,避免了不必要的框架抽象。
- 安全沙箱:集成 E2B 等沙箱環(huán)境,確保代碼執(zhí)行的安全性,降低潛在的安全風(fēng)險(xiǎn)。
- Hub 集成:無縫對(duì)接 Hugging Face Hub,方便工具和模型的共享與復(fù)用。
- 模型兼容性:支持 Hugging Face Hub 上的開源模型,以及 OpenAI、Anthropic 等主流 LLM。
代碼示例
smolagents 的上手門檻非常低,以下是一個(gè)簡單的代碼示例:
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")
可以看到,開發(fā)者只需要幾行代碼,就能創(chuàng)建一個(gè)可以執(zhí)行網(wǎng)絡(luò)搜索任務(wù)的 Agent。
更復(fù)雜的例子:
from typing import Optional
from smolagents import CodeAgent, HfApiModel, tool
@tool
def get_travel_duration(start_location: str, destination_location: str, departure_time: Optional[int] = None) -> str:
"""Gets the travel time in car between two places.
Args:
start_location: the place from which you start your ride
destination_location: the place of arrival
departure_time: the departure time, provide only a `datetime.datetime` if you want to specify this
"""
import googlemaps # All imports are placed within the function, to allow for sharing to Hub.
import os
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
if departure_time is None:
from datetime import datetime
departure_time = datetime(2025, 1, 6, 11, 0)
directions_result = gmaps.directions(
start_location,
destination_location,
mode="transit",
departure_time=departure_time
)
return directions_result[0]["legs"][0]["duration"]["text"]
agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"])
agent.run("Can you give me a nice one-day trip around Paris with a few locations and the times? Could be in the city or outside, but should fit in one day. I'm travelling only via public transportation.")
開源模型的潛力
smolagents 的實(shí)驗(yàn)結(jié)果表明,在一些復(fù)雜任務(wù)中,基于開源模型構(gòu)建的代碼 Agent,已經(jīng)展現(xiàn)出了不俗的性能,甚至可以和一些閉源模型相媲美。這對(duì)開源 Agent 技術(shù)的發(fā)展無疑是一個(gè)利好。
總結(jié)
Hugging Face 向來對(duì)開發(fā)者用戶理解深入,加上它社區(qū)的優(yōu)勢(shì),它發(fā)布的很多框架都能夠獲得不錯(cuò)的反響,smolagents 是 Hugging Face 在 AI Agent 領(lǐng)域的一次嘗試,上線沒幾天就已經(jīng)4k的星標(biāo),建議大家也使用使用,一起交流使用心得和體驗(yàn)。
項(xiàng)目地址:https://github.com/huggingface/smolagents
本文轉(zhuǎn)載自 ??AI工程化??,作者: ully
