Skip to content

feat(examples): add a knowledge base (RAG) plugin - #5

Closed
jasper-yl wants to merge 4 commits into
feat/plugin-frameworkfrom
feat/knowledge-base-plugin
Closed

jasper-yl wants to merge 4 commits into
feat/plugin-frameworkfrom
feat/knowledge-base-plugin

Conversation

@jasper-yl

@jasper-yl jasper-yl commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

这个 PR 是什么

在插件框架分支(feat/plugin-framework)之上,只加入一个知识库(RAG)插件,用来展示一个完整的插件怎么写。差异里只有插件本身、它的端到端测试和 pypdf 开发依赖,没有框架代码。

插件对应 RFC oceanbase#2549(插件接口)和 oceanbase#2579(热加载、插件接口与页面)。

怎么读

建议按这个顺序:

  1. README.zh.md:插件做什么、怎么部署、在 Dashboard 和接口中怎么用。插件开发的通用说明在框架 PR feat(plugins): plugin framework — Artifact plugins, plugin APIs, and Dashboard pages #6 的教程 docs/zh/docs/develop/plugins.md。
  2. examples/plugins/knowledge_base/plugin.py 末尾的 plugin = ArtifactPlugin(...):整个插件的声明。
    • family="document":每个文件对应一个 document Artifact。
    • inputs=Inputs(source_types=..., projections={...}):消费哪些 Source 类型、每种类型按哪个 Projection 交付;on_sources=True 表示有新 Source 就处理。
    • definitions=(...):插件自带的两个 Source 定义(上传 / 重新解析请求、切块编辑)。
    • api=PluginApi(routes=...):插件自己的 HTTP 接口,挂在 /v1/plugins/knowledge-base/api/...。
    • pages=PluginPages(...):Dashboard 页面,静态文件在 ui/。
    • create=create:工厂函数,拿到 ctx(专属存储、模型、请求辅助方法)后返回插件对象。
  3. Source 定义(DocumentUploadDefinition、ChunkEditDefinition):Source 记录"用户要求做什么"——上传记录文件摘要和解析方式,编辑记录编辑人、基于的版本和保存的切块组。文件和切块本身存在插件自己的存储里。
  4. 接口处理函数(KnowledgeBasePlugin 的各方法):
    • 知识库 = Scope:create_knowledge_base 调 ctx.scopes.create 建 Scope,再在插件库里记映射;响应里不出现 Scope。
    • 权限:ctx.allowed / ctx.require 按调用者检查 Scope 权限;建库、删库要 server.admin。
    • 上传:先存进插件库,知识库没有默认解析方式时停在"待配置",确认前可以撤销;process_documents 再用 ctx.sources.submit 提交 Source。
    • 读:ctx.reads(scope_id);写:ctx.writer(scope_id),只在 ctx.require("scope.contribute") 之后可用。
    • 检索与问答:_search 做关键词(BM25)+ 向量混合检索;ask 用 ctx.models.generate 调 Server 配置的生成模型。
  5. generate:后台处理。按 Source 类型分支:上传就解析、切块、生成向量;编辑就检查文档仍在被编辑的版本。慢的工作在事务外做,最后在一个 writer.transaction() 里写新版本,并引用对应的 Source 作为出处。
  6. forget_artifact、restore_artifact:让通用 Artifact 接口的遗忘、恢复对 document 生效。
  7. 辅助模块:kb_store.py(插件自己的表:知识库、原文件、提交记录、按内容寻址的切块组和向量)、kb_parsers.py(解析器注册表与 PDF 提取)、kb_text.py(分词、BM25、排序融合)。
  8. ui/:页面运行在沙箱 iframe 里,通过 powercontextPluginClient() 请 Dashboard 转发请求,只能调用本插件声明的路由,拿不到用户会话。
  9. tests/e2e/test_knowledge_base_plugin.py:启动真实 Server 走完整流程——建库、批量上传后统一选择解析方式、撤销上传、检索、问答、编辑切块(含编辑出处和并发编辑冲突)、恢复版本、换解析方式重新处理、PDF 按页切块、跨知识库检索。

插件之外需要部署者做的

  • PDF 解析用 pypdf,部署时需装进 Server 的 Python 环境(插件目录里的 requirements.txt 只是说明,core 不会读取)。没装时插件不接受 PDF,其他格式不受影响。
  • 向量和问答使用 Server 的推理配置;没配置 embedding 模型时退回内置的哈希向量,没配置生成模型时问答只返回检索到的片段。

验证

uv run pytest tests/e2e/test_knowledge_base_plugin.py
uv run ruff check examples/plugins/knowledge_base && uv run ty check examples/plugins/knowledge_base

另外在 Chromium 中配合真实 OpenAI 模型(text-embedding-3-small、gpt-4.1-mini)手动走过 Dashboard 页面的上传、处理、编辑、问答流程。

AI 使用说明

代码、测试和本说明由 Claude Code(Claude Opus 5.5)在作者的指导下编写,作者审阅设计决定;测试与手动验证由 Claude Code 执行并核对结果。

🤖 Generated with Claude Code

jasper-yl and others added 2 commits November 12, 2026 01:23
One plugin directory shows the plugin interfaces end to end: every knowledge
base is a Scope the plugin creates, every file a document Artifact. Uploads wait
for a parser unless the knowledge base has a default; processing and chunk edits
are Sources the plugin defines, so every revision records what produced it.
Generation parses Markdown, text, and PDF into content-addressed chunk sets,
embeds them with the Server's model, and search and question answering run in
the plugin's own routes. The ui directory is its Dashboard page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasper-yl
jasper-yl force-pushed the feat/knowledge-base-plugin branch from e8c02b5 to 94df366 Compare November 12, 2026 09:23
jasper-yl and others added 2 commits November 12, 2026 01:27
…irst revision commits

Generation marks a submission before its write commits, so a new document had no
Artifact for a moment and dropped out of the listing. The acceptance test also
waited on a condition that an empty listing satisfied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…heir revision commits

Marking before the commit let a document look ready at its old revision, so the
page reported a successful edit as dropped. A submission whose mark was lost is
recognized from the revision it produced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasper-yl

Copy link
Copy Markdown
Owner Author

Closing: based on the fork-only framework branch, which upstream replaced with oceanbase#2616. The branch is kept.

@jasper-yl jasper-yl closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant