# RAG

Retrieve relevant chunks from indexed documents and answer the user's query grounded in them.

## Overview

The RAG (Retrieval-Augmented Generation) node loads one or more documents, embeds them, runs a similarity search against the user query, and asks an LLM to answer using only the retrieved context. Use it to build knowledge-base Q&A, policy lookup, or doc-grounded chat.

What happens at run time:

1. Load every configured document (PDF, CSV, or plain text).
2. Embed with OpenAI embeddings, store in an in-memory FAISS index.
3. Retrieve the **top-k** most similar chunks for the query.
4. Ask the LLM: "answer using this context; if it isn't here, say so."
5. Write the answer to `last_output`, the retrieved text to `ctx.rag_context`, and the chunk metadata to `ctx.rag_sources`.

The query is taken from `data.query`, or the original `input`, or `last_output` — in that order. The input is preferred over upstream classifier labels (e.g. `"sales"`), which make poor retrieval queries.

## Configuration

| Field | Description |
| --- | --- |
| `documents` | List of `{path, originalName}` items. PDF, CSV, TXT, MD, JSON. |
| `filePath` | Single-document shorthand (legacy). |
| `query` | Optional override. Empty = use `input` / `last_output`. |
| `k` | Top-k retrieval, default 4. Higher = more context, more tokens. |

## Example

Knowledge-base Q&A from a PDF handbook:

```
Webhook → RAG (documents: [handbook.pdf], k: 4) → Respond webhook
```

Inside a chat agent's pipeline:

```
Start → RAG (documents: [policies/*]) → Agent (answer with sources)
```

Downstream nodes can quote sources from `ctx.rag_sources`:

```
HTTP Request body: {"answer": "{last_output}", "sources": "{rag_sources}"}
```

## Gotchas

- The FAISS index is rebuilt on every run. For large corpora, host a dedicated vector store and call it via **HTTP Request** instead.
- Only files inside the workspace's upload directory are accepted. Paths pointing elsewhere are silently skipped.
- CSV rows are embedded one per row as `col: val | col: val` — useful for structured lookups, not for prose.
- The LLM prompt enforces grounding: if the context doesn't contain the answer, the model says so rather than hallucinating. Don't override that behavior unless you know why.
- Top-k of 4 covers most short questions. Raise to 8–12 for cross-document synthesis; expect higher cost and latency.

---

*Source: https://agentbuilder.systalink.sn/docs/node-rag — human documentation.*
*Other language: [/docs-md/fr/node-rag.md](/docs-md/fr/node-rag.md).*
*Machine-readable index: [/llms.txt](/llms.txt).*
