Crearea unui agent cu instrumente

Agenți AI cu Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Crearea unui agent de cod (fără instrumente)

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
Agenți AI cu Hugging Face smolagents

De ce să utilizăm instrumente cu agenți de cod?

Agentul definit poate rezolva deja multe sarcini folosind:

  • Un model LLM
  • Cod Python

Dar poate necesita și acces la informații externe:

  • Exemplu: date web în timp real

Aici intervin instrumentele!

Agenți AI cu Hugging Face smolagents

Adăugarea unui instrument de căutare web

from smolagents import CodeAgent, InferenceClientModel, WebSearchTool

agent = CodeAgent(
    model=InferenceClientModel(),
    tools=[WebSearchTool()]
)
Agenți AI cu Hugging Face smolagents

Rezultatul agentului de cod cu instrument de căutare web

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
Agenți AI cu Hugging Face smolagents

Instrumente integrate (pe categorii)

Categorie Instrumente
Recuperare informații ApiWebSearchTool, DuckDuckGoSearchTool, GoogleSearchTool, WebSearchTool, WikipediaSearchTool
Interacțiune web VisitWebpageTool
Execuție cod PythonInterpreterTool
Interacțiune utilizator UserInputTool
Procesare vorbire SpeechToTextTool
Control flux de lucru FinalAnswerTool
1 https://huggingface.co/docs/smolagents/main/en/reference/default_tools
Agenți AI cu Hugging Face smolagents

Instrumente din Hugging Face Hub

Agenți AI cu Hugging Face smolagents

Utilizarea instrumentelor comunitare din 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
Agenți AI cu Hugging Face smolagents

Să exersăm!

Agenți AI cu Hugging Face smolagents

Preparing Video For Download...