Initial commit
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
4ecf7b351d
143 files changed
+24156
No files matched your search
@@ -0,0 +1,208 @@
|
||||
"""
|
||||
文件加载模块调试脚本
|
||||
|
||||
使用方法:
|
||||
cd rag_evaluation
|
||||
python debug_loader.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加父目录到路径
|
||||
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, parent_dir)
|
||||
|
||||
from rag_evaluation.testset.document_loader import load_documents, Document
|
||||
from rag_evaluation.testset.sampling import SamplingStrategy, DocumentChunker
|
||||
from rag_evaluation.core.config import TestsetConfig
|
||||
|
||||
|
||||
def debug_document_loading():
|
||||
"""调试文档加载"""
|
||||
print("=" * 60)
|
||||
print("调试: 文档加载")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. 加载文档
|
||||
file_paths = [
|
||||
"test_data/test.txt",
|
||||
"test_data/test.md"
|
||||
]
|
||||
|
||||
print(f"\n加载文件: {file_paths}")
|
||||
|
||||
try:
|
||||
documents = load_documents(file_paths)
|
||||
print(f"[OK] 成功加载 {len(documents)} 个文档")
|
||||
|
||||
for i, doc in enumerate(documents):
|
||||
print(f"\n文档 {i+1}:")
|
||||
print(f" - 来源: {doc.metadata.get('source')}")
|
||||
print(f" - 类型: {doc.metadata.get('file_type')}")
|
||||
print(f" - 大小: {doc.metadata.get('file_size')} 字节")
|
||||
print(f" - 内容长度: {len(doc.page_content)} 字符")
|
||||
print(f" - 内容预览: {doc.page_content[:100]}...")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 加载失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
return documents
|
||||
|
||||
|
||||
def debug_sampling(documents):
|
||||
"""调试采样策略"""
|
||||
print("\n" + "=" * 60)
|
||||
print("调试: 采样策略")
|
||||
print("=" * 60)
|
||||
|
||||
config = TestsetConfig()
|
||||
sampler = SamplingStrategy(config)
|
||||
|
||||
print(f"\n采样配置:")
|
||||
print(f" - 小文件阈值: {config.sampling_strategy['small_file_threshold_mb']} MB")
|
||||
print(f" - 中文件阈值: {config.sampling_strategy['medium_file_threshold_mb']} MB")
|
||||
print(f" - 小文件采样率: {config.sampling_strategy['small_file_sampling_rate']}")
|
||||
print(f" - 中文件采样率: {config.sampling_strategy['medium_file_sampling_rate']}")
|
||||
print(f" - 大文件采样率: {config.sampling_strategy['large_file_sampling_rate']}")
|
||||
|
||||
try:
|
||||
sampled_docs = sampler.sample_documents(documents)
|
||||
print(f"\n[OK] 采样完成: {len(sampled_docs)}/{len(documents)} 文档")
|
||||
|
||||
for i, doc in enumerate(sampled_docs):
|
||||
file_size = doc.metadata.get('file_size', 0)
|
||||
sampling_rate = sampler.get_sampling_rate(file_size)
|
||||
print(f" 文档 {i+1}: 采样率 = {sampling_rate:.2f}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 采样失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
return sampled_docs
|
||||
|
||||
|
||||
def debug_chunking(documents):
|
||||
"""调试文档分块"""
|
||||
print("\n" + "=" * 60)
|
||||
print("调试: 文档分块")
|
||||
print("=" * 60)
|
||||
|
||||
chunker = DocumentChunker(chunk_size=100, chunk_overlap=20)
|
||||
|
||||
print(f"\n分块配置:")
|
||||
print(f" - 块大小: {chunker.chunk_size} 字符")
|
||||
print(f" - 重叠大小: {chunker.chunk_overlap} 字符")
|
||||
|
||||
try:
|
||||
chunks = chunker.chunk_documents(documents)
|
||||
print(f"\n[OK] 分块完成: {len(chunks)} 个块")
|
||||
|
||||
for i, chunk in enumerate(chunks[:5]): # 只显示前5个
|
||||
print(f"\n 块 {i+1}:")
|
||||
print(f" - 来源: {chunk.metadata.get('source')}")
|
||||
print(f" - 块索引: {chunk.metadata.get('chunk_index')}")
|
||||
print(f" - 起始位置: {chunk.metadata.get('chunk_start')}")
|
||||
print(f" - 结束位置: {chunk.metadata.get('chunk_end')}")
|
||||
print(f" - 内容长度: {len(chunk.page_content)} 字符")
|
||||
print(f" - 内容预览: {chunk.page_content[:50]}...")
|
||||
|
||||
if len(chunks) > 5:
|
||||
print(f"\n ... 还有 {len(chunks) - 5} 个块")
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERROR] 分块失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return None
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def create_test_data():
|
||||
"""创建测试数据"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
test_dir = Path("test_data")
|
||||
test_dir.mkdir(exist_ok=True)
|
||||
|
||||
# 创建测试文本文件
|
||||
txt_file = test_dir / "test.txt"
|
||||
with open(txt_file, 'w', encoding='utf-8') as f:
|
||||
f.write("这是一个测试文本文件。\n")
|
||||
f.write("包含多行内容。\n")
|
||||
f.write("用于测试文档加载功能。\n")
|
||||
f.write("\n")
|
||||
f.write("This is a test text file.\n")
|
||||
f.write("With multiple lines.\n")
|
||||
f.write("For testing document loading.\n")
|
||||
|
||||
# 创建测试 Markdown 文件
|
||||
md_file = test_dir / "test.md"
|
||||
with open(md_file, 'w', encoding='utf-8') as f:
|
||||
f.write("# 测试 Markdown 文件\n\n")
|
||||
f.write("## 简介\n\n")
|
||||
f.write("这是一个用于测试的 Markdown 文件。\n\n")
|
||||
f.write("## 功能\n\n")
|
||||
f.write("- 支持标题\n")
|
||||
f.write("- 支持列表\n")
|
||||
f.write("- 支持代码块\n\n")
|
||||
f.write("```python\n")
|
||||
f.write("def hello():\n")
|
||||
f.write(" print('Hello, World!')\n")
|
||||
f.write("```\n\n")
|
||||
f.write("## 结论\n\n")
|
||||
f.write("测试完成。\n")
|
||||
|
||||
print(f"[OK] 测试数据已创建:")
|
||||
print(f" - {txt_file}")
|
||||
print(f" - {md_file}")
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("\n" + "=" * 60)
|
||||
print("文件加载模块调试")
|
||||
print("=" * 60)
|
||||
print()
|
||||
|
||||
# 0. 创建测试数据
|
||||
create_test_data()
|
||||
|
||||
# 1. 调试文档加载
|
||||
documents = debug_document_loading()
|
||||
if not documents:
|
||||
print("\n调试终止: 文档加载失败")
|
||||
return
|
||||
|
||||
# 2. 调试采样
|
||||
sampled_docs = debug_sampling(documents)
|
||||
if not sampled_docs:
|
||||
print("\n调试终止: 采样失败")
|
||||
return
|
||||
|
||||
# 3. 调试分块
|
||||
chunks = debug_chunking(sampled_docs)
|
||||
if not chunks:
|
||||
print("\n调试终止: 分块失败")
|
||||
return
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("调试完成!")
|
||||
print("=" * 60)
|
||||
print(f"\n总结:")
|
||||
print(f" - 加载文档: {len(documents)} 个")
|
||||
print(f" - 采样后: {len(sampled_docs)} 个")
|
||||
print(f" - 分块后: {len(chunks)} 个")
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user