擁抱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中文網其他相關文章!