Confirmation Advanced

Human-in-the-Loop: Adding User Confirmation to Tool Calls.

1"""
2Confirmation Advanced
3=============================
4
5Human-in-the-Loop: Adding User Confirmation to Tool Calls.
6"""
7
8import json
9
10import httpx
11from kern.agent import Agent
12from kern.db.sqlite import SqliteDb
13from kern.models.openai import OpenAIResponses
14from kern.tools import tool
15from kern.tools.wikipedia import WikipediaTools
16from kern.utils import pprint
17from rich.console import Console
18from rich.prompt import Prompt
19
20console = Console()
21
22
23@tool(requires_confirmation=True)
24def get_top_hackernews_stories(num_stories: int) -> str:
25 """Fetch top stories from Hacker News.
26
27 Args:
28 num_stories (int): Number of stories to retrieve
29
30 Returns:
31 str: JSON string containing story details
32 """
33 # Fetch top story IDs
34 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
35 story_ids = response.json()
36
37 # Yield story details
38 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)
48
49
50# ---------------------------------------------------------------------------
51# Create Agent
52# ---------------------------------------------------------------------------
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)
62
63# ---------------------------------------------------------------------------
64# Run Agent
65# ---------------------------------------------------------------------------
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 confirmation
74 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 )
84
85 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()
91
92 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 repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/10_human_in_the_loop
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python confirmation_advanced.py