智慧體課程文件

為您的智慧體構建和整合工具

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

為您的智慧體構建和整合工具

在本節中,我們將授予阿爾弗雷德訪問網路的許可權,讓他能夠找到最新的新聞和全球動態。此外,他將能夠訪問天氣資料和 Hugging Face Hub 模型下載統計資料,以便他能夠就新話題進行相關對話。

讓您的智慧體訪問網路

請記住,我們希望阿爾弗雷德以一個真正博學多才的主人的身份出現,對世界有著深刻的瞭解。

為此,我們需要確保阿爾弗雷德能夠獲取世界的最新新聞和資訊。

讓我們首先為阿爾弗雷德建立一個網頁搜尋工具!

smolagents
llama-index
langgraph
from smolagents import DuckDuckGoSearchTool

# Initialize the DuckDuckGo search tool
search_tool = DuckDuckGoSearchTool()

# Example usage
results = search_tool("Who's the current President of France?")
print(results)

預期輸出

The current President of France in Emmanuel Macron.

建立一個自定義工具用於天氣資訊以安排煙花表演

一場完美的盛會應該在晴朗的天空下燃放煙花,我們需要確保煙花不會因惡劣天氣而取消。

讓我們建立一個自定義工具,可用於呼叫外部天氣 API 並獲取給定位置的天氣資訊。

為了簡單起見,我們在此示例中使用一個虛擬天氣 API。如果您想使用真實的天氣 API,您可以實現一個天氣工具,像第一單元中那樣使用 OpenWeatherMap API。
smolagents
llama-index
langgraph
from smolagents import Tool
import random

class WeatherInfoTool(Tool):
    name = "weather_info"
    description = "Fetches dummy weather information for a given location."
    inputs = {
        "location": {
            "type": "string",
            "description": "The location to get weather information for."
        }
    }
    output_type = "string"

    def forward(self, location: str):
        # Dummy weather data
        weather_conditions = [
            {"condition": "Rainy", "temp_c": 15},
            {"condition": "Clear", "temp_c": 25},
            {"condition": "Windy", "temp_c": 20}
        ]
        # Randomly select a weather condition
        data = random.choice(weather_conditions)
        return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"

# Initialize the tool
weather_info_tool = WeatherInfoTool()

為有影響力的 AI 開發者建立 Hub 統計工具

出席晚會的都是 AI 界的知名人物。阿爾弗雷德希望透過討論他們最受歡迎的模型、資料集和空間來給他們留下深刻印象。我們將建立一個工具,根據使用者名稱從 Hugging Face Hub 獲取模型統計資料。

smolagents
llama-index
langgraph
from smolagents import Tool
from huggingface_hub import list_models

class HubStatsTool(Tool):
    name = "hub_stats"
    description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
    inputs = {
        "author": {
            "type": "string",
            "description": "The username of the model author/organization to find models from."
        }
    }
    output_type = "string"

    def forward(self, author: str):
        try:
            # List models from the specified author, sorted by downloads
            models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
            
            if models:
                model = models[0]
                return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
            else:
                return f"No models found for author {author}."
        except Exception as e:
            return f"Error fetching models for {author}: {str(e)}"

# Initialize the tool
hub_stats_tool = HubStatsTool()

# Example usage
print(hub_stats_tool("facebook")) # Example: Get the most downloaded model by Facebook

預期輸出

The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads.

有了 Hub 統計工具,阿爾弗雷德現在可以透過討論他們最受歡迎的模型來給有影響力的 AI 開發者留下深刻印象。

將工具與阿爾弗雷德整合

現在我們擁有了所有的工具,讓我們將它們整合到阿爾弗雷德的智慧體中

smolagents
llama-index
langgraph
from smolagents import CodeAgent, InferenceClientModel

# Initialize the Hugging Face model
model = InferenceClientModel()

# Create Alfred with all the tools
alfred = CodeAgent(
    tools=[search_tool, weather_info_tool, hub_stats_tool], 
    model=model
)

# Example query Alfred might receive during the gala
response = alfred.run("What is Facebook and what's their most popular model?")

print("🎩 Alfred's Response:")
print(response)

預期輸出

🎩 Alfred's Response:
Facebook is a social networking website where users can connect, share information, and interact with others. The most downloaded model by Facebook on the Hugging Face Hub is ESMFold_v1.

結論

透過整合這些工具,阿爾弗雷德現在能夠處理各種任務,從網路搜尋到天氣更新和模型統計。這確保他在晚會上保持資訊最靈通和最引人入勝的主人身份。

嘗試實現一個可以獲取特定主題最新新聞的工具。

完成後,在 tools.py 檔案中實現您的自定義工具。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.