Tworzenie agenta z narzędziami

Agenty AI z Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Tworzenie agenta kodu (bez narzędzi)

from smolagents import CodeAgent, InferenceClientModel

agent = CodeAgent(
    tools=[],
    model=InferenceClientModel()
)

agent.run("Calculate the average of the list [23, 45, 67, 89]")
Executing parsed code:
  numbers = [23, 45, 67, 89]                                                                               
  average = sum(numbers) / len(numbers)                                                                            
  final_answer(average)                                                                                             
Final answer: 56.0
[Step 1: Duration 4.14 seconds| Input tokens: 1,900 | Output tokens: 109]
56.0
Agenty AI z Hugging Face smolagents

Dlaczego używać narzędzi z agentami kodu?

Zdefiniowany agent może już rozwiązywać wiele zadań, korzystając z:

  • Modelu LLM
  • Kodu Python

Może jednak potrzebować dostępu do zewnętrznych informacji:

  • Przykład: dane z sieci w czasie rzeczywistym

Tu właśnie przydają się narzędzia!

Agenty AI z Hugging Face smolagents

Dodawanie narzędzia do wyszukiwania w sieci

from smolagents import CodeAgent, InferenceClientModel, WebSearchTool

agent = CodeAgent(
    model=InferenceClientModel(),
    tools=[WebSearchTool()]
)
Agenty AI z Hugging Face smolagents

Wynik agenta kodu z narzędziem do wyszukiwania w sieci

agent.run("What's the tallest building in the world right now?")
Executing parsed code:
  tallest_building_info = web_search("tallest building in the world 2023")                                
  print(tallest_building_info)                                                                                

# Search results omitted for brevity...

Executing parsed code: 
  final_answer("Burj Khalifa, Dubai, 828 meters")                                                              
Final answer: Burj Khalifa, Dubai, 828 meters
[Step 2: Duration 2.97 seconds| Input tokens: 5,078 | Output tokens: 153]
Burj Khalifa, Dubai, 828 meters
Agenty AI z Hugging Face smolagents

Wbudowane narzędzia (według kategorii)

Kategoria Narzędzia
Pobieranie informacji ApiWebSearchTool, DuckDuckGoSearchTool, GoogleSearchTool, WebSearchTool, WikipediaSearchTool
Interakcja z siecią VisitWebpageTool
Wykonywanie kodu PythonInterpreterTool
Interakcja z użytkownikiem UserInputTool
Przetwarzanie mowy SpeechToTextTool
Sterowanie przepływem FinalAnswerTool
1 https://huggingface.co/docs/smolagents/main/en/reference/default_tools
Agenty AI z Hugging Face smolagents

Narzędzia z Hugging Face Hub

Agenty AI z Hugging Face smolagents

Korzystanie z narzędzi społeczności Hugging Face

from smolagents import load_tool

# Load remote tool from Hugging Face
model_downloads_tool = load_tool(
    repo_id="example-repo/hf-model-downloads",
    trust_remote_code=True
)

# Create agent with remote + built-in tools
agent = CodeAgent(
    tools=[model_downloads_tool, WebSearchTool()],
    model=InferenceClientModel()
)

agent.run("Find the most downloaded image classification model on Hugging Face")
google/vit-base-patch16-224-in21k
Agenty AI z Hugging Face smolagents

Czas na ćwiczenia!

Agenty AI z Hugging Face smolagents

Preparing Video For Download...