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

Hugging Face smolagents के साथ AI Agents

Adel Nehme

VP of AI Curriculum, DataCamp

Hugging Face smolagents के साथ AI Agents

कस्टम टूल्स के फायदे

  • विश्वसनीयता: एजेंट के अनुमान पर निर्भर होने की बजाय लॉजिक को स्पष्ट रूप से लिखें और टेस्ट करें
  • पुन: उपयोग: टूल्स को अलग प्रोजेक्ट्स और एजेंट्स में दोबारा उपयोग करें
  • संगति: हर रन में पूर्वानुमेय व्यवहार पाएँ (डिबगिंग के लिए बढ़िया)
  • नियंत्रित एक्सेस: वही एक्सपोज़ करें जो आप चाहें (files, APIs, databases, आदि)
Hugging Face smolagents के साथ AI Agents

परिदृश्य: आप एक रिटेल स्टोर चलाते हैं

  • इन्वेंटरी डेटा एक CSV फाइल में है (size, color, quantity, और price)
  • Code agents CSV पढ़ने का कोड लिख सकते हैं
  • पर डिफॉल्ट रूप से उन्हें फाइल एक्सेस नहीं होता
  • आपको फाइल एक्सेस को एक कस्टम टूल में रैप करना होगा

Hugging Face smolagents के साथ AI Agents

कस्टम टूल की संरचना

from smolagents import tool
import pandas as pd

@tool
def check_inventory(product_name: str) -> int:
    """
    Check the available quantity of a product in the inventory CSV.

    Args:
        product_name (str): The name of the product to look up.

    Returns:
        int: The quantity in stock. Returns 0 if the product is not found.
    """
    df = pd.read_csv("store_inventory.csv")
    match = df[df["product_name"] == product_name]
    stock_quantity = int(match.iloc[0]["quantity"]) if not match.empty else 0
    return stock_quantity
Hugging Face smolagents के साथ AI Agents

कस्टम टूल्स के लिए बेस्ट प्रैक्टिसेज

Hugging Face smolagents के साथ AI Agents

क्या हमारे पास कोई t-shirts स्टॉक में हैं?

Hugging Face smolagents के साथ AI Agents

अपने एजेंट में कस्टम टूल रजिस्टर करना

from smolagents import CodeAgent

agent = CodeAgent(
    tools=[check_inventory], # Add custom tool
    model=InferenceClientModel(),
    additional_authorized_imports=["pandas"]  # Allow external package
)

agent.run("Do we have any large t-shirts in stock?")
Yes, we have 8 large t-shirts in stock.
Hugging Face smolagents के साथ AI Agents

प्रोडक्शन प्रोजेक्ट्स में कस्टम टूल्स

Hugging Face smolagents के साथ AI Agents

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

Hugging Face smolagents के साथ AI Agents

Preparing Video For Download...