文档摄取、分块、向量检索、引用溯源——读侧能力,与
wise-agent的写侧对称。它的设计目标很明确:让插件零代码把自己的数据变成知识库,并且每次回答都能给出出处。
安装
composer require wiseadmin/wise-rag
cd src && composer dump-autoload
php think migrate:run
php think menu:sync建 ai_kb_* 六张表 + ai_wiki_* 表。另含独立命名空间 wise\wiki 的知识编译引擎。
一个历史遗留坑:向量列必须是 BLOB。如果库里是早版本的 BINARY(255),检索会异常——修复顺序是 wise:rag:prune --apply → wise:rag:index --force。
配置
config/wise-rag.php:
| 节 | 关键项 |
|---|---|
retrieval | hybrid = true、fusion = 'rrf'、min_score = 0.25、rerank.enabled = false |
ingestion | async = true(异步摄取需要跑 queue:work) |
cache | enabled、driver = database、keep_days = 30 |
vector | brute_warn_threshold = 50000、stores.database.scan_page_size = 1000 |
stats | cache_ttl = 60 |
agent | auto_tool = true、tool_cache_ttl = 60 |
mcp | expose_tools / expose_resources |
hybrid 与 fusion 的组合决定检索质量:向量负责语义相近,关键词负责专有名词(型号、错误码),RRF 只是融合策略——别指望它救回"关键词没命中"的查询。
三个核心函数
$kbId = wise_rag_create('product_docs', ['name' => '商品文档']);
wise_rag_ingest($kbId, "# A100\n\n功率 250W。", ['external_id' => 'p100']);
$hits = wise_rag_search('A100 功率多少', ['product_docs'], 5);签名(src/rag/helper.php):
wise_rag_create(string $kbKey, array $options = []): int
wise_rag_ingest(int $kbId, string $text, array $meta = []): int
wise_rag_search(string $query, array $kbKeys = [], int $topK = 5): array$options支持name、description、owner_type、owner_id、visibility、embedding、vector_store、chunk_config、retrieval_config;$meta支持external_id、title、url、owner_type、owner_id、source_id——引用溯源就靠这几个字段,url为空时回答里给不出可点的出处。
另有 wise_rag_kb()、wise_rag_delete_document() 与 wise\wiki 的 helper。
插件零接入
在插件 manifest.json 写 ai_schema,不写一行 PHP:
{"ai_schema": {
"knowledge_kb_key": "smartstock_docs",
"knowledge_sources": [{
"key": "product_docs",
"title": "商品说明书",
"provider": "table",
"config": {
"table": "smartstock_product",
"id_field": "id",
"title_field": "name",
"content_fields": ["name", "description", "spec"],
"updated_field": "update_time",
"url_template": "/admin/smartstock/product/detail?id={id}"
},
"sync_mode": "manual",
"chunk": {"size": 400, "overlap": 60, "splitter": "structure"}
}]
}}provider 只有三种:
| provider | 适合 | 注意 |
|---|---|---|
table | 结构化数据 | 支持 updated_field 增量、where、url_template |
attachment | 上传的文件 | 仅本地驱动可读,云存储读不到 |
manual | 手工灌入 | 走 wise_rag_ingest() |
knowledge_kb_key 缺省等于插件标识;落库时 source_key 是 "{$ident}:{$decl['key']}"。sync_mode = manual 意味着数据改了不会自动重新索引,要自己跑 wise:rag:index 或放进定时任务。
命令
php think wise:rag:discover [--index] [--dump=smartstock]
php think wise:rag:index <kb> [--async] [--force] [--source=...]
php think wise:rag:stats
php think wise:rag:prune [--apply]
php think wise:rag:eval [./eval/cases.json] [--top-k=5] [--json]prune 默认只预览,--apply 才真正清理(含损坏向量)。eval 是唯一能量化检索质量的手段——改 chunk 或 fusion 后跑一次,比凭感觉调参可靠。
与 agent / mcp 的衔接
- 向
ToolRegistry注册kb.search与每库一个kb.{key}.search,权限ai.tool.kb_search(要跑php think wise:agent:permissions才落库); - 经 MCP 的 provider 扩展点外溢工具与
kb://资源; - 后台
/admin/kb/*默认挂RbacMiddleware。
三个容易搞错的认知
一、"插件要写 PHP 才能接知识库"。 不用,ai_schema 声明即可;但 sync_mode=manual 时要自己管索引更新。
二、"云存储的附件也能自动读"。 provider=attachment 只支持本地驱动。
三、"检索不准就调 min_score"。 先看 chunk 切分与关键词覆盖,min_score 只过滤不过召回。
自查:wise_rag_create → ingest → search 三连能返回带 cite 的命中;wise:rag:stats 里文档数与实际条数一致;wise:rag:eval 能给出分数。
完整的"灌库 → Agent 回答 → MCP 外露"闭环见实战:知识库问答闭环。