MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / route_preview

Function route_preview

uncommon_route/api_v2.py:56–117  ·  view source on GitHub ↗

Preview routing decision without sending the request.

(
    prompt: str,
    risk_tolerance: float = 0.5,
    system_prompt: str | None = None,
    step_index: int = 1,
    total_steps: int = 1,
)

Source from the content-addressed store, hash-verified

54
55
56def route_preview(
57 prompt: str,
58 risk_tolerance: float = 0.5,
59 system_prompt: str | None = None,
60 step_index: int = 1,
61 total_steps: int = 1,
62) -> dict[str, Any]:
63 """Preview routing decision without sending the request."""
64 global _sig_a, _sig_b, _sig_c
65 if _sig_a is None:
66 init_signals()
67
68 row = {
69 "messages": [{"role": "user", "content": prompt}],
70 "benchmark": "", "scenario": "general",
71 "step_index": step_index, "total_steps": total_steps,
72 }
73 if system_prompt:
74 row["messages"].insert(0, {"role": "system", "content": system_prompt})
75
76 vote_a = _sig_a.predict(row)
77 vote_b = _sig_b.predict(row)
78 vote_c = _sig_c.predict(row) if _sig_c else None
79
80 # 3-signal ensemble for preview with adaptive weights.
81 # MetadataSignal is constant for single prompts (always LOW 0.75) — low weight.
82 # Short prompts: structural features are unreliable, trust embedding semantics.
83 # Long prompts: structural features are informative, trust them more.
84 word_count = len(prompt.split())
85 if word_count <= 8:
86 w_a, w_b, w_c = 0.10, 0.20, 0.70
87 elif word_count <= 20:
88 w_a, w_b, w_c = 0.15, 0.35, 0.50
89 else:
90 w_a, w_b, w_c = 0.20, 0.45, 0.35
91
92 active_votes = [vote_a, vote_b]
93 active_weights = [w_a, w_b]
94 if vote_c and not vote_c.abstained:
95 active_votes.append(vote_c)
96 active_weights.append(w_c)
97
98 ensemble = Ensemble(weights=active_weights, risk_tolerance=risk_tolerance)
99 result = ensemble.decide(active_votes)
100 tier = result.tier_id if result.tier_id is not None else 1
101
102 signals = [
103 {"name": "metadata", "tier": vote_a.tier_id, "confidence": round(vote_a.confidence, 4)},
104 {"name": "structural", "tier": vote_b.tier_id, "confidence": round(vote_b.confidence, 4)},
105 ]
106 if vote_c:
107 signals.append({"name": "embedding", "tier": vote_c.tier_id, "confidence": round(vote_c.confidence, 4)})
108
109 return {
110 "tier": tier,
111 "tier_name": ID_TO_TIER.get(tier, "unknown"),
112 "confidence": round(result.confidence, 4),
113 "method": result.method,

Calls 6

decideMethod · 0.95
EnsembleClass · 0.90
init_signalsFunction · 0.85
predictMethod · 0.45
appendMethod · 0.45
getMethod · 0.45