Spaces:
Sleeping
Sleeping
Commit ·
b15a97a
1
Parent(s): c06d66f
added verbose
Browse files- agent_framework/agent.py +39 -0
- examples/demo.py +12 -2
agent_framework/agent.py
CHANGED
|
@@ -34,12 +34,14 @@ class Agent:
|
|
| 34 |
max_steps: int = 10,
|
| 35 |
name: str = "agent",
|
| 36 |
output_type: Optional[Type[BaseModel]] = None,
|
|
|
|
| 37 |
):
|
| 38 |
self.model = model
|
| 39 |
self.instructions = instructions
|
| 40 |
self.max_steps = max_steps
|
| 41 |
self.name = name
|
| 42 |
self.output_type = output_type
|
|
|
|
| 43 |
self.tools = self._setup_tools(tools or [])
|
| 44 |
|
| 45 |
def _setup_tools(self, tools: List[BaseTool]) -> List[BaseTool]:
|
|
@@ -115,6 +117,11 @@ class Agent:
|
|
| 115 |
|
| 116 |
async def step(self, context: ExecutionContext):
|
| 117 |
"""Execute one step of the agent loop."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Check if we should enforce structured output
|
| 119 |
# Only enforce if: we have output_type AND the last event had tool results (meaning tools were used)
|
| 120 |
# This allows tool calls to happen first, then we enforce format for final answer
|
|
@@ -134,6 +141,11 @@ class Agent:
|
|
| 134 |
# Prepare LLM request - don't enforce output type to allow tool calls
|
| 135 |
llm_request = self._prepare_llm_request(context, enforce_output_type=should_enforce_output)
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
# Get LLM's decision
|
| 138 |
llm_response = await self.think(llm_request)
|
| 139 |
|
|
@@ -145,9 +157,21 @@ class Agent:
|
|
| 145 |
)
|
| 146 |
context.add_event(response_event)
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
# Execute tools if the LLM requested any
|
| 149 |
tool_calls = [c for c in llm_response.content if isinstance(c, ToolCall)]
|
| 150 |
if tool_calls:
|
|
|
|
|
|
|
| 151 |
tool_results = await self.act(context, tool_calls)
|
| 152 |
tool_event = Event(
|
| 153 |
execution_id=context.execution_id,
|
|
@@ -155,8 +179,20 @@ class Agent:
|
|
| 155 |
content=tool_results,
|
| 156 |
)
|
| 157 |
context.add_event(tool_event)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
elif self.output_type and not should_enforce_output:
|
| 159 |
# No tool calls but we didn't enforce output type - make one more call to get structured output
|
|
|
|
|
|
|
| 160 |
final_request = self._prepare_llm_request(context, enforce_output_type=True)
|
| 161 |
final_response = await self.think(final_request)
|
| 162 |
|
|
@@ -169,6 +205,9 @@ class Agent:
|
|
| 169 |
)
|
| 170 |
|
| 171 |
context.increment_step()
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
async def run(
|
| 174 |
self,
|
|
|
|
| 34 |
max_steps: int = 10,
|
| 35 |
name: str = "agent",
|
| 36 |
output_type: Optional[Type[BaseModel]] = None,
|
| 37 |
+
verbose: bool = False,
|
| 38 |
):
|
| 39 |
self.model = model
|
| 40 |
self.instructions = instructions
|
| 41 |
self.max_steps = max_steps
|
| 42 |
self.name = name
|
| 43 |
self.output_type = output_type
|
| 44 |
+
self.verbose = verbose
|
| 45 |
self.tools = self._setup_tools(tools or [])
|
| 46 |
|
| 47 |
def _setup_tools(self, tools: List[BaseTool]) -> List[BaseTool]:
|
|
|
|
| 117 |
|
| 118 |
async def step(self, context: ExecutionContext):
|
| 119 |
"""Execute one step of the agent loop."""
|
| 120 |
+
if self.verbose:
|
| 121 |
+
print(f"\n{'='*60}")
|
| 122 |
+
print(f"Step {context.current_step + 1} - Agent Thinking...")
|
| 123 |
+
print(f"{'='*60}")
|
| 124 |
+
|
| 125 |
# Check if we should enforce structured output
|
| 126 |
# Only enforce if: we have output_type AND the last event had tool results (meaning tools were used)
|
| 127 |
# This allows tool calls to happen first, then we enforce format for final answer
|
|
|
|
| 141 |
# Prepare LLM request - don't enforce output type to allow tool calls
|
| 142 |
llm_request = self._prepare_llm_request(context, enforce_output_type=should_enforce_output)
|
| 143 |
|
| 144 |
+
if self.verbose:
|
| 145 |
+
print(f"[SENDING] Request to LLM...")
|
| 146 |
+
if should_enforce_output:
|
| 147 |
+
print(f" (Enforcing structured output format)")
|
| 148 |
+
|
| 149 |
# Get LLM's decision
|
| 150 |
llm_response = await self.think(llm_request)
|
| 151 |
|
|
|
|
| 157 |
)
|
| 158 |
context.add_event(response_event)
|
| 159 |
|
| 160 |
+
# Show what the LLM responded with
|
| 161 |
+
if self.verbose:
|
| 162 |
+
for item in llm_response.content:
|
| 163 |
+
if isinstance(item, Message):
|
| 164 |
+
print(f"\n[AGENT RESPONSE]")
|
| 165 |
+
print(f" {item.content[:200]}{'...' if len(item.content) > 200 else ''}")
|
| 166 |
+
elif isinstance(item, ToolCall):
|
| 167 |
+
print(f"\n[TOOL CALL] {item.name}")
|
| 168 |
+
print(f" Arguments: {item.arguments}")
|
| 169 |
+
|
| 170 |
# Execute tools if the LLM requested any
|
| 171 |
tool_calls = [c for c in llm_response.content if isinstance(c, ToolCall)]
|
| 172 |
if tool_calls:
|
| 173 |
+
if self.verbose:
|
| 174 |
+
print(f"\n[EXECUTING] {len(tool_calls)} tool(s)...")
|
| 175 |
tool_results = await self.act(context, tool_calls)
|
| 176 |
tool_event = Event(
|
| 177 |
execution_id=context.execution_id,
|
|
|
|
| 179 |
content=tool_results,
|
| 180 |
)
|
| 181 |
context.add_event(tool_event)
|
| 182 |
+
|
| 183 |
+
if self.verbose:
|
| 184 |
+
for result in tool_results:
|
| 185 |
+
status_marker = "[SUCCESS]" if result.status == "success" else "[ERROR]"
|
| 186 |
+
print(f" {status_marker} {result.name}: {result.status}")
|
| 187 |
+
if result.content and len(result.content) > 0:
|
| 188 |
+
result_preview = str(result.content[0])[:150]
|
| 189 |
+
if len(str(result.content[0])) > 150:
|
| 190 |
+
result_preview += "..."
|
| 191 |
+
print(f" Result: {result_preview}")
|
| 192 |
elif self.output_type and not should_enforce_output:
|
| 193 |
# No tool calls but we didn't enforce output type - make one more call to get structured output
|
| 194 |
+
if self.verbose:
|
| 195 |
+
print(f"\n[NO TOOLS] Requesting structured output...")
|
| 196 |
final_request = self._prepare_llm_request(context, enforce_output_type=True)
|
| 197 |
final_response = await self.think(final_request)
|
| 198 |
|
|
|
|
| 205 |
)
|
| 206 |
|
| 207 |
context.increment_step()
|
| 208 |
+
|
| 209 |
+
if self.verbose:
|
| 210 |
+
print(f"[COMPLETED] Step {context.current_step}\n")
|
| 211 |
|
| 212 |
async def run(
|
| 213 |
self,
|
examples/demo.py
CHANGED
|
@@ -22,22 +22,32 @@ class AnswerOutput(BaseModel):
|
|
| 22 |
|
| 23 |
|
| 24 |
async def main():
|
| 25 |
-
# Create agent with structured output
|
| 26 |
agent = Agent(
|
| 27 |
model=LlmClient(model="gpt-5-mini"),
|
| 28 |
tools=[],
|
| 29 |
instructions="You are a helpful assistant that answers questions accurately.",
|
| 30 |
output_type=AnswerOutput,
|
|
|
|
| 31 |
)
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
result = await agent.run(
|
| 34 |
"If Eliud Kipchoge could maintain his marathon pace, "
|
| 35 |
"how many thousand hours to reach the Moon?"
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
print(f"Answer: {result.output.final_answer}")
|
| 39 |
-
print(f"Steps: {result.context.current_step}")
|
|
|
|
| 40 |
|
|
|
|
|
|
|
| 41 |
display_trace(result.context)
|
| 42 |
|
| 43 |
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
async def main():
|
| 25 |
+
# Create agent with structured output and verbose mode enabled
|
| 26 |
agent = Agent(
|
| 27 |
model=LlmClient(model="gpt-5-mini"),
|
| 28 |
tools=[],
|
| 29 |
instructions="You are a helpful assistant that answers questions accurately.",
|
| 30 |
output_type=AnswerOutput,
|
| 31 |
+
verbose=True, # Enable verbose mode to see thinking process
|
| 32 |
)
|
| 33 |
|
| 34 |
+
print("Starting agent execution...")
|
| 35 |
+
print("=" * 60)
|
| 36 |
+
|
| 37 |
result = await agent.run(
|
| 38 |
"If Eliud Kipchoge could maintain his marathon pace, "
|
| 39 |
"how many thousand hours to reach the Moon?"
|
| 40 |
)
|
| 41 |
|
| 42 |
+
print("\n" + "=" * 60)
|
| 43 |
+
print("FINAL RESULTS")
|
| 44 |
+
print("=" * 60)
|
| 45 |
print(f"Answer: {result.output.final_answer}")
|
| 46 |
+
print(f"Steps taken: {result.context.current_step}")
|
| 47 |
+
print("=" * 60)
|
| 48 |
|
| 49 |
+
# Optionally show full trace
|
| 50 |
+
print("\nFull Execution Trace:")
|
| 51 |
display_trace(result.context)
|
| 52 |
|
| 53 |
|