API 参考
统一前缀 /api/v1。除 /api/v1/health(裸 JSON)与 /admin(HTML 页面)外,API 端点均返回统一包装:
{"code": 0, "data": {}, "message": "ok"}
路由总表(当前真正注册的 19 条)
app/main.py 只 include 了 auth_router、search_router、document_router、knowledge_router,外加模块内定义的 /api/v1/health 与 /admin。
| # | 方法 | 路径 | 鉴权 | 成功状态码 | 说明 |
|---|---|---|---|---|---|
| 1 | GET | /api/v1/health |
无 | 200 | 健康检查,裸返回 {"status":"ok"} |
| 2 | POST | /api/v1/search |
无 | 200 | 分层检索 |
| 3 | POST | /api/v1/documents |
JWT | 202 | JSON 文本异步入库 |
| 4 | GET | /api/v1/documents/tasks/{task_id} |
JWT | 200 | 入库任务状态 |
| 5 | GET | /api/v1/documents |
JWT | 200 | 文档列表(游标分页) |
| 6 | GET | /api/v1/documents/{doc_id} |
JWT | 200 | 文档详情 |
| 7 | DELETE | /api/v1/documents/{doc_id} |
JWT + admin | 200 | 删除文档(幂等) |
| 8 | GET | /api/v1/knowledge/categories |
无 | 200 | 类目集 |
| 9 | GET | /api/v1/knowledge/stats |
无 | 200 | 四层点数 + L1 类目分布 |
| 10 | POST | /api/v1/auth/login |
无 | 200 | 登录,签发 session token |
| 11 | GET | /api/v1/auth/me |
session | 200 | 当前用户 |
| 12 | POST | /api/v1/auth/logout |
session | 200 | 退出登录 |
| 13 | POST | /api/v1/auth/password |
session | 200 | 修改自己的密码 |
| 14 | GET | /api/v1/auth/users |
session + admin | 200 | 用户列表 |
| 15 | POST | /api/v1/auth/users |
session + admin | 200 | 创建用户 |
| 16 | PATCH | /api/v1/auth/users/{username} |
session + admin | 200 | 改角色 / 启用状态 |
| 17 | POST | /api/v1/auth/users/{username}/password |
session + admin | 200 | 重置他人密码 |
| 18 | DELETE | /api/v1/auth/users/{username} |
session + admin | 200 | 删除用户 |
| 19 | GET | /admin |
无 | 200 | 返回 app/static/admin.html |
「JWT」与「session」是两套不同体系,见下文「鉴权」一节。
鉴权
| 体系 | 携带方式 | 校验位置 | 适用端点 |
|---|---|---|---|
| session | Authorization: Bearer <token>,token 由 /auth/login 返回(64 位 hex) |
app/api/deps.py |
/auth/* |
| JWT | Authorization: Bearer <jwt> |
app/core/auth.py |
/documents*(全部 5 条) |
- session 端点:
get_current_user校验session:{token}是否存在 → 取user:{username}→ 校验enabled与must_change_password;logout/password走_resolve_user,放行must_change_password。 - JWT 端点:
decode_token验签后按sub查询auth:user:{username},查不到则降级用 JWT payload 构造AuthUser。 - 两套 token 与用户存储互不相通:用
/auth/login的 token 调/documents会验签失败(1005)。详见 已知限制与未实现。 DELETE /documents/{doc_id}用require_admin,因此实际要求 admin 角色(README 写作「admin/user 均可」)。
端点细节
1. GET /api/v1/health
curl -s http://localhost:8000/api/v1/health
# {"status":"ok"}
2. POST /api/v1/search
请求体(app/models/search.py SearchRequest):
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
query |
str | 是 | 查询文本 |
top_k |
int | null | 否 | 最终返回条数;空则用 retrieval_final_k=5 |
summarize |
bool | 否 | 默认 false;true 时调用 LLM 生成 summary |
curl -s -X POST http://localhost:8000/api/v1/search \
-H "Content-Type: application/json" \
-d '{"query": "如何配置 Redis 缓存", "top_k": 5, "summarize": true}'
响应 data(SearchResponse):
| 字段 | 类型 | 说明 |
|---|---|---|
query |
str | 原始 query |
hits[] |
array | text、doc_id、title、section_path、score、doc_summary |
routed_categories |
string[] | 路由命中的类目名 |
fallback |
bool | 是否走全库兜底 |
extracted_info |
object | null | rewrite、keywords、entities、intent、time_range、categories |
summary |
str | null | 仅 summarize=true 时非空 |
缓存:键 search:{sha256(query|top_k|summarize)[:16]},TTL cache_ttl=300;命中时直接返回缓存的完整包装体。
3. POST /api/v1/documents
请求体(DocumentInput):text(必填)、title、source、metadata(dict[str,str])。
curl -s -X POST http://localhost:8000/api/v1/documents \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <token>" \
-d '{"text": "正文……", "title": "安装说明", "source": "manual"}'
成功返回 HTTP 202:
{"code": 0, "data": {"task_id": "3f2a...", "status": "pending"}, "message": "ok"}
text 去空白后为空 → code=1001「文档内容不能为空」。
4. GET /api/v1/documents/tasks/{task_id}
返回任务记录:task_id、status、created_at、updated_at、result、error、filename、source。
status∈pending/summarizing/classifying/embedding/writing/done/faileddone时result为IngestionResult:document_id、summary、category、collection、chunks_count、tags、category_confidence、deduplicatedfailed时error为{stage, message, partial_summary}- 不存在 →
code=1004
5. GET /api/v1/documents
查询参数:limit(默认 20,1~100)、offset(游标字符串,空则首页)。
{"code":0,"data":{"items":[{"doc_id":"...","title":"...","category":"技术文档","tags":["Redis"],"summary":"..."}],"next_offset":null},"message":"ok"}
6. GET /api/v1/documents/{doc_id}
{"code":0,"data":{"l1":{"doc_id":"...","title":"...","category":"...","tags":[],"text":"...","metadata":{}},"l2_nodes":[{"doc_id":"...","section_path":"...","text":"...","category":"...","tags":[]}],"l3_nodes":[],"chunks_count":12,"file":null},"message":"ok"}
file 仅当 L1 metadata.raw_file_path 非空时返回 {filename, size_bytes, url};url 指向 /api/v1/documents/{doc_id}/file,该路由不存在。文档不存在 → code=1004。
7. DELETE /api/v1/documents/{doc_id}
{"code": 0, "data": {"doc_id": "...", "deleted": {"doc_l1": 1, "doc_l2": 3, "doc_l3": 5, "chunks": 12}, "deleted_total": 21}, "message": "ok"}
幂等:不存在的 doc_id 各集合计数为 0,仍返回成功。
8-9. 知识库
GET /knowledge/categories:
{"code": 0, "data": {"categories": [{"name": "技术文档", "description": "..."}], "count": 8}, "message": "ok"}
GET /knowledge/stats:
{"code":0,"data":{"collections":{"doc_l1":10,"doc_l2":42,"doc_l3":87,"chunks":130},"categories":{"技术文档":6,"uncategorized":4},"uncategorized_count":4,"documents_total":10},"message":"ok"}
10-18. 认证与用户
| 端点 | 请求体 | 响应 data |
|---|---|---|
POST /auth/login |
{username, password} |
{token, username, role, must_change_password} |
GET /auth/me |
— | {username, role, must_change_password, enabled, created_at} |
POST /auth/logout |
— | {logged_out: true} |
POST /auth/password |
{old_password, new_password} |
{username} |
GET /auth/users |
— | [{username, role, must_change_password, enabled, created_at}] |
POST /auth/users |
{username, password, role?} |
脱敏后的用户记录 |
PATCH /auth/users/{username} |
{role?, enabled?}(至少一项) |
脱敏后的用户记录 |
POST /auth/users/{username}/password |
{new_password} |
{username} |
DELETE /auth/users/{username} |
— | {username} |
curl -s -X POST http://localhost:8000/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "<password>"}'
# {"code":0,"data":{"token":"<64位hex>","username":"admin","role":"admin","must_change_password":false},"message":"ok"}
规则(均来自 app/core/users.py 与 app/api/v1/auth.py):
- 用户名
^[a-zA-Z0-9_-]{2,32}$,密码至少 8 位 - 被禁用用户登录 → 1005;
PATCH enabled=false会立即清除该用户全部 session PATCH空 body → 1001;非法 role → 1001;最后一个 admin 降级/禁用 → 1001DELETE删除自己 → 1001;删除最后一个 admin → 1001;成功时清除该用户全部 session- 重置密码成功 → 清除该用户全部 session
19. GET /admin
返回 app/static/admin.html(include_in_schema=False)。仓库中该文件为 91 KB 旧版单文件管理页;frontend/dist 被挂载到 app/static/admin,但 main.py 没有为 SPA 子路径注册路由。
错误码
| code | 含义 | 触发位置 |
|---|---|---|
| 0 | 成功 | — |
| 1001 | 请求参数校验失败 / 业务参数非法 | RequestValidationError 处理器、空文档、非法用户名、弱密码、最后管理员保护 |
| 1003 | 未提供认证凭证 | core/auth.get_current_user |
| 1004 | 资源不存在 | 任务不存在、文档不存在、用户不存在 |
| 1005 | 未认证 / token 无效或过期 / 账号已禁用 / 用户名或密码错误 | 两套鉴权模块 |
| 1006 | 权限不足 / 首次登录须先改密 | deps 与 core/auth |
| 2000 | 服务器内部错误(含检索失败、Qdrant 操作失败) | 全局 Exception 处理器、各端点包装 |
| 2001 | 认证存储不可用 | SessionStoreError / UserStoreError 转换 |
app/core/auth.py 还定义了 1007 用户名已存在、1008 凭证错误、1009 注册关闭,但当前无端点使用这三个常量。
当前不存在的端点
以下端点被 README.md / CLAUDE.md / 前端代码引用,但 app/api/v1/*.py 中没有对应路由,访问返回 FastAPI 默认 404(不套统一包装):
| 方法 | 路径 | 引用方 |
|---|---|---|
| POST | /api/v1/documents/upload |
README、frontend/src/api/documents.js、ApiDocs.vue、Ingest.vue |
| POST | /api/v1/documents/upload-batch |
CLAUDE.md、documents.js、Ingest.vue |
| POST | /api/v1/documents/{doc_id}/reingest |
CLAUDE.md、documents.js、Library.vue |
| GET | /api/v1/documents/{doc_id}/file |
CLAUDE.md、qdrant.get_doc_detail() 生成的 file.url |
| GET | /api/v1/documents/tasks |
CLAUDE.md、documents.js、Ingest.vue、TasksDrawer.vue |
| POST | /api/v1/documents/tasks/{task_id}/retry |
documents.js、Ingest.vue |
| DELETE | /api/v1/documents/tasks/{task_id} |
documents.js、Ingest.vue |
| POST | /api/v1/auth/register |
README、CLAUDE.md |
| GET/PUT/POST | /api/v1/settings、/settings/schema、/settings/reset |
app/api/v1/settings.py 已定义但未注册;frontend/src/api/settings.js、Settings.vue |
IngestTaskManager 内部已实现 list_tasks()、retry()、delete(),只是没有暴露为路由。