Spaces:
Sleeping
Sleeping
File size: 12,812 Bytes
ef31c8e bbea2be ef31c8e ddcb55f ef31c8e b282f5c ddcb55f ef31c8e ddcb55f b282f5c ddcb55f b282f5c ddcb55f ef31c8e ddcb55f ef31c8e ddcb55f ef31c8e ddcb55f ef31c8e ddcb55f ef31c8e bbea2be ef31c8e bbea2be ef31c8e ddcb55f bbea2be ddcb55f ef31c8e ddcb55f ef31c8e ddcb55f bbea2be ef31c8e ddcb55f | 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | """
Resolution Engine
-----------------
The central decision component that combines:
1. Intent Classification (top-K intents + scores from the cascade predictor)
2. Entity Extraction (from EntityExtractor)
3. Conversation Context (from ContextManager)
to produce a single :class:`ResolvedContext`.
Decision Logic
~~~~~~~~~~~~~~
::
score_diff = top1_score - top2_score
if score_diff >= AMBIGUITY_THRESHOLD:
→ DIRECT (high confidence, accept top intent as-is)
elif history_enabled AND context has matching entity/intent:
→ CONTEXT_RESOLVED (previous context disambiguates the question)
else:
→ AMBIGUOUS (ask the user a clarifying fallback question)
if model itself is unavailable / all scores near zero:
→ FALLBACK (graceful degradation)
Configurable Constant
~~~~~~~~~~~~~~~~~~~~~
``AMBIGUITY_THRESHOLD = 0.10``
Tune this on your validation data. Lower → fewer ambiguous triggers.
"""
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional
from .context_manager import ConversationContext
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Configurable threshold
# ---------------------------------------------------------------------------
AMBIGUITY_THRESHOLD: float = 0.15
"""
Score difference between top-1 and top-2 intent below which we treat the
classification as ambiguous. Raised from 0.10 to 0.15 to improve
Ambiguity Detection Rate (ADR).
Tune empirically against your validation set.
"""
# ---------------------------------------------------------------------------
# Data structures
# ---------------------------------------------------------------------------
class ResolutionType(str, Enum):
DIRECT = "DIRECT"
CONTEXT_RESOLVED = "CONTEXT_RESOLVED"
AMBIGUOUS = "AMBIGUOUS"
FALLBACK = "FALLBACK"
@dataclass
class UserInput:
"""Structured representation of the current user turn."""
question: str
entities: list[str]
keywords: list[str]
candidate_intents: list[dict] # [{"intent": str, "confidence": float}, ...]
context: ConversationContext
history_enabled: bool
@dataclass
class ResolvedContext:
"""Output of the Resolution Engine for a single turn."""
final_intent: str
entities: list[str]
keywords: list[str]
confidence: float
context_used: bool
previous_intents_used: list[str]
resolution_type: ResolutionType
fallback_options: Optional[list[tuple[str, str]]] = None # populated on AMBIGUOUS
# ---------------------------------------------------------------------------
# Engine
# ---------------------------------------------------------------------------
class ResolutionEngine:
"""
Combines intent candidates + entities + context into a :class:`ResolvedContext`.
Usage::
engine = ResolutionEngine()
resolved = engine.resolve(user_input)
"""
def __init__(
self,
threshold: float = AMBIGUITY_THRESHOLD,
absolute_threshold: float = 0.50,
ood_noise_floor: float = 0.08,
) -> None:
self.threshold = threshold
self.absolute_threshold = absolute_threshold
self.ood_noise_floor = ood_noise_floor
# ------------------------------------------------------------------
# Main entry point
# ------------------------------------------------------------------
def resolve(self, user_input: UserInput) -> ResolvedContext:
"""
Parameters
----------
user_input : UserInput
Current turn data including classifier candidates and context.
Returns
-------
ResolvedContext
The resolved intent, resolution type, and optional fallback options.
"""
candidates = user_input.candidate_intents
ctx = user_input.context
# ----- Graceful fallback: no candidates at all -----
if not candidates:
return ResolvedContext(
final_intent="unknown",
entities=user_input.entities,
keywords=user_input.keywords,
confidence=0.0,
context_used=False,
previous_intents_used=[],
resolution_type=ResolutionType.FALLBACK,
)
top1 = candidates[0]
top2 = candidates[1] if len(candidates) > 1 else None
top1_intent: str = top1["intent"]
top1_score: float = float(top1["confidence"])
top2_score: float = float(top2["confidence"]) if top2 else 0.0
score_diff = top1_score - top2_score
logger.debug(
"Intent resolution: top1=%s (%.3f), top2_score=%.3f, diff=%.3f, threshold=%.3f",
top1_intent, top1_score, top2_score, score_diff, self.threshold,
)
# 1. Complete out-of-domain noise check (e.g., non-DSA queries where all scores < 0.08)
if top1_score < self.ood_noise_floor:
logger.warning(
f"Top confidence {top1_score:.3f} is below noise floor {self.ood_noise_floor}. Triggering OOD fallback."
)
return ResolvedContext(
final_intent="out_of_scope",
entities=user_input.entities,
keywords=user_input.keywords,
confidence=top1_score,
context_used=False,
previous_intents_used=[],
resolution_type=ResolutionType.FALLBACK,
fallback_options=None,
)
# 2. Ambiguity check:
# Triggered when:
# - The top score difference is small (score_diff <= self.threshold) AND top2 is a viable candidate, OR
# - The top score is low (top1_score < self.absolute_threshold) AND top2 is close/competing (top2_score >= self.ood_noise_floor)
is_ambiguous = (
top2 is not None
and top2_score >= self.ood_noise_floor
and (score_diff <= self.threshold or (top1_score < self.absolute_threshold and score_diff <= 0.25))
)
if is_ambiguous:
# Try to resolve ambiguity using multi-turn conversation context
if user_input.history_enabled and ctx.history_enabled:
context_intent = self._try_context_resolution(user_input, top1_intent)
if context_intent is not None:
return ResolvedContext(
final_intent=context_intent,
entities=user_input.entities or list(ctx.recent_entities),
keywords=user_input.keywords,
confidence=top1_score,
context_used=True,
previous_intents_used=list(ctx.recent_intents),
resolution_type=ResolutionType.CONTEXT_RESOLVED,
)
# Context could not resolve: provide the top 2 candidate outputs with confidence scores
fallback_options = self._generate_fallback_options(
candidates=candidates,
keywords=user_input.keywords,
entities=user_input.entities,
context=ctx,
)
return ResolvedContext(
final_intent=top1_intent,
entities=user_input.entities,
keywords=user_input.keywords,
confidence=top1_score,
context_used=False,
previous_intents_used=[],
resolution_type=ResolutionType.AMBIGUOUS,
fallback_options=fallback_options,
)
# 3. Not ambiguous, but solitary top1 score is below absolute threshold
if top1_score < self.absolute_threshold:
logger.warning(
f"Top confidence {top1_score:.3f} is below threshold {self.absolute_threshold} with no viable second candidate."
)
return ResolvedContext(
final_intent="out_of_scope",
entities=user_input.entities,
keywords=user_input.keywords,
confidence=top1_score,
context_used=False,
previous_intents_used=[],
resolution_type=ResolutionType.FALLBACK,
fallback_options=None,
)
# 4. DIRECT — high confidence clear winner
return ResolvedContext(
final_intent=top1_intent,
entities=user_input.entities,
keywords=user_input.keywords,
confidence=top1_score,
context_used=False,
previous_intents_used=[],
resolution_type=ResolutionType.DIRECT,
)
# ------------------------------------------------------------------
# Internal helpers
# ------------------------------------------------------------------
def _try_context_resolution(
self, user_input: UserInput, top_intent: str
) -> Optional[str]:
"""
Returns the confirmed intent if the previous context provides enough
signal to resolve the ambiguity, otherwise ``None``.
Strategy (in priority order):
1. No current entities/keywords + context has entities → follow-up.
2. Current entities overlap with recent context entities.
3. Current keyword words overlap with active_topic / recent entities.
(Handles algorithm names classified as noun chunks, not NER entities,
e.g. "binary search" extracted as keyword, not as a named entity.)
"""
ctx = user_input.context
recent_entities = set(ctx.recent_entities)
current_entities = set(user_input.entities)
current_keywords = set(user_input.keywords)
# Build a set of significant words from the recent context
recent_keywords: set[str] = set()
if ctx.active_topic:
recent_keywords |= set(ctx.active_topic.lower().split())
recent_keywords |= recent_entities
# ── Rule 1: Follow-up (no new content, but context has entities)
if not current_entities and not current_keywords and recent_entities:
logger.debug(
"Context resolution (Rule 1 - follow-up, no new content): "
"confirming intent '%s' from context entity set %s",
top_intent, recent_entities,
)
return top_intent
# ── Rule 2: Named entity overlap
if current_entities & recent_entities:
logger.debug(
"Context resolution (Rule 2 - entity overlap %s): confirming '%s'.",
current_entities & recent_entities, top_intent,
)
return top_intent
# ── Rule 3: Keyword word-level overlap with active topic / recent entities
current_keyword_words: set[str] = set()
for kw in current_keywords:
current_keyword_words |= set(kw.lower().split())
if current_keyword_words & recent_keywords:
logger.debug(
"Context resolution (Rule 3 - keyword overlap %s): confirming '%s'.",
current_keyword_words & recent_keywords, top_intent,
)
return top_intent
return None
def _generate_fallback_options(
self,
candidates: list[dict],
keywords: list[str],
entities: list[str],
context: ConversationContext,
) -> list[tuple[str, str]]:
"""
Build human-readable clarifying options for the AMBIGUOUS case.
Format: ("<intent_label> → about <keyword_hint>", raw_intent)
"""
topic_hint = (
entities[0]
if entities
else (context.active_topic or (keywords[0] if keywords else "this topic"))
)
import re
options: list[tuple[str, str]] = []
for cand in candidates[:2]: # show top 2 outputs with confidence score levels
intent = cand["intent"]
score = cand["confidence"]
# Clean readable name without markdown syntax (**text**, `code`, etc.)
readable = intent.replace("_", " ").title()
clean_readable = re.sub(r"[\*\_`#\[\]]", "", readable).strip()
label = f"{clean_readable} ({score * 100:.1f}%)"
options.append((label, intent))
return options
|