run_tool_picker_rate.py 8.25 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
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
    )
24
    base_url = "http://172.30.0.37:30007"
25 26
    # 初始化工具
    tools = [
27 28 29
        RegionRateTool(base_url=base_url),
        RankingRateTool(base_url=base_url),
        MonitorPointTool(base_url=base_url),
30 31
    ]
    
32
    tool_dict = {tool.name: tool for tool in tools}
33 34 35 36 37 38 39 40 41 42 43 44 45
    # 初始化 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": "",
46
                    "month_statistics": False
47 48 49 50 51 52 53 54 55 56 57
                }
            }
        },
        {
            "query": "请分析下今天甘肃省设备在线率情况",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-11-19",
                    "end_time": "2024-11-19",
                    "region_name": "甘肃省",
58
                    "month_statistics": False
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
                }
            }
        },
        {
            "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": "甘肃省",
88
                    "month_statistics": True
89 90 91 92 93 94 95 96 97 98 99
                }
            }
        },
        {
            "query": "查询2024年甘肃省各月在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-01-01",
                    "end_time": "2024-12-31",
                    "region_name": "甘肃省",
100
                    "month_statistics": True
101 102 103 104
                }
            }
        },
        {
105
            "query": "2024年10月15日,兰州市的设备在线率是多少?",
106 107 108 109 110
            "expected": {
                "tool": "region_online_rate",
                "params": {
                    "start_time": "2024-10-15",
                    "end_time": "2024-10-15",
111 112
                    "region_name": "兰州市",
                    "month_statistics": False
113 114 115 116
                }
            }
        },
        {
117
            "query": "2024年,兰州市榆中县的设备在线率是多少?",
118 119 120
            "expected": {
                "tool": "region_online_rate",
                "params": {
121
                    "start_time": "2024-11-26",  "region_name": "兰州市榆中县", "month_statistics": False
122 123 124 125 126 127 128 129
                }
            }
        },
        {
            "query": "2023年甘肃省每月的设备在线率分别是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
130
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "甘肃省", "month_statistics": True
131 132 133 134 135 136 137 138
                }
            }
        },
        {
            "query": "2023年甘肃省按月统计设备在线率?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
139
                    "start_time": "2023-01-01", "region_name": "甘肃省", "month_statistics": True
140 141 142 143 144 145 146 147
                }
            }
        },
        {
            "query": "2023年全国每个月的设备在线率",
            "expected": {
                "tool": "region_online_rate",
                "params": {
148
                    "start_time": "2023-01-01", "region_name": "", "month_statistics": True
149 150 151 152
                }
            }
        },
        {
153
            "query": "2023年1月-2023年12月期间青海实验点在线率是多少?",
154 155 156
            "expected": {
                "tool": "region_online_rate",
                "params": {
157
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "青海", "month_statistics": False
158 159 160 161
                }
            }
        },
        {
162
            "query": "2023年1月-2023年12月期间青海实验点各月在线率是多少?",
163 164 165
            "expected": {
                "tool": "region_online_rate",
                "params": {
166
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "青海", "month_statistics": True
167 168 169 170 171 172 173 174
                }
            }
        },
        {
            "query": "2022年各个月设备在线率统计;",
            "expected": {
                "tool": "region_online_rate",
                "params": {
175
                    "start_time": "2022-01-01", "end_time": "2022-12-31", "region_name": "", "month_statistics": True
176 177 178 179 180 181 182 183
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
184
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_statistics": True
185 186 187 188 189 190 191 192
                }
            }
        },
        {
            "query": "2023年1月-2023年12月期间西藏实验点每个月在线率是多少?",
            "expected": {
                "tool": "region_online_rate",
                "params": {
193
                    "start_time": "2023-01-01", "end_time": "2023-12-31", "region_name": "西藏", "month_statistics": True
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
                }
            }
        }
    ]
    
    # 为每个测试案例创建一个表格
    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 "✗"
                )
232 233 234 235 236 237 238

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

            result =   tool.invoke(params)
            print(result)

239 240 241 242 243 244 245 246
        except Exception as e:
            table.add_row("错误", "", str(e), "✗")
        
        console.print(table)
        console.print("=" * 80)

if __name__ == "__main__":
    run_examples()