# 预览创建时的数据源

在创建知识库前，根据 Catalog 选择范围预览将选中的文件和数据表数量。该接口只读，不会创建知识库、数据源或处理任务。

```text
POST https://moi.matrixorigin.cn/newmoi/semantic-models/source-selections/preview
```

## 调用前准备

选择范围仅支持 Catalog 数据：`database_tables` 用于一个 Database 中的数据表，`volume_files` 用于一个 Volume 中的文件。准备 `$AI_STUDIO_API_KEY` 和 `$WORKSPACE_ID`。

`all_selected: false` 时必须传对应的 `selected_table_ids` 或 `selected_file_ids`；`all_selected: true` 时可传对应的排除 ID。

## 请求示例

```bash
curl -X POST "https://moi.matrixorigin.cn/newmoi/semantic-models/source-selections/preview" \
  -H "X-API-Key: $AI_STUDIO_API_KEY" \
  -H "X-Workspace-ID: $WORKSPACE_ID" \
  -H 'Content-Type: application/json' \
  -d '{
    "source_selections": [
      {
        "kind": "database_tables",
        "database_id": 101,
        "all_selected": false,
        "selected_table_ids": [201]
      }
    ]
  }'
```

## 成功响应

成功时返回 `200`。

```json
{
  "code": "OK",
  "msg": "OK",
  "data": {
    "file_count": 0,
    "table_count": 1,
    "total_count": 1
  }
}
```

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `data.file_count` | integer | 预览范围中的文件数量。 |
| `data.table_count` | integer | 预览范围中的数据表数量。 |
| `data.total_count` | integer | 去重后的总数量。 |

## 常见错误

| HTTP 状态码 | 错误代码 | 常见原因 | 建议操作 |
| --- | --- | --- | --- |
| `400` | `ErrParamInvalid` | 选择范围为空，或 `kind`、ID、选择规则无效。 | 按[创建知识库并添加数据源](create-knowledge-base-with-sources.md)修正选择范围。 |
| `401` | `ErrUnauthorized` | API Key 无效或已失效。 | 检查 API Key。 |
| `403` | `ErrForbidden` | 没有创建权限或所选范围的读取权限。 | 检查工作区和 Catalog 对象授权。 |

## 后续操作

数量符合预期后，使用相同的 `source_selections` 调用[创建知识库并添加数据源](create-knowledge-base-with-sources.md)。
