Spaces:
Configuration error
Configuration error
| # ๐ฏ 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 | |
| **ืืื ืืืื ืืฉืืืืฉ! ๐** | |