{ "cells": [ { "cell_type": "markdown", "id": "1079e444-91e1-4b81-a28a-2ce4763f4bc4", "metadata": {}, "source": [ "# Langfuse\n", "\n", "Ragas and Langfuse is a powerful combination that can help you evaluate and monitor your Retrieval-Augmented Generation (RAG) pipelines.\n", "\n", "## What is Langfuse?\n", "\n", "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).\n", "\n", "## Key Benefits of using Langfuse with Ragas\n", "\n", "- **Score Traces**: [Score](https://langfuse.com/docs/scores/overview) your traces and spans, providing insights into the performance of your RAG pipelines.\n", "- **Detailed Analytics**: Segment and [analyze](https://langfuse.com/docs/analytics/overview) traces to identify low-quality scores and improve your system's performance.\n", "- **Score Reporting**: Drill down into detailed reports for specific use cases and user segments.\n", "\n", "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.\n", "\n", "## Getting Started\n", "\n", "This guide will walk you through and end-to-end example of RAG evaluations with Ragas and Langfuse.\n", "\n", "### The Environment\n", "\n", "[Sign up](https://cloud.langfuse.com) for Langfuse to get your API keys." ] }, { "cell_type": "code", "execution_count": 2, "id": "017dc09a-c59c-4e5f-a632-d8a5110f931d", "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "# get keys for your project from https://cloud.langfuse.com\n", "os.environ[\"LANGFUSE_SECRET_KEY\"] = \"sk-...\"\n", "os.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"pk-...\"\n", "\n", "# your openai key\n", "# os.environ[\"OPENAI_API_KEY\"] = \"sk-...\"" ] }, { "cell_type": "code", "execution_count": null, "id": "90a9536a-4997-47a4-82a7-3970c1145dab", "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "%pip install datasets ragas llama_index python-dotenv --upgrade" ] }, { "cell_type": "markdown", "id": "580b6d2a-06e2-4682-8e03-47d054d7f240", "metadata": {}, "source": [ "### The Data\n", "\n", "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.\n", "\n", "The dataset contains the following columns:\n", "- `question`: *list[str]* - These are the questions your RAG pipeline will be evaluated on.\n", "- `answer`: *list[str]* - The answer generated from the RAG pipeline and given to the user.\n", "- `contexts`: *list[list[str]]* - The contexts which were passed into the LLM to answer the question.\n", "- `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." ] }, { "cell_type": "code", "execution_count": 2, "id": "ebfb8207-8ddc-4b61-bcbc-f257820bf671", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Found cached dataset amnesty_qa (/home/jjmachan/.cache/huggingface/datasets/vibrantlabsai___amnesty_qa/english_v2/2.0.0/d0ed9800191a31943ee52a5c22ee4305e28a33f5edcd9a323802112cff07cc24)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "77e7ed90dd244b5c93865eb284f31f6d", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1 [00:00