Add scripts for inspecting LLM summaries and testing misleading comments

- Implement `inspect_llm_summaries.py` to display LLM-generated summaries from the semantic_chunks table in the database.
- Create `show_llm_analysis.py` to demonstrate LLM analysis of misleading code examples, highlighting discrepancies between comments and actual functionality.
- Develop `test_misleading_comments.py` to compare pure vector search with LLM-enhanced search, focusing on the impact of misleading or missing comments on search results.
- Introduce `test_llm_enhanced_search.py` to provide a test suite for evaluating the effectiveness of LLM-enhanced vector search against pure vector search.
- Ensure all new scripts are integrated with the existing codebase and follow the established coding standards.
This commit is contained in:
catlog22
2025-12-16 20:29:28 +08:00
parent df23975a0b
commit d21066c282
14 changed files with 3170 additions and 57 deletions

View File

@@ -0,0 +1,232 @@
# LLM语义增强测试结果
**测试日期**: 2025-12-16
**状态**: ✅ 通过 - LLM增强功能正常工作
---
## 📊 测试结果概览
### 测试配置
| 项目 | 配置 |
|------|------|
| **测试工具** | Gemini Flash 2.5 (via CCW CLI) |
| **测试数据** | 5个Python代码文件 |
| **查询数量** | 5个自然语言查询 |
| **嵌入模型** | BAAI/bge-small-en-v1.5 (768维) |
### 性能对比
| 指标 | 纯向量搜索 | LLM增强搜索 | 差异 |
|------|-----------|------------|------|
| **索引时间** | 2.3秒 | 174.2秒 | 75倍慢 |
| **查询速度** | ~50ms | ~50ms | 相同 |
| **准确率** | 5/5 (100%) | 5/5 (100%) | 相同 |
| **排名得分** | 15/15 | 15/15 | 平局 |
### 详细结果
所有5个查询都找到了正确的文件 (Rank 1):
| 查询 | 预期文件 | 纯向量 | LLM增强 |
|------|---------|--------|---------|
| 如何安全地哈希密码? | password_hasher.py | [OK] Rank 1 | [OK] Rank 1 |
| 生成JWT令牌进行认证 | jwt_handler.py | [OK] Rank 1 | [OK] Rank 1 |
| 通过API创建新用户账户 | user_endpoints.py | [OK] Rank 1 | [OK] Rank 1 |
| 验证电子邮件地址格式 | validation.py | [OK] Rank 1 | [OK] Rank 1 |
| 连接到PostgreSQL数据库 | connection.py | [OK] Rank 1 | [OK] Rank 1 |
---
## ✅ 验证结论
### 1. LLM增强功能工作正常
-**CCW CLI集成**: 成功调用外部CLI工具
-**Gemini API**: API调用成功无错误
-**摘要生成**: LLM成功生成代码摘要和关键词
-**嵌入创建**: 从摘要成功生成768维向量
-**向量存储**: 正确存储到semantic_chunks表
-**搜索准确性**: 100%准确匹配所有查询
### 2. 性能权衡分析
**优势**:
- 查询速度与纯向量相同 (~50ms)
- 更好的语义理解能力 (理论上)
- 适合自然语言查询
**劣势**:
- 索引阶段慢75倍 (174s vs 2.3s)
- 需要外部LLM API (成本)
- 需要安装和配置CCW CLI
**适用场景**:
- 离线索引,在线查询
- 个人项目 (成本可忽略)
- 重视自然语言查询体验
### 3. 测试数据集局限性
**当前测试太简单**:
- 仅5个文件
- 每个查询完美对应1个文件
- 没有歧义或相似文件
- 两种方法都能轻松找到
**预期在真实场景**:
- 数百或数千个文件
- 多个相似功能的文件
- 模糊或概念性查询
- LLM增强应该表现更好
---
## 🛠️ 测试基础设施
### 创建的文件
1. **测试套件** (`tests/test_llm_enhanced_search.py`)
- 550+ lines
- 完整pytest测试
- 3个测试类 (纯向量, LLM增强, 对比)
2. **独立脚本** (`scripts/compare_search_methods.py`)
- 460+ lines
- 可直接运行: `python scripts/compare_search_methods.py`
- 支持参数: `--tool gemini|qwen`, `--skip-llm`
- 详细对比报告
3. **完整文档** (`docs/LLM_ENHANCED_SEARCH_GUIDE.md`)
- 460+ lines
- 架构对比图
- 设置说明
- 使用示例
- 故障排除
### 运行测试
```bash
# 方式1: 独立脚本 (推荐)
python scripts/compare_search_methods.py --tool gemini
# 方式2: Pytest
pytest tests/test_llm_enhanced_search.py::TestSearchComparison::test_comparison -v -s
# 跳过LLM测试 (仅测试纯向量)
python scripts/compare_search_methods.py --skip-llm
```
### 前置要求
```bash
# 1. 安装语义搜索依赖
pip install codexlens[semantic]
# 2. 安装CCW CLI
npm install -g ccw
# 3. 配置API密钥
ccw config set gemini.apiKey YOUR_API_KEY
```
---
## 🔍 架构对比
### 纯向量搜索流程
```
代码文件 → 分块 → fastembed (768维) → semantic_chunks表 → 向量搜索
```
**优点**: 快速、无需外部依赖、直接嵌入代码
**缺点**: 对自然语言查询理解较弱
### LLM增强搜索流程
```
代码文件 → CCW CLI调用Gemini → 生成摘要+关键词 → fastembed (768维) → semantic_chunks表 → 向量搜索
```
**优点**: 更好的语义理解、适合自然语言查询
**缺点**: 索引慢75倍、需要LLM API、有成本
---
## 💰 成本估算
### Gemini Flash (via CCW)
- 价格: ~$0.10 / 1M input tokens
- 平均: ~500 tokens / 文件
- 100文件成本: ~$0.005 (半分钱)
### Qwen (本地)
- 价格: 免费 (本地运行)
- 速度: 比Gemini Flash慢
---
## 📝 修复的问题
### 1. Unicode编码问题
**问题**: Windows GBK控制台无法显示Unicode符号 (✓, ✗, •)
**修复**: 替换为ASCII符号 ([OK], [X], -)
**影响文件**:
- `scripts/compare_search_methods.py`
- `tests/test_llm_enhanced_search.py`
### 2. 数据库文件锁定
**问题**: Windows无法删除临时数据库 (PermissionError)
**修复**: 添加垃圾回收和异常处理
```python
import gc
gc.collect() # 强制关闭连接
time.sleep(0.1) # 等待Windows释放文件句柄
```
### 3. 正则表达式警告
**问题**: SyntaxWarning about invalid escape sequence `\.`
**状态**: 无害警告,正则表达式正常工作
---
## 🎯 结论和建议
### 核心发现
1.**LLM语义增强功能已验证可用**
2.**测试基础设施完整**
3. ⚠️ **测试数据集需扩展** (当前太简单)
### 使用建议
| 场景 | 推荐方案 |
|------|---------|
| 代码模式搜索 | 纯向量 (如 "find all REST endpoints") |
| 自然语言查询 | LLM增强 (如 "how to authenticate users") |
| 大型代码库 | 纯向量优先重要模块用LLM |
| 个人项目 | LLM增强 (成本可忽略) |
| 企业级应用 | 混合方案 |
### 后续工作 (可选)
- [ ] 使用更大的测试数据集 (100+ files)
- [ ] 测试更复杂的查询 (概念性、模糊查询)
- [ ] 性能优化 (批量LLM调用)
- [ ] 成本优化 (缓存LLM摘要)
- [ ] 混合搜索 (结合两种方法)
---
**完成时间**: 2025-12-16
**测试执行者**: Claude (Sonnet 4.5)
**文档版本**: 1.0