-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
289 lines (261 loc) · 10.4 KB
/
Copy pathcli.py
File metadata and controls
289 lines (261 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
📚 LEARN: CLI — Command-Line Interface
========================================
This is how users interact with the RAG system from the terminal.
We use Python's built-in argparse module (no external dependencies).
Usage:
python cli.py ingest --source <file_or_url> Ingest a document
python cli.py query "your question" Ask a question
python cli.py list List indexed documents
"""
import argparse
import sys
from rag.pipeline import RAGPipeline
def main():
parser = argparse.ArgumentParser(
description="🔍 RAG From Scratch — Ask questions about your documents",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python cli.py ingest --source sample_data/sample.md
python cli.py ingest --source https://example.com
python cli.py query "What is machine learning?"
python cli.py query "What is overfitting?" --verbose
python cli.py list
""",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# ── Ingest command ─────────────────────────────────────────
'''
add_parser("ingest", ...):创建一个专门处理 ingest 子命令的解析器。返回的 ingest_parser 可以继续添加参数。
add_argument():为这个子命令添加参数。
"--source", "-s":同时支持长选项和短选项。-s 是 --source 的别名。
required=True:该参数必须提供,否则 argparse 会报错。
type=int:将输入的字符串转换为整数类型(如果转换失败则报错)。
default=500:如果用户未提供该选项,则使用默认值 500。
help:帮助信息中显示的说明。
参数 是否必需 说明
--source 或 -s ✅ 必需 指定要摄入的文档路径(本地文件)或 URL。
--chunk-size ❌ 可选(默认 500) 分块的目标字符数。
--overlap ❌ 可选(默认 100) 相邻块之间的重叠字符数。
'''
ingest_parser = subparsers.add_parser(
"ingest", help="Ingest a document (PDF, text, or URL)"
)
ingest_parser.add_argument(
"--source", "-s",
required=True,
help="Path to a file or URL to ingest",
)
ingest_parser.add_argument(
"--chunk-size",
type=int,
default=500,
help="Target chunk size in characters (default: 500)",
)
ingest_parser.add_argument(
"--overlap",
type=int,
default=100,
help="Overlap between chunks in characters (default: 100)",
)
# ── Query command ──────────────────────────────────────────
'''
位置参数 "question":没有 -- 前缀的参数是位置参数,用户必须按顺序提供。例如 query "What is AI?",字符串 "What is AI?" 会赋值给 args.question。
choices=[...]:限定参数的值只能是指定列表中的某一个,否则报错。
action="store_true":布尔标志。如果命令行中出现 --rerank,则 args.rerank = True;否则为 False。常用于开关选项。
'''
query_parser = subparsers.add_parser(
"query", help="Ask a question about your documents"
)
query_parser.add_argument(
"question",
help="Your question in natural language",
)
query_parser.add_argument(
"--top-k", "-k",
type=int,
default=5,
help="Number of chunks to retrieve (default: 5)",
)
query_parser.add_argument(
"--threshold", "-t",
type=float,
default=0.3,
help="Minimum similarity score (default: 0.3)",
)
query_parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Show retrieved chunks before the answer",
)
query_parser.add_argument(
"--search-mode", "-m",
choices=["vector", "keyword", "hybrid"],
default="vector",
help="Search mode: vector (default), keyword (BM25), or hybrid (both)",
)
query_parser.add_argument(
"--rerank",
action="store_true",
help="Rerank results with LLM for higher precision (slower)",
)
# ── List command ───────────────────────────────────────────
subparsers.add_parser("list", help="List all indexed documents")
# ── Agentic Query command ─────────────────────────────────
agentic_parser = subparsers.add_parser(
"agentic-query", help="Ask a question using the ReAct agent (iterative retrieval)"
)
agentic_parser.add_argument(
"question",
help="Your question in natural language",
)
agentic_parser.add_argument(
"--max-iterations", "-i",
type=int,
default=5,
help="Maximum ReAct loop iterations (default: 5)",
)
agentic_parser.add_argument(
"--top-k", "-k",
type=int,
default=5,
help="Default number of chunks to retrieve per iteration (default: 5)",
)
agentic_parser.add_argument(
"--threshold", "-t",
type=float,
default=0.3,
help="Minimum similarity score (default: 0.3)",
)
agentic_parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Show the full Thought/Action/Observation reasoning trace",
)
agentic_parser.add_argument(
"--search-mode", "-m",
choices=["vector", "keyword", "hybrid"],
default="vector",
help="Default search mode (default: vector). The agent may switch modes during iterations.",
)
agentic_parser.add_argument(
"--rerank",
action="store_true",
help="Rerank results with LLM for higher precision (slower)",
)
# ── Eval command ───────────────────────────────────────────
eval_parser = subparsers.add_parser(
"eval", help="Run evaluation against test questions"
)
eval_parser.add_argument(
"--test-file",
default="eval/test_questions.json",
help="Path to test questions JSON (default: eval/test_questions.json)",
)
eval_parser.add_argument(
"--top-k", "-k",
type=int,
default=5,
help="Number of chunks to retrieve (default: 5)",
)
eval_parser.add_argument(
"--threshold", "-t",
type=float,
default=0.3,
help="Minimum similarity score (default: 0.3)",
)
eval_parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Show detailed results per question",
)
eval_parser.add_argument(
"--search-mode", "-m",
choices=["vector", "keyword", "hybrid"],
default="vector",
help="Search mode: vector (default), keyword (BM25), or hybrid (both)",
)
eval_parser.add_argument(
"--rerank",
action="store_true",
help="Rerank results with LLM for higher precision (slower)",
)
# ── Parse and execute ──────────────────────────────────────
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
if args.command == "ingest":
pipeline = RAGPipeline(
chunk_size=args.chunk_size,
chunk_overlap=args.overlap,
)
pipeline.ingest(args.source)
elif args.command == "query":
pipeline = RAGPipeline(
top_k=args.top_k,
threshold=args.threshold,
search_mode=args.search_mode,
use_reranker=args.rerank,
)
# Auto-ingest sample documents on first run
pipeline.auto_ingest()
result = pipeline.query(args.question, verbose=args.verbose)
# Show sources
if result["sources"]:
print(f"\n📚 Sources: {', '.join(result['sources'])}")
elif args.command == "list":
pipeline = RAGPipeline()
# Auto-ingest sample documents on first run
pipeline.auto_ingest()
sources = pipeline.list_documents()
if sources:
print("📚 Indexed documents:")
for s in sources:
print(f" • {s}")
else:
print("No documents indexed yet. Run 'ingest' first.")
elif args.command == "agentic-query":
from rag.agentic_pipeline import AgenticRAGPipeline
pipeline = AgenticRAGPipeline(
max_iterations=args.max_iterations,
top_k=args.top_k,
threshold=args.threshold,
search_mode=args.search_mode,
use_reranker=args.rerank,
)
# Auto-ingest sample documents on first run
pipeline.auto_ingest()# auto_ingest()方法 没有返回值(返回 None),也没有删除 self 或重置任何标志。
# 它仅仅 修改对象内部状态(往 self.store 中添加一些文档)。
# 调用完成后,pipeline 对象已经拥有了索引数据,接下来调用 query() 自然可以正常工作。
result = pipeline.query(args.question, verbose=args.verbose)
# Show sources
if result["sources"]:
print(f"\n📚 Sources: {', '.join(result['sources'])}")
print(f"🔄 Iterations: {result['iterations']}")
elif args.command == "eval":
from rag.evaluator import load_test_questions, run_evaluation, save_results, print_scorecard
# Auto-ingest if needed
pipeline = RAGPipeline()
pipeline.auto_ingest()
# Load test questions
print(f"📋 Loading test questions from: {args.test_file}")
questions = load_test_questions(args.test_file)
print(f" → {len(questions)} questions loaded\n")
# Run evaluation
results = run_evaluation(
questions,
top_k=args.top_k,
threshold=args.threshold,
verbose=args.verbose,
search_mode=args.search_mode,
use_reranker=args.rerank,
)
# Print scorecard
print_scorecard(results)
# Save results
filepath = save_results(results)
print(f"\n💾 Results saved to: {filepath}")
if __name__ == "__main__":
main()