run_tool_picker_rate.py 10.6 KB
Newer Older
1 2 3
from langchain_openai import ChatOpenAI
from rich.console import Console
from rich.table import Table
4
import time
5
import sys,os
6

7 8 9 10 11 12
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
13
from src.agent.tool_warn import WarningTool
14

15
def create_tool_picker():
16 17 18 19 20 21
    llm = ChatOpenAI(
        openai_api_key="xxxxxx",
        openai_api_base="http://192.168.10.14:8000/v1",
        model_name="Qwen2-7B",
        verbose=True
    )
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    base_url = "http://172.30.0.37:30007"
    # 初始化工具
    tools = [
        RegionRateTool(base_url=base_url),
        RankingRateTool(base_url=base_url),
        MonitorPointTool(base_url=base_url),
        WarningTool(base_url=base_url),
    ]
    return ToolPicker(llm, tools)


def run_examples():
    # 初始化 rich console
    console = Console()
    
    # 初始化 LLM
    
40
    base_url = "http://172.30.0.37:30007"
41 42
    # 初始化工具
    tools = [
43 44 45
        RegionRateTool(base_url=base_url),
        RankingRateTool(base_url=base_url),
        MonitorPointTool(base_url=base_url),
46
        WarningTool(base_url=base_url),
47 48
    ]
    
49
    tool_dict = {tool.name: tool for tool in tools}
50 51 52 53 54 55 56 57 58 59 60 61 62
    # 初始化 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": "",
63
                    "month_statistics": False
64 65 66 67 68 69 70 71 72 73 74
                }
            }
        },
        {
            "query": "请分析下今天甘肃省设备在线率情况",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-11-19",
                    "end_time": "2024-11-19",
                    "region_name": "甘肃省",
75
                    "month_statistics": False
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
                }
            }
        },
        {
            "query": "查询2024年11月13日各地区排名情况",
            "expected": {
                "tool": "online_rate_ranking",
                "params": {
                    "rate_type": 1
                }
            }
        },
        {
            "query": "查询各厂商在线率排名情况",
            "expected": {
                "tool": "online_rate_ranking",
                "params": {
                    "rate_type": 2
                }
            }
        },
        {
            "query": "2023年甘肃省每月的设备在线率分别是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2023-01-01",
                    "end_time": "2023-12-31",
                    "region_name": "甘肃省",
105
                    "month_statistics": True
106 107 108 109 110 111 112 113 114 115 116
                }
            }
        },
        {
            "query": "查询2024年甘肃省各月在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-01-01",
                    "end_time": "2024-12-31",
                    "region_name": "甘肃省",
117
                    "month_statistics": True
118 119 120 121
                }
            }
        },
        {
122
            "query": "2024年10月15日,兰州市的设备在线率是多少?",
123 124 125 126 127
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-10-15",
                    "end_time": "2024-10-15",
128 129
                    "region_name": "兰州市",
                    "month_statistics": False
130 131 132 133
                }
            }
        },
        {
134
            "query": "2024年,兰州市榆中县的设备在线率是多少?",
135 136 137
            "expected": {
                "tool": "region_online_rate",
                "params": {
138
                    "start_time": "2024-11-26",  "region_name": "兰州市榆中县", "month_statistics": False
139 140 141 142 143 144 145 146
                }
            }
        },
        {
            "query": "2023年甘肃省每月的设备在线率分别是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
147
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "甘肃省", "month_statistics": True
148 149 150 151 152 153 154 155
                }
            }
        },
        {
            "query": "2023年甘肃省按月统计设备在线率?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
156
                    "start_time": "2023-01-01", "region_name": "甘肃省", "month_statistics": True
157 158 159 160 161 162 163 164
                }
            }
        },
        {
            "query": "2023年全国每个月的设备在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
165
                    "start_time": "2023-01-01", "region_name": "", "month_statistics": True
166 167 168 169
                }
            }
        },
        {
170
            "query": "2023年1月-2023年12月期间青海实验点在线率是多少?",
171 172 173
            "expected": {
                "tool": "region_online_rate",
                "params": {
174
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "青海", "month_statistics": False
175 176 177 178
                }
            }
        },
        {
179
            "query": "2023年1月-2023年12月期间青海实验点各月在线率是多少?",
180 181 182
            "expected": {
                "tool": "region_online_rate",
                "params": {
183
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "青海", "month_statistics": True
184 185 186 187 188 189 190 191
                }
            }
        },
        {
            "query": "2022年各个月设备在线率统计;",
            "expected": {
                "tool": "region_online_rate",
                "params": {
192
                    "start_time": "2022-01-01", "end_time": "2022-12-31", "region_name": "", "month_statistics": True
193 194 195 196 197 198 199 200
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
201
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_statistics": True
202 203 204 205 206 207 208 209
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
210
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_statistics": True
211 212
                }
            }
213 214 215 216 217 218 219 220 221
        },
        {
            "query": "贵阳市各县区设备在线率是多少",
            "expected": {
                "tool": "region_online_rate",   
                "params": {
                    "region_name": "贵阳市"
                }
            }
222 223 224
        }
    ]
    
225 226

    tool_selected_success = 0
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    # 为每个测试案例创建一个表格
    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 "✗"
            )
            
250 251 252
            if expected_tool == actual_tool:
                tool_selected_success += 1
            
253 254 255 256 257 258 259 260 261 262
            # 添加参数比较行
            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 "✗"
                )
263 264 265 266

            tool = tool_dict[result["tool"]]
            params = result["params"]

267 268 269
            # result =   tool.invoke(params)
            # print(result)
            
270 271 272 273 274 275
        except Exception as e:
            table.add_row("错误", "", str(e), "✗")
        
        console.print(table)
        console.print("=" * 80)

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    print(f"工具选择成功率: {tool_selected_success} / {len(test_cases)} = {tool_selected_success / len(test_cases) * 100:.2f}%")

def run_case_17():
    console = Console()
    picker = create_tool_picker()

    

    success_count = 0
    total_time = 0
    test_cases_count = 50
    for i in range(test_cases_count):
        start_time = time.time()
        query = "贵阳市各县区设备在线率是多少"
        expected = {
            "tool": "region_online_rate",
            "params": {
                "region_name": "贵阳市"
            }
        }

        result = picker.pick(query)
        exec_time = time.time() - start_time
        total_time += exec_time
        table = Table(title=f"第 {i+1:02d} 次测试结果 【{query}】")
        table.add_column("查询", style="cyan")
        table.add_column("预期工具", style="green")
        table.add_column("实际工具", style="yellow")
        table.add_column("是否匹配", style="magenta")
        table.add_column("耗时", style="magenta")
        table.add_row(query, expected["tool"], result["tool"], "✓" if result["tool"] == expected["tool"] else "✗", f"{exec_time:.2f}秒")

        if result["tool"] == expected["tool"]:
            success_count += 1
        console.print(table)
    
    print(f"工具选择成功率: {success_count} / {test_cases_count} = {success_count / test_cases_count * 100:.2f}%")
    print(f"平均耗时: {total_time / test_cases_count:.2f}秒 总共耗时: {total_time:.2f}秒")
314
if __name__ == "__main__":
315 316
    # run_examples()
    run_case_17()