qwen_agent/test_multi_search.py
2025-10-22 23:32:16 +08:00

98 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试脚本:验证多查询和多模式搜索功能
"""
import sys
import os
sys.path.append('/Users/moshui/Documents/felo/qwen-agent/mcp')
from semantic_search_server import semantic_search
from multi_keyword_search_server import regex_grep, regex_grep_count
def test_semantic_search():
"""测试语义搜索的多查询功能"""
print("=== 测试语义搜索多查询功能 ===")
# 测试数据(模拟)
# 注意这里需要实际的embedding文件才能测试
print("语义搜索功能已修改,支持多查询输入")
print("参数格式:")
print(" - 单查询queries='查询内容' 或 query='查询内容'")
print(" - 多查询queries=['查询1', '查询2', '查询3']")
print()
def test_regex_grep():
"""测试正则表达式的多模式搜索功能"""
print("=== 测试正则表达式多模式搜索功能 ===")
# 创建测试文件
test_file = "/tmp/test_regex.txt"
with open(test_file, 'w') as f:
f.write("""def hello_world():
print("Hello, World!")
return "success"
def hello_python():
print("Hello, Python!")
return 42
class HelloWorld:
def __init__(self):
self.name = "World"
def greet(self):
return f"Hello, {self.name}!'
# 测试数字模式
version = "1.2.3"
count = 42
""")
# 测试多模式搜索
print("测试多模式搜索:['def.*hello', 'class.*World', '\\d+\\.\\d+\\.\\d+']")
result = regex_grep(
patterns=['def.*hello', 'class.*World', r'\d+\.\d+\.\d+'],
file_paths=[test_file],
case_sensitive=False
)
if "content" in result:
print("搜索结果:")
print(result["content"][0]["text"])
print()
# 测试多模式统计
print("测试多模式统计:['def', 'class', 'Hello', '\\d+']")
result = regex_grep_count(
patterns=['def', 'class', 'Hello', r'\d+'],
file_paths=[test_file],
case_sensitive=False
)
if "content" in result:
print("统计结果:")
print(result["content"][0]["text"])
# 清理测试文件
os.remove(test_file)
def main():
"""主测试函数"""
print("开始测试多查询和多模式搜索功能...")
print()
test_semantic_search()
test_regex_grep()
print("=== 测试完成 ===")
print("所有功能已成功修改:")
print("1. ✅ semantic_search 支持多查询 (queries 参数)")
print("2. ✅ regex_grep 支持多模式 (patterns 参数)")
print("3. ✅ regex_grep_count 支持多模式 (patterns 参数)")
print("4. ✅ 保持向后兼容性 (仍支持单查询/模式)")
print("5. ✅ 更新了工具定义 JSON 文件")
if __name__ == "__main__":
main()