Spaces:
Sleeping
Sleeping
File size: 6,168 Bytes
dcc845c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | # Pattern: Make Task Return a Specific ToolMessage Directly
## Problem
When an agent emits a ToolMessage, you need to extract it from the task result. The naive approach is to search through `task.agent.message_history` to find the tool, but this is **error-prone** and **inefficient**.
## Solution
Use **TaskConfig with `done_if_tool=True`** combined with **bracket notation** to make the task:
1. Terminate as soon as a tool is emitted
2. Return the tool directly (typed as `ToolClass | None`)
## Code Pattern
### Wrong Approach (searching message_history)
```python
from langroid.agent.task import Task
task = Task(agent, interactive=False)
result = task.run(prompt, turns=5)
# BAD: Searching message_history
pruned_classes = None
for msg in task.agent.message_history:
if isinstance(msg, EmitPrunedModelTool):
pruned_classes = msg.classes
break
if not pruned_classes:
print("❌ Agent did not use the tool")
return 1
```
**Problems**:
- Iterating through entire message history
- Error-prone type checking with `isinstance`
- Can miss the tool if not searching correctly
- Not type-safe
### Correct Approach (TaskConfig + bracket notation)
```python
import langroid as lr
from langroid.agent.task import Task
# 1. Create TaskConfig with done_if_tool=True
task_config = lr.TaskConfig(done_if_tool=True)
# 2. Use bracket notation to specify return type
task = Task(agent, interactive=False, config=task_config)[EmitPrunedModelTool]
# 3. Run task - returns EmitPrunedModelTool | None
result: EmitPrunedModelTool | None = task.run(prompt, turns=5)
# 4. Check if tool was emitted
if not result:
print("❌ Agent did not use the tool")
return 1
# 5. Access tool data directly
pruned_classes = result.classes # Type-safe!
```
**Benefits**:
- Task terminates immediately when tool is emitted (efficient)
- Return type is explicit and type-safe
- No need to search message_history
- Clean, readable code
## Key Components
### 1. TaskConfig(done_if_tool=True)
```python
task_config = lr.TaskConfig(done_if_tool=True)
```
This tells the task to **stop as soon as any tool is emitted**, rather than continuing for `turns` iterations.
### 2. Bracket Notation: `Task(...)[ToolClass]`
```python
task = Task(agent, interactive=False, config=task_config)[EmitPrunedModelTool]
```
The bracket notation **specifies the expected return type**:
- If the agent emits `EmitPrunedModelTool`, task returns it
- If the agent doesn't emit the tool, task returns `None`
- Return type is `EmitPrunedModelTool | None`
### 3. Type-Safe Result Handling
```python
result: EmitPrunedModelTool | None = task.run(prompt, turns=5)
if not result:
# Agent didn't emit the tool
handle_failure()
else:
# Tool was emitted, access fields directly
data = result.classes # Type-safe attribute access
```
## Real-World Example
From `tools/prune_xsdata_models.py`:
```python
import langroid as lr
from langroid.agent.task import Task
from interop.agents.model_pruning_agent import (
EmitPrunedModelTool,
create_model_pruning_agent,
)
# Create agent
agent = create_model_pruning_agent(
raw_generated_code=raw_content,
reference_code=reference_code,
target_entity="Aircraft",
model="gpt-4o",
)
# Configure task to return tool directly
task_config = lr.TaskConfig(done_if_tool=True)
task = Task(agent, interactive=False, config=task_config)[EmitPrunedModelTool]
# Build prompt
prompt = f"""
Here is the raw xsdata-generated code for Aircraft:
```python
{raw_content[:50000]}
```
Please analyze this code and emit pruned class definitions using the tool.
"""
# Run task - returns tool or None
result: EmitPrunedModelTool | None = task.run(prompt, turns=5)
if not result:
print("❌ Agent did not use the EmitPrunedModelTool")
return 1
# Extract data from tool
pruned_classes = result.classes
print(f"✅ Agent produced {len(pruned_classes)} pruned classes")
# Use the data
for cls_def in pruned_classes:
print(f" • {cls_def.class_name}: {len(cls_def.fields)} fields")
```
## When to Use This Pattern
Use this pattern when:
- ✅ You expect the agent to emit a **specific tool** as its final output
- ✅ You want **type-safe access** to the tool data
- ✅ You want the task to **terminate immediately** when the tool is emitted
- ✅ The tool emission is the **primary goal** of the task (not intermediate step)
Don't use this pattern when:
- ❌ The agent might emit multiple different tools during conversation
- ❌ You need the full conversation history
- ❌ Tool emission is an intermediate step in a longer workflow
## Related Patterns
- **handle_llm_no_tool**: Use this in `ChatAgentConfig` to catch cases where the LLM doesn't use the tool
- **ToolMessage validation**: Use Pydantic models to ensure tool output is well-formed
- **Multi-turn tasks**: Combine with `turns` parameter for agents that need multiple attempts
## Common Mistakes
### Mistake 1: Forgetting `done_if_tool=True`
```python
# WRONG: Task will run for all turns even after tool is emitted
task = Task(agent)[EmitPrunedModelTool]
result = task.run(prompt, turns=5) # Wastes turns!
```
**Fix**: Always use `TaskConfig(done_if_tool=True)`
### Mistake 2: Not checking for None
```python
# WRONG: Will crash if agent doesn't emit tool
result = task.run(prompt, turns=5)
pruned_classes = result.classes # AttributeError if result is None!
```
**Fix**: Always check `if not result:` before accessing fields
### Mistake 3: Searching message_history instead
```python
# WRONG: Negates the entire point of bracket notation
result = task.run(prompt, turns=5)
for msg in task.agent.message_history:
if isinstance(msg, EmitPrunedModelTool):
# Why did you use bracket notation then?
```
**Fix**: Trust the bracket notation - result IS the tool
## Summary
**Pattern**: `Task(agent, config=TaskConfig(done_if_tool=True))[ToolClass]`
**Returns**: `ToolClass | None`
**Benefits**:
- Efficient (terminates immediately)
- Type-safe (explicit return type)
- Clean (no message_history iteration)
- Robust (can't miss the tool)
**Use when**: Tool emission is the primary goal of the task
|