|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import typing as t |
| 4 | + |
| 5 | +from langchain.chains.base import Chain |
| 6 | +from langchain.schema import RUN_KEY |
| 7 | +from langchain_openai.chat_models import ChatOpenAI |
| 8 | +from langchain_openai.embeddings import OpenAIEmbeddings |
| 9 | +from langsmith.evaluation import EvaluationResult, RunEvaluator |
| 10 | +from langsmith.schemas import Example, Run |
| 11 | + |
| 12 | +from ragas.embeddings import LangchainEmbeddingsWrapper |
| 13 | +from ragas.llms import LangchainLLMWrapper |
| 14 | +from ragas.metrics.base import ( |
| 15 | + EvaluationMode, |
| 16 | + Metric, |
| 17 | + MetricWithEmbeddings, |
| 18 | + MetricWithLLM, |
| 19 | + get_required_columns, |
| 20 | +) |
| 21 | +from ragas.run_config import RunConfig |
| 22 | +from ragas.validation import EVALMODE_TO_COLUMNS |
| 23 | + |
| 24 | +if t.TYPE_CHECKING: |
| 25 | + from langchain.callbacks.manager import ( |
| 26 | + AsyncCallbackManagerForChainRun, |
| 27 | + CallbackManagerForChainRun, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +class EvaluatorChain(Chain, RunEvaluator): |
| 32 | + """ |
| 33 | + Wrapper around ragas Metrics to use them with langsmith. |
| 34 | + """ |
| 35 | + |
| 36 | + metric: Metric |
| 37 | + |
| 38 | + def __init__(self, metric: Metric, **kwargs: t.Any): |
| 39 | + kwargs["metric"] = metric |
| 40 | + super().__init__(**kwargs) |
| 41 | + if "run_config" in kwargs: |
| 42 | + run_config = kwargs["run_config"] |
| 43 | + else: |
| 44 | + run_config = RunConfig() |
| 45 | + if isinstance(self.metric, MetricWithLLM): |
| 46 | + llm = kwargs.get("llm", ChatOpenAI()) |
| 47 | + t.cast(MetricWithLLM, self.metric).llm = LangchainLLMWrapper(llm) |
| 48 | + if isinstance(self.metric, MetricWithEmbeddings): |
| 49 | + embeddings = kwargs.get("embeddings", OpenAIEmbeddings()) |
| 50 | + t.cast( |
| 51 | + MetricWithEmbeddings, self.metric |
| 52 | + ).embeddings = LangchainEmbeddingsWrapper(embeddings) |
| 53 | + self.metric.init(run_config) |
| 54 | + |
| 55 | + @property |
| 56 | + def input_keys(self) -> list[str]: |
| 57 | + return get_required_columns(self.metric.evaluation_mode) |
| 58 | + |
| 59 | + @property |
| 60 | + def output_keys(self) -> list[str]: |
| 61 | + return [self.metric.name] |
| 62 | + |
| 63 | + def _call( |
| 64 | + self, |
| 65 | + inputs: dict[str, t.Any], |
| 66 | + run_manager: t.Optional[CallbackManagerForChainRun] = None, |
| 67 | + ) -> dict[str, t.Any]: |
| 68 | + """ |
| 69 | + Call the evaluation chain. |
| 70 | + """ |
| 71 | + self._validate(inputs) |
| 72 | + _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() |
| 73 | + callbacks = _run_manager.get_child() |
| 74 | + |
| 75 | + c = inputs.get("contexts", [""]) |
| 76 | + g = inputs.get("ground_truth", "") |
| 77 | + q = inputs.get("question", "") |
| 78 | + a = inputs.get("answer", "") |
| 79 | + score = self.metric.score( |
| 80 | + { |
| 81 | + "question": q, |
| 82 | + "answer": a, |
| 83 | + "contexts": c, |
| 84 | + "ground_truth": g, |
| 85 | + }, |
| 86 | + callbacks=callbacks, |
| 87 | + ) |
| 88 | + return {self.metric.name: score} |
| 89 | + |
| 90 | + async def _acall( |
| 91 | + self, |
| 92 | + inputs: t.Dict[str, t.Any], |
| 93 | + run_manager: t.Optional[AsyncCallbackManagerForChainRun] = None, |
| 94 | + ) -> t.Dict[str, t.Any]: |
| 95 | + """ |
| 96 | + Call the evaluation chain. |
| 97 | + """ |
| 98 | + self._validate(inputs) |
| 99 | + _run_manager = run_manager or AsyncCallbackManagerForChainRun.get_noop_manager() |
| 100 | + # TODO: currently AsyncCallbacks are not supported in ragas |
| 101 | + _run_manager.get_child() |
| 102 | + |
| 103 | + c = inputs.get("contexts", [""]) |
| 104 | + g = inputs.get("ground_truth", "") |
| 105 | + q = inputs.get("question", "") |
| 106 | + a = inputs.get("answer", "") |
| 107 | + score = await self.metric.ascore( |
| 108 | + { |
| 109 | + "question": q, |
| 110 | + "answer": a, |
| 111 | + "contexts": c, |
| 112 | + "ground_truth": g, |
| 113 | + }, |
| 114 | + callbacks=[], |
| 115 | + ) |
| 116 | + return {self.metric.name: score} |
| 117 | + |
| 118 | + def _validate( |
| 119 | + self, |
| 120 | + input: dict[str, t.Any], |
| 121 | + question_key: str = "question", |
| 122 | + prediction_key: str = "answer", |
| 123 | + context_key: str = "contexts", |
| 124 | + ) -> None: |
| 125 | + # validate each example |
| 126 | + required_columns = EVALMODE_TO_COLUMNS[self.metric.evaluation_mode] |
| 127 | + if "question" in required_columns and question_key not in input: |
| 128 | + raise ValueError( |
| 129 | + f'"{question_key}" is required in each example' |
| 130 | + f"for the metric[{self.metric.name}] you have chosen." |
| 131 | + ) |
| 132 | + if "answer" in required_columns and prediction_key not in input: |
| 133 | + raise ValueError( |
| 134 | + f'"{prediction_key}" is required in each prediction' |
| 135 | + f"for the metric[{self.metric.name}] you have chosen." |
| 136 | + ) |
| 137 | + if "contexts" in required_columns and context_key not in input: |
| 138 | + raise ValueError( |
| 139 | + f'"{context_key}" is required in each prediction for the ' |
| 140 | + f"metric[{self.metric.name}] you have chosen." |
| 141 | + ) |
| 142 | + if "ground_truth" in required_columns and "ground_truth" not in input: |
| 143 | + raise ValueError( |
| 144 | + f'"ground_truth" is required in each prediction for the ' |
| 145 | + f"metric[{self.metric.name}] you have chosen." |
| 146 | + ) |
| 147 | + |
| 148 | + @staticmethod |
| 149 | + def _keys_are_present(keys_to_check: list, dict_to_check: dict) -> list[str]: |
| 150 | + return [k for k in keys_to_check if k not in dict_to_check] |
| 151 | + |
| 152 | + def _validate_langsmith_eval(self, run: Run, example: t.Optional[Example]) -> None: |
| 153 | + if example is None: |
| 154 | + raise ValueError( |
| 155 | + "expected example to be provided. Please check langsmith dataset and ensure valid dataset is uploaded." |
| 156 | + ) |
| 157 | + if example.inputs is None: |
| 158 | + raise ValueError( |
| 159 | + "expected example.inputs to be provided. Please check langsmith dataset and ensure valid dataset is uploaded." |
| 160 | + ) |
| 161 | + if example.outputs is None: |
| 162 | + raise ValueError( |
| 163 | + "expected example.inputs to be provided. Please check langsmith dataset and ensure valid dataset is uploaded." |
| 164 | + ) |
| 165 | + if "question" not in example.inputs or "ground_truth" not in example.outputs: |
| 166 | + raise ValueError( |
| 167 | + "Expected 'question' and 'ground_truth' in example." |
| 168 | + f"Got: {[k for k in example.inputs.keys()]}" |
| 169 | + ) |
| 170 | + assert ( |
| 171 | + run.outputs is not None |
| 172 | + ), "the current run has no outputs. The chain should output 'answer' and 'contexts' keys." |
| 173 | + output_keys = get_required_columns( |
| 174 | + self.metric.evaluation_mode, ["question", "ground_truth"] |
| 175 | + ) |
| 176 | + missing_keys = self._keys_are_present(output_keys, run.outputs) |
| 177 | + if missing_keys: |
| 178 | + raise ValueError( |
| 179 | + "Expected 'answer' and 'contexts' in run.outputs." |
| 180 | + f"Got: {[k for k in run.outputs.keys()]}" |
| 181 | + ) |
| 182 | + |
| 183 | + def evaluate_run( |
| 184 | + self, run: Run, example: t.Optional[Example] = None |
| 185 | + ) -> EvaluationResult: |
| 186 | + """ |
| 187 | + Evaluate a langsmith run |
| 188 | + """ |
| 189 | + self._validate_langsmith_eval(run, example) |
| 190 | + |
| 191 | + # this is just to suppress the type checker error |
| 192 | + # actual check and error message is in the _validate_langsmith_eval |
| 193 | + assert run.outputs is not None |
| 194 | + assert example is not None |
| 195 | + assert example.inputs is not None |
| 196 | + assert example.outputs is not None |
| 197 | + |
| 198 | + chain_eval = run.outputs |
| 199 | + chain_eval["question"] = example.inputs["question"] |
| 200 | + if self.metric.evaluation_mode in [ |
| 201 | + EvaluationMode.gc, |
| 202 | + EvaluationMode.ga, |
| 203 | + EvaluationMode.qcg, |
| 204 | + EvaluationMode.qga, |
| 205 | + ]: |
| 206 | + if example.outputs is None or "ground_truth" not in example.outputs: |
| 207 | + raise ValueError("expected `ground_truth` in example outputs.") |
| 208 | + chain_eval["ground_truth"] = example.outputs["ground_truth"] |
| 209 | + eval_output = self(chain_eval, include_run_info=True) |
| 210 | + |
| 211 | + evaluation_result = EvaluationResult( |
| 212 | + key=self.metric.name, score=eval_output[self.metric.name] |
| 213 | + ) |
| 214 | + if RUN_KEY in eval_output: |
| 215 | + evaluation_result.evaluator_info[RUN_KEY] = eval_output[RUN_KEY] |
| 216 | + return evaluation_result |
0 commit comments