# Retrieve Context (RAG)

> Orchestrates the retrieval of context for RAG (Retrieval-Augmented Generation).

Source: https://docs.sommatic.ai/en/nodes/ai.rag.retrieve/

- Slug: `ai.rag.retrieve`
- Family: AI
- Version: 1.0.0
- Status: Experimental. The slug does not resolve to a registered worker (the implementation lives in `ai.rag.worker.js`, registered as `AiRagWorker`), so the node fails to spawn. The implementation itself only fills a prompt template with `query` and `context`: it performs no embedding, no vector search and no reranking.
- Execution: async

Orchestrates the retrieval of context for RAG (Retrieval-Augmented Generation).

This high-level operator abstracts the Embedding -> Vector Search -> Reranking pipeline into a single step. It takes a text query, handles the embedding internally (or via configuration), searches the vector store, and returns the best matching text chunks.

**Use Cases:**
- "One-click" RAG context retrieval.
- Simplifying the graph by hiding vector complexity.

## Configuration

- `knowledge_base_id` (string, required): ID of the managed Knowledge Base to search.
- `top_k` (number, optional): Default: `3`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `success` (default): Retrieved
- `empty`: No Data
- `error`: Error

## Input contract

- `query` (string, required): The user question/query.

## Output contract

- `context` (array<string>, optional): Retrieved text chunks.
- `sources` (array<object>, optional): Metadata about sources (page numbers, filenames).

## In expressions

What a later node can read from this node's output with `$("Retrieve Context (RAG)").item.json.<path>`, as the workflow editor suggests it under Previous Nodes (the output contract).

- `$("Retrieve Context (RAG)").item.json.context` (array): Retrieved text chunks.
- `$("Retrieve Context (RAG)").item.json.sources` (array): Metadata about sources (page numbers, filenames).


## How it works

Experimental and not runnable: the slug does not resolve to a registered worker, so the node fails before it starts. The code behind it only fills a prompt template; it performs no retrieval.

1. The runner looks for a worker registered for this slug, finds none and falls back to the name `AiRagRetrieveWorker`, which is not registered either.
2. Spawning throws `Worker not found: AiRagRetrieveWorker`; the engine records the step on `error` and fails the run.

## In a workflow

### Node

```json
{
  "node_id": "node-rag-retrieve",
  "label": "Retrieve context",
  "operator_slug": "ai.rag.retrieve",
  "operator_version": "1.0.0",
  "config": {
    "knowledge_base_id": "<knowledge_base_id>",
    "top_k": 3
  }
}
```

### Envelope

```json
{
  "ok": false,
  "port": "error",
  "data": {
    "error": "Worker not found: AiRagRetrieveWorker"
  }
}
```

## Output ports (behaviour in the code)

### `success`

Never fires.

### `empty`

Never fires.

### `error`

Every run fails at spawn. The error is thrown, so edges from `error` do not receive it; only an enclosing `flow.try` catches it.

```json
{
  "error": "Worker not found: AiRagRetrieveWorker"
}
```

## Behaviour in the code

- `knowledge_base_id / top_k`: Declared but never read.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Worker not found: AiRagRetrieveWorker` | `error` | Thrown, not returned as an envelope: the engine marks the run as failed, unless the node sits inside a `flow.try` scope, which receives the error on its catch branch. |

## Notes and limits

- Not implemented: embedding the query, searching a knowledge base, ranking and returning `context`/`sources`. The worker file that would serve it only replaces `{{context}}` and `{{query}}` in a template.
- There is no successful envelope: the example shows the failure the engine records.


## Contracts (JSON Schema)

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

```json
{
  "slug": "ai.rag.retrieve",
  "name": "Retrieve Context (RAG)",
  "category": "ai",
  "version": "1.0.0",
  "description": "Orchestrates the retrieval of context for RAG (Retrieval-Augmented Generation).\n\n  This high-level operator abstracts the Embedding -> Vector Search -> Reranking pipeline into a single step. It takes a text query, handles the embedding internally (or via configuration), searches the vector store, and returns the best matching text chunks.\n  \n  **Use Cases:**\n  - \"One-click\" RAG context retrieval.\n  - Simplifying the graph by hiding vector complexity.",
  "capabilities": [
    "rag",
    "retrieval",
    "search",
    "abstraction"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "knowledge_base_id"
    ],
    "properties": {
      "knowledge_base_id": {
        "type": "string",
        "title": "Knowledge Base",
        "description": "ID of the managed Knowledge Base to search."
      },
      "top_k": {
        "type": "number",
        "default": 3
      },
      "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/query."
      }
    },
    "required": [
      "query"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "context": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "description": "Retrieved text chunks."
      },
      "sources": {
        "type": "array",
        "items": {
          "type": "object"
        },
        "description": "Metadata about sources (page numbers, filenames)."
      }
    }
  },
  "ports": [
    {
      "port_id": "success",
      "label": "Retrieved",
      "is_default": true
    },
    {
      "port_id": "empty",
      "label": "No Data"
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "async",
  "tags": [
    "rag",
    "retrieval",
    "search"
  ],
  "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": [
    "knowledge_base_id"
  ],
  "properties": {
    "knowledge_base_id": {
      "type": "string",
      "title": "Knowledge Base",
      "description": "ID of the managed Knowledge Base to search."
    },
    "top_k": {
      "type": "number",
      "default": 3
    },
    "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/query."
    }
  },
  "required": [
    "query"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "context": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "description": "Retrieved text chunks."
    },
    "sources": {
      "type": "array",
      "items": {
        "type": "object"
      },
      "description": "Metadata about sources (page numbers, filenames)."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "success",
    "label": "Retrieved",
    "is_default": true
  },
  {
    "port_id": "empty",
    "label": "No Data"
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
