Skip to content

Commit 49c8eec

Browse files
committed
refactor: extract DEFAULT_HYBRID_ALPHA to constants
Move magic number 0.2 to a named constant in stackone_ai/constants.py to improve code maintainability and documentation. Changes: - Add DEFAULT_HYBRID_ALPHA constant with detailed documentation - Update ToolIndex.__init__() to use the constant - Update Tools.meta_tools() to use the constant - Document the rationale: 10.8% accuracy improvement, validation tested This makes the hybrid search configuration more discoverable and easier to maintain across the codebase. Matches constant extraction done in Node.js SDK (stackone-ai-node#136).
1 parent 9a921e3 commit 49c8eec

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

stackone_ai/constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33

44
# Use bundled specs directly
55
OAS_DIR = Path(str(importlib.resources.files("stackone_ai") / "oas"))
6+
7+
# Hybrid search default weight for BM25 vs TF-IDF
8+
# alpha=0.2 means: 20% BM25 + 80% TF-IDF
9+
# This value was optimized through validation testing and provides
10+
# 10.8% improvement in tool discovery accuracy
11+
DEFAULT_HYBRID_ALPHA: float = 0.2

stackone_ai/meta_tools.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
from pydantic import BaseModel
1111

12+
from stackone_ai.constants import DEFAULT_HYBRID_ALPHA
1213
from stackone_ai.models import ExecuteConfig, JsonDict, StackOneTool, ToolParameters
1314
from stackone_ai.utils.tfidf_index import TfidfDocument, TfidfIndex
1415

@@ -27,20 +28,23 @@ class MetaToolSearchResult(BaseModel):
2728
class ToolIndex:
2829
"""Hybrid BM25 + TF-IDF tool search index"""
2930

30-
DEFAULT_HYBRID_ALPHA: float = 0.2
31-
32-
def __init__(self, tools: list[StackOneTool], hybrid_alpha: float = DEFAULT_HYBRID_ALPHA) -> None:
31+
def __init__(
32+
self, tools: list[StackOneTool], hybrid_alpha: float | None = None
33+
) -> None:
3334
"""Initialize tool index with hybrid search
3435
3536
Args:
3637
tools: List of tools to index
37-
hybrid_alpha: Weight for BM25 in hybrid search (0-1). Default 0.2 gives
38-
more weight to BM25 scoring, which has been shown to provide better
39-
tool discovery accuracy (10.8% improvement in validation testing).
38+
hybrid_alpha: Weight for BM25 in hybrid search (0-1). If not provided,
39+
uses DEFAULT_HYBRID_ALPHA (0.2), which gives more weight to BM25 scoring
40+
and has been shown to provide better tool discovery accuracy
41+
(10.8% improvement in validation testing).
4042
"""
4143
self.tools = tools
4244
self.tool_map = {tool.name: tool for tool in tools}
43-
self.hybrid_alpha = max(0.0, min(1.0, hybrid_alpha))
45+
# Use default if not provided, then clamp to [0, 1]
46+
alpha = hybrid_alpha if hybrid_alpha is not None else DEFAULT_HYBRID_ALPHA
47+
self.hybrid_alpha = max(0.0, min(1.0, alpha))
4448

4549
# Prepare corpus for both BM25 and TF-IDF
4650
corpus = []

stackone_ai/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,16 @@ def to_langchain(self) -> Sequence[BaseTool]:
532532
"""
533533
return [tool.to_langchain() for tool in self.tools]
534534

535-
def meta_tools(self, hybrid_alpha: float = 0.2) -> Tools:
535+
def meta_tools(self, hybrid_alpha: float | None = None) -> Tools:
536536
"""Return meta tools for tool discovery and execution
537537
538538
Meta tools enable dynamic tool discovery and execution based on natural language queries
539539
using hybrid BM25 + TF-IDF search.
540540
541541
Args:
542-
hybrid_alpha: Weight for BM25 in hybrid search (0-1). Default 0.2 gives more weight
543-
to BM25 scoring, which has been shown to provide better tool discovery accuracy
542+
hybrid_alpha: Weight for BM25 in hybrid search (0-1). If not provided, uses
543+
ToolIndex.DEFAULT_HYBRID_ALPHA (0.2), which gives more weight to BM25 scoring
544+
and has been shown to provide better tool discovery accuracy
544545
(10.8% improvement in validation testing).
545546
546547
Returns:

0 commit comments

Comments
 (0)