cmdc_rag_arcana

Arcana-backed enterprise RAG tools and plugins for CMDC.

cmdc_rag_arcana 是 CMDC 的独立 RAG 扩展包。它不把 Arcana 依赖塞进 cmdc core,而是通过标准 CMDC.Tool / CMDC.Plugin 边界接入企业知识库。

能力范围

模块 用途
CMDCRAGArcana.Tool.Searchrag_search 只读检索,返回 chunks / citations / scores
CMDCRAGArcana.Tool.Answerrag_answer 基于 Arcana 生成带引用答案
CMDCRAGArcana.Tool.IngestStatusrag_ingest_status 只读查询索引状态
CMDCRAGArcana.Plugin.AccessControl collection ACL,在 :before_tool fail closed
CMDCRAGArcana.Plugin.CitationAudit citation 访问事件,在 :after_tool emit
CMDCRAGArcana.Ingestion Oban worker 可调用的导入 adapter contract
CMDCRAGArcana.Maintenance Arcana maintenance wrapper,统一 progress telemetry/event
CMDCRAGArcana.Backend Arcana 调用 behaviour,便于测试和替换

rag_search / rag_answer 默认 fail closed:即使 Agent 传入了 collections,也必须通过 allowed_collectionscollection_policies 或显式 default_allow?: true 放行。

明示不含

Knowledge Control Plane

v0.2 开始,本包提供企业知识库控制面接缝,但不持有企业 Ecto schema 或 Oban 依赖。生产平台应在 Phoenix app 中维护:

详细 schema 草案、Oban worker skeleton、Arcana dashboard 边界和 maintenance 用法见 Knowledge Control Plane guide

安装

defp deps do
  [
    {:cmdc, "~> 0.6"},
    {:cmdc_rag_arcana, "~> 0.2"}
  ]
end

Arcana 本身需要 Ecto Repo、PostgreSQL + pgvector 以及 embedder 配置。生产项目应按 Arcana 官方安装流程完成迁移和 supervision tree 配置。

Agent 集成

{:ok, session} =
  CMDC.create_agent(
    model: "anthropic:claude-sonnet-4-5",
    tools: [
      CMDCRAGArcana.Tool.Search,
      CMDCRAGArcana.Tool.Answer,
      CMDCRAGArcana.Tool.IngestStatus
    ],
    plugins: [
      {CMDCRAGArcana.Plugin.AccessControl,
       allowed_collections: ["policies", "sop"]},
      CMDCRAGArcana.Plugin.CitationAudit
    ],
    user_data: %{
      tenant_id: "tenant-a",
      user_id: "alice",
      roles: ["ops"],
      cmdc_rag_arcana: %{
        repo: MyApp.Repo,
        llm: "openai:gpt-4o-mini",
        status_backend: MyApp.Knowledge.RAGStatusBackend,
        allowed_collections: ["policies", "sop"]
      }
    }
  )

Agent 调用 rag_search 时应传入 collection:

{
  "query": "高风险操作需要几级审批?",
  "collections": ["policies"],
  "top_k": 5,
  "mode": "hybrid"
}

返回值是 JSON 字符串,包含 resultscitationsmetadataCitationAudit 会额外 emit:

Agent 调用 rag_ingest_status 时只读查询状态:

{
  "collection": "policies",
  "document_id": "doc-1",
  "version_id": "ver-2026-05"
}

返回值包含 status.statusstatus.graph_statusstatus.stale?status.chunk_count 等字段。该工具不会触发 ingest/delete/rebuild。

测试替换 backend

defmodule MyMockRAG do
  @behaviour CMDCRAGArcana.Backend

  def search(_query, _opts), do: {:ok, [%{id: "c1", text: "policy", score: 0.9}]}
  def answer(_question, _opts), do: {:ok, "answer", [%{id: "c1", text: "policy"}]}
end

然后在 user_data 或直接调用中配置:

cmdc_rag_arcana: %{backend: MyMockRAG, allowed_collections: ["policies"]}

开发环境如需临时放开 collection ACL,可以显式配置:

cmdc_rag_arcana: %{backend: MyMockRAG, default_allow?: true}

生产环境应使用 allowed_collectionscollection_policies,不要依赖 default_allow?: true

License

Apache 2.0