File size: 13,260 Bytes
8c10cf2 | 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | # Complete Reference Trajectory Configuration - SQLAssistant-MCP Project
#
# Version: Ideal trajectory based on code design (applicable to all models)
# Principle: Only include steps explicitly required by code design, not based on test statistics
#
# Code Design Analysis:
# 1. Execution Flow (orchestrator.py):
# - Orchestrator manages entire workflow with two-level retry logic:
# (a) Orchestrator-level retry: For exception errors (max 3 attempts, MAX_CREW_RETRIES=2)
# (b) Business logic retry: For compliance check failures (max 3 attempts, max_business_retries=2)
# - Ideal flow (no retry):
# Step 1-2: Generate and review SQL (SQLGenerationCrew)
# Step 3: Compliance check (ComplianceCheckerCrew)
# Step 4: Execute SQL query (only if compliance passes)
# Step 5: Interpret results (ResultInterpreterCrew, only if compliance passes)
#
# 2. Three Crew Agent Structure:
# - SQLGenerationCrew (sql_generation/sql_generation_crew.py):
# - Agent 1: Expert SQL Query Generator
# Tools: get_database_schema, get_table_sample, get_column_stats, list_tables
# - Agent 2: Senior SQL Code Reviewer
# Tools: validate_sql_syntax, get_database_schema, check_table_exists
# - ComplianceCheckerCrew (compliance_checker/compliance_checker_crew.py):
# - Agent: Data Security and Compliance Auditor
# Tools: get_database_schema, check_table_exists
# - ResultInterpreterCrew (result_interpreter/result_interpreter_crew.py):
# - Agent: Business Intelligence Analyst
# Tools: run_sql_query, count_rows
#
# 3. SPAN Hierarchy:
# - sql_assistant_workflow (top level, orchestrator.py line 226)
# - crew_execution (contains retry logic, orchestrator.py line 243)
# - generate_sql (SQL generation, orchestrator.py line 266)
# - check_compliance (compliance check, orchestrator.py line 316)
# - interpret_results (result interpretation, orchestrator.py line 380, only if compliance passes)
#
# 4. MCP Tools (tools/mcp_server.py):
# - Database exploration: get_database_schema, get_table_sample, get_column_stats, list_tables
# - SQL validation: validate_sql_syntax, check_table_exists
# - Query execution: run_sql_query, count_rows
# Project name
project_name: "SQLAssistant-MCP"
# Trajectory extraction type configuration
extract_types:
- "SPAN" # Multi-level SPAN structure
- "Chain" # Each Crew's kickoff chain
- "Agent" # Each Crew's Agent
- "LLM" # LLM calls
- "Tool" # MCP tool calls
# Repeatable pattern configuration
# SQLAssistant-MCP has no repeatable patterns (unlike book writing with multiple chapters)
repeatable_patterns: []
# ============================================================================
# Reference Trajectory (Ground Truth) - Based on Code Design
# ============================================================================
#
# Graphical Structure (ideal execution path, no retry, no error, each Agent uses
# at least the key tools required in config/tasks; other tools are optional):
#
# [SPAN] sql_assistant_workflow
# +-- [SPAN] crew_execution
# +-- [SPAN] generate_sql
# | +-- [Chain] Crew***.kickoff
# | +-- [Agent] Expert SQL Query Generator
# | | +-- [LLM] *
# | | +-- [Tool] get_database_schema
# | | +-- [LLM] *
# | +-- [Agent] Senior SQL Code Reviewer
# | +-- [LLM] *
# | +-- [Tool] validate_sql_syntax
# | +-- [LLM] *
# | +-- [Tool] get_database_schema
# | +-- [LLM] *
# +-- [SPAN] check_compliance
# | +-- [Chain] Crew***.kickoff
# | +-- [Agent] Data Security and Compliance Auditor
# | +-- [LLM] *
# | +-- [Tool] get_database_schema
# | +-- [LLM] *
# +-- [SPAN] interpret_results
# +-- [Chain] Crew***.kickoff
# +-- [Agent] Business Intelligence Analyst
# +-- [LLM] *
# +-- [Tool] run_sql_query (execute query)
# +-- [LLM] *
#
# Design Basis:
# - orchestrator.py: Sequential execution of 5 steps (generate, review, compliance, execute, interpret)
# - SQLGenerationCrew contains 2 Agents, sequential execution (Generator -> Reviewer)
# - ComplianceCheckerCrew contains 1 Agent
# - ResultInterpreterCrew contains 1 Agent, uses run_sql_query to execute query
# - All Agent roles defined in each crew's config/agents.yaml
# - Tools obtained from MCP server via MCPServerAdapter
#
# Ideal Trajectory Notes:
# - Top-level SPAN: sql_assistant_workflow
# - Second-level SPAN: crew_execution (contains two-level retry logic)
# - 3 sub-SPANs execute sequentially: generate_sql -> check_compliance -> interpret_results
# - generate_sql stage: Generator explores schema, Reviewer validates syntax
# - check_compliance stage: Auditor checks security and compliance, must pass (PASS verdict)
# - interpret_results stage: BI Analyst uses run_sql_query to execute and interpret results
# - This trajectory represents ideal execution path with no retry, no error
#
# Notes:
# - "LLM: *" means any LLM model (wildcard match)
# - AGENT names exactly match role field in agents.yaml
# - Tool names exactly match MCP server provided tool names
# - Tool calls may vary based on Agent decisions (e.g., Generator may use get_database_schema or list_tables)
# - Reviewer may skip tool calls (if SQL is already correct)
# - Auditor may skip tool calls (if no additional verification needed)
# ============================================================================
reference_trajectory:
# ===== Top-level SPAN =====
- "SPAN: sql_assistant_workflow"
# ===== Second-level SPAN (contains two-level retry logic) =====
- "SPAN: crew_execution"
# ===== Stage 1: Generate SQL (SQLGenerationCrew) =====
- "SPAN: generate_sql"
- "Chain: Crew***.kickoff"
# Agent 1: SQL Generator (uses key tools required in config/tasks)
- "Agent: Expert SQL Query Generator"
- "LLM: *" # Decide overall generation strategy
- "Tool: get_database_schema" # Per tasks.yaml, first view complete schema
- "LLM: *" # Generate candidate SQL query based on schema
# Other tools (list_tables / get_table_sample / get_column_stats) are optional exploration tools
# Agent 2: SQL Reviewer (uses key tools required in config/tasks)
- "Agent: Senior SQL Code Reviewer"
- "LLM: *" # Read and understand generated SQL
- "Tool: validate_sql_syntax" # Per tasks.yaml and backstory, must check syntax
- "LLM: *" # Analyze syntax check results
- "Tool: get_database_schema" # Per tasks.yaml, verify tables and columns exist
- "LLM: *" # Correct SQL based on schema check results
# ===== Stage 2: Check Compliance (ComplianceCheckerCrew) =====
- "SPAN: check_compliance"
- "Chain: Crew***.kickoff"
- "Agent: Data Security and Compliance Auditor"
- "LLM: *" # Initial SQL reading and risk identification
- "Tool: get_database_schema" # Required: view schema to assess table/field sensitivity
- "LLM: *" # Combine SQL text and schema to give PASS/FAIL verdict
# ===== Stage 3: Interpret Results (ResultInterpreterCrew) =====
# Note: This stage only executes if compliance passes
- "SPAN: interpret_results"
- "Chain: Crew***.kickoff"
- "Agent: Business Intelligence Analyst"
- "LLM: *" # Understand business question and reviewed SQL
- "Tool: run_sql_query" # Per tasks.yaml and backstory, must use run_sql_query to get real data
- "LLM: *" # Generate business interpretation based on real results (count_rows etc. are optional)
# Target tools list (for single-tool use metric)
# Lists all tools filtered for each Agent in configuration
target_tools:
# SQL Generation stage - Generator
- "Tool: get_database_schema"
- "Tool: get_table_sample"
- "Tool: get_column_stats"
- "Tool: list_tables"
# SQL Generation stage - Reviewer
- "Tool: validate_sql_syntax"
- "Tool: check_table_exists"
# Compliance stage
# (shares get_database_schema and check_table_exists with Reviewer, not repeated)
# Result Interpretation stage
- "Tool: run_sql_query"
- "Tool: count_rows"
# Models to evaluate
models:
- "GPT-5"
- "GPT-4o-mini"
- "DeepSeek-V3-1"
- "DeepSeek-R1"
- "Gemini-2.5-flash"
- "Gemini-2.5-flash-nothinking"
- "Qwen3-235b"
# ============================================================================
# Usage Instructions
# ============================================================================
#
# 1. Trajectory Hierarchy:
# - Level 1: SPAN (sql_assistant_workflow)
# - Level 2: SPAN (crew_execution)
# - Level 3: SPAN (generate_sql, check_compliance, interpret_results)
# - Level 4: Chain (Crew***.kickoff, wildcard handles UUID)
# - Level 5: AGENT (exact match agent role)
# - Level 6: LLM (wildcard matches any model)
# - Level 6: Tool (exact match tool name)
#
# 2. Wildcard Handling:
# - Crew_<UUID>.kickoff -> Crew***.kickoff (auto-handled by evaluate_trajectory.py)
# - LLM: * -> matches any model name (e.g., gpt-5-chat-latest, deepseek-reasoner)
# - AGENT names must match completely (including full role name)
#
# 3. Exact Match Requirements:
# - SPAN names: "sql_assistant_workflow", "crew_execution", "generate_sql",
# "check_compliance", "interpret_results"
# - Chain names: "Crew***.kickoff" (wildcard match)
# - Agent names: "Agent: Expert SQL Query Generator",
# "Agent: Senior SQL Code Reviewer",
# "Agent: Data Security and Compliance Auditor",
# "Agent: Business Intelligence Analyst"
# (exact match role field in each agents.yaml)
# - LLM names: "LLM: *" (wildcard matches any model)
# - Tool names: exact match MCP tool names (get_database_schema, list_tables,
# get_table_sample, get_column_stats, validate_sql_syntax,
# check_table_exists, run_sql_query, count_rows)
#
# 4. Evaluation Metric Meanings:
# - Exact Match: Requires identical trajectory (including LLM and tool call counts)
# - In-order Match: Allows extra calls, but core steps must appear in order
# - Any-order Match: Only requires all necessary steps present (ignores order)
# - Precision: Proportion of correct steps in predicted trajectory
# - Recall: Proportion of reference steps covered
# - Single-tool Use: Detects core tool usage
# - unique_path_ratio: Path diversity (unique complete trajectories / sample count)
# - path_entropy: Path entropy (Shannon entropy based on trajectory frequency, normalized to 0-1)
#
# 5. Run Command:
# cd SQLAssistant-MCP
# python3 evaluate_trajectory-Filter_Tools.py --config reference_trajectory.yaml
#
# 6. Design Notes:
# - This reference trajectory represents ideal execution path (no retry, no error)
# - 3 main stages execute sequentially: SQL Generation -> Compliance Check -> Result Interpretation
# - Stage 1 (SQL Generation): Generator uses tools to explore schema + Reviewer validates syntax
# - Stage 2 (Compliance Check): Auditor checks security, must pass
# - Stage 3 (Result Interpretation): BI Analyst uses run_sql_query to execute and interpret
# - In-order Match and Recall metrics are more suitable for evaluating actual performance
# - Reference trajectory only defines core required steps, allows extra tool calls and LLM reasoning
#
# 7. Differences from Actual Trajectories:
# - Actual trajectories may contain business logic retries (compliance check failure triggers SQL regeneration)
# - Actual trajectories may contain orchestrator-level retries (exception errors trigger workflow retry)
# - Generator may use multiple tools to explore database (get_database_schema, list_tables, etc.)
# - Reviewer may call validate_sql_syntax multiple times for syntax validation
# - Auditor may use get_database_schema to verify table and column existence
# - BI Analyst may use count_rows to verify result count
# - Reasoning models (e.g., DeepSeek-R1) may include many extra LLM thinking calls
# - Some models may skip certain tool calls and generate results directly
# - These differences don't represent errors, but different model execution strategies
#
# 8. MCP Version Features:
# - All tools provided via MCP server using SSE protocol
# - Tools obtained from server via MCPServerAdapter
# - Contains multi-level SPAN structure (orchestrator + crew_execution + 3 stage SPANs)
# - 3 specialized Crews collaborate to complete SQL query task
# - Each Crew has clear responsibilities and available tool sets
# - SQLGenerationCrew: 2 Agents, generate and review SQL
# - ComplianceCheckerCrew: 1 Agent, check security and compliance
# - ResultInterpreterCrew: 1 Agent, execute query and generate business insights
# - Tool categories: exploration tools (get_database_schema, list_tables, get_table_sample, etc.),
# validation tools (validate_sql_syntax, check_table_exists),
# execution tools (run_sql_query, count_rows)
#
# 9. Business Logic Notes:
# - Compliance check is critical: only if compliance passes will query execute
# - If compliance fails, SQL is regenerated (max 3 attempts)
# - If all attempts fail, workflow ends without executing query
# - interpret_results stage only appears after compliance passes
# - Therefore, some failed trajectories may lack interpret_results stage
|