|
2 | 2 |
|
3 | 3 | import logging
|
4 | 4 |
|
| 5 | +import ragas |
| 6 | + |
5 | 7 | from ...llm.client import ChatMessage, LLMClient, get_default_client
|
6 | 8 | from ...llm.embeddings import BaseEmbedding, get_default_embedding
|
7 | 9 | from ..base import AgentAnswer
|
@@ -104,20 +106,38 @@ def __call__(self, question_sample: dict, answer: AgentAnswer) -> dict:
|
104 | 106 |
|
105 | 107 | self.metric.init(run_config)
|
106 | 108 | if self.requires_context and answer.documents is None:
|
107 |
| - logger.warn( |
| 109 | + logger.warning( |
108 | 110 | f"No retrieved documents are passed to the evaluation function, computation of {self.name} cannot be done without it."
|
109 | 111 | "Make sure you pass 'retrieved_documents' to the evaluate function or that the 'answer_fn' return documents alongside the answer."
|
110 | 112 | )
|
111 | 113 | return {self.name: 0}
|
112 | 114 |
|
113 |
| - ragas_sample = { |
114 |
| - "question": question_sample["question"], |
115 |
| - "answer": answer.message, |
116 |
| - "contexts": answer.documents, |
117 |
| - "ground_truth": question_sample["reference_answer"], |
118 |
| - } |
| 115 | + ragas_sample = self.prepare_ragas_sample(question_sample, answer) |
| 116 | + |
119 | 117 | return {self.name: self.metric.score(ragas_sample)}
|
120 | 118 |
|
| 119 | + @staticmethod |
| 120 | + def prepare_ragas_sample(question_sample: dict, answer: AgentAnswer) -> dict: |
| 121 | + if ragas.__version__.startswith("0.1"): |
| 122 | + logger.warning( |
| 123 | + f"{DeprecationWarning.__name__}: You are using an older version (v0.1) of `ragas` package. " |
| 124 | + "Support for v0.1 is deprecated and may be removed in future versions. " |
| 125 | + "Please consider updating `ragas` to a later version." |
| 126 | + ) |
| 127 | + return { |
| 128 | + "question": question_sample["question"], |
| 129 | + "answer": answer.message, |
| 130 | + "contexts": answer.documents, |
| 131 | + "ground_truth": question_sample["reference_answer"], |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + "user_input": question_sample["question"], |
| 136 | + "response": answer.message, |
| 137 | + "retrieved_contexts": answer.documents, |
| 138 | + "reference": question_sample["reference_answer"], |
| 139 | + } |
| 140 | + |
121 | 141 |
|
122 | 142 | ragas_context_precision = RagasMetric(name="RAGAS Context Precision", metric=context_precision, requires_context=True)
|
123 | 143 | ragas_faithfulness = RagasMetric(name="RAGAS Faithfulness", metric=faithfulness, requires_context=True)
|
|
0 commit comments