# Langfuse Ragas and Langfuse is a powerful combination that can help you evaluate and monitor your Retrieval-Augmented Generation (RAG) pipelines. ## What is Langfuse? Langfuse ([GitHub](https://github.com/langfuse/langfuse)) is an open-source platform for LLM [tracing](https://langfuse.com/docs/tracing), [prompt management](https://langfuse.com/docs/prompts/get-started), and [evaluation](https://langfuse.com/docs/scores/overview). It allows you to score your traces and spans, providing insights into the performance of your RAG pipelines. Langfuse supports various integrations, including [OpenAI](https://langfuse.com/docs/integrations/openai/python/get-started), [LangChain](https://langfuse.com/docs/integrations/langchain/tracing), and [more](https://langfuse.com/docs/integrations/overview). ## Key Benefits of using Langfuse with Ragas - **Score Traces**: [Score](https://langfuse.com/docs/scores/overview) your traces and spans, providing insights into the performance of your RAG pipelines. - **Detailed Analytics**: Segment and [analyze](https://langfuse.com/docs/analytics/overview) traces to identify low-quality scores and improve your system's performance. - **Score Reporting**: Drill down into detailed reports for specific use cases and user segments. Ragas ([GitHub](https://github.com/vibrantlabsai/ragas)) is an open-source tool that can help you run [Model-Based Evaluation](https://langfuse.com/docs/scores/model-based-evals) on your traces/spans, especially for RAG pipelines. Ragas can perform reference-free evaluations of various aspects of your RAG pipeline. Because it is reference-free you don't need ground-truths when running the evaluations and can run it on production traces that you've collected with Langfuse. ## Getting Started This guide will walk you through and end-to-end example of RAG evaluations with Ragas and Langfuse. ### The Environment [Sign up](https://cloud.langfuse.com) for Langfuse to get your API keys. ```python import os # get keys for your project from https://cloud.langfuse.com os.environ["LANGFUSE_SECRET_KEY"] = "sk-..." os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-..." # your openai key # os.environ["OPENAI_API_KEY"] = "sk-..." ``` ```python %pip install datasets ragas llama_index python-dotenv --upgrade ``` ### The Data For this example, we are going to use a dataset that has already been prepared by querying a RAG system and gathering its outputs. See below for instruction on how to fetch your production data from Langfuse. The dataset contains the following columns: - `question`: *list[str]* - These are the questions your RAG pipeline will be evaluated on. - `answer`: *list[str]* - The answer generated from the RAG pipeline and given to the user. - `contexts`: *list[list[str]]* - The contexts which were passed into the LLM to answer the question. - `ground_truth`: list[list[str]] - The ground truth answer to the questions. However, this can be ignored for online evaluations since we will not have access to ground-truth data in our case. ```python from datasets import load_dataset amnesty_qa = load_dataset("vibrantlabsai/amnesty_qa", "english_v2")["eval"] amnesty_qa ``` Found cached dataset amnesty_qa (/home/jjmachan/.cache/huggingface/datasets/vibrantlabs___amnesty_qa/english_v2/2.0.0/d0ed9800191a31943ee52a5c22ee4305e28a33f5edcd9a323802112cff07cc24) 0%| | 0/1 [00:00