# 提示词模板

> 提示词模板把指令、带占位符的正文、变量、预期输出字段和示例集中在一条记录中。ai.prompt 节点用工作流数据填充它。

Source: https://docs.sommatic.ai/zh/ai/prompt-templates/ (updated 2026-11-26)

提示词模板把提示词从工作流中分离出来。负责调整措辞的人只需编辑模板；工作流仍指向同一条记录，下次运行时自动使用修改后的内容。

## 模板包含什么

| 字段                          | 说明                                                              |
| --------------------------- | --------------------------------------------------------------- |
| `slug`、`name`、`description` | 稳定键与显示名称。                                                       |
| `organization_id`           | 所属组织。                                                           |
| `llm_provider_id`           | 模板面向的[大模型提供方](https://docs.sommatic.ai/zh/ai/llm-providers.md)。 |
| `language`、`format`         | 提示词的语言，以及预期回答的格式。                                               |
| `system_instructions`       | 提示词的系统部分。                                                       |
| `template_body`             | 提示词正文，包含 `{{ customer_name }}` 这样的占位符。                          |
| `variables`                 | 正文所需的占位符。                                                       |
| `output_fields`             | 回答应包含的字段。                                                       |
| `style_guidelines`          | 一组风格规则。                                                         |
| `examples`                  | 输入与预期输出的完整示例。                                                   |
| `tags`、`is_system`          | 标签，以及是否为系统模板。                                                   |

状态：`enabled`、`review`、`deprecated`、`archived`。

### 变量

`variables` 中的每一项描述一个占位符：

```json
{
  "name": "customer_name",
  "type": "string",
  "description": "问候语中使用的姓名",
  "required": true,
  "default_value": "客户",
  "allowed_values": []
}
```

### 输出字段与示例

输出字段包含 `name`、`type`、`description` 和 `required`；示例包含 `inputs`、`expected_output` 和 `notes`。它们为编写或审阅模板的人记录了模板的契约。

## 在工作流中填充模板

[`ai.prompt`](https://docs.sommatic.ai/zh/nodes/ai.prompt.md) 节点读取模板并返回完整的提示词。

1. 把 `template_id` 设为模板的 id。
2. 传入 `variables`，其键名与占位符一致。
3. 节点把 `template_body` 中的每个 `{{ key }}`（花括号内允许有空格）替换为对应的值。

模板正文如下：

```text
请用{{ language }}将{{ customer_name }}的请求总结为三条要点。
```

节点配置如下：

```json
{
  "template_id": "<prompt-template-id>",
  "variables": {
    "customer_name": "{{ $json.sender.name }}",
    "language": "中文"
  }
}
```

节点返回：

```json
{
  "prompt": "请用中文将王芳的请求总结为三条要点。",
  "config": {
    "provider": "<llm-provider-slug>",
    "model": "<deployment-name>",
    "language": "<template-language>",
    "format": "<template-format>"
  }
}
```

`config.provider` 和 `config.model` 来自模板关联的大模型提供方（其 `slug`，以及 `deployment_name` 或 `default_parameters.model`）；`config.language` 和 `config.format` 来自模板本身。下一个节点通过 `{{ $json.prompt }}` 读取提示词。

> **说明: 模板没有版本历史**
>
> 节点始终读取模板的当前内容。修改 `template_body` 后，所有使用该模板的工作流在下次运行时都会用到新内容。`variables` 中没有提供值的占位符会原样保留在文本中。

## 管理模板

在控制台中进入 **Advanced → Cognitive Infrastructure → Prompt Template**。通过 API 使用 `/cognitive-infrastructure/cognitive-assets/prompt-template/`（见 [API 参考](https://docs.sommatic.ai/zh/api/reference/cognitive-infrastructure.md)）。

## 下一步

- [知识资源](https://docs.sommatic.ai/zh/ai/knowledge-resources.md): 让 AI 节点以受治理的内容为依据。
- [表达式](https://docs.sommatic.ai/zh/expressions.md): 用前序节点的数据填充变量。
