@bndynet/ragbox 0.1.0
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.
- package/README.md +765 -0
- package/README.zh-CN.md +774 -0
- package/dist/src/advanced.d.ts +13 -0
- package/dist/src/advanced.js +29 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +1013 -0
- package/dist/src/config-file.d.ts +69 -0
- package/dist/src/config-file.js +246 -0
- package/dist/src/folder-index/config.d.ts +2 -0
- package/dist/src/folder-index/config.js +56 -0
- package/dist/src/folder-index/hash.d.ts +1 -0
- package/dist/src/folder-index/hash.js +14 -0
- package/dist/src/folder-index/indexer.d.ts +2 -0
- package/dist/src/folder-index/indexer.js +154 -0
- package/dist/src/folder-index/llm-client.d.ts +3 -0
- package/dist/src/folder-index/llm-client.js +45 -0
- package/dist/src/folder-index/manifest.d.ts +17 -0
- package/dist/src/folder-index/manifest.js +158 -0
- package/dist/src/folder-index/multi-query.d.ts +45 -0
- package/dist/src/folder-index/multi-query.js +109 -0
- package/dist/src/folder-index/pageindex-runner.d.ts +3 -0
- package/dist/src/folder-index/pageindex-runner.js +218 -0
- package/dist/src/folder-index/path-utils.d.ts +5 -0
- package/dist/src/folder-index/path-utils.js +33 -0
- package/dist/src/folder-index/query.d.ts +19 -0
- package/dist/src/folder-index/query.js +597 -0
- package/dist/src/folder-index/queue.d.ts +1 -0
- package/dist/src/folder-index/queue.js +18 -0
- package/dist/src/folder-index/root-tree.d.ts +3 -0
- package/dist/src/folder-index/root-tree.js +82 -0
- package/dist/src/folder-index/scan.d.ts +14 -0
- package/dist/src/folder-index/scan.js +152 -0
- package/dist/src/folder-index/types.d.ts +368 -0
- package/dist/src/folder-index/types.js +2 -0
- package/dist/src/folder-index/watch.d.ts +17 -0
- package/dist/src/folder-index/watch.js +550 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.js +45 -0
- package/dist/src/sdk.d.ts +101 -0
- package/dist/src/sdk.js +352 -0
- package/dist/src/serve.d.ts +64 -0
- package/dist/src/serve.js +466 -0
- package/dist/src/setup-pageindex.d.ts +30 -0
- package/dist/src/setup-pageindex.js +184 -0
- package/package.json +43 -0
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
# RAGbox 中文文档
|
|
2
|
+
|
|
3
|
+
`ragbox` 可以让你从命令行、HTTP 服务或 Node.js 应用里直接询问 Markdown/MDX 文档。
|
|
4
|
+
|
|
5
|
+
它会把文档目录变成本地可查询索引,适合产品文档、API 指南、运维 runbook、内部手册和 monorepo 多包文档。基础流程不需要部署向量数据库。
|
|
6
|
+
|
|
7
|
+
适合这些场景:
|
|
8
|
+
|
|
9
|
+
- 对本地 docs 目录提问
|
|
10
|
+
- 查看一次回答用了哪些文档和章节
|
|
11
|
+
- 文档变化时持续刷新索引
|
|
12
|
+
- 通过 HTTP 把文档问答接进内部 backend
|
|
13
|
+
- 在本地、CI 和容器里使用同一套索引/查询流程
|
|
14
|
+
|
|
15
|
+
## 快速开始
|
|
16
|
+
|
|
17
|
+
默认路径按“开箱即用”设计:安装 `ragbox`,让它在当前项目里准备 PageIndex,然后直接索引和提问。
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
# 安装 CLI
|
|
21
|
+
npm install -g @bndynet/ragbox
|
|
22
|
+
|
|
23
|
+
# 克隆 PageIndex 到 ./.ragbox/PageIndex,创建 Python venv,
|
|
24
|
+
# 安装 PageIndex 依赖,并写入 ragbox.config.json
|
|
25
|
+
ragbox setup pageindex
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
继续下一步前,先编辑 `ragbox.config.json`:填好模型配置,并把 `docs.rootDir` / `docs.outputDir` 改成你的文档目录和索引目录。
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"version": 1,
|
|
33
|
+
"pageIndex": {
|
|
34
|
+
"cli": "./.ragbox/PageIndex/run_pageindex.py",
|
|
35
|
+
"python": "./.ragbox/pageindex-venv/bin/python"
|
|
36
|
+
},
|
|
37
|
+
"llm": {
|
|
38
|
+
"baseUrl": "https://api.openai.com/v1",
|
|
39
|
+
"model": "gpt-4o-mini",
|
|
40
|
+
"apiKey": "sk-..."
|
|
41
|
+
},
|
|
42
|
+
"docs": {
|
|
43
|
+
"rootDir": "./docs",
|
|
44
|
+
"outputDir": "./.ragbox-index"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
如果有多个文档目录,改用 [项目配置](#项目配置) 里的 `sources` 映射。然后索引和提问:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# 基于配置里的 docs/source 生成本地索引
|
|
53
|
+
ragbox index
|
|
54
|
+
|
|
55
|
+
# 提问
|
|
56
|
+
ragbox query "怎么配置认证?"
|
|
57
|
+
|
|
58
|
+
# 可选:文档变化时持续刷新索引
|
|
59
|
+
ragbox watch --jsonl
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
如果想用一个前台进程同时负责索引、watch 和 query HTTP API,使用 `start`:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
ragbox start
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
需要临时覆盖配置时,仍然可以显式传路径:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
ragbox index ./docs --output-dir ./.ragbox-index
|
|
72
|
+
ragbox query ./.ragbox-index "怎么配置认证?"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
如果不想把凭证写进 JSON,同样也支持通过环境变量或命令参数传入模型配置:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
export OPENAI_API_KEY=sk-...
|
|
79
|
+
export OPENAI_BASE_URL=https://api.openai.com/v1
|
|
80
|
+
|
|
81
|
+
ragbox index ./docs \
|
|
82
|
+
--output-dir ./.ragbox-index \
|
|
83
|
+
--model gpt-4o-mini
|
|
84
|
+
|
|
85
|
+
ragbox query ./.ragbox-index "怎么配置认证?" \
|
|
86
|
+
--api-key sk-... \
|
|
87
|
+
--base-url https://api.openai.com/v1 \
|
|
88
|
+
--model gpt-4o-mini
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Setup 做了什么
|
|
92
|
+
|
|
93
|
+
`ragbox setup pageindex` 会帮你准备本地 PageIndex 依赖:
|
|
94
|
+
|
|
95
|
+
- 克隆 PageIndex 到 `./.ragbox/PageIndex`
|
|
96
|
+
- 创建 `./.ragbox/pageindex-venv`
|
|
97
|
+
- 安装 PageIndex Python 依赖
|
|
98
|
+
- 把 `pageIndex.cli` 和 `pageIndex.python` 写入 `ragbox.config.json`
|
|
99
|
+
- 把 `.ragbox/` 加入 `.gitignore`
|
|
100
|
+
|
|
101
|
+
如果你已经有 PageIndex checkout,可以使用 `ragbox setup pageindex --dir ../PageIndex --skip-install`,或手动设置 `PAGEINDEX_CLI`。
|
|
102
|
+
|
|
103
|
+
## 前置条件
|
|
104
|
+
|
|
105
|
+
默认 setup 需要:
|
|
106
|
+
|
|
107
|
+
- Node.js 18 或更新版本
|
|
108
|
+
- Git,用于克隆 PageIndex
|
|
109
|
+
- 带 `venv` 和 `pip` 的 Python 3,用于安装 PageIndex 依赖
|
|
110
|
+
- 一个包含 `.md` 或 `.mdx` 的文档目录
|
|
111
|
+
- 一个兼容 OpenAI `/chat/completions` 的模型服务
|
|
112
|
+
- 模型服务 API key
|
|
113
|
+
|
|
114
|
+
## 常见使用方式
|
|
115
|
+
|
|
116
|
+
| 目标 | 使用方式 |
|
|
117
|
+
| --- | --- |
|
|
118
|
+
| 从零开始 | `npm install -g @bndynet/ragbox`,然后 `ragbox setup pageindex` |
|
|
119
|
+
| 先在一个 docs 目录试用 | `ragbox index ./docs --output-dir ./.ragbox-index`,然后 `ragbox query ./.ragbox-index "..."` |
|
|
120
|
+
| 不想每次重复路径 | 使用 `ragbox setup pageindex` 写入的 `ragbox.config.json`,或运行 `ragbox init` |
|
|
121
|
+
| 多个文档目录一起查询 | 配置 `sources`,分别跑 `ragbox index --source <name>`,再用 `ragbox query --all-sources "..."` |
|
|
122
|
+
| 调试回答质量 | `ragbox query --trace --json "..."` 或 `ragbox trace query "..."` |
|
|
123
|
+
| 检查索引是否可用 | `ragbox status ./.ragbox-index` |
|
|
124
|
+
| 诊断本地配置问题 | `ragbox doctor` |
|
|
125
|
+
| 编辑文档时自动更新索引 | `ragbox watch ./docs --output-dir ./.ragbox-index --jsonl` |
|
|
126
|
+
| 跑完整本地服务流程 | `ragbox start --auth-token <token>` |
|
|
127
|
+
| 只服务一个已经生成好的索引 | `ragbox serve ./.ragbox-index --auth-token <token>` |
|
|
128
|
+
|
|
129
|
+
## 项目配置
|
|
130
|
+
|
|
131
|
+
`ragbox setup pageindex` 会为默认本地 setup 创建或更新 `ragbox.config.json`。补上模型凭证后,典型配置如下:
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"version": 1,
|
|
136
|
+
"pageIndex": {
|
|
137
|
+
"cli": "./.ragbox/PageIndex/run_pageindex.py",
|
|
138
|
+
"python": "./.ragbox/pageindex-venv/bin/python"
|
|
139
|
+
},
|
|
140
|
+
"llm": {
|
|
141
|
+
"baseUrl": "https://api.openai.com/v1",
|
|
142
|
+
"model": "gpt-4o-mini",
|
|
143
|
+
"apiKey": "sk-..."
|
|
144
|
+
},
|
|
145
|
+
"docs": {
|
|
146
|
+
"rootDir": "./docs",
|
|
147
|
+
"outputDir": "./.ragbox-index"
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
如果你只想创建配置、不安装 PageIndex,也可以运行:
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
ragbox init
|
|
156
|
+
ragbox init --docs-dir ./content --output-dir ./.idx
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
配置文件中的相对路径会按配置文件所在目录解析。Server 端部署可以把 `baseUrl`、`model` 和 `apiKey` 一起放在私有 `ragbox.config.json`,或按环境拆成 `ragbox.config.prod.json`。如果配置文件会提交到仓库或共享给他人,就不要写真实 `apiKey`,改用环境变量或 secret manager。
|
|
160
|
+
|
|
161
|
+
只有一个文档源时,用顶层 `docs` 就够了,不需要传 `--source`。项目里确实有多个命名文档源时,再使用可选的 `sources` 映射。
|
|
162
|
+
|
|
163
|
+
之后命令会自动使用配置里的 docs:
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
ragbox index
|
|
167
|
+
ragbox query "怎么配置认证?"
|
|
168
|
+
ragbox watch --jsonl
|
|
169
|
+
ragbox start
|
|
170
|
+
ragbox --config ./ragbox.config.json index
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
如果有多个文档目录,在 `sources` 里给每个目录起一个名字。这个模式适合 monorepo、产品文档加 API 文档、多个 app/package 各自有 docs 的项目:
|
|
174
|
+
|
|
175
|
+
```json
|
|
176
|
+
{
|
|
177
|
+
"version": 1,
|
|
178
|
+
"pageIndex": {
|
|
179
|
+
"cli": "./.ragbox/PageIndex/run_pageindex.py",
|
|
180
|
+
"python": "./.ragbox/pageindex-venv/bin/python"
|
|
181
|
+
},
|
|
182
|
+
"llm": {
|
|
183
|
+
"baseUrl": "https://api.openai.com/v1",
|
|
184
|
+
"model": "gpt-4o-mini",
|
|
185
|
+
"apiKey": "sk-..."
|
|
186
|
+
},
|
|
187
|
+
"sources": {
|
|
188
|
+
"ragbox": {
|
|
189
|
+
"rootDir": "./ragbox",
|
|
190
|
+
"outputDir": "./.ragbox-index/ragbox",
|
|
191
|
+
"include": ["**/*.md", "**/*.mdx"]
|
|
192
|
+
},
|
|
193
|
+
"icharts": {
|
|
194
|
+
"rootDir": "./icharts",
|
|
195
|
+
"outputDir": "./.ragbox-index/icharts",
|
|
196
|
+
"include": ["**/*.md", "**/*.mdx"]
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
这个多 source 目录结构的可运行示例在 `./examples/ragbox.config.json`。
|
|
203
|
+
|
|
204
|
+
命名 source 分别索引,query 时可以全局查,也可以限定 source:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
ragbox index --source ragbox
|
|
208
|
+
ragbox index --source icharts
|
|
209
|
+
|
|
210
|
+
ragbox query "ragbox start 是做什么的?"
|
|
211
|
+
ragbox query --source ragbox "query trace 是怎么工作的?"
|
|
212
|
+
ragbox query --source ragbox,icharts "这些项目如何处理运行时流程?"
|
|
213
|
+
ragbox query --all-sources "当前示例有哪些文档主题?"
|
|
214
|
+
ragbox start --all-sources
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
配置了多个 source 时,`ragbox query "..."` 默认查询全部 source。`--all-sources` 是同样行为的显式写法;要缩小范围时再用 `--source`。
|
|
218
|
+
|
|
219
|
+
也可以按环境拆配置文件:
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
ragbox --config prod index
|
|
223
|
+
ragbox --config ./ragbox.config.prod.json query "怎么部署?"
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## 配置
|
|
227
|
+
|
|
228
|
+
Server 端使用时,建议把稳定配置集中写在 `ragbox.config.json`:PageIndex 路径、docs 路径、LLM `baseUrl`、`model`,以及私有配置文件里的 `apiKey`。环境变量和命令参数仍然支持,适合覆盖配置、接 secret manager,或临时运行。
|
|
229
|
+
|
|
230
|
+
配置解析优先级为:命令行参数、`ragbox.config.json`、环境变量、默认值。
|
|
231
|
+
|
|
232
|
+
| 配置 | 环境变量 | 命令参数 | 用于 | 默认值 |
|
|
233
|
+
| --- | --- | --- | --- | --- |
|
|
234
|
+
| PageIndex 脚本 | `PAGEINDEX_CLI` | `ragbox setup pageindex` 写入配置 | `index`, `watch` | 索引时必填 |
|
|
235
|
+
| Python 可执行文件 | `PAGEINDEX_PYTHON` | `--pageindex-python` | `index`, `watch` | `python3` |
|
|
236
|
+
| 输出目录 | `RAGBOX_OUTPUT_DIR` | `--output-dir` | `index`, `watch` | `<folder>/.pageindex` |
|
|
237
|
+
| 并发数 | `PAGEINDEX_CONCURRENCY` | `--concurrency` | `index`, `watch` | `1` |
|
|
238
|
+
| API Base URL | `OPENAI_BASE_URL` | `--base-url` | `index`, `watch`, `query` | `https://api.openai.com/v1` |
|
|
239
|
+
| API Key | `OPENAI_API_KEY` | `--api-key` | `index`, `watch`, `query` | query 必填,PageIndex 通常也需要 |
|
|
240
|
+
| 模型 | `PAGEINDEX_MODEL`, `LLM_MODEL` | `--model` | `index`, `watch`, `query` | `gpt-4o-mini` |
|
|
241
|
+
| Serve host | `RAGBOX_SERVE_HOST` | `--host` | `serve` | `127.0.0.1` |
|
|
242
|
+
| Serve port | `RAGBOX_SERVE_PORT` | `--port` | `serve` | `8787` |
|
|
243
|
+
| Serve token | `RAGBOX_SERVE_TOKEN` | `--auth-token` | `serve` | 无 |
|
|
244
|
+
| Watch debounce | `RAGBOX_WATCH_DEBOUNCE_MS` | `--debounce-ms` | `watch` | `500` |
|
|
245
|
+
| Watch 重试次数 | `RAGBOX_WATCH_RETRY_ATTEMPTS` | `--retry-attempts` | `watch` | `0` |
|
|
246
|
+
| Watch 重试延迟 | `RAGBOX_WATCH_RETRY_DELAY_MS` | `--retry-delay-ms` | `watch` | `1000` |
|
|
247
|
+
| Watch 锁文件 | `RAGBOX_WATCH_LOCK_FILE` | `--lock-file` | `watch` | 无 |
|
|
248
|
+
| Watch staging | `RAGBOX_WATCH_STAGING` | `--staging` | `watch` | 关闭 |
|
|
249
|
+
| Watch staging 输出目录 | `RAGBOX_WATCH_STAGING_OUTPUT_DIR` | `--staging-output-dir` | `watch` | `<outputDir>.staging` |
|
|
250
|
+
| Watch health 文件 | `RAGBOX_WATCH_HEALTH_FILE` | `--health-file` | `watch` | 无 |
|
|
251
|
+
| Watch webhook | `RAGBOX_WATCH_WEBHOOK_URL` | `--webhook` | `watch` | 无 |
|
|
252
|
+
|
|
253
|
+
私有 server 配置可以把 `llm.apiKey` 写进 JSON,让部署自包含。配置文件会提交、共享或交给平台 secret 管理时,不要在 JSON 里写真实 key,改用 `OPENAI_API_KEY`。`--api-key` 适合本地测试,但可能出现在 shell history 或进程列表里。
|
|
254
|
+
|
|
255
|
+
## 命令说明
|
|
256
|
+
|
|
257
|
+
这一节是命令参考。如果你是第一次使用,建议先看 [快速开始](#快速开始) 和 [常见使用方式](#常见使用方式)。
|
|
258
|
+
|
|
259
|
+
### `ragbox setup pageindex`
|
|
260
|
+
|
|
261
|
+
把 PageIndex 克隆到 `./.ragbox/PageIndex`,创建 `./.ragbox/pageindex-venv`,安装 PageIndex Python 依赖,更新 `ragbox.config.json`,并把 `.ragbox/` 加入 `.gitignore`。
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
ragbox setup pageindex
|
|
265
|
+
ragbox setup pageindex --ref v0.1.0
|
|
266
|
+
ragbox setup pageindex --skip-install
|
|
267
|
+
ragbox setup pageindex --dir ../PageIndex --no-write-config
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
自动化场景可以使用 `--json`。如果项目用其它方式管理本地生成工具,可以使用 `--no-gitignore`。
|
|
271
|
+
|
|
272
|
+
### `ragbox init`
|
|
273
|
+
|
|
274
|
+
只创建 `ragbox.config.json`,不安装 PageIndex。适合你想手动编辑路径,或自己管理 PageIndex 的场景。
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
ragbox init
|
|
278
|
+
ragbox init --docs-dir ./content --output-dir ./.idx
|
|
279
|
+
ragbox init --output ./configs/ragbox.config.json --force
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### `ragbox index <folder>`
|
|
283
|
+
|
|
284
|
+
为 Markdown/MDX 文档目录生成或更新本地索引。使用 `query` 或 `serve` 前需要先执行它。
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
ragbox index ./docs
|
|
288
|
+
ragbox index ./docs --output-dir ./.ragbox-index
|
|
289
|
+
ragbox index ./docs --output-dir ./.ragbox-index --json
|
|
290
|
+
ragbox index ./docs --output-dir /var/lib/ragbox/docs-index --concurrency 2
|
|
291
|
+
ragbox index ./docs --pageindex-python /opt/venvs/pageindex/bin/python
|
|
292
|
+
ragbox index ./docs --base-url https://api.openai.com/v1 --model gpt-4o-mini
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
它会扫描 `**/*.md` 和 `**/*.mdx`,计算文件 hash,只重新索引新增、修改、之前失败的文件,并跳过未变化的 ready 文件。
|
|
296
|
+
|
|
297
|
+
如果有文档索引失败,普通输出会继续把统计信息写到 stdout,并把失败文档路径和 PageIndex 错误写到 stderr。
|
|
298
|
+
|
|
299
|
+
使用 `--json` 可以输出带版本号的机器可读结果,包含输出路径、统计信息和失败文档明细:
|
|
300
|
+
|
|
301
|
+
```json
|
|
302
|
+
{
|
|
303
|
+
"version": 1,
|
|
304
|
+
"command": "index",
|
|
305
|
+
"rootDir": "/repo/docs",
|
|
306
|
+
"outputDir": "/repo/.ragbox-index",
|
|
307
|
+
"manifestPath": "/repo/.ragbox-index/manifest.json",
|
|
308
|
+
"rootTreePath": "/repo/.ragbox-index/root-tree.json",
|
|
309
|
+
"generatedAt": "2026-01-01T00:00:00.000Z",
|
|
310
|
+
"counts": {
|
|
311
|
+
"total": 12,
|
|
312
|
+
"ready": 12,
|
|
313
|
+
"failed": 0,
|
|
314
|
+
"added": 12,
|
|
315
|
+
"modified": 0,
|
|
316
|
+
"retryFailed": 0,
|
|
317
|
+
"unchanged": 0,
|
|
318
|
+
"deleted": 0
|
|
319
|
+
},
|
|
320
|
+
"failures": []
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
### `ragbox inspect [target]`
|
|
325
|
+
|
|
326
|
+
查看索引里有哪些文档、每篇文档的状态和统计信息。适合确认“到底索引进去了什么”。
|
|
327
|
+
|
|
328
|
+
```bash
|
|
329
|
+
ragbox inspect ./.ragbox-index
|
|
330
|
+
ragbox inspect --source ragbox
|
|
331
|
+
ragbox inspect --all-sources --json
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### `ragbox status [target]`
|
|
335
|
+
|
|
336
|
+
检查索引是否已经可以 query。适合 CI、部署脚本和 smoke check。
|
|
337
|
+
|
|
338
|
+
```bash
|
|
339
|
+
ragbox status ./.ragbox-index
|
|
340
|
+
ragbox status --all-sources
|
|
341
|
+
ragbox status --json
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### `ragbox doctor [target]`
|
|
345
|
+
|
|
346
|
+
检查本地配置、PageIndex CLI 路径、LLM 设置、API key 是否存在,以及索引是否有效。这个命令不会发起网络请求。
|
|
347
|
+
|
|
348
|
+
```bash
|
|
349
|
+
ragbox doctor
|
|
350
|
+
ragbox doctor --source ragbox --json
|
|
351
|
+
ragbox doctor --all-sources
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### `ragbox query [target] <question>`
|
|
355
|
+
|
|
356
|
+
基于 docs 目录或已有索引目录回答问题。如果传 docs 目录,目录下需要有默认的 `.pageindex` 索引。
|
|
357
|
+
|
|
358
|
+
```bash
|
|
359
|
+
ragbox query ./docs "怎么配置认证?"
|
|
360
|
+
ragbox query ./.ragbox-index "部署步骤是什么?"
|
|
361
|
+
ragbox query ./docs/.pageindex "怎么配置认证?"
|
|
362
|
+
ragbox query ./.ragbox-index "怎么配置认证?" --model gpt-4o-mini --api-key sk-...
|
|
363
|
+
ragbox query ./.ragbox-index "怎么配置认证?" --json
|
|
364
|
+
ragbox query ./.ragbox-index "怎么配置认证?" --trace
|
|
365
|
+
ragbox trace query ./.ragbox-index "怎么配置认证?"
|
|
366
|
+
ragbox query "部署步骤是什么?"
|
|
367
|
+
ragbox query --source ragbox,icharts "这些项目如何处理运行时流程?"
|
|
368
|
+
ragbox query --all-sources "部署步骤是什么?"
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
这里建议使用和索引时相同的 `--base-url`,通常是 OpenAI-compatible 根地址,例如 `https://api.openai.com/v1`。如果某些代理只能提供完整接口地址,`query` 也兼容完整的 `/chat/completions` URL。
|
|
372
|
+
|
|
373
|
+
显式传 target 时,第一个参数可以是:
|
|
374
|
+
|
|
375
|
+
- docs 目录,里面有 `.pageindex/manifest.json` 和 `.pageindex/root-tree.json`
|
|
376
|
+
- 索引输出目录,里面有:
|
|
377
|
+
|
|
378
|
+
```text
|
|
379
|
+
manifest.json
|
|
380
|
+
root-tree.json
|
|
381
|
+
indexes/
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
查询流程:
|
|
385
|
+
|
|
386
|
+
1. 读取 `manifest.json` 和 `root-tree.json`
|
|
387
|
+
2. 让 LLM 从文档树中选择相关文档
|
|
388
|
+
3. 读取相关文档的 PageIndex JSON
|
|
389
|
+
4. 去掉节点里的 `text` 字段,只让 LLM 基于结构选择相关节点
|
|
390
|
+
5. 回到完整 JSON 中取出选中节点的 `text`
|
|
391
|
+
6. 把这些文本拼成上下文,让 LLM 生成最终答案
|
|
392
|
+
|
|
393
|
+
对多个配置 source,`ragbox query "..."` 默认查询全部 source。可以用 `--source` 传逗号分隔的名字来缩小范围,也可以用 `--all-sources` 显式表达全局查询。多源 query 会对每个 source 执行正常的结构化查询流程,然后让 LLM 基于各 source 选出的片段融合成一个最终回答。来源引用会加上 source 前缀,例如 `ragbox:start-command.md#n1`。
|
|
394
|
+
|
|
395
|
+
使用 `--json` 可以输出带版本号的结果契约。单 source query 返回 `QueryResult`;多 source query 返回融合后的 `answer`、每个 source 的 `results`、带 source 前缀的 `sources`、`warnings` 和 `timingsMs`。
|
|
396
|
+
|
|
397
|
+
单 source `QueryResult` 字段:
|
|
398
|
+
|
|
399
|
+
- `answer`:最终回答文本
|
|
400
|
+
- `contextBytes` 和 `contextTokens`:最终 answer context 的大小;tokens 为估算值
|
|
401
|
+
- `selectedDocuments`:从 `root-tree.json` 中选中的文档,包含 `selectionReason`,必要时包含 `skipReason`
|
|
402
|
+
- `selectedNodes`:每篇文档中选中的 PageIndex 节点,包含 `selectionReason`、可选 `skipReason` 和 `textBytes`
|
|
403
|
+
- `sources`:最终回答使用的来源引用和节点文本
|
|
404
|
+
- `warnings`:不可用文档、缺失节点或空上下文等提醒
|
|
405
|
+
- `timingsMs`:解析、选择和生成回答的耗时
|
|
406
|
+
- `trace`:只有使用 `--trace` 或 `ragbox trace query` 时才会出现,包含文档/节点选择阶段的 LLM 原始响应、prompt/response 字节数、context 大小和非致命失败记录
|
|
407
|
+
|
|
408
|
+
致命 query 错误会带上失败阶段,例如 `Query failed during select-documents: ...`。
|
|
409
|
+
|
|
410
|
+
### `ragbox start [folder]`
|
|
411
|
+
|
|
412
|
+
运行完整本地服务流程:先生成初始索引,再持续 watch 文档变化,同时启动 HTTP query API。
|
|
413
|
+
|
|
414
|
+
```bash
|
|
415
|
+
ragbox start
|
|
416
|
+
ragbox start --auth-token dev-token
|
|
417
|
+
ragbox start --host 127.0.0.1 --port 8787 --jsonl
|
|
418
|
+
ragbox start --source ragbox
|
|
419
|
+
ragbox start --all-sources
|
|
420
|
+
ragbox start ./docs --output-dir ./.ragbox-index
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
当你已经通过 `ragbox setup pageindex` 准备好默认本地配置,并希望用一个前台进程跑本地开发、内网服务或容器时,优先使用 `start`。它会等初始索引完成后再启动 HTTP `serve`,之后每次 watch 成功更新索引,都会刷新 serve 里的索引快照。
|
|
424
|
+
|
|
425
|
+
配置了多个 source 时,`ragbox start` 默认启动全部 source。可以用 `--source ragbox,icharts` 限定范围,也可以用 `--all-sources` 显式表达全局启动。
|
|
426
|
+
|
|
427
|
+
`start` 不会创建或修改 `ragbox.config.json`;默认本地 setup 先运行 `ragbox setup pageindex`,如果你想自己管理 PageIndex,再用 `ragbox init` 手动配置。
|
|
428
|
+
|
|
429
|
+
### `ragbox serve [target]`
|
|
430
|
+
|
|
431
|
+
启动一个前台 HTTP 服务,供外部系统通过 REST API 查询文档。使用前先通过 `ragbox index` 生成索引,或者用 `ragbox watch` 持续刷新索引。
|
|
432
|
+
|
|
433
|
+
```bash
|
|
434
|
+
ragbox serve ./.ragbox-index \
|
|
435
|
+
--host 127.0.0.1 \
|
|
436
|
+
--port 8787 \
|
|
437
|
+
--auth-token dev-token
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
多 source 项目可以直接基于配置文件启动:
|
|
441
|
+
|
|
442
|
+
```bash
|
|
443
|
+
ragbox serve --config ./ragbox.config.json --host 0.0.0.0 --port 8787
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
Public HTTP contract:
|
|
447
|
+
|
|
448
|
+
- `GET /`:公开服务入口,返回 health 摘要和 endpoint 列表。
|
|
449
|
+
- `GET /health`:公开 readiness endpoint,适合 load balancer、Kubernetes、systemd 和 smoke check。所有已知索引都可 query 时返回 200,否则返回 503。
|
|
450
|
+
- `GET /indexes`:返回当前服务端缓存的索引校验快照。配置 token 后需要 `Authorization: Bearer <token>`。
|
|
451
|
+
- `POST /query`:基于单个 target、选定 source 或全部 source 回答问题。配置 token 后需要鉴权。
|
|
452
|
+
- `POST /reload`:重新读取 config/source target,并刷新服务端索引校验快照。配置 token 后需要鉴权。
|
|
453
|
+
|
|
454
|
+
单索引请求:
|
|
455
|
+
|
|
456
|
+
```bash
|
|
457
|
+
curl http://127.0.0.1:8787/
|
|
458
|
+
curl http://127.0.0.1:8787/health
|
|
459
|
+
|
|
460
|
+
curl -H "Authorization: Bearer dev-token" \
|
|
461
|
+
http://127.0.0.1:8787/indexes
|
|
462
|
+
|
|
463
|
+
curl -X POST http://127.0.0.1:8787/query \
|
|
464
|
+
-H "Authorization: Bearer dev-token" \
|
|
465
|
+
-H "Content-Type: application/json" \
|
|
466
|
+
-d '{"question":"怎么配置认证?","trace":true}'
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
多 source 请求:
|
|
470
|
+
|
|
471
|
+
```bash
|
|
472
|
+
curl -X POST http://localhost:8787/query \
|
|
473
|
+
-H "Content-Type: application/json" \
|
|
474
|
+
-d '{"source":"ragbox","question":"ragbox start 是做什么的?"}'
|
|
475
|
+
|
|
476
|
+
curl -X POST http://localhost:8787/query \
|
|
477
|
+
-H "Content-Type: application/json" \
|
|
478
|
+
-d '{"source":["ragbox","icharts"],"question":"这些项目如何处理运行时流程?"}'
|
|
479
|
+
|
|
480
|
+
curl -X POST http://localhost:8787/query \
|
|
481
|
+
-H "Content-Type: application/json" \
|
|
482
|
+
-d '{"allSources":true,"question":"当前示例有哪些文档主题?"}'
|
|
483
|
+
|
|
484
|
+
curl -X POST http://localhost:8787/reload
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
`serve` 首版面向本地服务、内网服务、container sidecar 和 docs backend。不要把 `.ragbox-index` 作为静态目录直接暴露,因为里面可能包含源文档正文。浏览器 widget 不应该直接携带 ragbox token;建议先请求自己的 backend,由 backend 负责用户登录、限流和审计,再转发给 `ragbox serve`。生产环境建议绑定 localhost 或内网地址,并配置 `--auth-token` 或 `RAGBOX_SERVE_TOKEN`。
|
|
488
|
+
|
|
489
|
+
### `ragbox watch <folder>`
|
|
490
|
+
|
|
491
|
+
先执行一次索引,然后监听文档变化并增量更新。
|
|
492
|
+
|
|
493
|
+
```bash
|
|
494
|
+
ragbox watch ./docs
|
|
495
|
+
ragbox watch ./docs --output-dir ./.ragbox-index
|
|
496
|
+
ragbox watch ./docs --output-dir /var/lib/ragbox/docs-index --concurrency 2
|
|
497
|
+
ragbox watch ./docs --base-url https://api.openai.com/v1 --model gpt-4o-mini
|
|
498
|
+
ragbox watch ./docs --output-dir ./.ragbox-index --jsonl
|
|
499
|
+
ragbox watch ./docs \
|
|
500
|
+
--output-dir /var/lib/ragbox/docs-index \
|
|
501
|
+
--staging \
|
|
502
|
+
--retry-attempts 3 \
|
|
503
|
+
--retry-delay-ms 2000 \
|
|
504
|
+
--lock-file /var/run/ragbox/docs.lock \
|
|
505
|
+
--health-file /var/run/ragbox/docs-health.json \
|
|
506
|
+
--jsonl
|
|
507
|
+
```
|
|
508
|
+
|
|
509
|
+
`watch` 监听 `.md` 和 `.mdx` 文件的新增、修改、删除。它会忽略 `node_modules`、`.git`、`.pageindex`、`dist`、`build`,以及位于文档目录内的自定义输出目录。
|
|
510
|
+
|
|
511
|
+
使用 `--jsonl` 可以为集成场景输出带版本号的 JSON Lines 事件流。事件包括 `watch-start`、`watch-lock-acquired`、`watch-file-event`、`watch-index-start`、`watch-index-retry`、`watch-index-partial-failure`、`watch-output-promoted`、`watch-index-done`、`watch-index-failed`、`watch-health`、`watch-webhook-failed`、`watch-lock-released`、`watch-stop` 和 `index-progress`。
|
|
512
|
+
|
|
513
|
+
生产化 watch 选项:
|
|
514
|
+
|
|
515
|
+
- `--retry-attempts` 和 `--retry-delay-ms` 会重试抛错的索引运行,以及仍然留下 failed document 的运行。
|
|
516
|
+
- `--lock-file` 会在 watch 运行期间创建排他锁文件。第二个 watcher 如果发现锁已存在,会直接退出。
|
|
517
|
+
- `--staging` 会先索引到 staging 目录,只有在零 failed document 的干净运行后才 promote 到 active output。默认 staging 目录是 `<outputDir>.staging`;为了基于 rename 切换,建议和 `outputDir` 放在同一文件系统。
|
|
518
|
+
- `--health-file` 会写 readiness JSON,包含 `status`、`ok`、`pid`、`lastSuccessAt`、`lastFailureAt` 和最新统计。
|
|
519
|
+
- `--webhook` 会把每个 watch 事件作为 JSON POST 出去。Webhook 投递失败会报告为 `watch-webhook-failed` 事件,不会中断 watch。
|
|
520
|
+
- `--debounce-ms` 控制文件变化后等待多久再重新索引。
|
|
521
|
+
|
|
522
|
+
`ragbox watch` 有意以前台进程运行,这更适合 systemd service 或 container。建议使用 supervisor/container 的 restart policy,而不是在 CLI 里自行 fork daemon。
|
|
523
|
+
|
|
524
|
+
## 输出目录
|
|
525
|
+
|
|
526
|
+
默认输出:
|
|
527
|
+
|
|
528
|
+
```text
|
|
529
|
+
docs/.pageindex/
|
|
530
|
+
manifest.json
|
|
531
|
+
root-tree.json
|
|
532
|
+
indexes/
|
|
533
|
+
<stable-doc-id>.pageindex.json
|
|
534
|
+
state/
|
|
535
|
+
file-state.json
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
自定义输出:
|
|
539
|
+
|
|
540
|
+
```bash
|
|
541
|
+
ragbox index ./docs --output-dir ./.ragbox-index
|
|
542
|
+
ragbox query ./.ragbox-index "..."
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
输出目录可能包含源文档正文和元数据。如果文档是私有的,不要把输出目录公开暴露。
|
|
546
|
+
|
|
547
|
+
## 生产使用建议
|
|
548
|
+
|
|
549
|
+
常见方式:
|
|
550
|
+
|
|
551
|
+
- 一个前台进程要同时负责索引、watch 和 serve 时,直接运行 `ragbox start`
|
|
552
|
+
- 部署时执行 `ragbox index`,再通过 `ragbox serve` 或 SDK 查询完成后的索引目录
|
|
553
|
+
- 文档会独立变化时,把 `ragbox watch` 作为后台服务运行
|
|
554
|
+
|
|
555
|
+
建议:
|
|
556
|
+
|
|
557
|
+
- 长期运行 `watch` 时,优先使用 `--jsonl`、`--lock-file`、`--health-file`、`--retry-attempts` 和 `--staging`
|
|
558
|
+
- 把输出目录放在源码目录外,例如 `/var/lib/ragbox/docs-index`
|
|
559
|
+
- 多副本应用需要读取同一份完整索引,可以挂载只读卷或随部署产物分发
|
|
560
|
+
- API key 可以放私有 server 配置、环境变量或 secret manager;不要提交真实 key
|
|
561
|
+
- 当 `serve` 不只绑定 localhost 时,使用 `RAGBOX_SERVE_TOKEN` 或 `--auth-token`
|
|
562
|
+
- 先用 `--concurrency 1`,确认 PageIndex 和模型服务限流后再提高
|
|
563
|
+
- 如果要求零停机更新,可以先索引到 staging 目录,成功后再切换读目录
|
|
564
|
+
|
|
565
|
+
私有 server 配置示例:
|
|
566
|
+
|
|
567
|
+
```json
|
|
568
|
+
{
|
|
569
|
+
"version": 1,
|
|
570
|
+
"pageIndex": {
|
|
571
|
+
"cli": "/opt/PageIndex/run_pageindex.py",
|
|
572
|
+
"python": "/opt/pageindex-venv/bin/python"
|
|
573
|
+
},
|
|
574
|
+
"llm": {
|
|
575
|
+
"baseUrl": "https://api.openai.com/v1",
|
|
576
|
+
"model": "gpt-4o-mini",
|
|
577
|
+
"apiKey": "sk-..."
|
|
578
|
+
},
|
|
579
|
+
"docs": {
|
|
580
|
+
"rootDir": "/srv/app/docs",
|
|
581
|
+
"outputDir": "/var/lib/ragbox/docs-index"
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
```bash
|
|
587
|
+
ragbox --config ./ragbox.config.prod.json index --concurrency 2
|
|
588
|
+
ragbox --config ./ragbox.config.prod.json query "怎么配置认证?"
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
### 后台运行
|
|
592
|
+
|
|
593
|
+
`ragbox start` 有意以前台进程运行。Server 部署时,建议交给进程管理器托管,这样关闭终端、SSH 断开或进程崩溃后都能继续运行或自动重启。
|
|
594
|
+
|
|
595
|
+
临时测试可以用 `nohup`:
|
|
596
|
+
|
|
597
|
+
```bash
|
|
598
|
+
nohup ragbox --config ./ragbox.config.prod.json start > ragbox.log 2>&1 &
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
Linux server 推荐用 `systemd`:
|
|
602
|
+
|
|
603
|
+
```ini
|
|
604
|
+
[Unit]
|
|
605
|
+
Description=ragbox service
|
|
606
|
+
After=network.target
|
|
607
|
+
|
|
608
|
+
[Service]
|
|
609
|
+
WorkingDirectory=/srv/ragbox
|
|
610
|
+
ExecStart=/usr/local/bin/ragbox --config ./ragbox.config.prod.json start
|
|
611
|
+
Restart=always
|
|
612
|
+
RestartSec=5
|
|
613
|
+
Environment=NODE_ENV=production
|
|
614
|
+
|
|
615
|
+
[Install]
|
|
616
|
+
WantedBy=multi-user.target
|
|
617
|
+
```
|
|
618
|
+
|
|
619
|
+
```bash
|
|
620
|
+
sudo systemctl enable ragbox
|
|
621
|
+
sudo systemctl start ragbox
|
|
622
|
+
sudo systemctl status ragbox
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
如果使用 Node 生态,也可以用 `pm2`:
|
|
626
|
+
|
|
627
|
+
```bash
|
|
628
|
+
pm2 start "ragbox --config ./ragbox.config.prod.json start" --name ragbox
|
|
629
|
+
pm2 save
|
|
630
|
+
pm2 startup
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
容器部署时,让 `ragbox start` 保持为前台命令,然后使用平台的 restart policy,例如 Docker `--restart unless-stopped` 或 Kubernetes `restartPolicy`。
|
|
634
|
+
|
|
635
|
+
## 在 Node.js 中使用 ragbox
|
|
636
|
+
|
|
637
|
+
当你的 Node.js 服务需要创建索引、查询文档、校验索引,或以编程方式启动 `serve` 时,可以使用 SDK。
|
|
638
|
+
|
|
639
|
+
```js
|
|
640
|
+
const {
|
|
641
|
+
createIndex,
|
|
642
|
+
inspectIndex,
|
|
643
|
+
queryIndex,
|
|
644
|
+
startServe,
|
|
645
|
+
validateIndex,
|
|
646
|
+
watchIndex
|
|
647
|
+
} = require("@bndynet/ragbox");
|
|
648
|
+
|
|
649
|
+
await createIndex("/srv/app/docs", {
|
|
650
|
+
configPath: "./ragbox.config.json",
|
|
651
|
+
outputDir: "/var/lib/ragbox/docs-index",
|
|
652
|
+
pageIndexCli: "/opt/PageIndex/run_pageindex.py"
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
const result = await queryIndex(
|
|
656
|
+
"/var/lib/ragbox/docs-index",
|
|
657
|
+
"怎么配置认证?"
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
console.log(result.answer);
|
|
661
|
+
console.log(result.sources);
|
|
662
|
+
|
|
663
|
+
const validation = await validateIndex("/var/lib/ragbox/docs-index");
|
|
664
|
+
console.log(validation.ok);
|
|
665
|
+
|
|
666
|
+
const server = await startServe({
|
|
667
|
+
target: "/var/lib/ragbox/docs-index",
|
|
668
|
+
port: 8787,
|
|
669
|
+
authToken: process.env.RAGBOX_SERVE_TOKEN
|
|
670
|
+
});
|
|
671
|
+
console.log(server.url);
|
|
672
|
+
await server.close();
|
|
673
|
+
|
|
674
|
+
const inspect = await inspectIndex("/var/lib/ragbox/docs-index");
|
|
675
|
+
console.log(inspect.counts);
|
|
676
|
+
|
|
677
|
+
const watcher = await watchIndex("/srv/app/docs", {
|
|
678
|
+
outputDir: "/var/lib/ragbox/docs-index",
|
|
679
|
+
pageIndexCli: "/opt/PageIndex/run_pageindex.py",
|
|
680
|
+
onEvent: (event) => console.log(event)
|
|
681
|
+
});
|
|
682
|
+
await watcher.ready;
|
|
683
|
+
await watcher.close();
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
自定义 LLM client:
|
|
687
|
+
|
|
688
|
+
```js
|
|
689
|
+
const { queryIndex, startServe } = require("@bndynet/ragbox");
|
|
690
|
+
|
|
691
|
+
const llmClient = {
|
|
692
|
+
async chatCompletion(request) {
|
|
693
|
+
// request.messages, request.model, request.temperature
|
|
694
|
+
return await callYourModelGateway(request);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
|
|
698
|
+
const result = await queryIndex(
|
|
699
|
+
"/var/lib/ragbox/docs-index",
|
|
700
|
+
"怎么配置认证?",
|
|
701
|
+
{
|
|
702
|
+
llmClient,
|
|
703
|
+
model: "internal-docs-model"
|
|
704
|
+
}
|
|
705
|
+
);
|
|
706
|
+
|
|
707
|
+
const server = await startServe({
|
|
708
|
+
target: "/var/lib/ragbox/docs-index",
|
|
709
|
+
llmClient,
|
|
710
|
+
model: "internal-docs-model",
|
|
711
|
+
port: 8787
|
|
712
|
+
});
|
|
713
|
+
```
|
|
714
|
+
|
|
715
|
+
`llmClient` 是一个只用于 SDK 的薄 provider 边界,负责 query 阶段的直接 chat completion。它适合接本地模型、内部模型网关、重试、超时、日志和测试 mock。`ragbox` 不会从配置文件动态加载 provider 插件;CLI 仍然使用 flags、config 和环境变量里的 OpenAI-compatible 设置。
|
|
716
|
+
|
|
717
|
+
包根入口导出稳定的产品化 SDK API。底层工具仍保留在 `advanced` namespace,适合更定制的集成:
|
|
718
|
+
|
|
719
|
+
```js
|
|
720
|
+
const { advanced } = require("@bndynet/ragbox");
|
|
721
|
+
|
|
722
|
+
const location = await advanced.resolveQueryIndexLocation("/var/lib/ragbox/docs-index");
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
## 查询时发生了什么
|
|
726
|
+
|
|
727
|
+
简单说,`ragbox` 会保留文档结构,而不是一开始就把所有内容切成匿名 chunk:
|
|
728
|
+
|
|
729
|
+
- 每个 `.md`/`.mdx` 文件会生成一棵结构化 PageIndex 树
|
|
730
|
+
- 文档目录会生成一份索引清单
|
|
731
|
+
- 查询时先选择可能相关的文档,再选择文档里的相关章节
|
|
732
|
+
- 最终回答只基于选中的章节正文生成
|
|
733
|
+
|
|
734
|
+
所以 `--trace` 能告诉你选了哪些文档和节点。基础流程也因此不需要部署向量数据库。
|
|
735
|
+
|
|
736
|
+
## 与传统 Vector DB RAG 的对比
|
|
737
|
+
|
|
738
|
+
传统 Vector RAG 通常会切 chunk、做 embedding,再按向量相似度召回。`ragbox` 则优先保留源文档层级,并让 LLM 基于这棵结构树做选择。
|
|
739
|
+
|
|
740
|
+
| 维度 | Vector DB RAG | `ragbox` |
|
|
741
|
+
| --- | --- | --- |
|
|
742
|
+
| 索引单位 | 文本 chunk | Markdown/MDX 文件和 PageIndex 节点 |
|
|
743
|
+
| 检索信号 | 向量相似度 | LLM 基于文档树和节点树选择 |
|
|
744
|
+
| 存储 | 向量数据库加文档存储 | 输出目录下的本地 JSON 文件 |
|
|
745
|
+
| 上下文形态 | 扁平 chunk 列表 | 带文件路径和 node id 的结构化节点 |
|
|
746
|
+
| 优势 | 大规模模糊召回快 | 保留文档层级,引用来源更清晰 |
|
|
747
|
+
| 取舍 | 需要 embedding 和索引基础设施 | 依赖 PageIndex 质量和 LLM 选择效果 |
|
|
748
|
+
|
|
749
|
+
两种方式也可以组合:先用向量检索做大范围候选召回,再用 PageIndex 树做结构化过滤、上下文组织和引用生成。
|
|
750
|
+
|
|
751
|
+
## 常见问题
|
|
752
|
+
|
|
753
|
+
- `PAGEINDEX_CLI is required to run PageIndex`:运行 `ragbox setup pageindex`,或设置 `PAGEINDEX_CLI=/path/to/run_pageindex.py`
|
|
754
|
+
- `OPENAI_API_KEY is required for query`:在私有 `ragbox.config.json` 里添加 `llm.apiKey`,或设置 `OPENAI_API_KEY`,也可以临时传 `--api-key`
|
|
755
|
+
- `Expected a docs folder... or a ragbox output directory`:`query` 的第一个参数可以传带 `.pageindex/` 的 docs 目录,也可以直接传索引输出目录
|
|
756
|
+
- `PageIndex completed but no generated JSON result was found`:默认情况下,ragbox 会读取 PageIndex 写到 `results/` 里的 JSON。如果你使用的自定义 wrapper 只支持显式输出路径,把 `PAGEINDEX_OUTPUT_ARG` 或 `pageIndex.outputArg` 设置成它的输出路径参数名。
|
|
757
|
+
|
|
758
|
+
## 限制
|
|
759
|
+
|
|
760
|
+
- 需要你本地已经安装并配置 PageIndex;`ragbox setup pageindex` 可以准备默认的本地 checkout 和虚拟环境
|
|
761
|
+
- 查询质量依赖 PageIndex JSON 结构和所使用的 LLM
|
|
762
|
+
- 当前基础流程是树结构选择,不是向量检索
|
|
763
|
+
|
|
764
|
+
## 贡献者开发
|
|
765
|
+
|
|
766
|
+
```bash
|
|
767
|
+
npm install
|
|
768
|
+
npm run build
|
|
769
|
+
npm run ragbox -- --help
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
### Examples
|
|
773
|
+
|
|
774
|
+
可运行的本地 fixture 和 smoke-test 命令都放在 [`examples/README.md`](./examples/README.md)。需要用真实 PageIndex 和 LLM 配置验证 index、query、多 source 或 `start` 服务循环时,看那里即可。
|