Commit a1c6a382 a1c6a38267d1db3e61cc3e9e5c107a927d3c9ca0 by 沈秋雨

Add testing workflow guide

1 parent 128018ac
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
2 2
3 独立的 WeKnora Ragas 评估项目。它只调用 WeKnora 公开 API,不依赖 WeKnora 内置的 `/evaluation` 接口。 3 独立的 WeKnora Ragas 评估项目。它只调用 WeKnora 公开 API,不依赖 WeKnora 内置的 `/evaluation` 接口。
4 4
5 完整服务器测试流程见 [TESTING_GUIDE.md](TESTING_GUIDE.md)
6
5 ## 安装 7 ## 安装
6 8
7 ```bash 9 ```bash
......
1 # WeKnora Ragas 评估测试流程指南
2
3 本文档用于在服务器上从零验证 WeKnora Ragas 独立评估项目是否可运行。
4
5 ## 1. 前置条件
6
7 确认服务器满足:
8
9 - Python 3.10 或更高版本。不要使用 Python 3.6。
10 - WeKnora API 可访问,例如 `http://localhost:9090/api/v1`
11 - vLLM 已提供 OpenAI-compatible Chat Completions endpoint,例如 `http://localhost:8000/v1`
12 - Infinity 已提供 OpenAI-compatible Embeddings endpoint,例如 `http://localhost:7997/v1`
13 - 可选:Infinity reranker endpoint 可访问,例如 `http://localhost:7998/v1`
14
15 检查 Python:
16
17 ```bash
18 python3 --version
19 python3.10 --version
20 python3.11 --version
21 ```
22
23 推荐使用 Python 3.11:
24
25 ```bash
26 cd /data/weknora_ragas
27 python3.11 -m venv .venv
28 source .venv/bin/activate
29 python --version
30 pip install -U pip setuptools wheel
31 pip install -e ".[pdf]"
32 ```
33
34 如果只跑 XLSX 或文本型 PDF,可以先安装基础依赖:
35
36 ```bash
37 pip install -e .
38 ```
39
40 ## 2. 配置 `.env`
41
42 复制示例文件:
43
44 ```bash
45 cp .env.example .env
46 ```
47
48 编辑 `.env`
49
50 ```bash
51 WEKNORA_BASE_URL=http://localhost:9090/api/v1
52 WEKNORA_API_KEY=your-weknora-api-key
53 WEKNORA_KB_ID=
54 WEKNORA_KB_NAME=ragas-eval-pilot
55
56 RAGAS_LLM_API_KEY=EMPTY
57 RAGAS_LLM_BASE_URL=http://localhost:8000/v1
58 RAGAS_GENERATOR_MODEL=your-vllm-model-id
59 RAGAS_JUDGE_MODEL=your-vllm-model-id
60
61 RAGAS_EMBEDDING_API_KEY=EMPTY
62 RAGAS_EMBEDDING_BASE_URL=http://localhost:7997/v1
63 RAGAS_EMBEDDING_MODEL=your-embedding-model-id
64
65 RAGAS_RERANKER_API_KEY=EMPTY
66 RAGAS_RERANKER_BASE_URL=http://localhost:7998/v1
67 RAGAS_RERANKER_MODEL=your-reranker-model-id
68
69 TESTSET_SIZE=10
70 REQUEST_INTERVAL_SECONDS=0.2
71 ```
72
73 如果服务没有鉴权,`RAGAS_*_API_KEY` 仍建议填 `EMPTY`,避免 OpenAI client 因空 key 报错。
74
75 确认模型 ID:
76
77 ```bash
78 curl http://localhost:8000/v1/models
79 curl http://localhost:7997/v1/models
80 ```
81
82 把返回的 `id` 精确填入 `RAGAS_JUDGE_MODEL``RAGAS_EMBEDDING_MODEL`
83
84 ## 3. 服务连通性检查
85
86 先检查 WeKnora 知识库:
87
88 ```bash
89 python scripts/00_create_kb.py
90 ```
91
92 如果 `.env``WEKNORA_KB_ID` 为空,该脚本会调用:
93
94 ```text
95 POST /api/v1/knowledge-bases
96 {"name": "..."}
97 ```
98
99 创建成功后会把 ID 写回 `.env`
100
101 再检查模型服务:
102
103 ```bash
104 python scripts/00_check_models.py
105 ```
106
107 期望输出包括:
108
109 ```text
110 [OK] Generator LLM
111 [OK] Judge LLM
112 [OK] Embedding
113 All configured model services are reachable.
114 ```
115
116 如果配置了 reranker,也会检查:
117
118 ```text
119 [OK] Reranker
120 ```
121
122 如果 reranker 当前不用,可以让 `RAGAS_RERANKER_BASE_URL``RAGAS_RERANKER_MODEL` 为空,脚本会跳过。
123
124 ## 4. 准备 Pilot 数据
125
126 首轮不要直接跑大规模数据。建议:
127
128 - 2 个 PDF。
129 - 1 个 XLSX。
130 - `TESTSET_SIZE=10`
131
132 放置文件:
133
134 ```bash
135 mkdir -p data/raw_docs/pdf data/raw_docs/xlsx
136 cp /path/to/*.pdf data/raw_docs/pdf/
137 cp /path/to/*.xlsx data/raw_docs/xlsx/
138 ```
139
140 ## 5. 执行完整 Pilot
141
142 按顺序执行:
143
144 ```bash
145 python scripts/01_upload_docs.py
146 python scripts/02_wait_ingestion.py
147 python scripts/03_export_chunks.py
148 python scripts/04_parse_docs.py
149 python scripts/05_generate_testset.py
150 python scripts/06_review_testset.py
151 python scripts/07_run_weknora_qa.py
152 python scripts/08_build_ragas_input.py
153 python scripts/09_run_ragas_eval.py
154 python scripts/10_report.py
155 ```
156
157 说明:
158
159 - `01_upload_docs.py` 上传 `data/raw_docs/pdf``data/raw_docs/xlsx`
160 - `02_wait_ingestion.py` 等待 WeKnora 解析完成。
161 - `03_export_chunks.py` 导出 WeKnora chunks。
162 - `04_parse_docs.py` 在评估侧解析原始文档,生成 Ragas 测试集来源。
163 - `05_generate_testset.py` 生成候选 QA。
164 - `06_review_testset.py` 当前会把候选 QA 标为 approved,后续可替换为人工审核。
165 - `07_run_weknora_qa.py` 逐条调用 WeKnora 问答并解析 SSE。
166 - `08_build_ragas_input.py` 合并 QA 和 WeKnora 输出。
167 - `09_run_ragas_eval.py` 调用 Ragas 打分。
168 - `10_report.py` 生成 Markdown 报告。
169
170 ## 6. 产物验收
171
172 检查这些文件是否生成:
173
174 ```bash
175 ls -lh data/exported/knowledge.jsonl
176 ls -lh data/exported/chunks.jsonl
177 ls -lh data/parsed_docs/documents.jsonl
178 ls -lh data/parsed_docs/parse_summary.json
179 ls -lh data/testsets/testset.reviewed.jsonl
180 ls -lh data/runs/weknora_answers.jsonl
181 ls -lh data/runs/ragas_input.jsonl
182 ls -lh data/reports/ragas_scores.csv
183 ls -lh data/reports/summary.md
184 ```
185
186 快速检查关键字段:
187
188 ```bash
189 python - <<'PY'
190 import json
191 from pathlib import Path
192
193 for path in [
194 "data/exported/chunks.jsonl",
195 "data/parsed_docs/documents.jsonl",
196 "data/runs/weknora_answers.jsonl",
197 "data/runs/ragas_input.jsonl",
198 ]:
199 rows = [json.loads(line) for line in Path(path).read_text(encoding="utf-8").splitlines() if line.strip()]
200 print(path, len(rows))
201 if rows:
202 print(rows[0].keys())
203 PY
204 ```
205
206 最低验收标准:
207
208 - `data/exported/chunks.jsonl` 非空。
209 - `data/parsed_docs/documents.jsonl` 非空。
210 - `data/runs/weknora_answers.jsonl` 中大部分 `response` 非空。
211 - `data/runs/ragas_input.jsonl``retrieved_contexts` 非空比例合理。
212 - `data/reports/ragas_scores.csv` 至少有一项指标列。
213 - `data/reports/summary.md` 可读。
214
215 ## 7. 常见故障
216
217 ### Python 版本过低
218
219 现象:
220
221 ```text
222 Could not find a version that satisfies the requirement setuptools>=68
223 ```
224
225 原因:当前虚拟环境是 Python 3.6。项目要求 Python 3.10+。
226
227 处理:
228
229 ```bash
230 rm -rf .venv
231 python3.11 -m venv .venv
232 source .venv/bin/activate
233 pip install -U pip setuptools wheel
234 pip install -e ".[pdf]"
235 ```
236
237 ### 模型 endpoint 填错
238
239 vLLM 和 Infinity 都要填 OpenAI-compatible `/v1` 地址,例如:
240
241 ```bash
242 RAGAS_LLM_BASE_URL=http://localhost:8000/v1
243 RAGAS_EMBEDDING_BASE_URL=http://localhost:7997/v1
244 ```
245
246 不要填 Ollama 原生 `/api` 或服务根路径。
247
248 ### Embedding 报 invalid input type
249
250 项目已经在 `ragas_runner.py` 中设置:
251
252 ```text
253 tiktoken_enabled=False
254 check_embedding_ctx_length=False
255 ```
256
257 如果仍报错,优先用 `scripts/00_check_models.py` 确认 Infinity endpoint 是否兼容 OpenAI embeddings API。
258
259 ### Ragas 指标超时或 NaN
260
261 本地或小模型 judge 可能无法稳定输出 Ragas 需要的结构化结果。先缩小指标集,例如只保留:
262
263 ```yaml
264 metrics:
265 - response_relevancy
266 ```
267
268 确认链路通后,再逐个打开:
269
270 ```yaml
271 - faithfulness
272 - context_precision
273 - context_recall
274 - factual_correctness
275 ```
276
277 也可以调大:
278
279 ```yaml
280 timeout_seconds: 600
281 max_workers: 1
282 max_tokens: 4096
283 ```
284
285 ### WeKnora 问答没有 retrieved_contexts
286
287 检查:
288
289 ```bash
290 python scripts/03_export_chunks.py
291 python scripts/07_run_weknora_qa.py
292 ```
293
294 重点看:
295
296 - 知识库是否解析完成。
297 - chunks 是否导出非空。
298 - WeKnora 问答 SSE 是否返回 `references` 事件。
299 - `data/runs/failed_requests.jsonl` 中是否记录 `empty_retrieval`
300
301 ## 8. 扩大样本规模
302
303 首轮 10 条样本通过后,再扩大:
304
305 ```bash
306 TESTSET_SIZE=50
307 ```
308
309 再逐步扩大到 100-300 条。每次扩大前先确认:
310
311 - Ragas judge 延迟可接受。
312 - `failed_requests.jsonl` 中失败比例低。
313 - `summary.md` 中检索失败样本可解释。
314 - QA 集经过人工审核或抽样审核。