# Building the RAG evaluation

## Building a RAG Workflow: A Practical Use Case

In this section, we will guide you through the process of creating a **Retrieval-Augmented Generation (RAG)** workflow using a real-world example. We will also discuss how to prepare your documents for effective evaluation by metrics during the evaluation phase.

<figure><img src="/files/gaLUvQMR6927L15qmnZF" alt=""><figcaption></figcaption></figure>

### 1. Setting Up Your RAG Workflow

#### Components of the Workflow

The RAG workflow consists of several key components that work together to produce relevant and coherent responses based on user queries. Below is an overview of each stage in the workflow:

**Input Stage**

* **Input Component**: The workflow begins with an input query from the user.
  * **Parameters**:
    * **query**: String input specifying the user's query.

**Embedding Stage**

* **Embedding Component**: OpenAI Text Embedder transforms the input query into an embedding—a numerical representation that captures the semantic meaning of the query.
  * **Parameters**:
    * **query**: The original user query.
    * **embedding**: The resulting list of floats representing the embedded query.

**Document Retrieval**

* **Retriever Component**: Pinecone Retriever utilizes the embeddings generated by the OpenAI Text Embedder to search through a pre-stored database of documents and retrieve those that are most relevant to the query.
  * **Parameters**:
    * **embedding**: The list of floats generated from the embedding stage.
    * **documents**: A list of documents retrieved based on the embedding.
    * **filters**: Any additional criteria used to refine document selection.

**Content Processing**

* **Processing Component**: This Python module processes the retrieved documents, allowing for customization in analyzing and extracting relevant context for generating the final response.
  * **Parameters**:
    * **input\_data**: Any relevant input data for processing the documents.
    * **content**: Content extracted from the retrieved documents.

**Response Generation**

* **Generation Component**: OpenAI Model generates a coherent answer based on the retrieved documents and the initial query.
  * **Parameters**:
    * **documents**: The list of documents identified as relevant.
    * **query**: The original query from the user.
    * **content**: Additional context to enhance the generative response.

**Output Stage**

* **Output Component**: The workflow concludes with an output that presents both the generated answer and the relevant context used during generation.
  * **Parameters**:
    * **answer**: The final generated response to the user’s query.
    * **context**: The supportive context leveraging the retrieved documents.

### 2. Selecting Metrics for RAG Evaluation

When it comes to evaluating the performance of your RAG workflow, selecting the right metrics is crucial. Here are recommended metrics to consider:

#### Key Metrics for RAG Workflows

1. **Factual Correctness**:
   * Measures the accuracy of the information presented in the generated answer against verified sources. This ensures that the answers provided are factually correct.
2. **Contextual Relevance**:
   * Assesses how well the context from the retrieved documents supports the generated response. This metric can help verify that relevant information is being effectively utilized.
3. **Clarity and Coherence**:
   * Evaluates the readability and logical flow of the generated answer. A coherent response is essential for user comprehension.
4. **Context Precision**:
   * Measures the precision with which the answer utilizes the provided context. This ensures that the model correctly represents relevant details.
5. **Response Completeness**:
   * Checks if the generated answer addresses all aspects of the user’s query. This metric evaluates whether the response is comprehensive enough to satisfy user intent.

### Conclusion

By setting up a RAG workflow and selecting appropriate evaluation metrics, you can effectively generate informative and relevant answers to user queries. This process not only showcases the capabilities of your RAG system but also ensures the quality and accuracy of the outputs.

Utilizing these components and metrics will help you refine your AI applications and enhance their performance in real-world scenarios.


---

# 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.getdynamiq.ai/evaluations/examples/building-the-rag-evaluation.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.
