Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- utils/__init__.py +8 -0
- utils/audit_logger.py +36 -0
- utils/config.py +41 -0
- utils/data_models.py +44 -0
- utils/evaluator.py +45 -0
utils/__init__.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Utility functions and configurations"""
|
| 2 |
+
|
| 3 |
+
from .config import Config
|
| 4 |
+
from .data_models import RetrievalResult, RAGResponse, QueryType
|
| 5 |
+
from .evaluator import Evaluator
|
| 6 |
+
from .audit_logger import AuditLogger
|
| 7 |
+
|
| 8 |
+
__all__ = ['Config', 'RetrievalResult', 'RAGResponse', 'QueryType', 'Evaluator', 'AuditLogger']
|
utils/audit_logger.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Audit logging for RAG system"""
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import hashlib
|
| 5 |
+
from typing import Dict
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class AuditLogger:
|
| 9 |
+
"""Comprehensive audit logging for queries and responses"""
|
| 10 |
+
|
| 11 |
+
def __init__(self, log_file: str = "audit_logs.jsonl"):
|
| 12 |
+
"""Initialize audit logger with log file path"""
|
| 13 |
+
self.log_file = log_file
|
| 14 |
+
|
| 15 |
+
def log(self, entry: Dict):
|
| 16 |
+
"""
|
| 17 |
+
Write audit entry to log file
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
entry: Dictionary containing audit information
|
| 21 |
+
"""
|
| 22 |
+
with open(self.log_file, 'a', encoding='utf-8') as f:
|
| 23 |
+
f.write(json.dumps(entry) + '\n')
|
| 24 |
+
|
| 25 |
+
@staticmethod
|
| 26 |
+
def hash_text(text: str) -> str:
|
| 27 |
+
"""
|
| 28 |
+
Hash text for privacy-preserving logging
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
text: Text to hash
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
First 16 characters of SHA256 hash
|
| 35 |
+
"""
|
| 36 |
+
return hashlib.sha256(text.encode()).hexdigest()[:16]
|
utils/config.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Configuration settings for RAG system"""
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class Config:
|
| 10 |
+
"""Centralized configuration for RAG system"""
|
| 11 |
+
|
| 12 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 13 |
+
COHERE_API_KEY = os.getenv("COHERE_API_KEY", "")
|
| 14 |
+
|
| 15 |
+
COLLECTION_NAME = "ford_f150_suspension"
|
| 16 |
+
CHUNKS_JSON = "chunks/suspension_chunks_final.json"
|
| 17 |
+
CHROMA_PATH = "chroma_db"
|
| 18 |
+
|
| 19 |
+
INITIAL_CANDIDATES = 100
|
| 20 |
+
RERANK_TOP_K = 50
|
| 21 |
+
MIN_CHUNKS = 1
|
| 22 |
+
MAX_CHUNKS = 20
|
| 23 |
+
|
| 24 |
+
CLEAR_WINNER_SCORE = 0.92
|
| 25 |
+
CLEAR_WINNER_GAP = 0.20
|
| 26 |
+
NATURAL_GAP_THRESHOLD = 0.15
|
| 27 |
+
|
| 28 |
+
FACTUAL_DEFAULT = 3
|
| 29 |
+
SUMMARY_DEFAULT = 12
|
| 30 |
+
PROCEDURAL_DEFAULT = 8
|
| 31 |
+
|
| 32 |
+
MMR_LAMBDA = 0.7
|
| 33 |
+
|
| 34 |
+
LLM_MODEL = "gpt-4o-mini"
|
| 35 |
+
LLM_TEMPERATURE = 0.1
|
| 36 |
+
LLM_MAX_TOKENS = 800
|
| 37 |
+
|
| 38 |
+
ENABLE_QUERY_REWRITING = True
|
| 39 |
+
ENABLE_MMR = True
|
| 40 |
+
ENABLE_EVALUATION = True
|
| 41 |
+
ENABLE_AUDIT = True
|
utils/data_models.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Data models for RAG system"""
|
| 2 |
+
|
| 3 |
+
from typing import List, Dict, Optional
|
| 4 |
+
from dataclasses import dataclass
|
| 5 |
+
from enum import Enum
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class QueryType(Enum):
|
| 9 |
+
"""Query classification types"""
|
| 10 |
+
FACTUAL = "factual"
|
| 11 |
+
MULTI_HOP = "multi_hop"
|
| 12 |
+
TEMPORAL = "temporal"
|
| 13 |
+
COMPARISON = "comparison"
|
| 14 |
+
PROCEDURAL = "procedural"
|
| 15 |
+
SUMMARY = "summary"
|
| 16 |
+
OUT_OF_SCOPE = "out_of_scope"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass
|
| 20 |
+
class RetrievalResult:
|
| 21 |
+
"""Single retrieval result with metadata"""
|
| 22 |
+
chunk_id: str
|
| 23 |
+
content: str
|
| 24 |
+
score: float
|
| 25 |
+
page: int
|
| 26 |
+
rank: int
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@dataclass
|
| 30 |
+
class RAGResponse:
|
| 31 |
+
"""Complete RAG response with all metadata"""
|
| 32 |
+
query: str
|
| 33 |
+
answer: str
|
| 34 |
+
query_type: str
|
| 35 |
+
num_chunks_used: int
|
| 36 |
+
chunks: List[Dict]
|
| 37 |
+
cutoff_reason: str
|
| 38 |
+
latency_ms: float
|
| 39 |
+
query_rewritten: bool = False
|
| 40 |
+
retrieval_query: str = ""
|
| 41 |
+
faithfulness_score: Optional[float] = None
|
| 42 |
+
citation_accuracy: Optional[float] = None
|
| 43 |
+
session_id: str = ""
|
| 44 |
+
timestamp: str = ""
|
utils/evaluator.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Answer quality evaluation"""
|
| 2 |
+
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from .config import Config
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class Evaluator:
|
| 8 |
+
"""Evaluate answer quality and faithfulness"""
|
| 9 |
+
|
| 10 |
+
def __init__(self, openai_client: OpenAI):
|
| 11 |
+
"""Initialize evaluator with OpenAI client"""
|
| 12 |
+
self.client = openai_client
|
| 13 |
+
|
| 14 |
+
def evaluate_faithfulness(self, answer: str, context: str) -> float:
|
| 15 |
+
"""
|
| 16 |
+
Check if answer is supported by context
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
answer: Generated answer
|
| 20 |
+
context: Source context
|
| 21 |
+
|
| 22 |
+
Returns:
|
| 23 |
+
Faithfulness score from 0.0 to 1.0
|
| 24 |
+
"""
|
| 25 |
+
prompt = f"""Evaluate if the answer is faithful to the context.
|
| 26 |
+
|
| 27 |
+
Context:
|
| 28 |
+
{context[:2000]}
|
| 29 |
+
|
| 30 |
+
Answer:
|
| 31 |
+
{answer}
|
| 32 |
+
|
| 33 |
+
Return a score from 0.0 (unfaithful) to 1.0 (perfectly faithful).
|
| 34 |
+
Output only the numeric score."""
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
response = self.client.chat.completions.create(
|
| 38 |
+
model=Config.LLM_MODEL,
|
| 39 |
+
messages=[{"role": "user", "content": prompt}],
|
| 40 |
+
temperature=0.0,
|
| 41 |
+
max_tokens=10
|
| 42 |
+
)
|
| 43 |
+
return float(response.choices[0].message.content.strip())
|
| 44 |
+
except:
|
| 45 |
+
return 0.0
|