You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/middleware.md
+120Lines changed: 120 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -801,6 +801,126 @@ agent = TinyMultiStepAgent(
801
801
802
802
---
803
803
804
+
### TinyVectorToolSelectorMiddleware
805
+
806
+
Selects the most relevant tools for each LLM call using semantic similarity between the user query and tool descriptions. No secondary LLM call required — selection is done purely via vector embeddings and cosine similarity.
807
+
808
+
**Features:**
809
+
- Uses an embedder to compute cosine similarity between the query and each tool description
810
+
- Ranks tools by similarity and selects the top candidates
811
+
- Supports always-include list for critical tools
812
+
- Configurable maximum tools limit and minimum similarity threshold
813
+
- Customizable query and tool transform functions for fine-grained embedding control
814
+
815
+
**How It Works:**
816
+
1. Before each LLM call, the last `TinyHumanMessage` is embedded as the query
817
+
2. Each tool's name and description is embedded
818
+
3. Cosine similarity is computed between the query and every tool embedding
819
+
4. Tools are ranked by similarity; only those above `similarity_threshold` (up to `max_tools`) are passed to the main agent
820
+
821
+
**Basic Usage:**
822
+
823
+
```python
824
+
from tinygent.agents.middleware import TinyVectorToolSelectorMiddleware
825
+
from tinygent.agents import TinyMultiStepAgent
826
+
from tinygent.core.factory import build_embedder, build_llm
Transform functions and the similarity threshold can also be set directly on the config:
879
+
880
+
```python
881
+
from tinygent.agents.middleware import TinyVectorToolSelectorMiddlewareConfig
882
+
883
+
config = TinyVectorToolSelectorMiddlewareConfig(
884
+
embedder='openai:text-embedding-3-small',
885
+
similarity_threshold=0.5,
886
+
max_tools=5,
887
+
always_include=['search'],
888
+
query_transform_fn=query_transform,
889
+
tool_transform_fn=tool_transform,
890
+
)
891
+
892
+
selector = config.build()
893
+
```
894
+
895
+
**Factory Configuration Options:**
896
+
897
+
| Field | Type | Default | Description |
898
+
|-------|------|---------|-------------|
899
+
|`type`|`Literal['vector_tool_classifier']`|`'vector_tool_classifier'`| Type identifier (frozen) |
900
+
|`embedder`|`AbstractEmbedderConfig \| AbstractEmbedder`| Required | Embedder used to compute similarity. Can be a string like `'openai:text-embedding-3-small'` or an embedder instance |
901
+
|`similarity_threshold`|`float \| None`|`None`| Minimum cosine similarity score for a tool to be selected. `None` = no threshold |
902
+
|`max_tools`|`int \| None`|`None`| Maximum number of tools to select. `None` = no limit |
903
+
|`always_include`|`list[str] \| None`|`None`| List of tool names to always include regardless of similarity score |
904
+
|`query_transform_fn`|`Callable[[TinyLLMInput], str] \| None`|`None`| Custom function to extract the query string from the LLM input. Defaults to last `TinyHumanMessage` found |
905
+
|`tool_transform_fn`|`Callable[[AbstractTool], str] \| None`|`None`| Custom function to produce the text embedded for each tool. Defaults to `"name - description"`|
0 commit comments