cmdc_rag_arcana

Arcana-backed enterprise RAG tools and plugins for CMDC.

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

v0.1 范围

模块 用途
CMDCRAGArcana.Tool.Searchrag_search 只读检索,返回 chunks / citations / scores
CMDCRAGArcana.Tool.Answerrag_answer 基于 Arcana 生成带引用答案
CMDCRAGArcana.Plugin.AccessControl collection ACL,在 :before_tool fail closed
CMDCRAGArcana.Plugin.CitationAudit citation 访问事件,在 :after_tool emit
CMDCRAGArcana.Backend Arcana 调用 behaviour,便于测试和替换

明示不含

安装

defp deps do
  [
    {:cmdc, "~> 0.6"},
    {:cmdc_rag_arcana, "~> 0.1"}
  ]
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
    ],
    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",
        allowed_collections: ["policies", "sop"]
      }
    }
  )

Agent 调用 rag_search 时应传入 collection:

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

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

测试替换 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"]}

License

Apache 2.0