页面简介
We’re on a journey to advance and democratize artificial intelligence through open source and open science.
正文摘录
smolagents documentation Human-in-the-Loop: Customize Agent Plan Interactively smolagents 🏡 View all docsAWS Trainium & InferentiaAccelerateBitsandbytesCLIChat UIDataset viewerDatasetsDeploying on AWSDiffusersGoogle CloudGoogle TPUsGradioHubHub Python LibraryHuggingface.jsInference Endpoints (dedicated)Inference ProvidersKernelsLeRobotLeaderboardsLightevalMicrosoft AzureOpenEnvOptimumPEFTReachy MiniSafetensorsSentence TransformersTRLTasksText Embeddings InferenceTokenizersTrackioTransformersTransformers.jsXetsmolagentstimm Search documentation mainv1.26.0v1.25.0v1.24.0v1.23.0v1.22.0v1.21.2v1.20.0v1.19.0v1.18.0v1.17.0v1.16.1v1.15.0v1.14.0v1.13.0v1.12.0v1.11.0v1.10.0v1.9.2v1.8.1v1.7.0v1.6.0v1.5.1v1.4.1v1.3.0v1.2.2v1.1.0v0.1.3 ENHIKOZH Join the Hugging Face community and get access to the augmented documentation experience Collaborate on models, datasets and Spaces Faster examples with accelerated inference Switch between documentation themes Sign Up to get started Copy page Human-in-the-Loop: Customize Agent Plan Interactively This page demonstrates advanced usage of the smolagents library, with a special focus on Human-in-the-Loop (HITL) approaches for interactive plan creation, user-driven plan modification, and memory preservation in agentic workflows. The example is based on the code in examples/plan_customization/plan_customization.py. Overview This example teaches you how to implement Human-in-the-Loop strategies to: Interrupt agent execution after a plan is created (using step callbacks) Allow users to review and modify the agent’s plan before execution (Human-in-the-Loop) Resume execution while preserving the agent’s memory Dynamically update plans based on user feedback, keeping the human in control Key Concepts Step Callbacks for Plan Interruption The agent is configured to pause after creating a plan. This is achieved by registering a step callback for the PlanningStep: Copied agent = CodeAgent( model=InferenceClientModel(), tools=[DuckDuckGoSearchTool()], planning_interval=5, # Plan every 5 steps step_callbacks={PlanningStep: interrupt_after_plan}, max_steps=10, verbosity_level=1 ) Human-in-the-Loop: Interactive Plan Review and Modification When the agent creates a plan, the callback displays it and prompts the human user to: Approve the plan Modify the plan Cancel execution Example interaction: Copied ============================================================ 🤖 AGENT PLAN CREATED ============================================================ 1. Search for recent AI developments 2. Analyze the top results 3. Summarize the 3 most significant breakthroughs 4. Include sources for each breakthrough ============================================================ Choose an option: 1. Approve plan 2. Modify plan 3. Cancel Your choice (1-3): This Human-in-the-Loop step enables a human to intervene and review or modify the plan before execution continues, and ensures that the agent’s actions align with human intent. If the user chooses to modify, they can edit the plan directly. The updated plan is then used for subsequent execution steps. Memory Preservation and Resuming Execution By running the agent with reset=False, all previous steps and memory are preserved. This allows you to resume execution after an interruption or plan modification: Copied # First run (may be interrupted) agent.run(task, reset=True) # Resume with preserved memory agent.run(task, reset=False) Inspecting Agent Memory You can inspect the agent’s memory to see all steps taken so far: Copied print(f"Current memory contains {len(agent.memory.steps)} steps:") for i, step in enumerate(agent.memory.steps): step_type = type(step).__name__ print(f" {i+1}. {step_type}") Example Human-in-the-Loop Workflow Agent starts with a complex task Planning step is created and execution pauses for human review Human reviews and optionally modifies the plan (Human-in-the-Loop) Execution resumes with the approved/modified plan All steps are preserved for future runs, maintaining transparency and control Error Handling The example includes error handling for: User cancellation Plan modification errors Resume execution failures Requirements smolagents library DuckDuckGoSearchTool (included with smolagents) InferenceClientModel (requires HuggingFace API token) Educational Value This example demonstrates: Step callback implementation for custom agent behavior Memory management in multi-step agents User interaction patterns in agentic systems Plan modification techniques for dynamic agent control Error handling in interactive agent systems For the full code, see examples/plan_customization. Update on GitHub ←Using different models Async Applications with Agents→