|
| 1 | +# LangSmith |
| 2 | + |
| 3 | +[LangSmith](https://docs.smith.langchain.com/) is an advanced tool designed to enhance the development and deployment of applications utilizing large language models (LLMs). It provides a comprehensive framework for tracing, analyzing, and optimizing LLM workflows, making it easier for developers to manage complex interactions within their applications. |
| 4 | + |
| 5 | +This tutorial explains how to log traces of Ragas evaluations using LangSmith. Since Ragas is built on LangChain, you only need to set up LangSmith, and it will handle logging the traces automatically. |
| 6 | + |
| 7 | +## Setting Up LangSmith |
| 8 | + |
| 9 | +To set up LangSmith, make sure you set the following environment variables (refer to the [LangSmith documentation](https://docs.smith.langchain.com/#quick-start) for more details): |
| 10 | + |
| 11 | +```bash |
| 12 | +export LANGCHAIN_TRACING_V2=true |
| 13 | +export LANGCHAIN_ENDPOINT=https://api.smith.langchain.com |
| 14 | +export LANGCHAIN_API_KEY=<your-api-key> |
| 15 | +export LANGCHAIN_PROJECT=<your-project> # Defaults to "default" if not set |
| 16 | +``` |
| 17 | + |
| 18 | +## Getting the Dataset |
| 19 | + |
| 20 | +When creating evaluation dataset or evaluating instance, ensure the terminology matches the schema used in `SingleTurnSample` or `MultiTurnSample`. |
| 21 | + |
| 22 | + |
| 23 | +```python |
| 24 | +from ragas import EvaluationDataset |
| 25 | + |
| 26 | + |
| 27 | +dataset = [ |
| 28 | + { |
| 29 | + "user_input": "Which CEO is widely recognized for democratizing AI education through platforms like Coursera?", |
| 30 | + "retrieved_contexts": [ |
| 31 | + "Andrew Ng, CEO of Landing AI, is known for his pioneering work in deep learning and for democratizing AI education through Coursera." |
| 32 | + ], |
| 33 | + "response": "Andrew Ng is widely recognized for democratizing AI education through platforms like Coursera.", |
| 34 | + "reference": "Andrew Ng, CEO of Landing AI, is known for democratizing AI education through Coursera.", |
| 35 | + }, |
| 36 | + { |
| 37 | + "user_input": "Who is Sam Altman?", |
| 38 | + "retrieved_contexts": [ |
| 39 | + "Sam Altman, CEO of OpenAI, has advanced AI research and advocates for safe, beneficial AI technologies." |
| 40 | + ], |
| 41 | + "response": "Sam Altman is the CEO of OpenAI and advocates for safe, beneficial AI technologies.", |
| 42 | + "reference": "Sam Altman, CEO of OpenAI, has advanced AI research and advocates for safe AI.", |
| 43 | + }, |
| 44 | + { |
| 45 | + "user_input": "Who is Demis Hassabis and how did he gain prominence?", |
| 46 | + "retrieved_contexts": [ |
| 47 | + "Demis Hassabis, CEO of DeepMind, is known for developing systems like AlphaGo that master complex games." |
| 48 | + ], |
| 49 | + "response": "Demis Hassabis is the CEO of DeepMind, known for developing systems like AlphaGo.", |
| 50 | + "reference": "Demis Hassabis, CEO of DeepMind, is known for developing AlphaGo.", |
| 51 | + }, |
| 52 | + { |
| 53 | + "user_input": "Who is the CEO of Google and Alphabet Inc., praised for leading innovation across Google's product ecosystem?", |
| 54 | + "retrieved_contexts": [ |
| 55 | + "Sundar Pichai, CEO of Google and Alphabet Inc., leads innovation across Google's product ecosystem." |
| 56 | + ], |
| 57 | + "response": "Sundar Pichai is the CEO of Google and Alphabet Inc., praised for leading innovation across Google's product ecosystem.", |
| 58 | + "reference": "Sundar Pichai, CEO of Google and Alphabet Inc., leads innovation across Google's product ecosystem.", |
| 59 | + }, |
| 60 | + { |
| 61 | + "user_input": "How did Arvind Krishna transform IBM?", |
| 62 | + "retrieved_contexts": [ |
| 63 | + "Arvind Krishna, CEO of IBM, transformed the company by focusing on cloud computing and AI solutions." |
| 64 | + ], |
| 65 | + "response": "Arvind Krishna transformed IBM by focusing on cloud computing and AI solutions.", |
| 66 | + "reference": "Arvind Krishna, CEO of IBM, transformed the company through cloud computing and AI.", |
| 67 | + }, |
| 68 | +] |
| 69 | + |
| 70 | +evaluation_dataset = EvaluationDataset.from_list(dataset) |
| 71 | +``` |
| 72 | + |
| 73 | +## Tracing ragas metrics |
| 74 | + |
| 75 | +Run the Ragas evaluations on your dataset, and the traces will appear in your LangSmith dashboard under the specified project name or "default." |
| 76 | + |
| 77 | + |
| 78 | +```python |
| 79 | +from ragas import evaluate |
| 80 | +from ragas.llms import LangchainLLMWrapper |
| 81 | +from langchain_openai import ChatOpenAI |
| 82 | +from ragas.metrics import LLMContextRecall, Faithfulness, FactualCorrectness |
| 83 | + |
| 84 | +llm = ChatOpenAI(model="gpt-4o-mini") |
| 85 | +evaluator_llm = LangchainLLMWrapper(llm) |
| 86 | + |
| 87 | +result = evaluate( |
| 88 | + dataset=evaluation_dataset, |
| 89 | + metrics=[LLMContextRecall(), Faithfulness(), FactualCorrectness()], |
| 90 | + llm=evaluator_llm, |
| 91 | +) |
| 92 | + |
| 93 | +result |
| 94 | +``` |
| 95 | + |
| 96 | +Output |
| 97 | +``` |
| 98 | +Evaluating: 0%| | 0/15 [00:00<?, ?it/s] |
| 99 | +
|
| 100 | +{'context_recall': 1.0000, 'faithfulness': 0.9333, 'factual_correctness': 0.8520} |
| 101 | +``` |
| 102 | + |
| 103 | +## LangSmith Dashboard |
| 104 | + |
0 commit comments