Confirmation Advanced
Human-in-the-Loop: Adding User Confirmation to Tool Calls.
1"""2Confirmation Advanced3=============================45Human-in-the-Loop: Adding User Confirmation to Tool Calls.6"""78import json910import httpx11from kern.agent import Agent12from kern.db.sqlite import SqliteDb13from kern.models.openai import OpenAIResponses14from kern.tools import tool15from kern.tools.wikipedia import WikipediaTools16from kern.utils import pprint17from rich.console import Console18from rich.prompt import Prompt1920console = Console()212223@tool(requires_confirmation=True)24def get_top_hackernews_stories(num_stories: int) -> str:25 """Fetch top stories from Hacker News.2627 Args:28 num_stories (int): Number of stories to retrieve2930 Returns:31 str: JSON string containing story details32 """33 # Fetch top story IDs34 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")35 story_ids = response.json()3637 # Yield story details38 all_stories = []39 for story_id in story_ids[:num_stories]:40 story_response = httpx.get(41 f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"42 )43 story = story_response.json()44 if "text" in story:45 story.pop("text", None)46 all_stories.append(story)47 return json.dumps(all_stories)484950# ---------------------------------------------------------------------------51# Create Agent52# ---------------------------------------------------------------------------53agent = Agent(54 model=OpenAIResponses(id="gpt-5-mini"),55 tools=[56 get_top_hackernews_stories,57 WikipediaTools(requires_confirmation_tools=["search_wikipedia"]),58 ],59 markdown=True,60 db=SqliteDb(db_file="tmp/confirmation_required_multiple_tools.db"),61)6263# ---------------------------------------------------------------------------64# Run Agent65# ---------------------------------------------------------------------------66if __name__ == "__main__":67 run_response = agent.run(68 "Fetch 2 articles about the topic 'python'. You can choose which source to use, but only use one source."69 )70 while run_response.is_paused:71 for requirement in run_response.active_requirements:72 if requirement.needs_confirmation:73 # Ask for confirmation74 console.print(75 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."76 )77 message = (78 Prompt.ask(79 "Do you want to continue?", choices=["y", "n"], default="y"80 )81 .strip()82 .lower()83 )8485 if message == "n":86 requirement.reject(87 "This is not the right tool to use. Use the other tool!"88 )89 else:90 requirement.confirm()9192 run_response = agent.continue_run(93 run_id=run_response.run_id,94 requirements=run_response.requirements,95 )96 pprint.pprint_run_response(run_response)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/10_human_in_the_loop45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python confirmation_advanced.py