|
| 1 | +""" |
| 2 | +Example demonstrating the ask_agent functionality for getting sidebar replies |
| 3 | +from the agent for a running conversation. |
| 4 | +
|
| 5 | +This example shows how to use ask_agent() to get quick responses from the agent |
| 6 | +about the current conversation state without interrupting the main execution flow. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import threading |
| 11 | +import time |
| 12 | +from datetime import datetime |
| 13 | + |
| 14 | +from pydantic import SecretStr |
| 15 | + |
| 16 | +from openhands.sdk import ( |
| 17 | + LLM, |
| 18 | + Agent, |
| 19 | + Conversation, |
| 20 | +) |
| 21 | +from openhands.sdk.conversation import ConversationVisualizerBase |
| 22 | +from openhands.sdk.event import Event |
| 23 | +from openhands.sdk.tool import Tool |
| 24 | +from openhands.tools.file_editor import FileEditorTool |
| 25 | +from openhands.tools.task_tracker import TaskTrackerTool |
| 26 | +from openhands.tools.terminal import TerminalTool |
| 27 | + |
| 28 | + |
| 29 | +# Configure LLM |
| 30 | +api_key = os.getenv("LLM_API_KEY") |
| 31 | +assert api_key is not None, "LLM_API_KEY environment variable is not set." |
| 32 | +model = os.getenv("LLM_MODEL", "anthropic/claude-sonnet-4-5-20250929") |
| 33 | +base_url = os.getenv("LLM_BASE_URL") |
| 34 | +llm = LLM( |
| 35 | + usage_id="agent", |
| 36 | + model=model, |
| 37 | + base_url=base_url, |
| 38 | + api_key=SecretStr(api_key), |
| 39 | +) |
| 40 | + |
| 41 | +# Tools |
| 42 | +cwd = os.getcwd() |
| 43 | +tools = [ |
| 44 | + Tool(name=TerminalTool.name), |
| 45 | + Tool(name=FileEditorTool.name), |
| 46 | + Tool(name=TaskTrackerTool.name), |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +class MinimalVisualizer(ConversationVisualizerBase): |
| 51 | + """A minimal visualizer that print the raw events as they occur.""" |
| 52 | + |
| 53 | + count = 0 |
| 54 | + |
| 55 | + def on_event(self, event: Event) -> None: |
| 56 | + """Handle events for minimal progress visualization.""" |
| 57 | + print(f"\n\n[EVENT {self.count}] {type(event).__name__}") |
| 58 | + self.count += 1 |
| 59 | + |
| 60 | + |
| 61 | +# Agent |
| 62 | +agent = Agent(llm=llm, tools=tools) |
| 63 | +conversation = Conversation( |
| 64 | + agent=agent, workspace=cwd, visualizer=MinimalVisualizer, max_iteration_per_run=5 |
| 65 | +) |
| 66 | + |
| 67 | + |
| 68 | +def timestamp() -> str: |
| 69 | + return datetime.now().strftime("%H:%M:%S") |
| 70 | + |
| 71 | + |
| 72 | +print("=== Ask Agent Example ===") |
| 73 | +print("This example demonstrates asking questions during conversation execution") |
| 74 | + |
| 75 | +# Step 1: Build conversation context |
| 76 | +print(f"\n[{timestamp()}] Building conversation context...") |
| 77 | +conversation.send_message("Explore the current directory and describe the architecture") |
| 78 | + |
| 79 | +# Step 2: Start conversation in background thread |
| 80 | +print(f"[{timestamp()}] Starting conversation in background thread...") |
| 81 | +thread = threading.Thread(target=conversation.run) |
| 82 | +thread.start() |
| 83 | + |
| 84 | +# Give the agent time to start processing |
| 85 | +time.sleep(2) |
| 86 | + |
| 87 | +# Step 3: Use ask_agent while conversation is running |
| 88 | +print(f"\n[{timestamp()}] Using ask_agent while conversation is processing...") |
| 89 | + |
| 90 | +# Ask context-aware questions |
| 91 | +questions_and_responses = [] |
| 92 | + |
| 93 | +question_1 = "Summarize the activity so far in 1 sentence." |
| 94 | +print(f"\n[{timestamp()}] Asking: {question_1}") |
| 95 | +response1 = conversation.ask_agent(question_1) |
| 96 | +questions_and_responses.append((question_1, response1)) |
| 97 | +print(f"Response: {response1}") |
| 98 | + |
| 99 | +time.sleep(1) |
| 100 | + |
| 101 | +question_2 = "How's the progress?" |
| 102 | +print(f"\n[{timestamp()}] Asking: {question_2}") |
| 103 | +response2 = conversation.ask_agent(question_2) |
| 104 | +questions_and_responses.append((question_2, response2)) |
| 105 | +print(f"Response: {response2}") |
| 106 | + |
| 107 | +time.sleep(1) |
| 108 | + |
| 109 | +question_3 = "Have you finished running?" |
| 110 | +print(f"\n[{timestamp()}] {question_3}") |
| 111 | +response3 = conversation.ask_agent(question_3) |
| 112 | +questions_and_responses.append((question_3, response3)) |
| 113 | +print(f"Response: {response3}") |
| 114 | + |
| 115 | +# Step 4: Wait for conversation to complete |
| 116 | +print(f"\n[{timestamp()}] Waiting for conversation to complete...") |
| 117 | +thread.join() |
| 118 | + |
| 119 | +# Step 5: Verify conversation state wasn't affected |
| 120 | +final_event_count = len(conversation.state.events) |
| 121 | +# Step 6: Ask a final question after conversation completion |
| 122 | +print(f"\n[{timestamp()}] Asking final question after completion...") |
| 123 | +final_response = conversation.ask_agent( |
| 124 | + "Can you summarize what you accomplished in this conversation?" |
| 125 | +) |
| 126 | +print(f"Final response: {final_response}") |
| 127 | + |
| 128 | +# Step 7: Summary |
| 129 | +print("\n" + "=" * 60) |
| 130 | +print("SUMMARY OF ASK_AGENT DEMONSTRATION") |
| 131 | +print("=" * 60) |
| 132 | + |
| 133 | +print("\nQuestions and Responses:") |
| 134 | +for i, (question, response) in enumerate(questions_and_responses, 1): |
| 135 | + print(f"\n{i}. Q: {question}") |
| 136 | + print(f" A: {response[:100]}{'...' if len(response) > 100 else ''}") |
| 137 | + |
| 138 | +final_truncated = final_response[:100] + ("..." if len(final_response) > 100 else "") |
| 139 | +print(f"\nFinal Question Response: {final_truncated}") |
| 140 | + |
| 141 | +# Report cost |
| 142 | +cost = llm.metrics.accumulated_cost |
| 143 | +print(f"EXAMPLE_COST: {cost:.4f}") |
0 commit comments