本手册聚焦“批量提问”能力:如何一次提交多条问题给 LightRAG 服务器,并获取答案或结构化检索数据。内容包含前置条件、请求体参数说明、curl 与 PowerShell 一行命令示例、响应结构以及大规模批量(如 500 条问题)的实践建议。
- 已启动 LightRAG 服务器(默认端口
9621):- 访问 Swagger:
http://localhost:9621/docs - 访问 ReDoc:
http://localhost:9621/redoc - 健康检查:
curl.exe http://localhost:9621/health
- 访问 Swagger:
- 如开启了认证:
- API Key:在请求头添加
X-API-Key: <你的KEY> - JWT:在请求头添加
Authorization: Bearer <你的JWT>
- API Key:在请求头添加
- 问题文件(可选):每行一个问题,例:
examples/questions_sample.txt
POST /query/batch:批量问答(RAG + LLM),返回每条问题的生成答案,引用可选。POST /query/data/batch:批量结构化检索数据(不调用 LLM),返回实体、关系、chunks 与引用。
queries(必填):字符串数组,每个元素是一条问题;每条至少 3 个字符。mode(选填):local/global/hybrid/naive/mix/bypass,推荐mix。top_k(选填):实体/关系检索范围(用于知识图谱检索)。chunk_top_k(选填):初始向量检索返回的文本片段数量。include_references(选填,默认 True):是否在响应中返回文档引用信息。include_chunk_content(选填,默认 False):在引用中包含 chunk 文本内容(响应更长)。max_concurrent(选填,默认 4):批量并发度上限。
其他高级参数(如
only_need_context、only_need_prompt、response_type、hl_keywords、ll_keywords、conversation_history、user_prompt、enable_rerank等)通常无需设置,按需使用即可。
curl -X POST "http://localhost:9621/query/batch" \ -H "Content-Type: application/json" \ -d "{ \"queries\":[ \"请解释什么是检索增强生成(RAG)以及它的优势?\", \"如何在本系统中选择适合的查询模式(local/global/mix)?\", \"向量检索中的 top_k 和 chunk_top_k 有什么区别?\" ], \"mode\":\"mix\", \"include_references\":true, \"max_concurrent\":4 }"
- 含认证(示例):
curl -X POST "http://localhost:9621/query/batch" \ -H "Content-Type: application/json" \ -H "X-API-Key: <你的KEY>" \ -H "Authorization: Bearer <你的JWT>" \ -d "{ ...同上... }"
$qs = Get-Content .\examples\questions_sample.txt | Where-Object { $_.Trim().Length -ge 3 } $body = @{ queries = $qs; mode = 'mix'; include_references = $true; max_concurrent = 4 } | ConvertTo-Json Invoke-RestMethod -Method Post -Uri http://localhost:9621/query/batch -ContentType 'application/json' -Body $body
- 含认证(示例):
Invoke-RestMethod -Method Post -Uri http://localhost:9621/query/batch \ -Headers @{ 'X-API-Key' = '<你的KEY>'; 'Authorization' = 'Bearer <你的JWT>' } \ -ContentType 'application/json' -Body $body
curl -X POST "http://localhost:9621/query/data/batch" \ -H "Content-Type: application/json" \ -d "{ \"queries\": [\"neural network architectures\", \"machine learning algorithms\"], \"mode\": \"mix\", \"chunk_top_k\": 5, \"max_concurrent\": 4 }"
-
脚本路径:
examples/batch_query_from_file.py -
示例命令:
- 指定模式与并发:
python examples/batch_query_from_file.py --file examples/questions_sample.txt --mode mix --max-concurrent 4 --include-references --output out/results.json
- 保存为 Markdown 汇总(更易读,便于预览):
python examples/batch_query_from_file.py --file examples/questions_sample.txt --include-references --save-md out/results.md
- 指定模式与并发:
-
参数说明:
--file:txt 文件路径(每行一个问题)。--endpoint:默认http://localhost:9621/query/batch。--mode:local/global/hybrid/naive/mix/bypass,推荐mix。--top-k、--chunk-top-k:检索范围控制(不设则用后端默认)。--include-references:返回引用信息。--include-chunk-content:引用中包含 chunk 内容(响应较长)。--max-concurrent:批量并发控制,默认 4。--limit:仅取前 N 个问题做测试。--api-key/--token:认证用(API Key 或 JWT)。--output:保存结果到文件,.json写完整响应;.ndjson/.jsonl每行一个结果项。--save-md:保存可读的 Markdown 汇总文件,包含每个问题、回答与引用。
{
"results": [
{
"response": "...生成答案...",
"references": [
{ "reference_id": "1", "file_path": "/documents/ai_overview.pdf", "content": ["...chunk1...", "...chunk2..."] }
]
},
{ "response": "...", "references": [] }
]
}references在include_references=true时返回;content仅在include_chunk_content=true时返回,且为“同一个文件可能对应多个文本片段”的数组。
{
"results": [
{
"status": "success",
"message": "Query executed successfully",
"data": {
"entities": [ { "entity_name": "Neural Networks", "entity_type": "CONCEPT", ... } ],
"relationships": [ { "src_id": "Neural Networks", "tgt_id": "Machine Learning", ... } ],
"chunks": [ { "content": "...", "file_path": "...", "chunk_id": "...", "reference_id": "1" } ],
"references": [ { "reference_id": "1", "file_path": "..." } ]
},
"metadata": { "query_mode": "mix", "keywords": { ... }, "processing_info": { ... } }
}
]
}路径:examples/questions_sample.txt
请解释什么是检索增强生成(RAG)以及它的优势?
如何在本系统中选择适合的查询模式(local/global/mix)?
向量检索中的 top_k 和 chunk_top_k 有什么区别?
如何根据引用的来源定位原始文档内容?