Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

feat(autoroute): prompt for semantic keywords per tier in configure wizard (#33508)

devin-ai-integration[bot]5a0e1dd1dd25
diff --git a/litellm/proxy/client/cli/commands/autoroute/config.py b/litellm/proxy/client/cli/commands/autoroute/config.py
index fe2c4e467e7..2d760ef0f8a 100644
--- a/litellm/proxy/client/cli/commands/autoroute/config.py
+++ b/litellm/proxy/client/cli/commands/autoroute/config.py
@@ -80,24 +80,32 @@ class NoSemanticMatching(BaseModel):
kind: Literal["none"] = "none"
+class KeywordTierRule(BaseModel):
+ model_config = ConfigDict(frozen=True)
+ keywords: tuple[str, ...]
+ tier: str
+
+
+# Satisfies complexity_router's "semantic matching requires non-empty keyword_tier_rules"
+# invariant with a sane starting point; the wizard lets the user override these per tier.
+DEFAULT_KEYWORD_TIER_RULES: tuple[KeywordTierRule, ...] = (
+ KeywordTierRule(keywords=("hi", "hello", "thanks"), tier="SIMPLE"),
+ KeywordTierRule(keywords=("explain", "how does"), tier="MEDIUM"),
+ KeywordTierRule(keywords=("refactor", "implement", "debug"), tier="COMPLEX"),
+ KeywordTierRule(keywords=("step by step", "think through", "prove"), tier="REASONING"),
+)
+
+
class SemanticMatching(BaseModel):
model_config = ConfigDict(frozen=True)
kind: Literal["semantic"] = "semantic"
embedding_model: str
match_threshold: float = 0.5
+ keyword_tier_rules: tuple[KeywordTierRule, ...] = DEFAULT_KEYWORD_TIER_RULES
SemanticMatchingChoice = Union[NoSemanticMatching, SemanticMatching]
-# Satisfies complexity_router's "semantic matching requires non-empty keyword_tier_rules"
-# invariant with a sane starting point; the generated config.yaml can be hand-edited afterward.
-_DEFAULT_KEYWORD_TIER_RULES: tuple[dict[str, JsonValue], ...] = (
- {"keywords": ["hi", "hello", "thanks"], "tier": "SIMPLE"},
- {"keywords": ["explain", "how does"], "tier": "MEDIUM"},
- {"keywords": ["refactor", "implement", "debug"], "tier": "COMPLEX"},
- {"keywords": ["step by step", "think through", "prove"], "tier": "REASONING"},
-)
-
class AutorouteConfig(BaseModel):
model_config = ConfigDict(frozen=True)
@@ -181,7 +189,9 @@ def build_generated_model_list(config: AutorouteConfig) -> list[JsonValue]:
complexity_router_config["semantic_keyword_matching"] = True
complexity_router_config["embedding_model"] = config.semantic_matching.embedding_model
complexity_router_config["match_threshold"] = config.semantic_matching.match_threshold
- complexity_router_config["keyword_tier_rules"] = list(_DEFAULT_KEYWORD_TIER_RULES)
+ complexity_router_config["keyword_tier_rules"] = [
+ {"keywords": list(rule.keywords), "tier": rule.tier} for rule in config.semantic_matching.keyword_tier_rules
+ ]
if config.adaptive:
complexity_router_config["adaptive"] = True
@@ -222,8 +232,10 @@ __all__ = [
"AutorouteConfig",
"ClassifierChoice",
"ConfigGenerationError",
+ "DEFAULT_KEYWORD_TIER_RULES",
"DiscoveredModel",
"HeuristicClassifier",
+ "KeywordTierRule",
"LLMClassifier",
"NoSemanticMatching",
"SemanticMatching",
diff --git a/litellm/proxy/client/cli/commands/autoroute/wizard.py b/litellm/proxy/client/cli/commands/autoroute/wizard.py
index 89b53d8f9fd..60696fb2e7e 100644
--- a/litellm/proxy/client/cli/commands/autoroute/wizard.py
+++ b/litellm/proxy/client/cli/commands/autoroute/wizard.py
@@ -8,11 +8,13 @@ from InquirerPy.base.control import Choice
from .... import Client
from .config import (
+ DEFAULT_KEYWORD_TIER_RULES,
TIER_NAMES,
AutorouteConfig,
ConfigGenerationError,
DiscoveredModel,
HeuristicClassifier,
+ KeywordTierRule,
LLMClassifier,
NoSemanticMatching,
SemanticMatching,
@@ -63,6 +65,25 @@ def _render_and_prompt_for_models(models: tuple[DiscoveredModel, ...], prompt_la
return tuple(_fuzzy_pick(models, prompt_label, multiselect=True))
+def _parse_keywords(raw: str) -> tuple[str, ...]:
+ return tuple(keyword.strip() for keyword in raw.split(",") if keyword.strip())
+
+
+def _prompt_for_keyword_tier_rules() -> tuple[KeywordTierRule, ...]:
+ """Let the user supply the semantic-matching keywords per tier, since matching those
+ keywords against the request is the whole point of enabling it. Each prompt is prefilled
+ with the built-in default, so pressing enter keeps it."""
+ click.echo("\nEnter example keywords/phrases per tier (comma-separated); press enter to keep the default:")
+ defaults = {rule.tier: rule.keywords for rule in DEFAULT_KEYWORD_TIER_RULES}
+
+ def _rule_for(tier: str) -> KeywordTierRule:
+ default_keywords = defaults.get(tier, ())
+ raw = click.prompt(f" {tier} keywords", default=", ".join(default_keywords), show_default=True)
+ return KeywordTierRule(keywords=_parse_keywords(raw) or default_keywords, tier=tier)
+
+ return tuple(_rule_for(tier) for tier in TIER_NAMES)
+
+
def run_configure_wizard(ctx: click.Context) -> Path:
"""Discover the caller's accessible models, walk them through tier assignment, write config."""
base_url = ctx.obj["base_url"]
@@ -96,7 +117,8 @@ def run_configure_wizard(ctx: click.Context) -> Path:
semantic_matching = NoSemanticMatching()
if embedding_pool and click.confirm("\nEnable semantic keyword matching?", default=False):
embedding_model = _render_and_prompt_for_model(embedding_pool, "semantic embeddings")
- semantic_matching = SemanticMatching(embedding_model=embedding_model)
+ keyword_tier_rules = _prompt_for_keyword_tier_rules()
+ semantic_matching = SemanticMatching(embedding_model=embedding_model, keyword_tier_rules=keyword_tier_rules)
adaptive = click.confirm("\nEnable adaptive (bandit) selection on top of tiering?", default=False)
diff --git a/tests/test_litellm/proxy/client/cli/autoroute/test_config.py b/tests/test_litellm/proxy/client/cli/autoroute/test_config.py
index 9fa01524ef3..f8d82476ef0 100644
--- a/tests/test_litellm/proxy/client/cli/autoroute/test_config.py
+++ b/tests/test_litellm/proxy/client/cli/autoroute/test_config.py
@@ -3,10 +3,12 @@ from typing import Any, Dict, Tuple
import pytest
from litellm.proxy.client.cli.commands.autoroute.config import (
+ DEFAULT_KEYWORD_TIER_RULES,
AutorouteConfig,
ConfigGenerationError,
DiscoveredModel,
HeuristicClassifier,
+ KeywordTierRule,
LLMClassifier,
NoSemanticMatching,
SemanticMatching,
@@ -129,6 +131,31 @@ class TestBuildGeneratedModelList:
assert router_config["keyword_tier_rules"]
assert "classifier_type" not in router_config
+ def test_semantic_matching_defaults_emit_builtin_keyword_rules(self):
+ config = _base_config(semantic_matching=SemanticMatching(embedding_model="text-embedding-3-small"))
+ autorouter = next(m for m in build_generated_model_list(config) if m["model_name"] == "autorouter")
+ router_config = autorouter["litellm_params"]["complexity_router_config"]
+ assert router_config["keyword_tier_rules"] == [
+ {"keywords": list(rule.keywords), "tier": rule.tier} for rule in DEFAULT_KEYWORD_TIER_RULES
+ ]
+
+ def test_semantic_matching_serializes_custom_keyword_rules(self):
+ config = _base_config(
+ semantic_matching=SemanticMatching(
+ embedding_model="text-embedding-3-small",
+ keyword_tier_rules=(
+ KeywordTierRule(keywords=("yo", "sup"), tier="SIMPLE"),
+ KeywordTierRule(keywords=("architect", "design a system"), tier="COMPLEX"),
+ ),
+ )
+ )
+ autorouter = next(m for m in build_generated_model_list(config) if m["model_name"] == "autorouter")
+ router_config = autorouter["litellm_params"]["complexity_router_config"]
+ assert router_config["keyword_tier_rules"] == [
+ {"keywords": ["yo", "sup"], "tier": "SIMPLE"},
+ {"keywords": ["architect", "design a system"], "tier": "COMPLEX"},
+ ]
+
def test_complexity_router_config_reflects_adaptive(self):
config = _base_config(adaptive=True)
autorouter = next(m for m in build_generated_model_list(config) if m["model_name"] == "autorouter")
diff --git a/tests/test_litellm/proxy/client/cli/autoroute/test_wizard.py b/tests/test_litellm/proxy/client/cli/autoroute/test_wizard.py
index 75431a2c588..2b9240aafc7 100644
--- a/tests/test_litellm/proxy/client/cli/autoroute/test_wizard.py
+++ b/tests/test_litellm/proxy/client/cli/autoroute/test_wizard.py
@@ -156,7 +156,7 @@ class TestRunConfigureWizardSemanticMatching:
tmp_path,
CHAT_AND_EMBEDDING_GROUPS,
_SIMPLE_TIER_PICKS,
- input_str="n\ny\nn\n",
+ input_str="n\ny\n\n\n\n\nn\n",
embedding_pick="text-embedding-3-small",
)
@@ -165,6 +165,42 @@ class TestRunConfigureWizardSemanticMatching:
assert router_config["semantic_keyword_matching"] is True
assert router_config["embedding_model"] == "text-embedding-3-small"
+ def test_blank_keyword_answers_keep_the_builtin_defaults(self, tmp_path):
+ result, config_path = _run(
+ tmp_path,
+ CHAT_AND_EMBEDDING_GROUPS,
+ _SIMPLE_TIER_PICKS,
+ input_str="n\ny\n\n\n\n\nn\n",
+ embedding_pick="text-embedding-3-small",
+ )
+
+ assert result.exit_code == 0, result.output
+ router_config = _router_config(config_path)
+ assert router_config["keyword_tier_rules"] == [
+ {"keywords": ["hi", "hello", "thanks"], "tier": "SIMPLE"},
+ {"keywords": ["explain", "how does"], "tier": "MEDIUM"},
+ {"keywords": ["refactor", "implement", "debug"], "tier": "COMPLEX"},
+ {"keywords": ["step by step", "think through", "prove"], "tier": "REASONING"},
+ ]
+
+ def test_custom_keyword_answers_are_recorded_per_tier(self, tmp_path):
+ result, config_path = _run(
+ tmp_path,
+ CHAT_AND_EMBEDDING_GROUPS,
+ _SIMPLE_TIER_PICKS,
+ input_str="n\ny\nyo, sup\n\nbuild a service, migrate\nderive, prove rigorously\nn\n",
+ embedding_pick="text-embedding-3-small",
+ )
+
+ assert result.exit_code == 0, result.output
+ router_config = _router_config(config_path)
+ assert router_config["keyword_tier_rules"] == [
+ {"keywords": ["yo", "sup"], "tier": "SIMPLE"},
+ {"keywords": ["explain", "how does"], "tier": "MEDIUM"},
+ {"keywords": ["build a service", "migrate"], "tier": "COMPLEX"},
+ {"keywords": ["derive", "prove rigorously"], "tier": "REASONING"},
+ ]
+
class TestRunConfigureWizardAdaptive:
def test_accepting_adaptive_sets_adaptive_flag(self, tmp_path):