Spaces:
Running
Running
Deploy f2988f91
Browse files
.opencodereview/vectors/chroma.sqlite3
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 9547776
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fef491e07d9211673b688a69eae3aea60245ee35cd2ec978fb6096c7b7eed193
|
| 3 |
size 9547776
|
eval_data/ragas_eval.py
CHANGED
|
@@ -154,26 +154,30 @@ def compute_ragas_retrieval_scores(
|
|
| 154 |
llm = _get_evaluator_llm()
|
| 155 |
scores: dict[str, float] = {}
|
| 156 |
|
| 157 |
-
# ββ Context Precision βββββββββββββββββββββββββββ
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
| 172 |
|
| 173 |
-
# ββ Context Recall βββββββββββββββββββββββ
|
| 174 |
if ground_truth:
|
| 175 |
try:
|
| 176 |
-
from ragas.metrics
|
| 177 |
|
| 178 |
recall_scorer = ContextRecall(llm=llm)
|
| 179 |
recall_sample = SingleTurnSample(
|
|
@@ -181,8 +185,8 @@ def compute_ragas_retrieval_scores(
|
|
| 181 |
retrieved_contexts=retrieved_contexts,
|
| 182 |
reference=ground_truth,
|
| 183 |
)
|
| 184 |
-
recall_result = recall_scorer.
|
| 185 |
-
scores["context_recall"] = round(float(recall_result
|
| 186 |
logger.info("RAGAS context_recall: %.4f", scores["context_recall"])
|
| 187 |
except Exception as exc:
|
| 188 |
logger.warning("RAGAS context_recall failed: %s", exc)
|
|
@@ -199,8 +203,8 @@ def compute_ragas_retrieval_scores(
|
|
| 199 |
response=answer,
|
| 200 |
retrieved_contexts=retrieved_contexts,
|
| 201 |
)
|
| 202 |
-
faithful_result = faithfulness_scorer.
|
| 203 |
-
scores["faithfulness"] = round(float(faithful_result
|
| 204 |
logger.info("RAGAS faithfulness: %.4f", scores["faithfulness"])
|
| 205 |
except Exception as exc:
|
| 206 |
logger.warning("RAGAS faithfulness failed: %s", exc)
|
|
@@ -211,13 +215,27 @@ def compute_ragas_retrieval_scores(
|
|
| 211 |
try:
|
| 212 |
from ragas.metrics import ResponseRelevancy
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
relevancy_sample = SingleTurnSample(
|
| 216 |
user_input=query,
|
| 217 |
response=answer,
|
| 218 |
)
|
| 219 |
-
relevancy_result = relevancy_scorer.
|
| 220 |
-
scores["answer_relevancy"] = round(float(relevancy_result
|
| 221 |
logger.info("RAGAS answer_relevancy: %.4f", scores["answer_relevancy"])
|
| 222 |
except Exception as exc:
|
| 223 |
logger.warning("RAGAS answer_relevancy failed: %s", exc)
|
|
@@ -232,9 +250,62 @@ def compute_ragas_retrieval_scores(
|
|
| 232 |
logger.warning("RAGAS mmr failed: %s", exc)
|
| 233 |
scores["mmr"] = 0.0
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
return scores
|
| 236 |
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
def _compute_mmr(
|
| 239 |
query: str,
|
| 240 |
retrieved_contexts: list[str],
|
|
|
|
| 154 |
llm = _get_evaluator_llm()
|
| 155 |
scores: dict[str, float] = {}
|
| 156 |
|
| 157 |
+
# ββ Context Precision (reference-free) βββββββββββββββββββββββββββ
|
| 158 |
+
# Uses LLMContextPrecisionWithoutReference which needs user_input +
|
| 159 |
+
# response + retrieved_contexts (no ground-truth reference required).
|
| 160 |
+
if answer:
|
| 161 |
+
try:
|
| 162 |
+
from ragas.metrics import LLMContextPrecisionWithoutReference
|
| 163 |
|
| 164 |
+
scorer = LLMContextPrecisionWithoutReference(llm=llm)
|
| 165 |
+
sample = SingleTurnSample(
|
| 166 |
+
user_input=query,
|
| 167 |
+
response=answer,
|
| 168 |
+
retrieved_contexts=retrieved_contexts,
|
| 169 |
+
)
|
| 170 |
+
result = scorer.single_turn_score(sample)
|
| 171 |
+
scores["context_precision"] = round(float(result), 4)
|
| 172 |
+
logger.info("RAGAS context_precision: %.4f", scores["context_precision"])
|
| 173 |
+
except Exception as exc:
|
| 174 |
+
logger.warning("RAGAS context_precision failed: %s", exc)
|
| 175 |
+
scores["context_precision"] = 0.0
|
| 176 |
|
| 177 |
+
# ββ Context Recall (requires ground_truth) βββββββββββββββββββββββ
|
| 178 |
if ground_truth:
|
| 179 |
try:
|
| 180 |
+
from ragas.metrics import ContextRecall
|
| 181 |
|
| 182 |
recall_scorer = ContextRecall(llm=llm)
|
| 183 |
recall_sample = SingleTurnSample(
|
|
|
|
| 185 |
retrieved_contexts=retrieved_contexts,
|
| 186 |
reference=ground_truth,
|
| 187 |
)
|
| 188 |
+
recall_result = recall_scorer.single_turn_score(recall_sample)
|
| 189 |
+
scores["context_recall"] = round(float(recall_result), 4)
|
| 190 |
logger.info("RAGAS context_recall: %.4f", scores["context_recall"])
|
| 191 |
except Exception as exc:
|
| 192 |
logger.warning("RAGAS context_recall failed: %s", exc)
|
|
|
|
| 203 |
response=answer,
|
| 204 |
retrieved_contexts=retrieved_contexts,
|
| 205 |
)
|
| 206 |
+
faithful_result = faithfulness_scorer.single_turn_score(faithful_sample)
|
| 207 |
+
scores["faithfulness"] = round(float(faithful_result), 4)
|
| 208 |
logger.info("RAGAS faithfulness: %.4f", scores["faithfulness"])
|
| 209 |
except Exception as exc:
|
| 210 |
logger.warning("RAGAS faithfulness failed: %s", exc)
|
|
|
|
| 215 |
try:
|
| 216 |
from ragas.metrics import ResponseRelevancy
|
| 217 |
|
| 218 |
+
# ResponseRelevancy also needs an embedding model for cosine
|
| 219 |
+
# similarity. We create a lightweight one from the project's
|
| 220 |
+
# ChromaDB embedding function (ONNX MiniLM, already cached).
|
| 221 |
+
embeddings = _create_ragas_embeddings()
|
| 222 |
+
if embeddings is None:
|
| 223 |
+
raise RuntimeError(
|
| 224 |
+
"No embedding model available for ResponseRelevancy β "
|
| 225 |
+
"install chromadb"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
relevancy_scorer = ResponseRelevancy(
|
| 229 |
+
llm=llm,
|
| 230 |
+
embeddings=embeddings,
|
| 231 |
+
strictness=1, # Groq only supports n=1
|
| 232 |
+
)
|
| 233 |
relevancy_sample = SingleTurnSample(
|
| 234 |
user_input=query,
|
| 235 |
response=answer,
|
| 236 |
)
|
| 237 |
+
relevancy_result = relevancy_scorer.single_turn_score(relevancy_sample)
|
| 238 |
+
scores["answer_relevancy"] = round(float(relevancy_result), 4)
|
| 239 |
logger.info("RAGAS answer_relevancy: %.4f", scores["answer_relevancy"])
|
| 240 |
except Exception as exc:
|
| 241 |
logger.warning("RAGAS answer_relevancy failed: %s", exc)
|
|
|
|
| 250 |
logger.warning("RAGAS mmr failed: %s", exc)
|
| 251 |
scores["mmr"] = 0.0
|
| 252 |
|
| 253 |
+
# ββ Context Precision (reference-based, fallback when no answer) β
|
| 254 |
+
if "context_precision" not in scores and ground_truth:
|
| 255 |
+
try:
|
| 256 |
+
from ragas.metrics import ContextPrecision
|
| 257 |
+
|
| 258 |
+
scorer = ContextPrecision(llm=llm)
|
| 259 |
+
sample = SingleTurnSample(
|
| 260 |
+
user_input=query,
|
| 261 |
+
retrieved_contexts=retrieved_contexts,
|
| 262 |
+
reference=ground_truth,
|
| 263 |
+
)
|
| 264 |
+
result = scorer.single_turn_score(sample)
|
| 265 |
+
scores["context_precision"] = round(float(result), 4)
|
| 266 |
+
logger.info("RAGAS context_precision: %.4f", scores["context_precision"])
|
| 267 |
+
except Exception as exc:
|
| 268 |
+
logger.warning("RAGAS context_precision failed: %s", exc)
|
| 269 |
+
scores["context_precision"] = 0.0
|
| 270 |
+
|
| 271 |
return scores
|
| 272 |
|
| 273 |
|
| 274 |
+
def _create_ragas_embeddings():
|
| 275 |
+
"""Return a lightweight embedding object compatible with RAGAS metrics.
|
| 276 |
+
|
| 277 |
+
Wraps the ChromaDB ONNX MiniLM embedding function so that metrics like
|
| 278 |
+
``ResponseRelevancy`` can compute cosine similarity between embeddings.
|
| 279 |
+
The underlying ONNX model is cached globally and loads in <1 s.
|
| 280 |
+
"""
|
| 281 |
+
try:
|
| 282 |
+
from chromadb.utils import embedding_functions
|
| 283 |
+
|
| 284 |
+
if hasattr(embedding_functions, "ONNXMiniLM_L6_V2"):
|
| 285 |
+
ef = embedding_functions.ONNXMiniLM_L6_V2()
|
| 286 |
+
else:
|
| 287 |
+
ef = embedding_functions.SentenceTransformerEmbeddingFunction(
|
| 288 |
+
model_name="all-MiniLM-L6-v2"
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
class _RagasEmbeddings:
|
| 292 |
+
def __init__(self, ef):
|
| 293 |
+
self._ef = ef
|
| 294 |
+
|
| 295 |
+
def embed_query(self, text: str) -> list[float]:
|
| 296 |
+
result = self._ef([text])[0]
|
| 297 |
+
return list(result) if not isinstance(result, list) else result
|
| 298 |
+
|
| 299 |
+
def embed_documents(self, texts: list[str]) -> list[list[float]]:
|
| 300 |
+
results = self._ef(texts)
|
| 301 |
+
return [list(r) if not isinstance(r, list) else r for r in results]
|
| 302 |
+
|
| 303 |
+
return _RagasEmbeddings(ef)
|
| 304 |
+
except Exception as exc:
|
| 305 |
+
logger.warning("Cannot create RAGAS embeddings: %s", exc)
|
| 306 |
+
return None
|
| 307 |
+
|
| 308 |
+
|
| 309 |
def _compute_mmr(
|
| 310 |
query: str,
|
| 311 |
retrieved_contexts: list[str],
|