light-infer-chat commited on
Commit
52e2d2c
·
1 Parent(s): f7c1a9c
app/api/v1/semantic_router.py CHANGED
@@ -34,6 +34,7 @@ async def route_query(
34
  models=result.get("models", []),
35
  error=result.get("error"),
36
  confidence=result.get("confidence"),
 
37
  threshold=result.get("threshold"),
38
  matched_utterance=result.get("matched_utterance"),
39
  )
 
34
  models=result.get("models", []),
35
  error=result.get("error"),
36
  confidence=result.get("confidence"),
37
+ margin=result.get("margin"),
38
  threshold=result.get("threshold"),
39
  matched_utterance=result.get("matched_utterance"),
40
  )
app/services/semantic_router_service.py CHANGED
@@ -1,6 +1,7 @@
1
  from __future__ import annotations
2
 
3
  import logging
 
4
  from typing import Any, Optional
5
 
6
  import numpy as np
@@ -57,17 +58,25 @@ class SemanticRouterService:
57
  best_idx = int(np.argmax(similarities))
58
  best_score = float(similarities[best_idx])
59
 
60
- sorted_scores = sorted(similarities, reverse=True)
61
- margin = (sorted_scores[0] - sorted_scores[1]) if len(sorted_scores) > 1 else 1.0
 
 
62
 
63
- if best_score < threshold or margin < 0.01:
 
 
 
 
 
 
64
  return {
65
  "success": True,
66
  "name": None,
67
  "models": [],
68
  "error": None,
69
  "confidence": best_score,
70
- "margin": margin,
71
  "threshold": threshold,
72
  "matched_utterance": all_utterances[best_idx],
73
  }
@@ -81,7 +90,7 @@ class SemanticRouterService:
81
  "models": matched_route.get("models", []),
82
  "error": None,
83
  "confidence": best_score,
84
- "margin": margin,
85
  "threshold": threshold,
86
  "matched_utterance": all_utterances[best_idx],
87
  }
 
1
  from __future__ import annotations
2
 
3
  import logging
4
+ from collections import defaultdict
5
  from typing import Any, Optional
6
 
7
  import numpy as np
 
58
  best_idx = int(np.argmax(similarities))
59
  best_score = float(similarities[best_idx])
60
 
61
+ # Route-level scoring: take the max score per route
62
+ route_scores: dict[int, list[float]] = defaultdict(list)
63
+ for i, score in enumerate(similarities):
64
+ route_scores[utterance_to_route[i]].append(float(score))
65
 
66
+ route_best_scores = {rid: max(scores) for rid, scores in route_scores.items()}
67
+ sorted_routes = sorted(route_best_scores.items(), key=lambda x: x[1], reverse=True)
68
+ best_route_score = sorted_routes[0][1]
69
+ second_best_route_score = sorted_routes[1][1] if len(sorted_routes) > 1 else 1.0
70
+ route_margin = best_route_score - second_best_route_score
71
+
72
+ if best_score < threshold or route_margin < 0.001:
73
  return {
74
  "success": True,
75
  "name": None,
76
  "models": [],
77
  "error": None,
78
  "confidence": best_score,
79
+ "margin": route_margin,
80
  "threshold": threshold,
81
  "matched_utterance": all_utterances[best_idx],
82
  }
 
90
  "models": matched_route.get("models", []),
91
  "error": None,
92
  "confidence": best_score,
93
+ "margin": route_margin,
94
  "threshold": threshold,
95
  "matched_utterance": all_utterances[best_idx],
96
  }