|
| 1 | +import asyncio |
| 2 | +from abc import ABC, abstractmethod |
| 3 | +from itertools import chain |
| 4 | +from typing import Generic, TypeVar |
| 5 | + |
| 6 | +import litellm |
| 7 | +from continuous_eval.llm_factory import LLMInterface |
| 8 | +from continuous_eval.metrics.base import LLMBasedMetric |
| 9 | +from continuous_eval.metrics.generation.text import ( |
| 10 | + LLMBasedAnswerCorrectness, |
| 11 | + LLMBasedAnswerRelevance, |
| 12 | + LLMBasedFaithfulness, |
| 13 | + LLMBasedStyleConsistency, |
| 14 | +) |
| 15 | +from typing_extensions import Self |
| 16 | + |
| 17 | +from ragbits.agents.types import QuestionAnswerPromptOutputT |
| 18 | +from ragbits.core.llms.base import LLM |
| 19 | +from ragbits.core.llms.litellm import LiteLLM |
| 20 | +from ragbits.core.utils.helpers import batched |
| 21 | +from ragbits.evaluate.metrics.base import Metric |
| 22 | +from ragbits.evaluate.pipelines.question_answer import QuestionAnswerResult |
| 23 | + |
| 24 | +MetricT = TypeVar("MetricT", bound=LLMBasedMetric) |
| 25 | + |
| 26 | + |
| 27 | +class _MetricLMM(LLMInterface): |
| 28 | + """ |
| 29 | + Implementation of required interface of Relari generative metrics based on LiteLMM. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, model_name: str, api_base: str | None = None, api_version: str | None = None, api_key: str | None = None |
| 34 | + ) -> None: |
| 35 | + self._model_name = model_name |
| 36 | + self._api_base = api_base |
| 37 | + self._api_version = api_version |
| 38 | + self._api_key = api_key |
| 39 | + |
| 40 | + def run(self, prompt: dict[str, str], temperature: float = 0, max_tokens: int = 1024) -> str: |
| 41 | + """ |
| 42 | + Run the prompt. |
| 43 | +
|
| 44 | + Args: |
| 45 | + prompt: Dict with system_prompt and user_prompt entries. |
| 46 | + temperature: Temperature to use. |
| 47 | + max_tokens: Max tokens to use. |
| 48 | + """ |
| 49 | + response = litellm.completion( |
| 50 | + model=self._model_name, |
| 51 | + messages=[ |
| 52 | + {"role": "system", "content": prompt["system_prompt"]}, |
| 53 | + {"role": "user", "content": prompt["user_prompt"]}, |
| 54 | + ], |
| 55 | + base_url=self._api_base, |
| 56 | + api_version=self._api_version, |
| 57 | + api_key=self._api_key, |
| 58 | + ) |
| 59 | + return response.choices[0].message.content |
| 60 | + |
| 61 | + |
| 62 | +class QuestionAnswerMetric(Generic[MetricT], Metric[QuestionAnswerResult], ABC): |
| 63 | + """ |
| 64 | + Metric for question answer evaluation based on Relari backend. |
| 65 | + More details can be found [here](https://docs.relari.ai/category/text-generation). |
| 66 | + """ |
| 67 | + |
| 68 | + metric_cls: type[MetricT] |
| 69 | + |
| 70 | + def __init__(self, llm: LiteLLM, batch_size: int = 15, weight: float = 1.0) -> None: |
| 71 | + """ |
| 72 | + Initialize the agent metric. |
| 73 | +
|
| 74 | + Args: |
| 75 | + llm: Judge LLM instance. |
| 76 | + batch_size: Batch size for metric computation. |
| 77 | + weight: Metric value weight in the final score, used during optimization. |
| 78 | + """ |
| 79 | + super().__init__(weight=weight) |
| 80 | + self.metric = self.metric_cls( |
| 81 | + _MetricLMM( |
| 82 | + model_name=llm.model_name, |
| 83 | + api_base=llm.api_base, |
| 84 | + api_version=llm.api_version, |
| 85 | + api_key=llm.api_key, |
| 86 | + ) |
| 87 | + ) |
| 88 | + self.batch_size = batch_size |
| 89 | + |
| 90 | + @classmethod |
| 91 | + def from_config(cls, config: dict) -> Self: |
| 92 | + """ |
| 93 | + Create an instance of `QuestionAnswerMetric` from a configuration dictionary. |
| 94 | +
|
| 95 | + Args: |
| 96 | + config: A dictionary containing configuration settings for the metric. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + An instance of the metric class initialized with the provided configuration. |
| 100 | + """ |
| 101 | + config["llm"] = LLM.from_config(config["llm"]) |
| 102 | + config["batch_size"] = config.get("batch_size", 15) |
| 103 | + config["weight"] = config.get("weight", 1.0) |
| 104 | + return super().from_config(config) |
| 105 | + |
| 106 | + async def compute(self, results: list[QuestionAnswerResult[QuestionAnswerPromptOutputT]]) -> dict: |
| 107 | + """ |
| 108 | + Compute the metric. |
| 109 | +
|
| 110 | + Args: |
| 111 | + results: The evaluation results. |
| 112 | +
|
| 113 | + Returns: |
| 114 | + The computed metric. |
| 115 | + """ |
| 116 | + metric_results = chain.from_iterable( |
| 117 | + [ |
| 118 | + await asyncio.gather(*[asyncio.to_thread(self._call_metric, result) for result in batch]) |
| 119 | + for batch in batched(results, self.batch_size) |
| 120 | + ] |
| 121 | + ) |
| 122 | + return self.metric.aggregate(list(metric_results)) |
| 123 | + |
| 124 | + @abstractmethod |
| 125 | + def _call_metric(self, result: QuestionAnswerResult[QuestionAnswerPromptOutputT]) -> dict: |
| 126 | + """ |
| 127 | + Call the metric with the proper arguments. |
| 128 | + """ |
| 129 | + |
| 130 | + |
| 131 | +class QuestionAnswerAnswerCorrectness(QuestionAnswerMetric[LLMBasedAnswerCorrectness]): |
| 132 | + """ |
| 133 | + Metric checking answer correctness based on LLM. |
| 134 | + More details can be found [here](https://docs.relari.ai/metrics/Generation/LLM-Based/llm_correctness). |
| 135 | + """ |
| 136 | + |
| 137 | + metric_cls: type[LLMBasedAnswerCorrectness] = LLMBasedAnswerCorrectness |
| 138 | + |
| 139 | + def _call_metric(self, result: QuestionAnswerResult[QuestionAnswerPromptOutputT]) -> dict: |
| 140 | + return self.metric( |
| 141 | + question=result.question, |
| 142 | + answer=( |
| 143 | + result.predicted_result.content |
| 144 | + if isinstance(result.predicted_result.content, str) |
| 145 | + else result.predicted_result.content.answer |
| 146 | + ), |
| 147 | + ground_truth_answers=result.reference_answer, |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +class QuestionAnswerAnswerFaithfulness(QuestionAnswerMetric[LLMBasedFaithfulness]): |
| 152 | + """ |
| 153 | + Metric checking answer faithfulness based on LLM. |
| 154 | + More details can be found [here](https://docs.relari.ai/metrics/Generation/LLM-Based/llm_faithfulness). |
| 155 | + """ |
| 156 | + |
| 157 | + metric_cls: type[LLMBasedFaithfulness] = LLMBasedFaithfulness |
| 158 | + |
| 159 | + def _call_metric(self, result: QuestionAnswerResult[QuestionAnswerPromptOutputT]) -> dict: |
| 160 | + return self.metric( |
| 161 | + question=result.question, |
| 162 | + answer=( |
| 163 | + result.predicted_result.content |
| 164 | + if isinstance(result.predicted_result.content, str) |
| 165 | + else result.predicted_result.content.answer |
| 166 | + ), |
| 167 | + retrieved_context=result.reference_context, |
| 168 | + ) |
| 169 | + |
| 170 | + |
| 171 | +class QuestionAnswerAnswerRelevance(QuestionAnswerMetric[LLMBasedAnswerRelevance]): |
| 172 | + """ |
| 173 | + Metric checking answer relevance based on LLM. |
| 174 | + More details can be found [here](https://docs.relari.ai/metrics/Generation/LLM-Based/llm_relevance). |
| 175 | + """ |
| 176 | + |
| 177 | + metric_cls: type[LLMBasedAnswerRelevance] = LLMBasedAnswerRelevance |
| 178 | + |
| 179 | + def _call_metric(self, result: QuestionAnswerResult[QuestionAnswerPromptOutputT]) -> dict: |
| 180 | + return self.metric( |
| 181 | + question=result.question, |
| 182 | + answer=( |
| 183 | + result.predicted_result.content |
| 184 | + if isinstance(result.predicted_result.content, str) |
| 185 | + else result.predicted_result.content.answer |
| 186 | + ), |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +class QuestionAnswerAnswerConsistency(QuestionAnswerMetric[LLMBasedStyleConsistency]): |
| 191 | + """ |
| 192 | + Metric checking answer relevance based on LLM. |
| 193 | + More details can be found [here](https://docs.relari.ai/metrics/Generation/LLM-Based/llm_style). |
| 194 | + """ |
| 195 | + |
| 196 | + metric_cls: type[LLMBasedStyleConsistency] = LLMBasedStyleConsistency |
| 197 | + |
| 198 | + def _call_metric(self, result: QuestionAnswerResult[QuestionAnswerPromptOutputT]) -> dict: |
| 199 | + return self.metric( |
| 200 | + answer=( |
| 201 | + result.predicted_result.content |
| 202 | + if isinstance(result.predicted_result.content, str) |
| 203 | + else result.predicted_result.content.answer |
| 204 | + ), |
| 205 | + ground_truth_answers=result.reference_answer, |
| 206 | + ) |
0 commit comments