การสร้าง Agent พร้อม Tools

AI Agents ด้วย Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

การสร้าง Code Agent (ไม่มี Tools)

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
AI Agents ด้วย Hugging Face smolagents

ทำไมต้องใช้ Tools กับ Code Agents?

Agent ที่กำหนดไว้สามารถแก้ปัญหาได้หลากหลายโดยใช้:

  • โมเดล LLM
  • โค้ด Python

แต่บางครั้งก็ต้องการข้อมูลจากภายนอกด้วย:

  • ตัวอย่าง: ข้อมูลเว็บแบบเรียลไทม์

นี่คือที่มาของ tools!

AI Agents ด้วย Hugging Face smolagents

การเพิ่ม Web Search Tool

from smolagents import CodeAgent, InferenceClientModel, WebSearchTool

agent = CodeAgent(
    model=InferenceClientModel(),
    tools=[WebSearchTool()]
)
AI Agents ด้วย Hugging Face smolagents

ผลลัพธ์ของ Code Agent พร้อม Web Search Tool

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
AI Agents ด้วย Hugging Face smolagents

Tools ในตัว (จำแนกตามหมวดหมู่)

หมวดหมู่ Tools
การค้นหาข้อมูล ApiWebSearchTool, DuckDuckGoSearchTool, GoogleSearchTool, WebSearchTool, WikipediaSearchTool
การโต้ตอบกับเว็บ VisitWebpageTool
การรันโค้ด PythonInterpreterTool
การโต้ตอบกับผู้ใช้ UserInputTool
การประมวลผลเสียง SpeechToTextTool
การควบคุม Workflow FinalAnswerTool
1 https://huggingface.co/docs/smolagents/main/en/reference/default_tools
AI Agents ด้วย Hugging Face smolagents

Tools จาก Hugging Face Hub

AI Agents ด้วย Hugging Face smolagents

การใช้ Community Tools จาก 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
AI Agents ด้วย Hugging Face smolagents

มาฝึกกันเถอะ!

AI Agents ด้วย Hugging Face smolagents

Preparing Video For Download...