Agentic User Input
Human-in-the-Loop: Allowing users to provide input externally.
1"""2Agentic User Input3=============================45Human-in-the-Loop: Allowing users to provide input externally.6"""78from typing import Any, Dict, List910from kern.agent import Agent11from kern.db.sqlite import SqliteDb12from kern.models.openai import OpenAIResponses13from kern.tools import Toolkit14from kern.tools.function import UserInputField15from kern.tools.user_control_flow import UserControlFlowTools16from kern.utils import pprint171819class EmailTools(Toolkit):20 def __init__(self, *args, **kwargs):21 super().__init__(22 name="EmailTools", tools=[self.send_email, self.get_emails], *args, **kwargs23 )2425 def send_email(self, subject: str, body: str, to_address: str) -> str:26 """Send an email to the given address with the given subject and body.2728 Args:29 subject (str): The subject of the email.30 body (str): The body of the email.31 to_address (str): The address to send the email to.32 """33 return f"Sent email to {to_address} with subject {subject} and body {body}"3435 def get_emails(self, date_from: str, date_to: str) -> list[dict[str, str]]:36 """Get all emails between the given dates.3738 Args:39 date_from (str): The start date (in YYYY-MM-DD format).40 date_to (str): The end date (in YYYY-MM-DD format).41 """42 return [43 {44 "subject": "Hello",45 "body": "Hello, world!",46 "to_address": "test@test.com",47 "date": date_from,48 },49 {50 "subject": "Random other email",51 "body": "This is a random other email",52 "to_address": "john@doe.com",53 "date": date_to,54 },55 ]565758# ---------------------------------------------------------------------------59# Create Agent60# ---------------------------------------------------------------------------61agent = Agent(62 model=OpenAIResponses(id="gpt-5-mini"),63 tools=[EmailTools(), UserControlFlowTools()],64 markdown=True,65 db=SqliteDb(db_file="tmp/agentic_user_input.db"),66)6768# ---------------------------------------------------------------------------69# Run Agent70# ---------------------------------------------------------------------------71if __name__ == "__main__":72 run_response = agent.run(73 "Send an email with the body 'What is the weather in Tokyo?'"74 )7576 # We use a while loop to continue the running until the agent is satisfied with the user input77 while run_response.is_paused:78 for requirement in run_response.active_requirements:79 if requirement.needs_user_input:80 input_schema: List[UserInputField] = requirement.user_input_schema # type: ignore8182 for field in input_schema:83 # Get user input for each field in the schema84 field_type = field.field_type # type: ignore85 field_description = field.description # type: ignore8687 # Display field information to the user88 print(f"\nField: {field.name}") # type: ignore89 print(f"Description: {field_description}")90 print(f"Type: {field_type}")9192 # Get user input93 if field.value is None: # type: ignore94 user_value = input(f"Please enter a value for {field.name}: ") # type: ignore95 else:96 print(f"Value: {field.value}") # type: ignore97 user_value = field.value # type: ignore9899 # Update the field value100 field.value = user_value # type: ignore101102 run_response = agent.continue_run(103 run_id=run_response.run_id,104 requirements=run_response.requirements,105 )106 if not run_response.is_paused:107 pprint.pprint_run_response(run_response)108 break109110 run_response = agent.run("Get me all my emails")111112 while run_response.is_paused:113 for requirement in run_response.active_requirements:114 if requirement.needs_user_input:115 input_schema: Dict[str, Any] = requirement.user_input_schema # type: ignore116117 for field in input_schema:118 # Get user input for each field in the schema119 field_type = field.field_type # type: ignore120 field_description = field.description # type: ignore121122 # Display field information to the user123 print(f"\nField: {field.name}") # type: ignore124 print(f"Description: {field_description}")125 print(f"Type: {field_type}")126127 # Get user input128 if field.value is None: # type: ignore129 user_value = input(f"Please enter a value for {field.name}: ") # type: ignore130 else:131 print(f"Value: {field.value}") # type: ignore132 user_value = field.value # type: ignore133134 # Update the field value135 field.value = user_value # type: ignore136137 run_response = agent.continue_run(138 run_id=run_response.run_id,139 requirements=run_response.requirements,140 )141142 if not run_response.is_paused:143 pprint.pprint_run_response(run_response)144 breakRun 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 agentic_user_input.py