# 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`  [(See how to find it)](https://docs.snackprompt.com/how-to/how-to-obtain-your-user-id-tenant-id)&#x20;
* 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" \
  -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" \
  -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" \
  -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 - Implement streaming in the frontend
2. Filter by Tags - Direct context with precision
3. Error Handling - Handle errors gracefully

***

**Estimated time:** 10 minutes ✅


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.snackprompt.com/bring-your-data-into-ai/tutorials/chat-with-your-data-rag.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
