Hugging Face smolagents के साथ AI Agents
Adel Nehme
VP of AI Curriculum, DataCamp



agent = CodeAgent(
tools=[document_search_tool],
model=model,
planning_interval=3,
max_steps=12
)
planning_interval=3: एजेंट हर 3 स्टेप के बाद रुककर सोचता है."यूरोप में 2 सप्ताह की फैमिली वेकेशन प्लान करें जिसमें बड़ों के लिए ऐतिहासिक स्थल और बच्चों के लिए मजेदार गतिविधियाँ हों, बजट $5000 के भीतर।"
[Step 1] "Paris hotels" सर्च करें -> लग्जरी होटल मिले (~$4000 कुल)
[Step 2] "Paris attractions for families" सर्च करें -> Eiffel Tower, Louvre, थीम पार्क टिकट मिले
[Step 3] विराम लेकर पुनर्विचार (planning interval) होटल बहुत महंगे -> बजट पार. मुझे सस्ते विकल्प और बच्चों के अनुकूल गतिविधियाँ देखनी चाहिए.
[Step 4] "affordable family hotels in Europe" सर्च करें -> कई शहरों में मिड-रेंज विकल्प मिले

def callback_function(agent_step, agent):
# Do something with the agent step or the agent itself
pass
agent_step: स्टेप का विवरण (प्लान, स्टेप नंबर, आदि) agent: पूरा एजेंट ऑब्जेक्ट (state + methods)def planning_callback(agent_step, agent):
print("AGENT PLANNING")
print("=" * 50)
print(agent_step.plan[:300])
if len(agent_step.plan) > 300:
print("\n... (plan truncated)")
print("=" * 50)
AGENT PLANNING
==================================================
सस्ते होटल + बच्चों की गतिविधियाँ सर्च करें
फिर संतुलित यात्रा-योजना बनाएँ...
... (plan truncated)
==================================================
def action_callback(agent_step, agent):
step_num = agent_step.step_number
print(f"Step {step_num}: Taking action")
if agent_step.is_final_answer:
total_tokens = agent_step.token_usage.total_tokens
print(f"Total tokens used: {total_tokens}")
Step 2: कार्रवाई कर रहे हैं!
Step 3: कार्रवाई कर रहे हैं!
Step 4: कार्रवाई कर रहे हैं!
Total tokens used: 4,218
from smolagents import ActionStep, PlanningStep
agent = CodeAgent(
tools=[document_search_tool],
model=model,
step_callbacks={PlanningStep: planning_callback, ActionStep: action_callback}
)
Hugging Face smolagents के साथ AI Agents