M10: harden knowledge trust and telemetry
This commit is contained in:
@@ -110,7 +110,7 @@ def demo_reset(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Demo reset is disabled on this deployment.",
|
||||
)
|
||||
result = reset_and_seed(db)
|
||||
result = reset_and_seed(db, preserve_integration_telemetry=True)
|
||||
integrity = scenario_integrity_report(db)
|
||||
record_audit_event(
|
||||
db,
|
||||
|
||||
@@ -80,9 +80,17 @@ def _read_csv(name: str) -> list[dict[str, str]]:
|
||||
return list(csv.DictReader(handle))
|
||||
|
||||
|
||||
def clear_all(db: Session) -> None:
|
||||
_PERSISTENT_TELEMETRY_ACTIONS = (
|
||||
"mcp_tool_request",
|
||||
"n8n_return_followup_recorded",
|
||||
"n8n_workflow_failure_registered",
|
||||
"n8n_procedures_synced",
|
||||
"knowledge_question_asked",
|
||||
)
|
||||
|
||||
|
||||
def clear_all(db: Session, *, preserve_integration_telemetry: bool = False) -> None:
|
||||
for model in (
|
||||
AuditEvent,
|
||||
OutboxEvent,
|
||||
IdempotencyRecord,
|
||||
DataQualityIssue,
|
||||
@@ -94,6 +102,10 @@ def clear_all(db: Session) -> None:
|
||||
User,
|
||||
):
|
||||
db.execute(delete(model))
|
||||
if preserve_integration_telemetry:
|
||||
db.execute(delete(AuditEvent).where(AuditEvent.action.not_in(_PERSISTENT_TELEMETRY_ACTIONS)))
|
||||
else:
|
||||
db.execute(delete(AuditEvent))
|
||||
|
||||
|
||||
def load_seed(db: Session) -> SeedResult:
|
||||
@@ -428,10 +440,12 @@ def load_seed(db: Session) -> SeedResult:
|
||||
return SeedResult(counts=counts, anchor_date=today, seeded_at=seeded_at)
|
||||
|
||||
|
||||
def reset_and_seed(db: Session) -> SeedResult:
|
||||
def reset_and_seed(
|
||||
db: Session, *, preserve_integration_telemetry: bool = False
|
||||
) -> SeedResult:
|
||||
from app.services.data_quality import run_scan
|
||||
|
||||
clear_all(db)
|
||||
clear_all(db, preserve_integration_telemetry=preserve_integration_telemetry)
|
||||
result = load_seed(db)
|
||||
db.commit()
|
||||
scan = run_scan(db)
|
||||
|
||||
@@ -21,6 +21,54 @@ _LEAD_ANSWER_TEMPLATE = {
|
||||
}
|
||||
_DEFAULT_LANGUAGE = "en-GB"
|
||||
|
||||
_DOMAIN_CONCEPTS: dict[str, tuple[str, ...]] = {
|
||||
"damage": ("damage", "damaged", "schade", "beschadigd", "dommage", "endommagé"),
|
||||
"vehicle": ("vehicle", "car", "voertuig", "wagen", "véhicule", "voiture"),
|
||||
"return": ("return", "returned", "retour", "terugbrengen", "restitution"),
|
||||
"fuel": ("fuel", "brandstof", "carburant"),
|
||||
"booking": ("booking", "reservation", "boeking", "réservation"),
|
||||
"odometer": ("odometer", "mileage", "kilometer", "kilométrage", "compteur"),
|
||||
"customer": ("customer", "client", "klant"),
|
||||
"cleaning": ("cleaning", "reiniging", "poetsen", "nettoyage"),
|
||||
"maintenance": ("maintenance", "service", "onderhoud", "entretien"),
|
||||
"conflict": ("conflict", "overlap", "overlapping", "conflit", "chevauchement"),
|
||||
"checkout": ("checkout", "departure", "vertrek", "départ"),
|
||||
"availability": ("available", "availability", "beschikbaar", "disponible", "disponibilité"),
|
||||
"technical": ("technical", "warning", "technisch", "waarschuwing", "technique", "alerte"),
|
||||
}
|
||||
|
||||
|
||||
def _question_concepts(question: str) -> set[str]:
|
||||
normalized = question.casefold()
|
||||
return {
|
||||
concept
|
||||
for concept, terms in _DOMAIN_CONCEPTS.items()
|
||||
if any(term in normalized for term in terms)
|
||||
}
|
||||
|
||||
|
||||
def _deduplicate_sources(sources: list[SourceCard]) -> list[SourceCard]:
|
||||
seen: set[tuple[str, str]] = set()
|
||||
unique: list[SourceCard] = []
|
||||
for source in sources:
|
||||
key = (source.document_id, source.section)
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
unique.append(source)
|
||||
return unique
|
||||
|
||||
|
||||
def _rank_sources_for_concepts(sources: list[SourceCard], concepts: set[str]) -> list[SourceCard]:
|
||||
if "damage" not in concepts:
|
||||
return sources
|
||||
return sorted(
|
||||
sources,
|
||||
key=lambda source: (
|
||||
0 if "damage" in f"{source.document_id} {source.title}".casefold() else 1
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class RAGcoreKnowledgeProvider:
|
||||
"""Adapter for the central RAGcore service, against its real `/v1/*` contract
|
||||
@@ -199,6 +247,9 @@ class RAGcoreKnowledgeProvider:
|
||||
except (TypeError, KeyError, ValueError):
|
||||
return unavailable
|
||||
|
||||
sources = _deduplicate_sources(sources)
|
||||
concepts = _question_concepts(question)
|
||||
sources = _rank_sources_for_concepts(sources, concepts)
|
||||
if not sources:
|
||||
return GroundedAnswer(
|
||||
answer="",
|
||||
@@ -208,6 +259,15 @@ class RAGcoreKnowledgeProvider:
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
if not concepts:
|
||||
return GroundedAnswer(
|
||||
answer="",
|
||||
evidence_state="insufficient",
|
||||
sources=sources,
|
||||
provider=self.name,
|
||||
correlation_id=correlation_id,
|
||||
)
|
||||
|
||||
template = _LEAD_ANSWER_TEMPLATE.get(language, _LEAD_ANSWER_TEMPLATE[_DEFAULT_LANGUAGE])
|
||||
lead = sources[0]
|
||||
answer = template.format(title=lead.title, excerpt=lead.excerpt)
|
||||
|
||||
Reference in New Issue
Block a user