Spaces:
Sleeping

File size: 4,556 Bytes
3e964dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
RAGAS evaluation runner for Finora RAG pipeline.

Uses Groq llama-3.1-8b-instant (GROQ_MODEL_FAST) as the evaluation LLM
and all-MiniLM-L6-v2 (local HF) for embeddings — zero OpenAI dependency.

Targets:
    Faithfulness       > 0.85
    Answer Relevance   > 0.80
    Context Recall     > 0.75
    Context Precision  > 0.70

Usage:
    from backend.rag.evaluation.ragas_eval import run_ragas_eval
    results = run_ragas_eval(qa_pairs, tickers=["AAPL", "MSFT"])
"""
from __future__ import annotations

import os
import warnings
from typing import Any

import structlog

log = structlog.get_logger()

TARGETS = {
    "faithfulness":      0.85,
    "answer_relevancy":  0.80,
    "context_recall":    0.75,
    "context_precision": 0.70,
}


def _build_llm():
    """Groq llama-3.1-8b-instant wrapped for RAGAS.

    Groq rejects n > 1. RAGAS passes n=3 via LangchainLLMWrapper.generate(n=3).
    Subclass the wrapper itself to hard-cap n=1 at the exact call site.
    """
    from langchain_groq import ChatGroq
    from ragas.llms import LangchainLLMWrapper

    groq = ChatGroq(
        model=os.getenv("GROQ_MODEL_FAST", "llama-3.1-8b-instant"),
        api_key=os.getenv("GROQ_API_KEY"),
        temperature=0.0,
        max_tokens=512,
        n=1,
    )

    return LangchainLLMWrapper(groq, bypass_n=True)


def _build_embeddings():
    """Local all-MiniLM-L6-v2 wrapped for RAGAS — no API key needed."""
    from langchain_community.embeddings import HuggingFaceEmbeddings
    from ragas.embeddings import LangchainEmbeddingsWrapper

    return LangchainEmbeddingsWrapper(HuggingFaceEmbeddings(
        model_name=os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2"),
        cache_folder=os.getenv("HF_CACHE_DIR", "data/hf_cache"),
    ))


def run_ragas_eval(
    qa_pairs: list[dict],
    tickers: list[str] | None = None,
) -> dict[str, Any]:
    """
    Run RAGAS evaluation over QA pairs.

    qa_pairs schema:
        [{"question": str, "answer": str, "contexts": list[str], "ground_truth": str}]

    Returns metrics dict with scores + pass/fail per metric.
    """
    try:
        from datasets import Dataset
        from ragas import evaluate
        from ragas.metrics import (
            answer_relevancy,
            context_precision,
            context_recall,
            faithfulness,
        )
    except ImportError as exc:
        log.error("ragas_import_failed", error=str(exc))
        return {"error": "ragas / datasets not installed"}

    if not qa_pairs:
        return {"error": "no QA pairs provided"}

    try:
        llm = _build_llm()
        embeddings = _build_embeddings()
    except Exception as exc:
        log.error("ragas_llm_build_failed", error=str(exc))
        return {"error": f"LLM/embeddings init failed: {exc}"}

    dataset = Dataset.from_list(qa_pairs)

    # Cap answer_relevancy to n=1 — Groq free tier can't handle n=3 sequential calls
    answer_relevancy.n = 1  # type: ignore[attr-defined]

    metrics = [faithfulness, answer_relevancy, context_recall, context_precision]

    log.info("ragas_eval_start", pairs=len(qa_pairs), tickers=tickers)

    try:
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", DeprecationWarning)
            result = evaluate(
                dataset,
                metrics=metrics,
                llm=llm,
                embeddings=embeddings,
                raise_exceptions=False,
                show_progress=True,
            )
        # EvaluationResult is DataFrame-like — extract per-row scores then average
        result_df = result.to_pandas()
        metric_cols = [c for c in result_df.columns if c in TARGETS]
        scores: dict[str, float] = result_df[metric_cols].mean().dropna().to_dict()
    except Exception as exc:
        log.error("ragas_eval_failed", error=str(exc))
        return {"error": str(exc)}

    report: dict[str, Any] = {"scores": scores, "results": {}}
    for metric, target in TARGETS.items():
        score = scores.get(metric)
        if score is None:
            continue
        report["results"][metric] = {
            "score": round(score, 4),
            "target": target,
            "passed": score >= target,
            "delta": round(score - target, 4),
        }

    all_pass = all(r["passed"] for r in report["results"].values())
    report["overall_pass"] = all_pass
    report["tickers"] = tickers or []
    report["n_pairs"] = len(qa_pairs)

    log.info("ragas_eval_complete", pass_all=all_pass, scores=scores)
    return report