isage-rag-benchmark 0.1.0.1__cp311-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- isage_rag_benchmark-0.1.0.1.dist-info/METADATA +63 -0
- isage_rag_benchmark-0.1.0.1.dist-info/RECORD +48 -0
- isage_rag_benchmark-0.1.0.1.dist-info/WHEEL +5 -0
- isage_rag_benchmark-0.1.0.1.dist-info/licenses/LICENSE +21 -0
- isage_rag_benchmark-0.1.0.1.dist-info/top_level.txt +1 -0
- sage/__init__.py +0 -0
- sage/benchmark_rag/__init__.py +16 -0
- sage/benchmark_rag/_version.py +4 -0
- sage/benchmark_rag/config/config_bm25s.yaml +51 -0
- sage/benchmark_rag/config/config_dense_milvus.yaml +61 -0
- sage/benchmark_rag/config/config_hf.yaml +43 -0
- sage/benchmark_rag/config/config_mixed.yaml +53 -0
- sage/benchmark_rag/config/config_monitoring_demo.yaml +59 -0
- sage/benchmark_rag/config/config_multiplex.yaml +79 -0
- sage/benchmark_rag/config/config_qa_chroma.yaml +51 -0
- sage/benchmark_rag/config/config_ray.yaml +57 -0
- sage/benchmark_rag/config/config_refiner.yaml +75 -0
- sage/benchmark_rag/config/config_rerank.yaml +56 -0
- sage/benchmark_rag/config/config_selfrag.yaml +24 -0
- sage/benchmark_rag/config/config_source.yaml +30 -0
- sage/benchmark_rag/config/config_source_local.yaml +21 -0
- sage/benchmark_rag/config/config_sparse_milvus.yaml +49 -0
- sage/benchmark_rag/evaluation/__init__.py +10 -0
- sage/benchmark_rag/evaluation/benchmark_runner.py +337 -0
- sage/benchmark_rag/evaluation/config/benchmark_config.yaml +35 -0
- sage/benchmark_rag/evaluation/evaluate_results.py +389 -0
- sage/benchmark_rag/implementations/__init__.py +31 -0
- sage/benchmark_rag/implementations/pipelines/__init__.py +24 -0
- sage/benchmark_rag/implementations/pipelines/qa_bm25_retrieval.py +55 -0
- sage/benchmark_rag/implementations/pipelines/qa_dense_retrieval.py +56 -0
- sage/benchmark_rag/implementations/pipelines/qa_dense_retrieval_chroma.py +71 -0
- sage/benchmark_rag/implementations/pipelines/qa_dense_retrieval_milvus.py +78 -0
- sage/benchmark_rag/implementations/pipelines/qa_dense_retrieval_mixed.py +58 -0
- sage/benchmark_rag/implementations/pipelines/qa_dense_retrieval_ray.py +174 -0
- sage/benchmark_rag/implementations/pipelines/qa_hf_model.py +57 -0
- sage/benchmark_rag/implementations/pipelines/qa_monitoring_demo.py +139 -0
- sage/benchmark_rag/implementations/pipelines/qa_multimodal_fusion.py +318 -0
- sage/benchmark_rag/implementations/pipelines/qa_multiplex.py +91 -0
- sage/benchmark_rag/implementations/pipelines/qa_refiner.py +91 -0
- sage/benchmark_rag/implementations/pipelines/qa_rerank.py +76 -0
- sage/benchmark_rag/implementations/pipelines/qa_sparse_retrieval_milvus.py +76 -0
- sage/benchmark_rag/implementations/pipelines/selfrag.py +226 -0
- sage/benchmark_rag/implementations/tools/__init__.py +17 -0
- sage/benchmark_rag/implementations/tools/build_chroma_index.py +261 -0
- sage/benchmark_rag/implementations/tools/build_milvus_dense_index.py +86 -0
- sage/benchmark_rag/implementations/tools/build_milvus_index.py +59 -0
- sage/benchmark_rag/implementations/tools/build_milvus_sparse_index.py +85 -0
- sage/benchmark_rag/implementations/tools/loaders/document_loaders.py +42 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from sage.common.utils.config.loader import load_config
|
|
5
|
+
from sage.libs.rag import CharacterSplitter
|
|
6
|
+
from sage.libs.rag.document_loaders import TextLoader
|
|
7
|
+
from sage.middleware.operators.rag import MilvusSparseRetriever
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def load_knowledge_to_milvus(config):
|
|
11
|
+
"""
|
|
12
|
+
增量加载多文件知识库到 Milvus(Sparse 版本)
|
|
13
|
+
"""
|
|
14
|
+
knowledge_files = config.get("preload_knowledge_file")
|
|
15
|
+
if not isinstance(knowledge_files, list):
|
|
16
|
+
knowledge_files = [knowledge_files]
|
|
17
|
+
|
|
18
|
+
persistence_path = config.get("milvus_sparse").get("persistence_path")
|
|
19
|
+
collection_name = config.get("milvus_sparse").get("collection_name")
|
|
20
|
+
|
|
21
|
+
print("=== 增量加载知识库到 Milvus ===")
|
|
22
|
+
print(f"DB: {persistence_path} | 集合: {collection_name}")
|
|
23
|
+
|
|
24
|
+
print("初始化Milvus...")
|
|
25
|
+
milvus_backend = MilvusSparseRetriever(config)
|
|
26
|
+
|
|
27
|
+
all_chunks = []
|
|
28
|
+
|
|
29
|
+
for file_path in knowledge_files:
|
|
30
|
+
if not os.path.exists(file_path):
|
|
31
|
+
print(f"⚠ 文件不存在,跳过: {file_path}")
|
|
32
|
+
continue
|
|
33
|
+
|
|
34
|
+
print(f"\n=== 处理文件: {file_path} ===")
|
|
35
|
+
loader = TextLoader(file_path)
|
|
36
|
+
document = loader.load()
|
|
37
|
+
print(f"已加载文本,长度: {len(document['content'])}")
|
|
38
|
+
|
|
39
|
+
splitter = CharacterSplitter({"separator": "\n\n"})
|
|
40
|
+
chunks = splitter.execute(document)
|
|
41
|
+
print(f"分块数: {len(chunks)}")
|
|
42
|
+
|
|
43
|
+
all_chunks.extend(chunks)
|
|
44
|
+
print(f"✓ 已准备 {len(chunks)} 个文本块")
|
|
45
|
+
|
|
46
|
+
if all_chunks:
|
|
47
|
+
milvus_backend.add_documents(all_chunks)
|
|
48
|
+
print(f"\n✓ 已写入 {len(all_chunks)} 个文本块到集合 {collection_name}")
|
|
49
|
+
print(f"✓ 数据库信息: {milvus_backend.get_collection_info()}")
|
|
50
|
+
|
|
51
|
+
# 简单检索测试
|
|
52
|
+
text_query = "什么是ChromaDB?"
|
|
53
|
+
results = milvus_backend.execute(text_query)
|
|
54
|
+
print(f"检索结果: {results}")
|
|
55
|
+
|
|
56
|
+
# 测试检索
|
|
57
|
+
text_query = "RAG 系统的主要优势是什么?"
|
|
58
|
+
results = milvus_backend.execute(text_query)
|
|
59
|
+
print(f"检索结果: {results}")
|
|
60
|
+
else:
|
|
61
|
+
print("⚠ 没有有效的知识文件,未写入任何数据")
|
|
62
|
+
|
|
63
|
+
return True
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
# 检查是否在测试模式下运行
|
|
68
|
+
if os.getenv("SAGE_EXAMPLES_MODE") == "test" or os.getenv("SAGE_TEST_MODE") == "true":
|
|
69
|
+
print("🧪 Test mode detected - build_milvus_sparse_index example")
|
|
70
|
+
print("✅ Test passed: Example structure validated")
|
|
71
|
+
sys.exit(0)
|
|
72
|
+
|
|
73
|
+
config_path = "./examples/config/config_sparse_milvus.yaml"
|
|
74
|
+
if not os.path.exists(config_path):
|
|
75
|
+
print(f"配置文件不存在: {config_path}")
|
|
76
|
+
print("Please create the configuration file first.")
|
|
77
|
+
sys.exit(1)
|
|
78
|
+
|
|
79
|
+
config = load_config(config_path)
|
|
80
|
+
result = load_knowledge_to_milvus(config["retriever"])
|
|
81
|
+
if result:
|
|
82
|
+
print("知识库已成功加载,可运行检索/问答脚本")
|
|
83
|
+
else:
|
|
84
|
+
print("知识库加载失败")
|
|
85
|
+
sys.exit(1)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""
|
|
2
|
+
document_loaders.py
|
|
3
|
+
SAGE RAG 示例:文本加载工具
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TextLoader:
|
|
10
|
+
"""
|
|
11
|
+
加载文本文件,每行为一个文档。
|
|
12
|
+
支持简单的分块和元数据。
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self, filepath: str, encoding: str = "utf-8"):
|
|
16
|
+
self.filepath = filepath
|
|
17
|
+
self.encoding = encoding
|
|
18
|
+
|
|
19
|
+
def load(self) -> list[dict]:
|
|
20
|
+
"""
|
|
21
|
+
加载文本文件,返回文档列表,每个文档为 dict: {"content": ..., "metadata": ...}
|
|
22
|
+
"""
|
|
23
|
+
documents = []
|
|
24
|
+
if not os.path.exists(self.filepath):
|
|
25
|
+
raise FileNotFoundError(f"File not found: {self.filepath}")
|
|
26
|
+
with open(self.filepath, encoding=self.encoding) as f:
|
|
27
|
+
for idx, line in enumerate(f):
|
|
28
|
+
text = line.strip()
|
|
29
|
+
if text:
|
|
30
|
+
documents.append(
|
|
31
|
+
{
|
|
32
|
+
"content": text,
|
|
33
|
+
"metadata": {"line": idx + 1, "source": self.filepath},
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
return documents
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# 用法示例:
|
|
40
|
+
# loader = TextLoader('data/qa_knowledge_base.txt')
|
|
41
|
+
# docs = loader.load()
|
|
42
|
+
# print(docs[0]["content"])
|