Spaces:
Configuration error
Configuration error
File size: 1,462 Bytes
6733714 | 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 | from app.generation.confidence import estimate_confidence
from app.generation.source_formatter import format_sources
from app.generation.synthesizer import synthesize_answer
from app.generation.safety import apply_safety_guards
def build_final_answer(question: str, context: str, chunks: list) -> dict:
"""
Builds the final, safe, demo-ready answer.
"""
raw_answer = synthesize_answer(question, context)
confidence = estimate_confidence(context)
sources = format_sources(chunks)
# Parse Metadata (Reasoning)
if "---METADATA---" in raw_answer:
parts = raw_answer.split("---METADATA---")
content = parts[0].strip()
try:
import json
metadata_str = parts[1].strip()
reasoning = json.loads(metadata_str)
except Exception as e:
print(f"Error parsing metadata: {e}")
reasoning = None
else:
content = raw_answer
reasoning = None
result = apply_safety_guards(
answer=content,
confidence=confidence,
sources=sources
)
# Attach reasoning if available (assuming apply_safety_guards returns a dict,
# if it returns a string we might need to adjust app.py instead.
# Checking app.py, 'final_answer' is just a string.
# We should probably return a tuple or dict here to pass reasoning up.)
return {
"content": result,
"reasoning": reasoning
}
|