# RAG Compose Prompt

> 将查询与检索到的上下文组合，构建 RAG（检索增强生成）提示词。

Source: https://docs.sommatic.ai/zh/nodes/ai.rag.compose/

- 标识: `ai.rag.compose`
- 类别: AI
- 版本: 1.0.0
- 状态: 实验性. 桩实现：返回固定的 `{ message, timestamp }` 对象，忽略 `query`、`context` 和 `template`。
- 执行方式: sync

将查询与检索到的上下文组合，构建 RAG（检索增强生成）提示词。

该算子将检索到的文档列表格式化为上下文块，并注入“根据上下文作答”的提示词模板。

**适用场景：**
- 针对内部文档的问答。
- 汇总搜索结果。
- 让 LLM 的回答以事实数据为依据。

## 配置

- `template` (string, 可选): 必须包含 {{context}} 和 {{query}} 占位符。 默认值: `Context:
{{context}}

Query:
{{query}}

Answer using only the context above:`.
- `max_context_tokens` (number, 可选): 限制上下文大小以适配模型窗口。 默认值: `4000`.

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

## 输出端口

- `out` (默认): Composed
- `error`: Error

## 输入契约

- `query` (string, 必填): 用户的问题。
- `context` (array | string, 必填): 文本分块列表或已拼接好的字符串。

## 输出契约

- `prompt` (string, 可选): 可直接用于生成的最终提示词。

## 在表达式中

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

- `$("RAG Compose Prompt").item.json.prompt` (string): 可直接用于生成的最终提示词。 **worker 不返回：读取结果为 undefined.**
- `$("RAG Compose Prompt").item.json.message` (string) = `"Executed ai.rag.compose"` (编辑器不提示)
- `$("RAG Compose Prompt").item.json.timestamp` (string) = `"2026-11-26T15:04:05.000Z"` (编辑器不提示)


## 工作原理

实验性桩实现。它通过 `out` 返回固定的 `{ message, timestamp }` 对象，不会构建任何提示词：`query`、`context`、`template` 和 `max_context_tokens` 均被忽略。

1. 解析节点配置：配置键中的表达式会基于本次运行求值；传入条目按原样使用。
2. 返回不带端口的 `{ message: "Executed ai.rag.compose", timestamp }`，因此引擎将其路由到 `out`。

## 在工作流中

### 节点

```json
{
  "node_id": "node-rag-compose",
  "label": "Compose RAG prompt",
  "operator_slug": "ai.rag.compose",
  "operator_version": "1.0.0",
  "config": {
    "max_context_tokens": 4000
  }
}
```

### Envelope

```json
{
  "ok": true,
  "port": "out",
  "data": {
    "message": "Executed ai.rag.compose",
    "timestamp": "2026-11-26T15:04:05.000Z"
  }
}
```

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

### `out`

每次运行。

```json
{
  "message": "Executed ai.rag.compose",
  "timestamp": "2026-11-26T15:04:05.000Z"
}
```

### `error`

代码从不输出此端口。

## 代码中的行为

- `template / max_context_tokens`: 已声明但被忽略。

## 说明与限制

- 尚未实现：占位符替换、上下文拼接和上下文大小限制。如需现在构建 RAG 提示词，请使用 `data.template` 或 `ai.generate` 搭配自定义模板。


## 契约（JSON Schema）

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

```json
{
  "slug": "ai.rag.compose",
  "name": "RAG Compose Prompt",
  "category": "ai",
  "version": "1.0.0",
  "description": "Constructs a RAG (Retrieval-Augmented Generation) prompt by combining a query with retrieved context.\n\n  This operator formats a list of retrieved documents into a context block and injects it into a \"Answer with context\" prompt template.\n\n  **Use Cases:**\n  - Q&A over internal documentation.\n  - Summarizing search results.\n  - Grounding LLM responses in factual data.",
  "capabilities": [
    "rag",
    "prompt-engineering",
    "context-window"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "template": {
        "type": "string",
        "title": "RAG Template",
        "format": "textarea",
        "default": "Context:\n{{context}}\n\nQuery:\n{{query}}\n\nAnswer using only the context above:",
        "description": "Must contain {{context}} and {{query}} placeholders."
      },
      "max_context_tokens": {
        "type": "number",
        "default": 4000,
        "title": "Max Context Tokens",
        "description": "Limit context size to fit model window."
      },
      "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": {
      "query": {
        "type": "string",
        "description": "The user question."
      },
      "context": {
        "type": [
          "array",
          "string"
        ],
        "description": "List of text chunks or already joined string."
      }
    },
    "required": [
      "query",
      "context"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "prompt": {
        "type": "string",
        "description": "The final prompt ready for generation."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Composed",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "sync",
  "tags": [
    "rag",
    "prompt",
    "compose"
  ],
  "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",
  "properties": {
    "template": {
      "type": "string",
      "title": "RAG Template",
      "format": "textarea",
      "default": "Context:\n{{context}}\n\nQuery:\n{{query}}\n\nAnswer using only the context above:",
      "description": "Must contain {{context}} and {{query}} placeholders."
    },
    "max_context_tokens": {
      "type": "number",
      "default": 4000,
      "title": "Max Context Tokens",
      "description": "Limit context size to fit model window."
    },
    "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": {
    "query": {
      "type": "string",
      "description": "The user question."
    },
    "context": {
      "type": [
        "array",
        "string"
      ],
      "description": "List of text chunks or already joined string."
    }
  },
  "required": [
    "query",
    "context"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "prompt": {
      "type": "string",
      "description": "The final prompt ready for generation."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Composed",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
