# End-to-end RAG application workflow
URL: https://docs.datarobot.com/en/docs/api/dev-learning/python/genai/rag-workflow.html

# End-to-end RAG application workflow

This example demonstrates how to build a complete RAG (Retrieval-Augmented Generation) application, combining LLM blueprints, vector databases, moderation, and testing. This workflow shows how to integrate all the components of a production-ready generative AI application.

### Create a playground

A playground is a workspace for experimenting with LLM configurations. All LLM blueprints must be associated with a playground.

```
import datarobot as dr
playground = dr.genai.Playground.create(
    name="RAG Application Playground",
    use_case=use_case.id
)
```

### Select an LLM

Choose from available LLMs provided by DataRobot. You can filter by use case or select based on model capabilities.

```
llms = dr.genai.LLMDefinition.list()
llm = [l for l in llms if 'gpt-4' in l.name.lower()][0]
```

### Build a vector database

Upload your documents as a dataset, then create a vector database with appropriate chunking parameters. The vector database stores document embeddings for RAG.

```
dataset = dr.Dataset.upload("company_docs.csv")
supported = dr.genai.VectorDatabase.get_supported_embeddings(dataset_id=dataset.id)
from datarobot.models.genai.vector_database import ChunkingParameters
chunking_params = ChunkingParameters(
    embedding_model=supported.default_embedding_model,
    chunking_method="semantic",
    chunk_size=500,
    chunk_overlap_percentage=10
)
use_case = dr.UseCase.get(use_case_id)
vector_db = dr.genai.VectorDatabase.create(
    dataset_id=dataset.id,
    use_case=use_case.id,
    name="Company Knowledge Base",
    chunking_parameters=chunking_params
)
```

### Create an LLM blueprint

Combine the LLM with the vector database to create a RAG-enabled blueprint. Configure system prompts and other LLM settings.

```
blueprint = dr.genai.LLMBlueprint.create(
    playground=playground.id,
    name="RAG Customer Support",
    description="RAG-enabled customer support assistant",
    llm=llm.id,
    llm_settings={
        "system_prompt": "You are a customer support assistant. Use the provided context to answer questions accurately.",
        "temperature": 0.7
    },
    vector_database=vector_db.id,
    vector_database_settings={
        "max_documents_retrieved_per_prompt": 3
    }
)
```

### Register custom model

Register the blueprint as a custom model version so it can be deployed and used in production.

```
custom_model_version = blueprint.register_custom_model()
```

### Add moderation

Set up content filtering to ensure safe and appropriate responses. Moderation can block or warn on problematic content.

```
template = dr.ModerationTemplate.list()[0]
moderation = dr.ModerationConfiguration.create(
    template_id=template.id,
    name="Content Safety",
    description="Filter inappropriate content",
    stages=[dr.ModerationGuardStage.PROMPT, dr.ModerationGuardStage.RESPONSE],
    entity_id=custom_model_version.id,
    entity_type=dr.ModerationGuardEntityType.CUSTOM_MODEL_VERSION,
    intervention=dr.ModerationIntervention.BLOCK
)
```

### Interact via chats

Create chat sessions to interact with your RAG application. Chats maintain conversation history for context-aware responses.

```
chat = dr.genai.Chat.create(
    name="Customer Support Session",
    llm_blueprint=blueprint.id
)
prompt = dr.genai.ChatPrompt.create(
    chat=chat.id,
    text="What is your return policy?"
)
prompt.text
prompt.result_text
```

---
