# Get Prompt Template

> 用动态变量填充受管理的提示词模板。

Source: https://docs.sommatic.ai/zh/nodes/ai.prompt/

- 标识: `ai.prompt`
- 类别: AI
- 版本: 1.0.0
- 状态: 部分可用. 执行成功时该 worker 不返回端口，引擎会沿 `out` 继续，而该节点并未声明 `out`：从其成功端口引出的连线永远不会触发。修复前，请勿在其成功路径后串接其他节点。
- 执行方式: sync

用动态变量填充受管理的提示词模板。

不要在工作流中硬编码提示词，而是使用该算子从 Prompt Registry 获取带版本的受管理模板，并用工作流数据填充。

**优势：**
- **关注点分离：** 提示词工程师无需改动代码即可调优提示词。
- **版本控制：** 可使用提示词的特定版本。
- **可复用性：** 在多个工作流之间共享标准的系统提示词。

## 配置

- `template_id` (string, 必填): 受管理提示词模板的 ID。
- `version` (string, 可选): 特定版本标签或“latest”。 默认值: `latest`.

配置中会显示通用执行设置，但该节点目前不执行重试（`retryOnFail`、`maxTries`）。所有节点均未实现 `continueOnFail`。

## 输出端口

- `success` (默认): Success
- `error`: Error

## 输入契约

- `variables` (object, 必填): 注入到模板占位符中的键值对。

## 输出契约

- `prompt` (string, 可选): 填充完成的提示词字符串。
- `config` (object, 可选): 与该提示词关联的模型配置。

## 在表达式中

后续节点可通过 `$("Get Prompt Template").item.json.<路径>` 读取此节点输出中的内容（请把 `Get Prompt Template` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `success` 返回的路径，依据“端口”一节所示的输出。

- `$("Get Prompt Template").item.json.prompt` (string) = `"Summarize the complaint from Ana Ruiz in two…"`: 填充完成的提示词字符串。
- `$("Get Prompt Template").item.json.config` (object) = `{…}`: 与该提示词关联的模型配置。
- `$("Get Prompt Template").item.json.config.provider` (string) = `"azure-openai"` (编辑器不提示)
- `$("Get Prompt Template").item.json.config.model` (string) = `"gpt-4o-mini"` (编辑器不提示)
- `$("Get Prompt Template").item.json.config.language` (string) = `"en"` (编辑器不提示)
- `$("Get Prompt Template").item.json.config.format` (string) = `"text"` (编辑器不提示)


## 工作原理

从后端加载托管的提示词模板，用 `variables` 替换其中的 `{{ key }}` 占位符，返回提示词以及模板关联的提供方和模型。不会调用任何 LLM。

1. 解析节点配置：配置键中的表达式会基于本次运行求值；传入条目按原样使用。
2. 将传入条目与解析后的配置合并，并要求提供 `template_id`。
3. 从后端提示词注册表获取模板记录，并依次从 `template_body`、`content` 或 `template` 中取模板正文。
4. 将每个 `{{ key }}`（空格可选）替换为 `variables.key` 的值。没有对应变量的占位符保持原样。
5. 如果模板含有 `llm_provider_id`，则读取该提供方记录以填充 `config.provider`（其 slug）和 `config.model`（部署名称或默认模型）；此步骤失败只会记录日志并将二者设为 `null`。
6. 返回不带端口的 `{ prompt, config }`，因此引擎将其路由到 `out`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-load-prompt",
  "label": "Load complaint prompt",
  "operator_slug": "ai.prompt",
  "operator_version": "1.0.0",
  "config": {
    "template_id": "<prompt_template_id>",
    "version": "latest"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "prompt": "Summarize the complaint from Ana Ruiz in two sentences.",
    "config": {
      "provider": "azure-openai",
      "model": "gpt-4o-mini",
      "language": "en",
      "format": "text"
    }
  }
}
```

## 输出端口 (代码中的行为)

### `success`

永远不会触发。成功时 worker 不返回端口，引擎使用 `out`，而此节点并未声明 `out`：从 `success` 引出的连线不会执行，工作流会在此停止。

```json
{
  "prompt": "Summarize the complaint from Ana Ruiz in two sentences.",
  "config": {
    "provider": "azure-openai",
    "model": "gpt-4o-mini",
    "language": "en",
    "format": "text"
  }
}
```

### `error`

校验或后端调用失败。`data` 为 `{ error }`；HTTP 调用失败时还包含带有后端响应体的 `response`。

```json
{
  "error": "Prompt Template <template_id> not found"
}
```

## 代码中的行为

- `version`: 已声明但被忽略：始终使用模板的当前记录。
- `variables`: 从传入条目读取；配置中的 `variables` 键（未声明，可以是表达式）会覆盖它。值以字符串插入，因此对象会变成 `[object Object]`。键会未经转义地用于正则表达式，请使用简单名称（字母、数字、`_`）。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `Missing 'template_id'` | `error` | `template_id` 为空。 |
| `Prompt Template <template_id> not found` | `error` | 注册表中没有该 id 的模板。 |
| `Template <template_id> has no content` | `error` | 模板记录没有正文。 |
| `Request failed with status code <status>` | `error` | 后端对模板查询返回了非 2xx 状态。 |

## 说明与限制

- 此节点为部分实现：在成功端口修复之前，请不要在其后连接节点。可改为在 `ai.*` 节点中直接使用模板正文。
- 后端请求未设置超时。
- 引擎不会对此节点应用 `execution.retryOnFail`、`maxTries`、`waitBetweenTries` 或 `continueOnFail`。请通过连接 `error` 端口处理失败。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "ai.prompt",
  "name": "Get Prompt Template",
  "category": "ai",
  "version": "1.0.0",
  "description": "Hydrates a managed prompt template with dynamic variables.\n\n  Instead of hardcoding prompts in your workflow, use this operator to fetch versioned, managed templates from the Prompt Registry and fill them with workflow data.\n\n  **Benefits:**\n  - **Separation of Concerns:** Prompts can be tuned by Prompt Engineers without changing code.\n  - **Version Control:** Use specific versions of a prompt.\n  - **Reusability:** Share standard system prompts across workflows.",
  "capabilities": [
    "prompt-engineering",
    "template",
    "ai"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "template_id"
    ],
    "properties": {
      "template_id": {
        "type": "string",
        "title": "Template ID",
        "description": "ID of the managed prompt template.",
        "ui_widget": "prompt_picker"
      },
      "version": {
        "type": "string",
        "title": "Version",
        "default": "latest",
        "description": "Specific version tag or \"latest\"."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "variables": {
        "type": "object",
        "description": "Key-value pairs to inject into the template placeholders."
      }
    },
    "required": [
      "variables"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "prompt": {
        "type": "string",
        "description": "The fully hydrated prompt string."
      },
      "config": {
        "type": "object",
        "description": "Model configuration associated with the prompt."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Success",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "prompt",
    "template",
    "registry"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "template_id"
  ],
  "properties": {
    "template_id": {
      "type": "string",
      "title": "Template ID",
      "description": "ID of the managed prompt template.",
      "ui_widget": "prompt_picker"
    },
    "version": {
      "type": "string",
      "title": "Version",
      "default": "latest",
      "description": "Specific version tag or \"latest\"."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "variables": {
      "type": "object",
      "description": "Key-value pairs to inject into the template placeholders."
    }
  },
  "required": [
    "variables"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "prompt": {
      "type": "string",
      "description": "The fully hydrated prompt string."
    },
    "config": {
      "type": "object",
      "description": "Model configuration associated with the prompt."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Success",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
