137 lines
No EOL
5.4 KiB
Python
137 lines
No EOL
5.4 KiB
Python
# contentValidator.py
|
|
# Content validation for adaptive React mode
|
|
|
|
import logging
|
|
import json
|
|
import re
|
|
from typing import List, Dict, Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class ContentValidator:
|
|
"""Validates delivered content against user intent"""
|
|
|
|
def __init__(self, services=None):
|
|
self.services = services
|
|
|
|
async def validateContent(self, documents: List[Any], intent: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Validates delivered content against user intent using AI"""
|
|
try:
|
|
# Use AI for comprehensive validation
|
|
return await self._validateWithAI(documents, intent)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error validating content: {str(e)}")
|
|
return self._createFailedValidationResult(str(e))
|
|
|
|
def _extractContent(self, doc: Any) -> str:
|
|
"""Extracts content from a document"""
|
|
try:
|
|
if hasattr(doc, 'documentData'):
|
|
data = doc.documentData
|
|
if isinstance(data, dict) and 'content' in data:
|
|
return str(data['content'])
|
|
else:
|
|
return str(data)
|
|
return ""
|
|
except Exception:
|
|
return ""
|
|
|
|
def _createFailedValidationResult(self, error: str) -> Dict[str, Any]:
|
|
"""Creates a failed validation result"""
|
|
return {
|
|
"overallSuccess": False,
|
|
"qualityScore": 0.0,
|
|
"validationDetails": [],
|
|
"improvementSuggestions": [f"NEXT STEP: Fix validation error - {error}. Check system logs for more details and retry the operation."]
|
|
}
|
|
|
|
async def _validateWithAI(self, documents: List[Any], intent: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""AI-based comprehensive validation - single main function"""
|
|
try:
|
|
if not hasattr(self, 'services') or not self.services or not hasattr(self.services, 'ai'):
|
|
return self._createFailedValidationResult("AI service not available")
|
|
|
|
# Extract content from all documents
|
|
documentContents = []
|
|
for doc in documents:
|
|
content = self._extractContent(doc)
|
|
documentContents.append({
|
|
"name": getattr(doc, 'documentName', 'Unknown'),
|
|
"content": content[:2000] # Limit content for AI processing
|
|
})
|
|
|
|
# Create comprehensive AI validation prompt
|
|
validationPrompt = f"""
|
|
You are a comprehensive task completion validator. Analyze if the delivered content fulfills the user's request.
|
|
|
|
USER REQUEST: {intent.get('primaryGoal', 'Unknown')}
|
|
EXPECTED DATA TYPE: {intent.get('dataType', 'unknown')}
|
|
EXPECTED FORMAT: {intent.get('expectedFormat', 'unknown')}
|
|
SUCCESS CRITERIA: {intent.get('successCriteria', [])}
|
|
|
|
DELIVERED CONTENT:
|
|
{json.dumps(documentContents, indent=2)}
|
|
|
|
Perform comprehensive validation:
|
|
1. Check if content matches expected data type
|
|
2. Check if content matches expected format
|
|
3. Verify success criteria are met
|
|
4. Assess overall quality and completeness
|
|
5. Identify specific gaps and issues
|
|
6. Provide actionable next steps
|
|
|
|
Respond with JSON only:
|
|
{{
|
|
"overallSuccess": true/false,
|
|
"qualityScore": 0.0-1.0,
|
|
"dataTypeMatch": true/false,
|
|
"formatMatch": true/false,
|
|
"successCriteriaMet": [true/false for each criterion],
|
|
"gapAnalysis": "Detailed analysis: what's missing/incorrect AND what specific next step to do",
|
|
"improvementSuggestions": ["NEXT STEP: specific action 1", "NEXT STEP: specific action 2"],
|
|
"validationDetails": [
|
|
{{
|
|
"documentName": "Document name",
|
|
"issues": ["specific issue 1", "specific issue 2"],
|
|
"suggestions": ["NEXT STEP: specific fix 1", "NEXT STEP: specific fix 2"]
|
|
}}
|
|
]
|
|
}}
|
|
"""
|
|
|
|
# Call AI service for validation
|
|
from modules.datamodels.datamodelAi import AiCallOptions, OperationType
|
|
request_options = AiCallOptions()
|
|
request_options.operationType = OperationType.GENERAL
|
|
|
|
response = await self.services.ai.callAi(
|
|
prompt=validationPrompt,
|
|
documents=None,
|
|
options=request_options
|
|
)
|
|
if response:
|
|
import re
|
|
result = response.strip()
|
|
json_match = re.search(r'\{.*\}', result, re.DOTALL)
|
|
if json_match:
|
|
result = json_match.group(0)
|
|
|
|
aiResult = json.loads(result)
|
|
|
|
return {
|
|
"overallSuccess": aiResult.get("overallSuccess", False),
|
|
"qualityScore": aiResult.get("qualityScore", 0.0),
|
|
"validationDetails": aiResult.get("validationDetails", [{
|
|
"documentName": "AI Validation",
|
|
"gapAnalysis": aiResult.get("gapAnalysis", ""),
|
|
"successCriteriaMet": aiResult.get("successCriteriaMet", [False])
|
|
}]),
|
|
"improvementSuggestions": aiResult.get("improvementSuggestions", [])
|
|
}
|
|
|
|
return self._createFailedValidationResult("AI validation failed - no response")
|
|
|
|
except Exception as e:
|
|
logger.error(f"AI validation failed: {str(e)}")
|
|
return self._createFailedValidationResult(f"AI validation error: {str(e)}") |