File size: 1,801 Bytes
905e3b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Validation node for DocIntel."""
from app.agents.state import AgentState
from app.config import settings

def validate_response(state: AgentState) -> AgentState:
    """Validate response and calculate confidence"""
    print("๐Ÿ” Validating response...")
    
    response = state.get("response", "")
    documents = state.get("documents", [])
    current_confidence = state.get("confidence", 0.0)
    
    if not response:
        state["validated"] = False
        state["confidence"] = 0.0
        print("โŒ No response to validate")
        return state
    
    # Factor 1: Document relevance (40% weight)
    doc_scores = [d.get("score", 0) for d in documents[:3]]
    avg_doc_score = sum(doc_scores) / len(doc_scores) if doc_scores else 0
    
    # Factor 2: Response length (20% weight) - longer responses indicate more content
    length_score = min(1.0, len(response) / 500)  # Cap at 500 chars
    
    # Factor 3: Number of sources (20% weight)
    source_score = min(1.0, len(documents) / settings.TOP_K) if documents else 0
    
    # Factor 4: Confidence from retrieval (20% weight)
    retrieval_score = min(1.0, current_confidence * 2)  # Scale up
    
    # Calculate weighted confidence
    confidence = (
        avg_doc_score * 0.40 +
        length_score * 0.20 +
        source_score * 0.20 +
        retrieval_score * 0.20
    )
    
    # Cap at 0.95
    confidence = min(0.95, confidence)
    
    # If no documents, low confidence
    if not documents:
        confidence = 0.1
    
    state["confidence"] = confidence
    state["validated"] = confidence > 0.3  # Threshold for validation
    
    print(f"โœ… Confidence: {confidence:.3f} (doc: {avg_doc_score:.2f}, len: {length_score:.2f}, src: {source_score:.2f}, ret: {retrieval_score:.2f})")
    
    return state