# Answer and Context Relevancy Evaluations in LlamaIndex

This example demonstrates how to evaluate the quality of answers generated by a LlamaIndex RAG system and the relevancy of the retrieved context using `AnswerRelevancyEvaluator` and `ContextRelevancyEvaluator`. Evaluating these aspects is crucial for ensuring the effectiveness of LLM applications, especially those relying on external knowledge.

The process involves:
1.  Setting up a basic RAG pipeline with a `VectorStoreIndex`.
2.  Generating synthetic questions and reference answers using `RagDatasetGenerator` (though for simplicity, a small predefined dataset is used here).
3.  Running the RAG pipeline to get predictions (answers and retrieved context).
4.  Using `AnswerRelevancyEvaluator` to assess if the generated answers directly address the user's query.
5.  Using `ContextRelevancyEvaluator` to check if the retrieved context is pertinent to the question asked.
6.  Displaying the evaluation results, which include a score (0-1) and feedback from an LLM acting as a judge.

## Requirements

* Python 3.8+
* An OpenAI API key set as an environment variable (`OPENAI_API_KEY`).

## Steps for running this example

1.  Install dependencies:
    ```bash
    python -m venv .venv
    source .venv/bin/activate  # Windows cmd: .venv\Scripts\activate # Windows PowerShell: .venv\Scripts\Activate.ps1
    pip install -r requirements.txt
    ```

2. Set environment variables:
    Ensure your OpenAI API key is set as an environment variable. You can do this by creating a `.env` file in the example directory with the following content:
    ```
    OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
    ```
    Replace `"YOUR_OPENAI_API_KEY"` with your actual OpenAI API key.

3. Run the script:
    ```bash
    python answer_context_evaluation.py
    ```

## Output

The script will output the results of the answer relevancy and context relevancy evaluations. For each query, it will show the score and feedback provided by the LLM evaluator, giving insights into how well the RAG system performed.
