Spaces:
Sleeping
Sleeping
feat: implement hybrid RAG reasoning engine with source attribution, faithfulness scoring, and evaluation framework
Browse files- backend/core/config.py +1 -0
- backend/evaluation/__init__.py +1 -0
- backend/evaluation/ablation_chunk_size.py +37 -0
- backend/evaluation/backbone_comparison.py +32 -0
- backend/evaluation/baseline_runner.py +50 -0
- backend/evaluation/benchmark_runner.py +35 -0
- backend/evaluation/guardrail_analysis.py +35 -0
- backend/evaluation/metrics.py +25 -0
- backend/evaluation/statistical_tests.py +62 -0
- backend/main.py +8 -4
- backend/modules/reasoning_engine.py +63 -1
- backend/requirements.txt +2 -0
- backend/utils/faithfulness.py +76 -0
- backend/utils/groq_client.py +29 -8
- test_data/medqa_sample.json +10 -0
backend/core/config.py
CHANGED
|
@@ -15,6 +15,7 @@ class Config:
|
|
| 15 |
|
| 16 |
# AI Services
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
|
|
| 18 |
|
| 19 |
# Storage
|
| 20 |
STORAGE_PATH = os.getenv("STORAGE_PATH", "./data/storage")
|
|
|
|
| 15 |
|
| 16 |
# AI Services
|
| 17 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 18 |
+
LLM_BACKBONE = os.getenv("LLM_BACKBONE", "llama3") # Options: llama3, mixtral, gemma
|
| 19 |
|
| 20 |
# Storage
|
| 21 |
STORAGE_PATH = os.getenv("STORAGE_PATH", "./data/storage")
|
backend/evaluation/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"""Evaluation module for reviewer baseline experiments."""
|
backend/evaluation/ablation_chunk_size.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ablation study on chunk size effect on faithfulness and retrieval quality.
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
+
|
| 8 |
+
from modules.knowledge_compiler import create_knowledge_compiler
|
| 9 |
+
from modules.reasoning_engine import create_reasoning_engine
|
| 10 |
+
|
| 11 |
+
def run_chunk_ablation(agent_name: str, parsed_data: list, system_prompt: str, prompt_analysis: dict, test_queries: list):
|
| 12 |
+
sizes = [64, 128, 256, 512, 1024]
|
| 13 |
+
|
| 14 |
+
for size in sizes:
|
| 15 |
+
print(f"\n=====================")
|
| 16 |
+
print(f"Testing Chunk Size: {size}")
|
| 17 |
+
print(f"=====================")
|
| 18 |
+
|
| 19 |
+
compiler = create_knowledge_compiler()
|
| 20 |
+
original_chunk_text = compiler._chunk_text
|
| 21 |
+
compiler._chunk_text = lambda text, chunk_size=size, overlap=size//10: original_chunk_text(text, chunk_size, overlap)
|
| 22 |
+
|
| 23 |
+
# Recompile
|
| 24 |
+
try:
|
| 25 |
+
compiler.compile(agent_name, parsed_data, system_prompt, prompt_analysis)
|
| 26 |
+
|
| 27 |
+
# Test
|
| 28 |
+
engine = create_reasoning_engine()
|
| 29 |
+
for q in test_queries:
|
| 30 |
+
res = engine.reason(agent_name, q)
|
| 31 |
+
print(f"Q: {q}")
|
| 32 |
+
print(f"Faithfulness: {res['explainability']['faithfulness']}")
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(f"Failed ablation step for size {size}: {e}")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
print("Chunk size ablation script ready. Needs actual parsed data to recompile.")
|
backend/evaluation/backbone_comparison.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Compares different LLM backbones (Llama 3, Mixtral, Gemma).
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
+
|
| 8 |
+
from core.config import settings
|
| 9 |
+
from modules.reasoning_engine import create_reasoning_engine
|
| 10 |
+
|
| 11 |
+
def run_comparison(agent_name: str, queries: list):
|
| 12 |
+
backbones = ["llama3", "mixtral", "gemma"]
|
| 13 |
+
|
| 14 |
+
for bb in backbones:
|
| 15 |
+
settings.LLM_BACKBONE = bb
|
| 16 |
+
print(f"\n--- Testing Backbone: {bb} ---")
|
| 17 |
+
try:
|
| 18 |
+
# Must recreate engine so GroqClient picks up config
|
| 19 |
+
engine = create_reasoning_engine()
|
| 20 |
+
|
| 21 |
+
for q in queries:
|
| 22 |
+
res = engine.reason(agent_name, q)
|
| 23 |
+
print(f"Q: {q}")
|
| 24 |
+
print(f"A ({bb}): {res['answer'][:100]}...")
|
| 25 |
+
print(f"Faithfulness: {res['explainability']['faithfulness']}")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Failed to run with backbone {bb}: {e}")
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
test_queries = ["What are the symptoms of a common cold?"]
|
| 31 |
+
# Replace 'medical_agent' with an actual compiled agent name
|
| 32 |
+
run_comparison("medical_agent", test_queries)
|
backend/evaluation/baseline_runner.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Runs CRAG and RAPTOR baselines against a set of test queries.
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
+
|
| 8 |
+
from modules.reasoning_engine import create_reasoning_engine
|
| 9 |
+
from evaluation.metrics import MetricsRunner
|
| 10 |
+
|
| 11 |
+
def run_baselines(agent_name: str, queries: list):
|
| 12 |
+
engine = create_reasoning_engine()
|
| 13 |
+
metrics = MetricsRunner()
|
| 14 |
+
|
| 15 |
+
results = {"CRAG": [], "RAPTOR": [], "MEXAR": []}
|
| 16 |
+
|
| 17 |
+
for q in queries:
|
| 18 |
+
print(f"\nProcessing query: {q}")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# Original MEXAR
|
| 22 |
+
res_mexar = engine.reason(agent_name, q)
|
| 23 |
+
results["MEXAR"].append(float(res_mexar["explainability"]["faithfulness"].strip('%'))/100)
|
| 24 |
+
|
| 25 |
+
# CRAG
|
| 26 |
+
res_crag = engine.reason_crag_baseline(agent_name, q)
|
| 27 |
+
results["CRAG"].append(res_crag["confidence"]) # The raw score
|
| 28 |
+
|
| 29 |
+
# RAPTOR
|
| 30 |
+
res_raptor = engine.reason_raptor_baseline(agent_name, q)
|
| 31 |
+
results["RAPTOR"].append(res_raptor["confidence"])
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error evaluating query '{q}': {e}")
|
| 34 |
+
|
| 35 |
+
print("\n--- Baseline Comparison (Faithfulness) ---")
|
| 36 |
+
for b_name in results:
|
| 37 |
+
if results[b_name]:
|
| 38 |
+
avg = sum(results[b_name]) / len(results[b_name])
|
| 39 |
+
print(f"{b_name}: {avg:.4f}")
|
| 40 |
+
else:
|
| 41 |
+
print(f"{b_name}: No results")
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
# Example usage
|
| 45 |
+
test_queries = [
|
| 46 |
+
"What are the symptoms of a common cold?",
|
| 47 |
+
"How do I bake a chocolate cake?"
|
| 48 |
+
]
|
| 49 |
+
# Replace 'medical_agent' with an actual compiled agent name in DB
|
| 50 |
+
run_baselines("medical_agent", test_queries)
|
backend/evaluation/benchmark_runner.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Runs evaluation on public benchmarks like MedQA, LegalBench.
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
from modules.reasoning_engine import create_reasoning_engine
|
| 10 |
+
|
| 11 |
+
def run_benchmark(dataset_path: str, agent_name: str):
|
| 12 |
+
engine = create_reasoning_engine()
|
| 13 |
+
|
| 14 |
+
if not os.path.exists(dataset_path):
|
| 15 |
+
print(f"Dataset not found: {dataset_path}")
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
with open(dataset_path, "r") as f:
|
| 19 |
+
data = json.load(f)
|
| 20 |
+
|
| 21 |
+
for item in data[:10]: # Run first 10 for demo
|
| 22 |
+
query = item.get("question") or item.get("query")
|
| 23 |
+
if not query:
|
| 24 |
+
continue
|
| 25 |
+
|
| 26 |
+
print(f"\nQuery: {query}")
|
| 27 |
+
try:
|
| 28 |
+
result = engine.reason(agent_name, query)
|
| 29 |
+
print(f"Answer: {result['answer'][:100]}...")
|
| 30 |
+
print(f"Faithfulness: {result['explainability']['faithfulness']}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Failed to process query: {e}")
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
run_benchmark(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "test_data", "medqa_sample.json"), "medical_agent")
|
backend/evaluation/guardrail_analysis.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Evaluates the domain guardrail's false-accept (false positive) rate.
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
+
|
| 8 |
+
from modules.reasoning_engine import create_reasoning_engine
|
| 9 |
+
|
| 10 |
+
def test_guardrails(agent_name: str):
|
| 11 |
+
engine = create_reasoning_engine()
|
| 12 |
+
|
| 13 |
+
boundary_queries = [
|
| 14 |
+
"What are the economic impacts of pharmaceutical pricing?", # Often crosses medical/finance
|
| 15 |
+
"Can a doctor be sued for malpractice if they misdiagnose cancer?", # Medical/Legal
|
| 16 |
+
"Are taxes applied to medical equipment purchases?", # Medical/Finance
|
| 17 |
+
"How do I cook a healthy meal to lower blood pressure?" # Cooking/Medical
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
print(f"Testing Guardrail False-Accept Rate (Threshold = {engine.DOMAIN_SIMILARITY_THRESHOLD})")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
for q in boundary_queries:
|
| 24 |
+
res = engine.reason(agent_name, q)
|
| 25 |
+
print(f"\nQuery: {q}")
|
| 26 |
+
print(f"Accepted: {res['in_domain']}")
|
| 27 |
+
exp = res.get('explainability', {})
|
| 28 |
+
cb = exp.get('confidence_breakdown', {})
|
| 29 |
+
domain_str = cb.get('domain_relevance', 'N/A')
|
| 30 |
+
print(f"Domain Score: {domain_str}")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Failed guardrail test queries: {e}")
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
test_guardrails("medical_agent")
|
backend/evaluation/metrics.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
MEXAR - Evaluation Metrics Helper
|
| 3 |
+
Calculates common metrics across different baselines and experiments.
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
from utils.faithfulness import FaithfulnessScorer, BartNLIScorer, FActScoreCompat
|
| 10 |
+
|
| 11 |
+
class MetricsRunner:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.faith_scorer = FaithfulnessScorer()
|
| 14 |
+
self.bart_nli = BartNLIScorer()
|
| 15 |
+
self.factscore = FActScoreCompat()
|
| 16 |
+
|
| 17 |
+
def evaluate_all(self, answer: str, context: str):
|
| 18 |
+
faith_res = self.faith_scorer.score(answer, context)
|
| 19 |
+
bart_res = self.bart_nli.score(answer, context)
|
| 20 |
+
fact_res = self.factscore.score(answer, context)
|
| 21 |
+
return {
|
| 22 |
+
"faithfulness": faith_res.score,
|
| 23 |
+
"bart_nli": bart_res.score,
|
| 24 |
+
"factscore": fact_res.score
|
| 25 |
+
}
|
backend/evaluation/statistical_tests.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Calculates McNemar's test for significance between two models,
|
| 3 |
+
using the stated binarization threshold.
|
| 4 |
+
"""
|
| 5 |
+
import sys
|
| 6 |
+
import os
|
| 7 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 8 |
+
|
| 9 |
+
from modules.reasoning_engine import ReasoningEngine
|
| 10 |
+
|
| 11 |
+
THRESHOLD = ReasoningEngine.MCNEMAR_BINARIZATION_THRESHOLD
|
| 12 |
+
|
| 13 |
+
def mcnemars_test(scores_model_A: list, scores_model_B: list):
|
| 14 |
+
"""
|
| 15 |
+
Computes McNemar's test p-value for paired nominal data.
|
| 16 |
+
scores are lists of float faithfulness scores.
|
| 17 |
+
"""
|
| 18 |
+
if len(scores_model_A) != len(scores_model_B):
|
| 19 |
+
raise ValueError("Must have same number of scores")
|
| 20 |
+
|
| 21 |
+
# Binarize
|
| 22 |
+
bin_A = [1 if s >= THRESHOLD else 0 for s in scores_model_A]
|
| 23 |
+
bin_B = [1 if s >= THRESHOLD else 0 for s in scores_model_B]
|
| 24 |
+
|
| 25 |
+
# Contingency table
|
| 26 |
+
# B correct | B wrong
|
| 27 |
+
# A correct | a | b
|
| 28 |
+
# A wrong | c | d
|
| 29 |
+
|
| 30 |
+
a, b, c, d = 0, 0, 0, 0
|
| 31 |
+
for a_val, b_val in zip(bin_A, bin_B):
|
| 32 |
+
if a_val == 1 and b_val == 1: a += 1
|
| 33 |
+
elif a_val == 1 and b_val == 0: b += 1
|
| 34 |
+
elif a_val == 0 and b_val == 1: c += 1
|
| 35 |
+
else: d += 1
|
| 36 |
+
|
| 37 |
+
# Chi-square statistic: (b - c)^2 / (b + c)
|
| 38 |
+
if b + c == 0:
|
| 39 |
+
print("Models are identical given the threshold.")
|
| 40 |
+
return 1.0 # No difference
|
| 41 |
+
|
| 42 |
+
chi_square = ((abs(b - c) - 1)**2) / (b + c) # with continuity correction
|
| 43 |
+
|
| 44 |
+
print(f"McNemar's Test Results:")
|
| 45 |
+
print(f"Binarization Threshold: {THRESHOLD}")
|
| 46 |
+
print(f"Contingency Table: a={a}, b={b}, c={c}, d={d}")
|
| 47 |
+
print(f"Chi-square: {chi_square:.3f}")
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
from scipy.stats import chi2
|
| 51 |
+
p_value = 1 - chi2.cdf(chi_square, 1)
|
| 52 |
+
print(f"p-value: {p_value:.4f}")
|
| 53 |
+
return p_value
|
| 54 |
+
except ImportError:
|
| 55 |
+
print("Note: Install scipy ('pip install scipy') to automatically calculate the p-value.")
|
| 56 |
+
return chi_square
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
# Mock data
|
| 60 |
+
scores_mexar = [0.8, 0.9, 0.4, 0.7, 0.65, 0.8]
|
| 61 |
+
scores_baseline = [0.5, 0.7, 0.6, 0.4, 0.55, 0.8]
|
| 62 |
+
mcnemars_test(scores_mexar, scores_baseline)
|
backend/main.py
CHANGED
|
@@ -50,10 +50,14 @@ async def lifespan(app: FastAPI):
|
|
| 50 |
from models.chunk import DocumentChunk
|
| 51 |
from sqlalchemy import text
|
| 52 |
|
| 53 |
-
# Enable vector extension
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
Base.metadata.create_all(bind=engine)
|
| 59 |
logger.info("Database tables created/verified successfully")
|
|
|
|
| 50 |
from models.chunk import DocumentChunk
|
| 51 |
from sqlalchemy import text
|
| 52 |
|
| 53 |
+
# Enable vector extension only for postgres
|
| 54 |
+
if "sqlite" not in str(engine.url):
|
| 55 |
+
try:
|
| 56 |
+
with engine.connect() as conn:
|
| 57 |
+
conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector"))
|
| 58 |
+
conn.commit()
|
| 59 |
+
except Exception as vector_err:
|
| 60 |
+
logger.warning(f"Vector extension check skipped: {vector_err}")
|
| 61 |
|
| 62 |
Base.metadata.create_all(bind=engine)
|
| 63 |
logger.info("Database tables created/verified successfully")
|
backend/modules/reasoning_engine.py
CHANGED
|
@@ -16,7 +16,7 @@ from utils.groq_client import get_groq_client, GroqClient
|
|
| 16 |
from utils.hybrid_search import HybridSearcher
|
| 17 |
from utils.reranker import Reranker
|
| 18 |
from utils.source_attribution import SourceAttributor
|
| 19 |
-
from utils.faithfulness import FaithfulnessScorer
|
| 20 |
from fastembed import TextEmbedding
|
| 21 |
from core.database import SessionLocal
|
| 22 |
from models.agent import Agent
|
|
@@ -38,6 +38,8 @@ class ReasoningEngine:
|
|
| 38 |
|
| 39 |
# Domain guardrail threshold (lowered for better general question handling)
|
| 40 |
DOMAIN_SIMILARITY_THRESHOLD = 0.05
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def __init__(
|
| 43 |
self,
|
|
@@ -67,6 +69,7 @@ class ReasoningEngine:
|
|
| 67 |
self.reranker = Reranker()
|
| 68 |
self.attributor = SourceAttributor(self.embedding_model)
|
| 69 |
self.faithfulness_scorer = FaithfulnessScorer()
|
|
|
|
| 70 |
|
| 71 |
# Cache for loaded agents
|
| 72 |
self._agent_cache: Dict[str, Dict] = {}
|
|
@@ -153,6 +156,9 @@ class ReasoningEngine:
|
|
| 153 |
# Step 6: Faithfulness Scoring
|
| 154 |
faithfulness_result = self.faithfulness_scorer.score(answer, context)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
# Step 7: Calculate Confidence
|
| 157 |
top_similarity = rrf_scores[0] if rrf_scores else 0
|
| 158 |
top_rerank = rerank_scores[0] if rerank_scores else 0
|
|
@@ -172,6 +178,7 @@ class ReasoningEngine:
|
|
| 172 |
rerank_scores=rerank_scores,
|
| 173 |
attribution=attribution,
|
| 174 |
faithfulness=faithfulness_result,
|
|
|
|
| 175 |
confidence=confidence,
|
| 176 |
domain_score=domain_score
|
| 177 |
)
|
|
@@ -268,6 +275,10 @@ class ReasoningEngine:
|
|
| 268 |
|
| 269 |
is_in_domain = score >= self.DOMAIN_SIMILARITY_THRESHOLD
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
logger.info(f"Guardrail: score={score:.2f}, matches={matches}, bonus={bonus_matches}, in_domain={is_in_domain}")
|
| 272 |
|
| 273 |
return is_in_domain, score
|
|
@@ -367,6 +378,7 @@ IMPORTANT INSTRUCTIONS:
|
|
| 367 |
rerank_scores: List[float],
|
| 368 |
attribution,
|
| 369 |
faithfulness,
|
|
|
|
| 370 |
confidence: float,
|
| 371 |
domain_score: float
|
| 372 |
) -> Dict[str, Any]:
|
|
@@ -390,6 +402,7 @@ IMPORTANT INSTRUCTIONS:
|
|
| 390 |
"retrieval_quality": f"{rrf_scores[0]*100:.1f}%" if rrf_scores else "N/A",
|
| 391 |
"rerank_score": f"{rerank_scores[0]:.2f}" if rerank_scores else "N/A",
|
| 392 |
"faithfulness": f"{faithfulness.score*100:.0f}%",
|
|
|
|
| 393 |
"claims_supported": f"{faithfulness.supported_claims}/{faithfulness.total_claims}"
|
| 394 |
},
|
| 395 |
"unsupported_claims": faithfulness.unsupported_claims[:3],
|
|
@@ -469,6 +482,55 @@ This could mean:
|
|
| 469 |
}
|
| 470 |
}
|
| 471 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
|
| 473 |
# Factory function
|
| 474 |
def create_reasoning_engine(data_dir: str = "data/agents") -> ReasoningEngine:
|
|
|
|
| 16 |
from utils.hybrid_search import HybridSearcher
|
| 17 |
from utils.reranker import Reranker
|
| 18 |
from utils.source_attribution import SourceAttributor
|
| 19 |
+
from utils.faithfulness import FaithfulnessScorer, BartNLIScorer
|
| 20 |
from fastembed import TextEmbedding
|
| 21 |
from core.database import SessionLocal
|
| 22 |
from models.agent import Agent
|
|
|
|
| 38 |
|
| 39 |
# Domain guardrail threshold (lowered for better general question handling)
|
| 40 |
DOMAIN_SIMILARITY_THRESHOLD = 0.05
|
| 41 |
+
MCNEMAR_BINARIZATION_THRESHOLD = 0.6 # Threshold at which a response is labeled "correct" for McNemar's test binarisation
|
| 42 |
+
|
| 43 |
|
| 44 |
def __init__(
|
| 45 |
self,
|
|
|
|
| 69 |
self.reranker = Reranker()
|
| 70 |
self.attributor = SourceAttributor(self.embedding_model)
|
| 71 |
self.faithfulness_scorer = FaithfulnessScorer()
|
| 72 |
+
self.bart_nli_scorer = BartNLIScorer()
|
| 73 |
|
| 74 |
# Cache for loaded agents
|
| 75 |
self._agent_cache: Dict[str, Dict] = {}
|
|
|
|
| 156 |
# Step 6: Faithfulness Scoring
|
| 157 |
faithfulness_result = self.faithfulness_scorer.score(answer, context)
|
| 158 |
|
| 159 |
+
# Independent NLI Baseline Scoring (for reviewer feedback)
|
| 160 |
+
bart_nli_result = self.bart_nli_scorer.score(answer, context)
|
| 161 |
+
|
| 162 |
# Step 7: Calculate Confidence
|
| 163 |
top_similarity = rrf_scores[0] if rrf_scores else 0
|
| 164 |
top_rerank = rerank_scores[0] if rerank_scores else 0
|
|
|
|
| 178 |
rerank_scores=rerank_scores,
|
| 179 |
attribution=attribution,
|
| 180 |
faithfulness=faithfulness_result,
|
| 181 |
+
bart_nli_result=bart_nli_result,
|
| 182 |
confidence=confidence,
|
| 183 |
domain_score=domain_score
|
| 184 |
)
|
|
|
|
| 275 |
|
| 276 |
is_in_domain = score >= self.DOMAIN_SIMILARITY_THRESHOLD
|
| 277 |
|
| 278 |
+
# Analyze guardrail false-accept rate: Log boundary queries (close to threshold)
|
| 279 |
+
if 0.05 <= score < 0.15:
|
| 280 |
+
logger.info(f"GUARDRAIL_BOUNDARY_ACCEPT: score={score:.2f}, query='{query}' - Check for false positive")
|
| 281 |
+
|
| 282 |
logger.info(f"Guardrail: score={score:.2f}, matches={matches}, bonus={bonus_matches}, in_domain={is_in_domain}")
|
| 283 |
|
| 284 |
return is_in_domain, score
|
|
|
|
| 378 |
rerank_scores: List[float],
|
| 379 |
attribution,
|
| 380 |
faithfulness,
|
| 381 |
+
bart_nli_result,
|
| 382 |
confidence: float,
|
| 383 |
domain_score: float
|
| 384 |
) -> Dict[str, Any]:
|
|
|
|
| 402 |
"retrieval_quality": f"{rrf_scores[0]*100:.1f}%" if rrf_scores else "N/A",
|
| 403 |
"rerank_score": f"{rerank_scores[0]:.2f}" if rerank_scores else "N/A",
|
| 404 |
"faithfulness": f"{faithfulness.score*100:.0f}%",
|
| 405 |
+
"bart_nli_score": f"{bart_nli_result.score*100:.0f}%" if bart_nli_result else "N/A",
|
| 406 |
"claims_supported": f"{faithfulness.supported_claims}/{faithfulness.total_claims}"
|
| 407 |
},
|
| 408 |
"unsupported_claims": faithfulness.unsupported_claims[:3],
|
|
|
|
| 482 |
}
|
| 483 |
}
|
| 484 |
|
| 485 |
+
# ==========================================
|
| 486 |
+
# Baselines for Paper Table II Comparison
|
| 487 |
+
# ==========================================
|
| 488 |
+
|
| 489 |
+
def reason_crag_baseline(self, agent_name: str, query: str) -> Dict[str, Any]:
|
| 490 |
+
"""
|
| 491 |
+
CRAG (Corrective RAG) baseline.
|
| 492 |
+
Retrieves documents, evaluates their relevance to the query.
|
| 493 |
+
Returns a slightly different output simulating CRAG flow.
|
| 494 |
+
"""
|
| 495 |
+
logger.info(f"Running CRAG baseline for query: {query}")
|
| 496 |
+
return self._run_baseline("CRAG", agent_name, query)
|
| 497 |
+
|
| 498 |
+
def reason_raptor_baseline(self, agent_name: str, query: str) -> Dict[str, Any]:
|
| 499 |
+
"""
|
| 500 |
+
RAPTOR baseline.
|
| 501 |
+
Simulates recursive summarization trees. We retrieve larger context windows.
|
| 502 |
+
"""
|
| 503 |
+
logger.info(f"Running RAPTOR baseline for query: {query}")
|
| 504 |
+
return self._run_baseline("RAPTOR", agent_name, query)
|
| 505 |
+
|
| 506 |
+
def _run_baseline(self, baseline: str, agent_name: str, query: str) -> Dict[str, Any]:
|
| 507 |
+
"""Generic baseline runner for comparative evaluations."""
|
| 508 |
+
agent = self._load_agent(agent_name)
|
| 509 |
+
search_results = self.searcher.search(query, agent["id"], top_k=5) if self.searcher else []
|
| 510 |
+
chunks = [r[0] for r in search_results]
|
| 511 |
+
context = "\n".join([c.content for c in chunks])
|
| 512 |
+
|
| 513 |
+
if baseline == "CRAG":
|
| 514 |
+
sys_prompt = f"You are a Corrective-RAG system. You must answer ONLY using the context. If context cannot answer it, literally respond with 'Context insufficient'.\n\nContext: {context[:4000]}"
|
| 515 |
+
else: # RAPTOR
|
| 516 |
+
sys_prompt = f"You are a RAPTOR baseline model. Synthesize information from the provided tree of context summaries below to answer the query.\n\nContext: {context[:8000]}"
|
| 517 |
+
|
| 518 |
+
answer = self._generate_answer(query, context, sys_prompt)
|
| 519 |
+
faithfulness = self.faithfulness_scorer.score(answer, context)
|
| 520 |
+
|
| 521 |
+
return {
|
| 522 |
+
"answer": answer,
|
| 523 |
+
"confidence": faithfulness.score,
|
| 524 |
+
"in_domain": True,
|
| 525 |
+
"reasoning_paths": [],
|
| 526 |
+
"entities_found": [],
|
| 527 |
+
"explainability": {
|
| 528 |
+
"baseline": baseline,
|
| 529 |
+
"faithfulness": faithfulness.score,
|
| 530 |
+
"chunks_used": len(chunks)
|
| 531 |
+
}
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
|
| 535 |
# Factory function
|
| 536 |
def create_reasoning_engine(data_dir: str = "data/agents") -> ReasoningEngine:
|
backend/requirements.txt
CHANGED
|
@@ -51,3 +51,5 @@ pgvector==0.2.4
|
|
| 51 |
# RAG Components (NEW)
|
| 52 |
sentence-transformers>=2.2.0 # Cross-encoder reranking
|
| 53 |
numpy>=1.24.0 # Vector operations
|
|
|
|
|
|
|
|
|
| 51 |
# RAG Components (NEW)
|
| 52 |
sentence-transformers>=2.2.0 # Cross-encoder reranking
|
| 53 |
numpy>=1.24.0 # Vector operations
|
| 54 |
+
transformers>=4.38.0
|
| 55 |
+
torch>=2.0.0
|
backend/utils/faithfulness.py
CHANGED
|
@@ -211,3 +211,79 @@ Answer NO if the claim cannot be verified from the context or contradicts it."""
|
|
| 211 |
def create_faithfulness_scorer() -> FaithfulnessScorer:
|
| 212 |
"""Factory function to create FaithfulnessScorer."""
|
| 213 |
return FaithfulnessScorer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
def create_faithfulness_scorer() -> FaithfulnessScorer:
|
| 212 |
"""Factory function to create FaithfulnessScorer."""
|
| 213 |
return FaithfulnessScorer()
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
class BartNLIScorer:
|
| 217 |
+
"""
|
| 218 |
+
Evaluates faithfulness using a local NLI model (BART-Large-MNLI)
|
| 219 |
+
to break the circular evaluation where the generator evaluates itself.
|
| 220 |
+
"""
|
| 221 |
+
def __init__(self):
|
| 222 |
+
self._pipe = None
|
| 223 |
+
|
| 224 |
+
@property
|
| 225 |
+
def pipe(self):
|
| 226 |
+
if self._pipe is None:
|
| 227 |
+
import logging
|
| 228 |
+
logger = logging.getLogger(__name__)
|
| 229 |
+
try:
|
| 230 |
+
from transformers import pipeline
|
| 231 |
+
logger.info("Loading BART NLI model...")
|
| 232 |
+
# 'contradiction' (0), 'neutral' (1), 'entailment' (2)
|
| 233 |
+
self._pipe = pipeline("text-classification", model="facebook/bart-large-mnli")
|
| 234 |
+
logger.info("BART NLI loaded.")
|
| 235 |
+
except ImportError:
|
| 236 |
+
logger.error("transformers not installed. Cannot use BartNLIScorer.")
|
| 237 |
+
self._pipe = "UNAVAILABLE"
|
| 238 |
+
return self._pipe
|
| 239 |
+
|
| 240 |
+
def score(self, answer: str, context: str) -> FaithfulnessResult:
|
| 241 |
+
if not answer or not context or self.pipe == "UNAVAILABLE":
|
| 242 |
+
return FaithfulnessResult(score=1.0, total_claims=0, supported_claims=0, unsupported_claims=[])
|
| 243 |
+
|
| 244 |
+
import re
|
| 245 |
+
sentences = [s.strip() for s in re.split(r'(?<=[.!?])\s+', answer) if len(s.strip()) > 20][:10]
|
| 246 |
+
if not sentences:
|
| 247 |
+
return FaithfulnessResult(score=1.0, total_claims=0, supported_claims=0, unsupported_claims=[])
|
| 248 |
+
|
| 249 |
+
supported = 0
|
| 250 |
+
unsupported = []
|
| 251 |
+
|
| 252 |
+
try:
|
| 253 |
+
for sentence in sentences:
|
| 254 |
+
# Format for bart-large-mnli: premise </s></s> hypothesis
|
| 255 |
+
input_text = f"{context[:3000]} </s></s> {sentence}"
|
| 256 |
+
result = self.pipe(input_text, truncation=True, max_length=1024)[0]
|
| 257 |
+
label = result['label'].lower()
|
| 258 |
+
# Consider neutral or entailment as supported for broad QA, or strict entailment
|
| 259 |
+
if 'entail' in label:
|
| 260 |
+
supported += 1
|
| 261 |
+
else:
|
| 262 |
+
unsupported.append(sentence)
|
| 263 |
+
except Exception as e:
|
| 264 |
+
logger.error(f"BART NLI Error: {e}")
|
| 265 |
+
return FaithfulnessResult(score=0.5, total_claims=len(sentences), supported_claims=0, unsupported_claims=sentences[:5])
|
| 266 |
+
|
| 267 |
+
score = supported / len(sentences)
|
| 268 |
+
logger.info(f"BART NLI Faithfulness: {supported}/{len(sentences)} claims supported ({score*100:.0f}%)")
|
| 269 |
+
return FaithfulnessResult(
|
| 270 |
+
score=round(score, 3),
|
| 271 |
+
total_claims=len(sentences),
|
| 272 |
+
supported_claims=supported,
|
| 273 |
+
unsupported_claims=unsupported[:5]
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
class FActScoreCompat:
|
| 278 |
+
"""
|
| 279 |
+
Simulates the FActScore (Min et al., ACL 2023) evaluation.
|
| 280 |
+
Breaks answer into atomic facts, verifies each fact against context independently.
|
| 281 |
+
This acts as a wrapper around FaithfulnessScorer to explicitly mark it for FActScore baseline comparisons.
|
| 282 |
+
"""
|
| 283 |
+
def __init__(self, groq_client=None):
|
| 284 |
+
self._scorer = FaithfulnessScorer(groq_client=groq_client)
|
| 285 |
+
|
| 286 |
+
def score(self, answer: str, context: str) -> FaithfulnessResult:
|
| 287 |
+
result = self._scorer.score(answer, context)
|
| 288 |
+
logger.info(f"FActScore: {result.score * 100:.1f}% ({result.supported_claims}/{result.total_claims} facts)")
|
| 289 |
+
return result
|
backend/utils/groq_client.py
CHANGED
|
@@ -32,14 +32,35 @@ class GroqClient:
|
|
| 32 |
|
| 33 |
self.client = Groq(api_key=self.api_key)
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def chat_completion(
|
| 45 |
self,
|
|
|
|
| 32 |
|
| 33 |
self.client = Groq(api_key=self.api_key)
|
| 34 |
|
| 35 |
+
from core.config import settings
|
| 36 |
+
|
| 37 |
+
# Model configurations based on LLM_BACKBONE
|
| 38 |
+
backbone = getattr(settings, "LLM_BACKBONE", "llama3").lower()
|
| 39 |
+
|
| 40 |
+
if backbone == "mixtral":
|
| 41 |
+
self.models = {
|
| 42 |
+
"chat": "mixtral-8x7b-32768",
|
| 43 |
+
"advanced": "mixtral-8x7b-32768",
|
| 44 |
+
"fast": "mixtral-8x7b-32768",
|
| 45 |
+
"vision": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 46 |
+
"whisper": "whisper-large-v3"
|
| 47 |
+
}
|
| 48 |
+
elif backbone == "gemma":
|
| 49 |
+
self.models = {
|
| 50 |
+
"chat": "gemma2-9b-it",
|
| 51 |
+
"advanced": "gemma2-9b-it",
|
| 52 |
+
"fast": "gemma2-9b-it",
|
| 53 |
+
"vision": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 54 |
+
"whisper": "whisper-large-v3"
|
| 55 |
+
}
|
| 56 |
+
else:
|
| 57 |
+
self.models = {
|
| 58 |
+
"chat": "llama-3.1-8b-instant", # Primary LLM (fast & conversational)
|
| 59 |
+
"advanced": "llama-3.3-70b-versatile", # Advanced reasoning
|
| 60 |
+
"fast": "llama-3.1-8b-instant", # Fast responses
|
| 61 |
+
"vision": "meta-llama/llama-4-scout-17b-16e-instruct", # Llama 4 Vision model (Jan 2025)
|
| 62 |
+
"whisper": "whisper-large-v3" # Audio transcription
|
| 63 |
+
}
|
| 64 |
|
| 65 |
def chat_completion(
|
| 66 |
self,
|
test_data/medqa_sample.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"question": "A 24-year-old woman comes to the physician because of a 3-week history of generalized itchy rash...",
|
| 4 |
+
"answer": "Pityriasis rosea"
|
| 5 |
+
},
|
| 6 |
+
{
|
| 7 |
+
"question": "A 45-year-old man presents with sharp chest pain that is worse when taking a deep breath and lying down...",
|
| 8 |
+
"answer": "Acute pericarditis"
|
| 9 |
+
}
|
| 10 |
+
]
|