|
|
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." |
|
|
), |
|
|
} |
|
|
} |
|
|
} |
|
|
|