# Tutorial: Building a Robust RAG App

Build a retrieval augmented generation (RAG) app running on your own data that can generate answers in a friendly and conversational tone. Learn how to test different prompts and insert them into your pipeline from Prompt Explorer.

***

- **Level**: Basic
- **Time to complete**: 10 minutes
- **Prerequisites**: 
  - You must be an Admin to complete this tutorial.
  - You must have an API key from an active OpenAI account as this pipeline uses the _gpt-3.5-turbo_ model by OpenAI.
- **Goal**: After completing this tutorial, you will have built a RAG system that can answer questions about treating various diseases based on the documents from [Mayo Clinic](https://www.mayoclinic.org/diseases-conditions). This system will run on the data you provide to it to minimize the possibility of hallucinations.
- **Keywords**: large language models, retrieval augmented generation, RAG, gpt, Prompt Explorer

***

## Create a Workspace

We need a <ProductShortName/> workspace to store our files and the generative pipeline.

1. Log in to <a href="https://cloud.deepset.ai/" target="_blank"><ProductName/></a>. 
2. In the upper left corner, click the name of the workspace, type _RAG_ as the workspace name, and click **Create**. 

<ClickableImage
  src="/img/tutorials/tutorial_building_robust_rag_workspace_creation.png"
  alt="The workspace creation window expanded"
  size="large"
/>

**Result**: You have created a workspace called `RAG`, where you'll upload the Mayo Clinic files.

## Upload Files to Your Workspace

1. First, download the [mayoclinic.zip](https://drive.google.com/file/d/16Y14-niu3-2shMnRP6-bPYNVRSTcMtEb/view?usp=sharing) file and unpack it on your computer. (You can also use your own files.)
2. In <ProductShortName/>, make sure you're in the _RAG_ workspace, and go to _Files>Upload Files_.
3. Open the folder you downloaded and unzipped in step 1, select all the files in it, and drag them to the Upload Files window. Click **Upload**.
4. Wait until the upload finishes. You should have 1096 files in your workspace.  

**Result**: Your files are in the `RAG` workspace and you can see them on the Files page. 

<ClickableImage
  src="/img/tutorials/tutorial_first_doc_search_file_list.png"
  alt="The Files page with the uploaded files showing in a list"
  size="standard"
/>

## Connect Your OpenAI Account

Once you connect <ProductShortName/> to your OpenAI account, you can use OpenAI models without passing the API keys in the pipeline.

1. Click your initials in the top right corner and choose **Settings**. 

<ClickableImage
  src="/img/getting-started/settings_option.png"
  alt="The personal menu expanded with the Settings option marked"
  size="large"
/>

2. Go to *Workspace>Integrations* and find OpenAI.
3. Next to OpenAI, click **Connect**, paste your OpenAI API key, and click **Submit**. 

**Result**: You're connected to your OpenAI account and can use OpenAI models in your pipelines. 

<ClickableImage
  src="/img/tutorials/connected.png"
  alt="The integrations section with the OpenAI option showing as connected."
  size="standard"
/>

<CreateIndex />

## Create a Draft Pipeline

Let's create a pipeline that will be a starting point for the generative question answering app:

1. In the navigation, go to **Pipeline Templates**.
2. Choose **Basic QA,** find _RAG Question Answering_, hover your mouse over the template and click **Use Template**. 

<ClickableImage
  src="/img/tutorials/tutorial-first-rag-choose-template.png"
  alt="Screenshot of a 'Basic QA' section on a webpage showcasing pipeline templates for question-answering systems. The section is one of several categories listed in a sidebar on the left. Each template offers a brief description of its functionality, emphasizing the use of semantic similarity in searching for answers. Icons indicate the creator of the templates, 'deepset'. At the bottom of each template, there are options to 'View Details' and 'Use Template', with the latter having a notification bubble with a curled arrow symbol, suggesting an update or new feature. The interface is clean with a color scheme consisting primarily of blue, white, and gray."
  size="standard"
/>
 
3. Type _RAG_ as the pipeline name and click **Create Pipeline**. You're redirected to Builder, where you can view and edit your pipeline. 
4. Find the `OpenSearchDocumentStore` component (it has a warning message prompting you to choose and index) and choose the `standard-index` you previously created from the list on the component card. 

<ClickableImage
  src="/img/tutorials/indexes_choose_index_tutorials.png"
  alt="The index selection on the document store card"
  size="large"
/>
5. To test your pipeline, click **Run** on the toolbar and type "what are the symptoms of the flu?". You should see the answer in the chat window.
  :::tip Changing the Model

  This pipeline uses the gpt-5.4 model by default. You can change the model on the `LLM` component card. Click the **Model** parameter and choose a different model from the list. 

  <ClickableImage
    src="/img/tutorials/change-model.gif"
    alt="The model selection on the LLM component card"
    size="standard"
  />
  
  Make sure the model provider is connected to your account. For help, see [Add Integrations](/docs/how-to-guides/managing-access/add-integrations.mdx).
  :::

5. Click **Deploy**. Wait until the pipeline is deployed.

**Result**: You now have an indexed RAG pipeline that generates answers based on your data. Your pipeline status is *Deployed*, and it's ready for use. Your pipeline is at the development service level. We recommend you test it before setting it to the production service level.

<ClickableImage
  src="/img/tutorials/rag_pipeline.png"
  alt="The pipelines page with the generative QA pipeline showing as indexed"
  size="standard"
/>

## Work on Your Prompt

The default prompt makes the model act as a matter-of-fact technical expert, while we want our system to be friendly and empathetic. Let's experiment with different prompts to achieve this effect. 

1. In the navigation, click **Prompt Explorer**. 
2. Choose the _RAG_ pipeline (1). Your current prompt is shown in the Prompt Editor pane (2). 

<ClickableImage
  src="/img/tutorials/tutorial_rag_prompt_explorer.png"
  alt="Descriptive alt text"
  size="standard"
/>

3. In the `Type your query here` field, ask some questions about treating medical conditions, such as: "I had my wisdom tooth removed, but my gum hurts and is swollen. What should I do?" 

<ClickableImage
  src="/img/tutorials/query_field.png"
  alt="Query asked in Prompt Explorer"
  size="standard"
/>

The model generates an answer and provides its sources, which are the documents it's based on.

4. Now, let's try a different prompt. In Prompt Editor, change the prompt to adjust the tone of the answer. Replace "You are a technical expert." with "You are a friendly nurse." and add "Your answers are friendly, clear, and conversational.", like in the prompt below:

```
You are a friendly, empathetic nurse.
You answer questions truthfully based on provided documents.
Your answers are friendly, clear, and conversational. 
For each document check whether it is related to the question.
Only use documents that are related to the question to answer it.
Ignore documents that are not related to the question.
If the answer exists in several documents, summarize them.
Only answer based on the documents provided. Don't make things up.
If the documents can't answer the question or you are unsure say: 'The answer can't be found in the text'.
These are the documents:
{% for document in documents %}
Document[{{ loop.index }}]:
{{ document.content }}
{% endfor %}
Question: {{question}}
Answer:
```

5. Try the same query or experiment with other queries related to treating medical conditions. The answers should now be in a more empathetic and friendly tone. Here are some example questions you can ask:  
   "I have been diagnosed with a wheat allergy, what do I do now?"  
   "How do you treat swollen wrists?"  
   "What is meningitis?"
6. Insert the updated prompt into your RAG pipeline. Click **Update** in Prompt Editor and confirm your action. This replaces the current prompt.

**Result**: You have tweaked your prompt to generate more friendly and conversational answers. You updated your pipeline with this prompt. 

## Test the Pipeline

Time to see your pipeline in action!

1. In the navigation, click **Playground** and make sure the _RAG_ pipeline is selected.
2. Try asking something like "my eyes hurt, what should I do?".
3. Once the answer is generated, check the sources to see if the answers are actually in the documents. 

<ClickableImage
  src="/img/tutorials/check_sources.png"
  alt="The answer to the query with the Sources section expanded"
  size="standard"
/>

You can also check the prompt by clicking the More Actions button next to the search result.

**Congratulations!** You have built a generative question answering system that can answer questions about treating various diseases in a friendly and conversational tone. Your system also shows references to documents it based its answers on.

## What To Do Next

Your pipeline is now a development pipeline. Once it's ready for production, change its service level to _Production_. You can do this on the Pipeline Details page shown after clicking a pipeline name. To learn more, see [Pipeline Service Levels](/docs/concepts/about-pipelines/about-pipelines.mdx#pipeline-service-levels).
