> For the complete documentation index, see [llms.txt](https://docs.snackprompt.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.snackprompt.com/bring-your-data-into-ai/get-started/chat-with-your-data-rag.md).

# Chat with your Data (RAG)

In this tutorial, you'll learn how to use chat to converse with your data using AI.

### What is RAG?

**RAG (Retrieval-Augmented Generation)** is a technique that combines:

1. **Retrieval:** Finds the most relevant information in your data
2. **Augmented Generation:** Uses that information as context for AI to generate a response

**Result:** Accurate answers based on your data, without hallucinations.

### Search vs Chat: Which to Use?

| Scenario                     | Use        | Reason                         |
| ---------------------------- | ---------- | ------------------------------ |
| Need exact excerpts          | **Search** | Returns original chunks        |
| Need an elaborate answer     | **Chat**   | AI synthesizes the information |
| Need to cite exact source    | **Both**   | Chat returns `sources`         |
| High performance/low latency | **Search** | No LLM call                    |
| Complex questions            | **Chat**   | AI interprets and responds     |

### Prerequisites

* A valid `tenant_id`
* Your `API Key` for authentication (see [Authentication](https://snack-prompt.gitbook.io/snack-prompt-docs/api-reference/authentication))
* Data already ingested in the Knowledge Base

### Step 1: Ask a Question

```bash
curl -X POST https://api-integrations.snackprompt.com/v1/kb/chat \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "query": "what are the benefits of the premium plan?",
    "filters": {
      "tenant_id": "YOUR_TENANT_ID"
    }
  }'
```

### Step 2: Understand the Response

```json
{
  "answer": "The Premium plan offers the following benefits:\n\n1. **24/7 Priority Support** - Response within 1 hour\n2. **Unlimited Storage** - No upload limits\n3. **Advanced API** - Access to all endpoints\n4. **Custom Reports** - Customizable dashboards\n\nThe price is $99/month with a 20% discount on the annual plan.",
  "sources": [
    {
      "id": "chunk-uuid-1",
      "score": 0.91,
      "snack_item_id": "item-plans-001",
      "snack_elemental_id": "doc-pricing-123",
      "text": "Premium Plan: 24/7 priority support, unlimited storage...",
      "tag_names": ["Plans", "Pricing"]
    },
    {
      "id": "chunk-uuid-2",
      "score": 0.85,
      "snack_item_id": "item-plans-002",
      "snack_elemental_id": "doc-pricing-123",
      "text": "Prices: Premium $99/month, 20% annual discount...",
      "tag_names": ["Plans", "Pricing"]
    }
  ]
}
```

#### Response Fields

| Field                          | Description                                 |
| ------------------------------ | ------------------------------------------- |
| `answer`                       | AI-generated answer based on your data      |
| `sources`                      | List of sources used to generate the answer |
| `sources[].id`                 | Chunk ID in the vector database             |
| `sources[].score`              | Source relevance (0-1)                      |
| `sources[].text`               | Original excerpt used as context            |
| `sources[].snack_item_id`      | Source item ID                              |
| `sources[].snack_elemental_id` | Source elemental ID                         |

### Step 3: Use Filters for Specific Context

You can direct chat to search in specific data:

```bash
curl -X POST https://api-integrations.snackprompt.com/v1/kb/chat \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "query": "what are the benefits of the premium plan?",
    "filters": {
      "tenant_id": "YOUR_TENANT_ID",
      "tag_names": ["Plans", "Commercial"]
    }
  }'
```

This ensures the AI only uses documents with these tags as context.

### Using Streaming for Real-time Chat

For real-time chat interfaces, use the streaming endpoint:

```bash
curl -X POST https://api-integrations.snackprompt.com/v1/kb/chat/stream \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "query": "what are the benefits of the premium plan?",
    "filters": {
      "tenant_id": "YOUR_TENANT_ID"
    }
  }' \
  --no-buffer
```

The response comes as **Server-Sent Events (SSE)**:

```
data: {"event":"message","data":{"content":"The Premium"}}

data: {"event":"message","data":{"content":" plan"}}

data: {"event":"message","data":{"content":" offers"}}

data: {"event":"message","data":{"content":"..."}}

data: [DONE]
```

> To implement streaming in the frontend, see the guide Real-time Chat.

### Understanding Citations (Sources)

The `sources` allow you to verify where each piece of information came from:

```json
{
  "sources": [
    {
      "snack_elemental_id": "doc-pricing-123",
      "snack_item_id": "item-plans-001",
      "text": "Original excerpt...",
      "score": 0.91
    }
  ]
}
```

You can use these IDs to:

* Link to the original document in your interface
* Show users the source of information
* Validate response accuracy

### Tips for Better Responses

#### 1. Be Specific in Your Question

```diff
- "tell me about plans"
+ "what are the differences between the basic and premium plans?"
```

#### 2. Use Filters for Context

If you know where the information is, use filters:

```json
{
  "query": "delivery time to New York",
  "filters": {
    "tenant_id": "...",
    "tag_names": ["Logistics", "Shipping"]
  }
}
```

#### 3. Comparison Questions Work Well

```
"compare plan A with plan B"
"what are the advantages of X over Y?"
```

#### 4. List Questions Work Well

```
"list the required documents for..."
"what are the steps to..."
```

### When Chat Doesn't Find Information

If the AI doesn't find relevant information, it will respond something like:

```json
{
  "answer": "I didn't find information about this topic in the knowledge base. Could you rephrase the question or verify if the related data has been indexed?",
  "sources": []
}
```

**What to do:**

1. Check if the data was ingested correctly
2. Try rephrasing the question
3. Remove overly restrictive filters
4. Use semantic search to explore available data

### Next Steps

Now that you've mastered RAG chat:

1. [Real-time Chat](/bring-your-data-into-ai/how-to/how-to-implement-real-time-chat.md) - Implement streaming in the frontend
2. [Filter by Tags](/bring-your-data-into-ai/reference/filters.md) - Direct context with precision
3. [Error Handling](/bring-your-data-into-ai/how-to/how-to-handle-errors.md) - Handle errors gracefully

***

**Estimated time:** 10 minutes ✅
