> ## Documentation Index
> Fetch the complete documentation index at: https://docs-docflow.textin.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 规则库管理

> 通过 REST API 管理审核规则库、规则组和规则

<Tip>
  本文介绍如何通过 REST API 管理审核规则库、规则组和规则。规则库管理采用三层结构：规则库 → 规则组 → 规则。您需要先创建规则库，然后在规则库下创建规则组，最后在规则组下创建规则。
</Tip>

智能审核的规则库管理采用三层结构：**规则库（Rule Repository）** → **规则组（Rule Group）** → **规则（Rule）**。本文介绍如何通过 REST API 进行规则库管理。

## 规则库管理

规则库是审核规则的顶层容器，用于组织和管理相关的审核规则。

### 创建规则库

创建审核规则库：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "name": "审核规则库1"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_repo/create"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests
  import json

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_repo/create"

  payload = {
      "workspace_id": workspace_id,
      "name": "审核规则库1"
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      repo_id = result.get("result", {}).get("repo_id")
      print(f"规则库创建成功，ID: {repo_id}")
  else:
      print(f"创建失败: {result.get('msg')}")
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `name` (必填): 规则库名称，最大长度30

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "repo_id": "31415926"
  }
}
```

### 获取审核规则库列表

获取工作空间下的所有审核规则库列表，包含规则库下的规则组和规则信息：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_repo/list?workspace_id=<your-workspace-id>&page=1&page_size=10"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_repo/list"

  resp = requests.get(
      url=f"{host}{url}",
      params={
          "workspace_id": workspace_id,
          "page": 1,
          "page_size": 10
      },
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      repos = result.get("result", {}).get("repos", [])
      total = result.get("result", {}).get("total", 0)
      print(f"共找到 {total} 个规则库")
      for repo in repos:
          print(f"规则库ID: {repo.get('repo_id')}, 名称: {repo.get('name')}")
          groups = repo.get("groups", [])
          print(f"  包含 {len(groups)} 个规则组")
  else:
      print(f"获取失败: {result.get('msg')}")
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `page` (选填): 页码，默认为 1
* `page_size` (选填): 每页条数，默认为 10

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "repos": [
      {
        "repo_id": "31415926",
        "name": "审核规则库1",
        "category_ids": ["invoice_category_id"],
        "groups": [
          {
            "group_id": "31415926",
            "name": "审核规则组1",
            "rules": [
              {
                "rule_id": "31415926",
                "name": "审核规则1",
                "prompt": "检查发票金额是否大于0且小于1000000，如果不在范围内则审核不通过",
                "category_ids": ["invoice_category_id"],
                "risk_level": 10,
                "referenced_fields": [
                  {
                    "category_id": "invoice_category_id",
                    "category_name": "发票",
                    "fields": [
                      {
                        "field_id": "amount_field_id",
                        "field_name": "发票金额"
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ],
    "total": 1,
    "page": 1,
    "page_size": 10
  }
}
```

### 获取审核规则库

根据规则库ID获取单个审核规则库的详细信息：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_repo/get?workspace_id=<your-workspace-id>&repo_id=31415926"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  repo_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_repo/get"

  resp = requests.get(
      url=f"{host}{url}",
      params={
          "workspace_id": workspace_id,
          "repo_id": repo_id
      },
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      repo = result.get("result", {})
      print(f"规则库ID: {repo.get('repo_id')}")
      print(f"规则库名称: {repo.get('name')}")
      print(f"适用分类: {repo.get('category_ids', [])}")
      groups = repo.get("groups", [])
      print(f"包含 {len(groups)} 个规则组")
      for group in groups:
          print(f"  规则组: {group.get('name')} (ID: {group.get('group_id')})")
          rules = group.get("rules", [])
          print(f"    包含 {len(rules)} 个规则")
  else:
      print(f"获取失败: {result.get('msg')}")
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `repo_id` (必填): 审核规则库ID

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "repo_id": "31415926",
    "name": "审核规则库1",
    "category_ids": ["invoice_category_id"],
    "groups": [
      {
        "group_id": "31415926",
        "name": "审核规则组1",
        "rules": [
          {
            "rule_id": "31415926",
            "name": "审核规则1",
            "prompt": "检查发票金额是否大于0且小于1000000，如果不在范围内则审核不通过",
            "category_ids": ["invoice_category_id"],
            "risk_level": 10,
            "referenced_fields": [
              {
                "category_id": "invoice_category_id",
                "category_name": "发票",
                "fields": [
                  {
                    "field_id": "amount_field_id",
                    "field_name": "发票金额"
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
}
```

### 更新规则库

更新审核规则库的名称：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "repo_id": "31415926",
      "name": "更新后的规则库名称"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_repo/update"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  repo_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_repo/update"

  payload = {
      "workspace_id": workspace_id,
      "repo_id": repo_id,
      "name": "更新后的规则库名称"
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `repo_id` (必填): 规则库ID
* `name` (必填): 新的规则库名称，最大长度30

### 删除规则库

删除审核规则库（可以批量删除）：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "repo_ids": ["31415926", "31415927"]
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_repo/delete"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_repo/delete"

  payload = {
      "workspace_id": workspace_id,
      "repo_ids": ["31415926", "31415927"]
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `repo_ids` (必填): 规则库ID数组

<Warning>
  删除规则库会同时删除其下的所有规则组和规则，请谨慎操作。
</Warning>

## 规则组管理

规则组是规则库下的二级分类，用于对规则进行更细粒度的分组管理。

### 创建规则组

在规则库下创建规则组：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "repo_id": "31415926",
      "name": "审核规则组1"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_group/create"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  repo_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_group/create"

  payload = {
      "workspace_id": workspace_id,
      "repo_id": repo_id,
      "name": "审核规则组1"
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      group_id = result.get("result", {}).get("group_id")
      print(f"规则组创建成功，ID: {group_id}")
  else:
      print(f"创建失败: {result.get('msg')}")
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `repo_id` (必填): 规则库ID
* `name` (必填): 规则组名称，最大长度30

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "group_id": "31415926"
  }
}
```

### 更新规则组

更新规则组的名称：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "group_id": "31415926",
      "name": "更新后的规则组名称"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_group/update"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  group_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_group/update"

  payload = {
      "workspace_id": workspace_id,
      "group_id": group_id,
      "name": "更新后的规则组名称"
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `group_id` (必填): 规则组ID
* `name` (必填): 新的规则组名称，最大长度30

### 删除规则组

删除规则组：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "group_id": "31415926"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule_group/delete"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  group_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule_group/delete"

  payload = {
      "workspace_id": workspace_id,
      "group_id": group_id
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `group_id` (必填): 规则组ID

<Warning>
  删除规则组会同时删除其下的所有规则，请谨慎操作。
</Warning>

## 规则管理

规则是审核的最小执行单元，定义了具体的审核标准和逻辑。

### 创建规则

在规则组下创建审核规则：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "repo_id": "31415926",
      "group_id": "31415926",
      "name": "审核规则1",
      "prompt": "检查发票金额是否大于0且小于1000000，如果不在范围内则审核不通过",
      "category_ids": ["invoice_category_id"],
      "risk_level": 10,
      "referenced_fields": [
        {
          "category_id": "invoice_category_id",
          "category_name": "发票",
          "fields": [
            {
              "field_id": "amount_field_id",
              "field_name": "发票金额"
            }
          ]
        }
      ]
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule/create"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  repo_id = "31415926"
  group_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule/create"

  payload = {
      "workspace_id": workspace_id,
      "repo_id": repo_id,
      "group_id": group_id,
      "name": "审核规则1",
      "prompt": "检查发票金额是否大于0且小于1000000，如果不在范围内则审核不通过",
      "category_ids": ["invoice_category_id"],
      "risk_level": 10,
      "referenced_fields": [
          {
              "category_id": "invoice_category_id",
              "category_name": "发票",
              "fields": [
                  {
                      "field_id": "amount_field_id",
                      "field_name": "发票金额"
                  }
              ]
          }
      ]
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      rule_id = result.get("result", {}).get("rule_id")
      print(f"规则创建成功，ID: {rule_id}")
  else:
      print(f"创建失败: {result.get('msg')}")
  ```
</CodeGroup>

### 获取分类ID和字段ID

在创建规则之前，需要先获取分类ID（`category_id`）和字段ID（`field_id`）。这些ID可以通过以下接口获取：

#### 获取分类列表

获取工作空间下的所有分类，用于获取分类ID：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/category/list?workspace_id=<your-workspace-id>"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/category/list"

  resp = requests.get(
      url=f"{host}{url}",
      params={"workspace_id": workspace_id},
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      categories = result.get("result", {}).get("categories", [])
      for category in categories:
          print(f"分类ID: {category.get('id')}, 分类名称: {category.get('name')}")
  ```
</CodeGroup>

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "total": 10,
    "page": 1,
    "page_size": 1000,
    "categories": [
      {
        "id": "invoice_category_id",
        "name": "发票",
        "description": "发票分类描述",
        "enabled": 1
      },
      {
        "id": "contract_category_id",
        "name": "合同",
        "description": "合同分类描述",
        "enabled": 1
      }
    ]
  }
}
```

从响应中获取 `categories[].id` 即为分类ID（`category_id`）。

#### 获取分类字段列表

获取指定分类下的所有字段，用于获取字段ID：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/category/fields/list?workspace_id=<your-workspace-id>&category_id=<category-id>"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  category_id = "<category-id>"  # 从分类列表接口获取

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/category/fields/list"

  resp = requests.get(
      url=f"{host}{url}",
      params={
          "workspace_id": workspace_id,
          "category_id": category_id
      },
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  if result.get("code") == 200:
      fields = result.get("result", {}).get("fields", [])
      tables = result.get("result", {}).get("tables", [])
      
      print("普通字段：")
      for field in fields:
          print(f"  字段ID: {field.get('id')}, 字段名称: {field.get('name')}")
      
      print("表格字段：")
      for table in tables:
          print(f"  表格ID: {table.get('id')}, 表格名称: {table.get('name')}")
          for field in table.get("fields", []):
              print(f"    字段ID: {field.get('id')}, 字段名称: {field.get('name')}")
  ```
</CodeGroup>

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "fields": [
      {
        "id": "amount_field_id",
        "name": "发票金额",
        "description": "发票金额描述",
        "enabled": 1
      },
      {
        "id": "invoice_code_field_id",
        "name": "发票代码",
        "description": "发票代码描述",
        "enabled": 1
      }
    ],
    "tables": [
      {
        "id": "invoice_items_table_id",
        "name": "发票明细",
        "description": "发票明细表格",
        "fields": [
          {
            "id": "item_name_field_id",
            "name": "货物名称",
            "description": "货物名称描述",
            "enabled": 1
          }
        ]
      }
    ]
  }
}
```

从响应中获取：

* `fields[].id` 即为普通字段ID（`field_id`）
* `tables[].id` 即为表格ID（`table_id`）
* `tables[].fields[].id` 即为表格字段ID（`field_id`）

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `repo_id` (必填): 规则库ID
* `group_id` (必填): 规则组ID
* `name` (必填): 规则名称
* `prompt` (必填): 规则提示词，描述审核规则的提示词，用于指导AI进行审核判断
* `category_ids` (选填): 适用分类ID数组，规则适用于哪些文档分类。通过[获取分类列表](#获取分类列表)接口获取
* `risk_level` (选填): 风险等级，可选值：`10`(高风险)、`20`(中风险)、`30`(低风险)
* `referenced_fields` (选填): 关联字段数组，规则需要关联的抽取字段。字段ID通过[获取分类字段列表](#获取分类字段列表)接口获取

**关联字段结构说明：**

```json theme={null}
{
  "referenced_fields": [
    {
      "category_id": "分类ID",
      "category_name": "分类名称",
      "fields": [
        {
          "field_id": "字段ID",
          "field_name": "字段名称"
        }
      ],
      "tables": [
        {
          "table_id": "表格ID",
          "table_name": "表格名称",
          "fields": [
            {
              "field_id": "字段ID",
              "field_name": "字段名称"
            }
          ]
        }
      ]
    }
  ]
}
```

**响应示例：**

```json theme={null}
{
  "code": 200,
  "msg": "success",
  "result": {
    "rule_id": "31415926"
  }
}
```

### 更新规则

更新审核规则：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "rule_id": "31415926",
      "group_id": "31415926",
      "name": "更新后的规则名称",
      "prompt": "更新后的规则提示词",
      "category_ids": ["invoice_category_id"],
      "risk_level": 20,
      "referenced_fields": [
        {
          "category_id": "invoice_category_id",
          "category_name": "发票",
          "fields": [
            {
              "field_id": "amount_field_id",
              "field_name": "发票金额"
            }
          ]
        }
      ]
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule/update"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  rule_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule/update"

  payload = {
      "workspace_id": workspace_id,
      "rule_id": rule_id,
      "group_id": "31415926",
      "name": "更新后的规则名称",
      "prompt": "更新后的规则提示词",
      "category_ids": ["invoice_category_id"],
      "risk_level": 20,
      "referenced_fields": [
          {
              "category_id": "invoice_category_id",
              "category_name": "发票",
              "fields": [
                  {
                      "field_id": "amount_field_id",
                      "field_name": "发票金额"
                  }
              ]
          }
      ]
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `rule_id` (必填): 规则ID
* 其他参数与创建规则相同，用于更新规则属性

### 删除规则

删除审核规则：

<CodeGroup>
  ```bash curl icon=terminal wrap theme={null}
  curl -X POST \
    -H "Content-Type: application/json" \
    -H "x-ti-app-id: <your-app-id>" \
    -H "x-ti-secret-code: <your-secret-code>" \
    -d '{
      "workspace_id": "<your-workspace-id>",
      "rule_id": "31415926"
    }' \
    "https://docflow.textin.com/api/app-api/sip/platform/v2/review/rule/delete"
  ```

  ```python Python expandable icon=python lines theme={null}
  import requests

  ti_app_id = "<your-app-id>"
  ti_secret_code = "<your-secret-code>"
  workspace_id = "<your-workspace-id>"
  rule_id = "31415926"

  host = "https://docflow.textin.com"
  url = "/api/app-api/sip/platform/v2/review/rule/delete"

  payload = {
      "workspace_id": workspace_id,
      "rule_id": rule_id
  }

  resp = requests.post(
      url=f"{host}{url}",
      json=payload,
      headers={
          "x-ti-app-id": ti_app_id,
          "x-ti-secret-code": ti_secret_code,
      },
      timeout=60,
  )

  result = resp.json()
  print(result)
  ```
</CodeGroup>

**请求参数：**

* `workspace_id` (必填): 工作空间ID
* `rule_id` (必填): 规则ID

## 规则创建流程示例

完整的规则创建流程（包含获取分类ID和字段ID）：

```python Python icon=python expandable theme={null}
import requests

ti_app_id = "<your-app-id>"
ti_secret_code = "<your-secret-code>"
workspace_id = "<your-workspace-id>"
host = "https://docflow.textin.com"

# 1. 获取分类列表，获取分类ID
categories_resp = requests.get(
    f"{host}/api/app-api/sip/platform/v2/category/list",
    params={"workspace_id": workspace_id},
    headers={"x-ti-app-id": ti_app_id, "x-ti-secret-code": ti_secret_code},
)
categories = categories_resp.json()["result"]["categories"]
# 假设我们要使用"发票"分类
invoice_category = next((c for c in categories if c["name"] == "发票"), None)
if not invoice_category:
    print("未找到发票分类")
    exit(1)
category_id = invoice_category["id"]
category_name = invoice_category["name"]
print(f"找到分类: {category_name}, ID: {category_id}")

# 2. 获取分类字段列表，获取字段ID
fields_resp = requests.get(
    f"{host}/api/app-api/sip/platform/v2/category/fields/list",
    params={
        "workspace_id": workspace_id,
        "category_id": category_id
    },
    headers={"x-ti-app-id": ti_app_id, "x-ti-secret-code": ti_secret_code},
)
fields_result = fields_resp.json()["result"]
fields = fields_result.get("fields", [])
# 假设我们要使用"发票金额"字段
amount_field = next((f for f in fields if f["name"] == "发票金额"), None)
if not amount_field:
    print("未找到发票金额字段")
    exit(1)
field_id = amount_field["id"]
field_name = amount_field["name"]
print(f"找到字段: {field_name}, ID: {field_id}")

# 3. 创建规则库
repo_payload = {
    "workspace_id": workspace_id,
    "name": "发票审核规则库"
}
repo_resp = requests.post(
    f"{host}/api/app-api/sip/platform/v2/review/rule_repo/create",
    json=repo_payload,
    headers={"x-ti-app-id": ti_app_id, "x-ti-secret-code": ti_secret_code},
)
repo_id = repo_resp.json()["result"]["repo_id"]
print(f"规则库创建成功，ID: {repo_id}")

# 4. 创建规则组
group_payload = {
    "workspace_id": workspace_id,
    "repo_id": repo_id,
    "name": "发票合规性检查"
}
group_resp = requests.post(
    f"{host}/api/app-api/sip/platform/v2/review/rule_group/create",
    json=group_payload,
    headers={"x-ti-app-id": ti_app_id, "x-ti-secret-code": ti_secret_code},
)
group_id = group_resp.json()["result"]["group_id"]
print(f"规则组创建成功，ID: {group_id}")

# 5. 创建规则（使用获取到的分类ID和字段ID）
rule_payload = {
    "workspace_id": workspace_id,
    "repo_id": repo_id,
    "group_id": group_id,
    "name": "发票金额校验",
    "prompt": "检查发票金额是否大于0且小于1000000，如果不在范围内则审核不通过",
    "category_ids": [category_id],  # 使用获取到的分类ID
    "risk_level": 10,
    "referenced_fields": [
        {
            "category_id": category_id,  # 使用获取到的分类ID
            "category_name": category_name,  # 使用获取到的分类名称
            "fields": [
                {
                    "field_id": field_id,  # 使用获取到的字段ID
                    "field_name": field_name  # 使用获取到的字段名称
                }
            ]
        }
    ]
}
rule_resp = requests.post(
    f"{host}/api/app-api/sip/platform/v2/review/rule/create",
    json=rule_payload,
    headers={"x-ti-app-id": ti_app_id, "x-ti-secret-code": ti_secret_code},
)
rule_id = rule_resp.json()["result"]["rule_id"]
print(f"规则创建成功，ID: {rule_id}")
```

## 相关页面

* [审核概念](./quickstart) - 了解审核功能的核心概念
* [创建审核任务](./create_task) - 学习如何创建审核任务
* [获取审核结果](./get_result) - 学习如何获取和使用审核结果
