run_tool_picker_rate.py 8.22 KB
Newer Older
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
from langchain_openai import ChatOpenAI
from rich.console import Console
from rich.table import Table

import sys,os
sys.path.append("../")


from src.server.tool_picker import ToolPicker
from src.agent.tool_rate import RegionRateTool, RankingRateTool
from src.agent.tool_monitor import MonitorPointTool

def run_examples():
    # 初始化 rich console
    console = Console()
    
    # 初始化 LLM
    llm = ChatOpenAI(
        openai_api_key="xxxxxx",
        openai_api_base="http://192.168.10.14:8000/v1",
        model_name="Qwen2-7B",
        verbose=True
    )
    
    # 初始化工具
    tools = [
        RegionRateTool(),
        RankingRateTool(),
        MonitorPointTool(),
    ]
    
    # 初始化 ToolPicker
    picker = ToolPicker(llm, tools)
    
    # 测试案例和预期结果
    test_cases = [
        {
            "query": "请分析下今天全国各地区在线率情况",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-11-19",
                    "end_time": "2024-11-19",
                    "region_name": "",
                    "month_required": False
                }
            }
        },
        {
            "query": "请分析下今天甘肃省设备在线率情况",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-11-19",
                    "end_time": "2024-11-19",
                    "region_name": "甘肃省",
                    "month_required": False
                }
            }
        },
        {
            "query": "查询2024年11月13日各地区排名情况",
            "expected": {
                "tool": "online_rate_ranking",
                "params": {
                    "rate_type": 1
                }
            }
        },
        {
            "query": "查询各厂商在线率排名情况",
            "expected": {
                "tool": "online_rate_ranking",
                "params": {
                    "rate_type": 2
                }
            }
        },
        {
            "query": "甘肃省监控点的状态如何?",
            "expected": {
                "tool": "monitor_points_query",
                "params": {
                    "key": "甘肃省"
                }
            }
        },
        {
            "query": "2023年甘肃省每月的设备在线率分别是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01",
                    "end_time": "2023-12-31",
                    "region_name": "甘肃省",
                    "month_required": True
                }
            }
        },
        {
            "query": "查询2024年甘肃省各月在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-01-01",
                    "end_time": "2024-12-31",
                    "region_name": "甘肃省",
                    "month_required": True
                }
            }
        },
        {
            "query": "2024年10月15日,成都市武侯区的设备在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-10-15",
                    "end_time": "2024-10-15",
                    "region_name": "成都市武侯区",
                    "month_required": False
                }
            }
        },
        {
            "query": "2024年,成都市武侯区的设备在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-01-01",  "region_name": "成都市武侯区", "month_required": False
                }
            }
        },
        {
            "query": "2023年甘肃省每月的设备在线率分别是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "甘肃省", "month_required": True
                }
            }
        },
        {
            "query": "2023年甘肃省按月统计设备在线率?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "region_name": "甘肃省", "month_required": True
                }
            }
        },
        {
            "query": "2023年全国每个月的设备在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "region_name": "", "month_required": True
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_required": False
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点各月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_required": True
                }
            }
        },
        {
            "query": "2022年各个月设备在线率统计;",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2022-01-01", "end_time": "2022-12-31", "region_name": "", "month_required": True
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_required": True
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_required": True
                }
            }
        }
    ]
    
    # 为每个测试案例创建一个表格
    for i, case in enumerate(test_cases, 1):
        console.print(f"\n[bold cyan]=== 测试案例 {i} ===[/bold cyan]")
        
        table = Table(title=f"查询: {case['query']}")
        table.add_column("项目", style="cyan")
        table.add_column("预期结果", style="green")
        table.add_column("实际结果", style="yellow")
        table.add_column("是否匹配", style="magenta")
        
        try:
            result = picker.pick(case["query"])
            
            # 添加工具比较行
            expected_tool = case["expected"]["tool"]
            actual_tool = result["tool"]
            table.add_row(
                "选择的工具",
                expected_tool,
                actual_tool,
                "✓" if expected_tool == actual_tool else "✗"
            )
            
            # 添加参数比较行
            for param_key in case["expected"]["params"]:
                expected_value = str(case["expected"]["params"][param_key])
                actual_value = str(result["params"].get(param_key, "未提供"))
                table.add_row(
                    f"参数: {param_key}",
                    expected_value,
                    actual_value,
                    "✓" if expected_value == actual_value else "✗"
                )
                
        except Exception as e:
            table.add_row("错误", "", str(e), "✗")
        
        console.print(table)
        console.print("=" * 80)

if __name__ == "__main__":
    run_examples()