Spaces:
Configuration error
Configuration error
File size: 5,471 Bytes
096cc99 | 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 | # ๐ฏ Tool Calling Integration - Quick Summary
## โ
What Was Implemented
### 1. Tool Executor (`tool_executor.py`)
- **ToolCall** dataclass - Represents single tool call with results
- **ToolExecutionBatch** - Batch of tool calls with statistics
- **ToolExecutor** - Main executor class
- Executes tool calls in batch
- Tracks tokens (input + output)
- Logs execution times
- Formats results for LLM
### 2. LLM Client Updates (`llm_client.py`)
- Added `tool_calls` field to **LLMResponse**
- Support for `tools` parameter in `generate()`
- Extracts function calls from Gemini response
- Added `tool_tokens` tracking to **LLMStats**
- New method: `_convert_tool_schema()` for Gemini format
### 3. AI Manager Integration (`ai_manager.py`)
- Imports **AgentTools** and **ToolExecutor**
- Updates agent tools with game state each turn
- **Tool calling loop** in `_send_to_llm()`:
1. Send prompt with tools
2. Execute any tool calls
3. Send results back to LLM
4. Repeat until final answer (max 5 iterations)
- Adds tool tokens to statistics
### 4. Logging System (`ai_logger.py`)
- New method: `log_tool_execution(batch)`
- Logs to `llm_communication.log` with details
- Saves to `tool_executions.json` with full data
- Shows:
- Tool name and parameters
- Execution time per call
- Token counts (input/output)
- Success/failure status
- Result previews
### 5. Testing (`test_tools_integration.py`)
- 4 comprehensive tests:
- Basic tool operations
- Multiple tool calls in batch
- Schema generation
- Execution history and stats
- Sample outputs and verification
### 6. Documentation (`TOOLS_INTEGRATION.md`)
- Complete guide to tool system
- Architecture diagrams
- Usage examples
- Logging format reference
- Troubleshooting guide
---
## ๐ How It Works
```
Game Turn
โ
AI Manager updates AgentTools with game state
โ
Send prompt to LLM (with tool schemas)
โ
LLM responds with tool_calls?
โ YES
Execute tools via ToolExecutor
โ
Log execution (time, tokens, results)
โ
Send results back to LLM
โ
LLM provides final answer
```
---
## ๐ What You See in Logs
### `llm_communication.log`
```
[12:34:56] [TOOL_REQUEST] ๐ง LLM requested 2 tool(s) (iteration 1)
[12:34:56] [TOOL] โ
inspect_node({"node_id": 14})
[12:34:56] [TOOL] Time: 12.3ms | Tokens: 5 in + 45 out = 50 total
[12:34:56] [TOOL] โ
find_best_nodes({"min_pips": 10})
[12:34:56] [TOOL] Time: 32.9ms | Tokens: 10 in + 82 out = 92 total
[12:34:56] [TOOL] Total: 2/2 successful | 142 tokens | 45.2ms
```
### `tool_executions.json`
Complete JSON record with:
- Timestamp per batch
- All tool calls with full parameters
- Complete results
- Token breakdown
- Execution times
### Token Statistics
```python
{
"total_tokens": 15432,
"tool_tokens": 1250, # NEW: from tools
"llm_tokens": 14182 # NEW: from LLM only
}
```
---
## ๐ฎ Benefits
### For AI Agent
- โ
**No hallucinations** - Gets real data from tools
- โ
**Multiple queries** - Can ask about several nodes at once
- โ
**Strategic insight** - Path analysis, best locations, etc.
### For Developers
- โ
**Full visibility** - See exactly what tools were called
- โ
**Token tracking** - Know the cost of each tool
- โ
**Easy debugging** - Detailed logs with timing
- โ
**Performance monitoring** - Execution statistics
### For System
- โ
**Prevents infinite loops** - Max 5 tool iterations
- โ
**Graceful errors** - Failed tools don't crash the system
- โ
**Scalable** - Easy to add new tools
---
## ๐งช Testing
Run:
```bash
python examples/ai_testing/test_tools_integration.py
```
Expected: All 4 tests pass with detailed output
---
## ๐ Files Created/Modified
### New Files
- `pycatan/ai/tool_executor.py` - Tool execution engine
- `examples/ai_testing/test_tools_integration.py` - Test suite
- `docs/TOOLS_INTEGRATION.md` - Complete documentation
### Modified Files
- `pycatan/ai/llm_client.py` - Function calling support
- `pycatan/ai/ai_manager.py` - Tool integration
- `pycatan/ai/ai_logger.py` - Tool logging
### Existing (Used)
- `pycatan/ai/agent_tools.py` - The 3 tools (already existed)
---
## ๐ Next Steps
The system is **ready to use**! The LLM can now:
1. โ
Call `inspect_node()` for specific nodes
2. โ
Call `find_best_nodes()` to search board
3. โ
Call `analyze_path_potential()` for road planning
4. โ
Make multiple calls in one turn
5. โ
All executions are logged and tracked
**Just run the game normally** - tools are automatically available!
---
## ๐ก Example Usage
The AI will automatically use tools when needed:
```
AI thinking: "I need to know about node 14..."
โ Calls: inspect_node(14)
โ Gets: {"node_id": 14, "total_pips": 12, "resources": {...}}
โ Decides: "Great location, I'll build there!"
```
All of this is **logged automatically** in:
- `llm_communication.log` (real-time)
- `tool_executions.json` (detailed batch records)
---
## โ
Summary
**What you asked for:**
- โ Support for multiple tool calls (e.g., query two nodes)
- โ Clear logging of tool usage
- โ Complete token calculation from tools
- โ Visible tool loop with parameters and outputs
**What you got:**
- Complete tool calling system with Gemini integration
- Detailed execution logging with timing and tokens
- Automatic token tracking separate from LLM
- Test suite to verify everything works
- Full documentation
**ืืื ืืืื ืืฉืืืืฉ! ๐**
|