Skip to main content

README.md

# 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.Search` | `rag_search` 只读检索,返回 chunks / citations / scores |
| `CMDCRAGArcana.Tool.Answer` | `rag_answer` 基于 Arcana 生成带引用答案 |
| `CMDCRAGArcana.Plugin.AccessControl` | collection ACL,在 `:before_tool` fail closed |
| `CMDCRAGArcana.Plugin.CitationAudit` | citation 访问事件,在 `:after_tool` emit |
| `CMDCRAGArcana.Backend` | Arcana 调用 behaviour,便于测试和替换 |

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

## 明示不含

- 不让 Agent 直接 ingest / delete 企业知识库文档。
- 不默认暴露 Arcana Loop,避免 Agent 套 Agent 后削弱 CMDC trace / 成本 / 审批控制。
- 不在 `cmdc` core 引入 Arcana / pgvector / Nx / Bumblebee 依赖。
- 不在 v0.1 做完整 Knowledge UI / GraphRAG / 数据飞轮。

## 安装

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

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

## Agent 集成

```elixir
{: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:

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

返回值是 JSON 字符串,包含 `results``citations``metadata``CitationAudit`
会额外 emit:

- `:rag_retrieved`
- `:rag_answered`
- `:rag_citation_used`

## 测试替换 backend

```elixir
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` 或直接调用中配置:

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

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

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

生产环境应使用 `allowed_collections``collection_policies`,不要依赖
`default_allow?: true`
## License

Apache 2.0