टूल्स के साथ एक एजेंट बनाना

Hugging Face smolagents के साथ AI Agents

Adel Nehme

VP of AI Curriculum, DataCamp

कोड एजेंट बनाना (नो टूल्स)

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
Hugging Face smolagents के साथ AI Agents

कोड एजेंट्स के साथ टूल्स क्यों?

जो एजेंट हमने परिभाषित किया है, वह पहले से कई कार्य हल कर सकता है:

  • एक LLM मॉडल
  • Python कोड

लेकिन इसे बाहरी जानकारी की भी ज़रूरत पड़ सकती है:

  • उदाहरण: लाइव वेब डेटा

यहीं टूल्स काम आते हैं!

Hugging Face smolagents के साथ AI Agents

वेब सर्च टूल जोड़ना

from smolagents import CodeAgent, InferenceClientModel, WebSearchTool

agent = CodeAgent(
    model=InferenceClientModel(),
    tools=[WebSearchTool()]
)
Hugging Face smolagents के साथ AI Agents

वेब सर्च टूल के साथ कोड एजेंट आउटपुट

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
Hugging Face smolagents के साथ AI Agents

बिल्ट-इन टूल्स (श्रेणी अनुसार)

Category Tools
Information Retrieval ApiWebSearchTool, DuckDuckGoSearchTool, GoogleSearchTool, WebSearchTool, WikipediaSearchTool
Web Interaction VisitWebpageTool
Code Execution PythonInterpreterTool
User Interaction UserInputTool
Speech Processing SpeechToTextTool
Workflow Control FinalAnswerTool
1 https://huggingface.co/docs/smolagents/main/en/reference/default_tools
Hugging Face smolagents के साथ AI Agents

Hugging Face Hub से टूल्स

Hugging Face smolagents के साथ AI Agents

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
Hugging Face smolagents के साथ AI Agents

अभ्यास करते हैं!

Hugging Face smolagents के साथ AI Agents

Preparing Video For Download...