architecture.md 6.51 KB

Architecture

Overview

intent-fusion uses a dual-channel parallel architecture to classify user intents. Two independent channels run concurrently, and their results are merged into a single scored candidate list with a three-state verdict.

                         ┌──────────────────────┐
                         │    User Message       │
                         └──────────┬───────────┘
                                    │
                    ┌───────────────┴───────────────┐
                    │                               │
            ┌───────▼───────┐              ┌────────▼────────┐
            │   Embedding   │              │   Intent Tree   │
            │   Channel     │              │   Channel       │
            │               │              │                 │
            │ bge-small-*   │              │ LLM + CoT       │
            │ cosine sim    │              │ hierarchical    │
            │ top-k recall  │              │ reasoning       │
            └───────┬───────┘              └────────┬────────┘
                    │                               │
                    └───────────────┬───────────────┘
                                    │
                         ┌──────────▼───────────┐
                         │   Merge & Score      │
                         │                      │
                         │ final = α·emb +      │
                         │        (1-α)·tree    │
                         └──────────┬───────────┘
                                    │
                         ┌──────────▼───────────┐
                         │  Three-state Decide  │
                         │                      │
                         │ CLEAR / AMBIGUOUS /  │
                         │ LOW                  │
                         └──────────────────────┘

Channel 1: Embedding Recall

Purpose: Fast lexical/surface similarity matching.

Uses a sentence-transformer model (default: BAAI/bge-small-en-v1.5) to encode both the user message and each intent's embed_text into dense vectors. Cosine similarity (via normalized dot product) produces a ranked candidate list.

Strengths:

  • Very fast (5-15ms for ~100 intents)
  • Excellent for direct keyword matches ("cancel order" → cancel_order)
  • No LLM call needed

Weaknesses:

  • Fails on indirect phrasing ("I changed my mind" → should be cancel_order)
  • Cannot reason about intent semantics

Channel 2: Intent Tree Reasoning

Purpose: Deep semantic inference via LLM chain-of-thought.

Constructs a domain-grouped intent tree from all IntentEntry definitions, then sends a single LLM call with structured reasoning instructions:

── Order Management ──
  query_order_status: user wants to know where their order is
  cancel_order: user wants to cancel an order they placed
── Returns & Refunds ──
  initiate_return: user wants to start a return process
  ...

The LLM reasons step-by-step inside <think> tags, then outputs a JSON array of 3 candidates with scores.

Strengths:

  • Excels at indirect/metaphorical phrasing
  • Can disambiguate semantically similar intents using domain context
  • Handles the "report domain" cases that pure embedding consistently fails

Weaknesses:

  • Depends on LLM availability and latency (200-800ms typical)
  • Score calibration varies across LLM providers

Fusion Formula

For each candidate that appears in either channel's output:

final_score = α × emb_score + (1 - α) × tree_score

Where:

  • α (alpha) controls the embedding weight. Default: 0.10 (tree-dominant).
  • Candidates in both channels naturally score higher (cross-channel agreement).
  • Candidates in only one channel get a partial score.
Scenario Formula
Both channels α × emb + (1-α) × tree
Embedding only α × emb
Tree only (1-α) × tree

Why α = 0.10? Calibrated via grid search on 104 labeled production cases. The tree channel contributes much stronger signal for ambiguous queries, while embedding mainly serves as a recall safety net.

Three-state Verdict

After scoring, the decision layer produces one of three verdicts:

Verdict Condition Recommended Action
CLEAR top.score >= 0.20 AND gap >= δ Route directly to top.name
AMBIGUOUS top.score >= 0.20 AND gap < δ Execute top, surface runner-up as suggestion
LOW top.score < 0.20 Fall back to free chat / search

Where δ (delta) is the gap threshold between top-1 and top-2 scores. Default: 0.10.

The three-state design acknowledges uncertainty rather than forcing a binary choice, enabling downstream systems to handle each case appropriately.

Single-channel Degradation

If one channel fails (LLM timeout, embedding model error), the other channel still produces a result:

  • Tree fails → embedding only: α forced to 1.0
  • Embedding fails → tree only: α forced to 0.0
  • Both fail: verdict = LOW

The FusionResult.degraded flag and active_channels list let callers know when degradation occurred.

The embed_text vs tree_text Design

Each IntentEntry has two independent text fields optimized for different channels:

IntentEntry(
    name="query_order_status",
    domain="Order Management",
    embed_text="track order, where is my package, shipping status, delivery ETA",
    tree_text="user wants to know where their order is, current shipping status, or estimated delivery time",
)
  • embed_text: Keyword-dense, synonym-rich. Optimized for vector similarity.
  • tree_text: Descriptive, boundary-clarifying. Optimized for LLM reasoning. Should include disambiguation hints (e.g., "unlike X, this intent covers Y").

This separation is a key insight from production: a single description cannot simultaneously optimize for both lexical matching and semantic reasoning.