File size: 3,923 Bytes
336f4a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pydantic import BaseModel, Field, field_validator

STRUCTURED_RAG_PROMPT = """You are an analytical question-answering assistant. Follow these steps systematically:

1. Context Evaluation:
    - Analyze all context relevance to: "{question}"
    - Identify directly relevant segments
    - Flag gaps/contradictions in context

2. Response Formulation:
    - If sufficient context:
      * Synthesize key evidence comprehensively
      * Construct complete evidence-based answer
      * Maintain objective tone
    - If insufficient context:
      * Specify missing information in detail
      * State knowledge boundaries clearly
      * Decline speculation

3. Final Verification:
    - Ensure full factual alignment with context
    - Remove any external knowledge

**Question:** {question}
**Relevant Context:**
{context}

**Reasoning Process:** [Complete analysis with all required sections]
**Final Answer:** [Thorough response or detailed decline explanation]"""


class RAGResponseModel(BaseModel):
    """Structured validation for RAG response process compliance."""

    reasoning_chain: str = Field(
        ...,
        description=(
            "Complete analytical process containing:\n"
            "- 1. Context Evaluation: Relevance assessment\n"
            "- 2. Response Formulation: Answer construction logic\n"
            "- 3. Final Verification: Factual consistency check"
        ),
    )

    final_answer: str = Field(
        ...,
        description=(
            "Verified response containing either:\n"
            "- Comprehensive evidence-based explanation\n"
            "- Detailed decline with missing context specification"
        ),
    )

    @field_validator("reasoning_chain")
    @classmethod
    def validate_analysis_steps(cls, chain: str) -> str:
        """Validate the structure and completeness of the analytical reasoning chain.

        Ensures the reasoning chain contains all required section headers and meets length constraints.

        Args:
            chain (str): The reasoning chain to validate.

        Returns:
            str: The validated reasoning chain.

        Raises:
            ValueError: If any required section headers are missing from the chain.
        """
        required_sections = ["1. Context Evaluation", "2. Response Formulation", "3. Final Verification"]

        missing = [step for step in required_sections if step not in chain]
        if missing:
            msg = f"Missing required analysis steps: {missing}"
            raise ValueError(msg)

        return chain

    model_config = {
        "json_schema_extra": {
            "example": {
                "reasoning_chain": (
                    "1. Context Evaluation: Analyzed 5 documents on climate models\n"
                    "Identified relevant sections on temperature projections\n"
                    "Noted absence of economic impact data\n\n"
                    "2. Response Formulation: Combined IPCC and NOAA projections\n"
                    "Structured timeline-based response\n"
                    "Excluded unrelated urban heat island content\n\n"
                    "3. Final Verification: Cross-referenced all statistics\n"
                    "Confirmed absence of external knowledge"
                ),
                "final_answer": (
                    "Current climate models project a temperature increase range of 1.5°C to 4.5°C by 2100, "
                    "depending on emission scenarios. The IPCC AR6 report indicates a 66% likelihood of staying "
                    "below 2°C if net-zero emissions are achieved by 2070. NOAA data shows accelerated warming "
                    "trends in Arctic regions, with models predicting ice-free summers by 2050 under high-emission "
                    "pathways. Economic impact projections remain unavailable in provided context."
                ),
            }
        }
    }