calibration.md 3.69 KB

Calibration Guide

Why calibrate?

The two key parameters — alpha (embedding weight) and delta (CLEAR/AMBIGUOUS gap threshold) — directly affect routing accuracy. Their optimal values depend on:

  • Your intent set size and similarity structure
  • Your embedding model's quality for your domain
  • Your LLM's scoring calibration
  • Your tolerance for AMBIGUOUS vs misrouted CLEAR verdicts

Quick start

# 1. Prepare labeled cases (see format below)
# 2. Run grid search
python -m intent_fusion.calibration \
    --cases your_cases.yaml \
    --intents your_intents.yaml \
    --output calibration_results.csv

# Output:
# Best: alpha=0.10, delta=0.10 → accuracy=0.971 (101/104)

Labeled case format

cases:
  - message: "where is my order"
    expected: "query_order_status"

  - message: "I changed my mind"
    expected: "cancel_order"
    note: "no 'cancel' keyword — tests semantic reasoning"

  - message: "hello"
    expected: "free_chat"

  - message: "how do I apply a coupon"
    expected: "guide_search"
    note: "how-to question, not a data query"

Expected values for fallback cases

  • Use "free_chat" for greetings, off-topic, and too-vague messages
  • Use "guide_search" for how-to / navigation questions
  • Use "orchestrate" for multi-action compound messages

These special targets are matched when the verdict is LOW.

Understanding the grid search

The calibration tool tests every combination of alpha and delta values:

Parameter Default search range What it controls
alpha 0.00, 0.05, 0.10, 0.20, 0.30, 0.50 Embedding vs tree weight
delta 0.05, 0.10, 0.15, 0.20, 0.25 CLEAR vs AMBIGUOUS boundary

For each (alpha, delta) pair, it runs all cases and reports:

  • accuracy: fraction of cases where top matches expected
  • ambiguous_count: number of AMBIGUOUS verdicts (neither good nor bad)
  • low_count: number of LOW verdicts (potential misses)

The "best" configuration maximizes accuracy, with ties broken by fewer AMBIGUOUS verdicts.

Interpreting results

Open the CSV output to understand the parameter landscape:

alpha,delta,accuracy,correct,total,ambiguous_count,low_count
0.00,0.05,0.923,96,104,12,3
0.10,0.10,0.971,101,104,5,3
0.20,0.10,0.952,99,104,7,3
0.50,0.10,0.894,93,104,8,5

Common patterns:

  • alpha too high (>0.3): Embedding dominates, indirect phrasing fails
  • alpha too low (0.0): Embedding provides no recall safety net
  • delta too low (<0.05): Too many CLEAR verdicts, some misrouted
  • delta too high (>0.20): Too many AMBIGUOUS verdicts, user gets asked to clarify unnecessarily

Custom search ranges

from intent_fusion.calibration import run_grid_search

best = await run_grid_search(
    cases,
    entries,
    llm,
    alpha_values=[0.05, 0.08, 0.10, 0.12, 0.15],  # fine-grained around 0.10
    delta_values=[0.08, 0.10, 0.12, 0.15],
    output_csv="fine_calibration.csv",
)

Tips for building a good case set

  1. Cover all intents — at least 2-3 cases per intent
  2. Include hard cases — indirect phrasing, slang, metaphorical language
  3. Include boundary cases — messages that could plausibly match 2+ intents
  4. Include fallbacks — greetings, off-topic, how-to questions
  5. Weight toward production patterns — if 40% of real messages are order queries, your case set should reflect that

A good starting point is 5-10 cases per intent, plus 10-15 fallback cases.

Re-calibration triggers

Re-run calibration when you:

  • Add or remove intents
  • Significantly rewrite embed_text or tree_text
  • Switch embedding models
  • Switch LLM providers
  • Observe accuracy degradation in production logs