Tool Search Strategies
Relevant source files
The following files were used as context for generating this wiki page:Tool Search Strategies are a pluggable component within the UTCP client responsible for implementing tool search and ranking algorithms. This system allows developers to choose or create different search methods, such as keyword matching or semantic similarity, to find the most relevant tools from a potentially large collection. The strategy is configured during the UtcpClient initialization and is used by the client's search_tools method.
The core of this system is the ToolSearchStrategy abstract base class, which defines a consistent contract for all search implementations. This ensures that any custom or built-in strategy can be seamlessly integrated into the client.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:1-12, README.md:21-24
Core Concepts
The search strategy architecture is designed for extensibility and is centered around a main interface and a configuration serializer.
ToolSearchStrategy Interface
The ToolSearchStrategy is an abstract base class that all search implementations must inherit from. It establishes the fundamental contract for how the UtcpClient interacts with the search mechanism.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:14-30
The following class diagram shows the relationship between the abstract interface and its concrete implementations.
search_tools Method
This is the primary abstract method that all strategies must implement. It executes a search against the tools in a ConcurrentToolRepository and returns a ranked list of relevant matches.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:33-55
| Parameter | Type | Description |
|---|---|---|
tool_repository | ConcurrentToolRepository | The tool repository to search within. |
query | str | The search query string. The format depends on the strategy (e.g., keywords, natural language). |
limit | int | The maximum number of tools to return. A value of 0 means no limit. |
any_of_tags_required | Optional[List[str]] | An optional list of tags. A tool must have at least one of these tags to be considered a match. |
Sources: core/src/utcp/interfaces/tool_search_strategy.py:39-47
The general flow of a tool search is shown in the sequence diagram below.
This diagram illustrates how the UtcpClient delegates the search operation to the configured ToolSearchStrategy, which retrieves tools from the repository and performs the ranking.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:33-55, core/tests/client/test_utcp_client.py:300-315
ToolSearchStrategyConfigSerializer
This serializer is responsible for converting ToolSearchStrategy configurations to and from dictionaries. This is essential for initializing the UtcpClient from a configuration file (e.g., config.json). It maintains a registry of available strategy implementations. The default strategy is tag_and_description_word_match.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:57-90
Implementations
UTCP provides several built-in search strategies.
TagAndDescriptionWordMatchStrategy
This is the default search strategy. It performs a keyword-based search by matching words from the query against the name, description, and tags of each tool. It is a simple and fast option suitable for many use cases.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:65, core/README.md:83
InMemEmbeddingsSearchStrategy
This is a more advanced semantic search strategy that uses sentence embeddings to find tools based on meaning similarity, not just keyword matches. It is provided as a separate plugin.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:1-12
Search Process
The strategy follows these steps to find relevant tools:
- Ensures the sentence-transformer model is loaded.
- Retrieves all tools from the tool repository.
- If
any_of_tags_requiredis provided, it filters the tools to include only those with at least one matching tag. - Generates a numerical embedding for the search query.
- For each tool, it generates an embedding from its name, description, and tags. These embeddings can be cached for performance.
- Calculates the cosine similarity between the query embedding and each tool embedding.
- Filters out tools with a similarity score below the configured
similarity_threshold. - Sorts the remaining tools by their similarity score in descending order.
- Returns the top results, respecting the
limit.
The following flowchart visualizes this process.
Configuration
This strategy offers several configuration options:
| Field | Type | Default | Description |
|---|---|---|---|
tool_search_strategy_type | str | "in_mem_embeddings" | The identifier for this strategy. |
model_name | str | "all-MiniLM-L6-v2" | The sentence-transformer model to use for generating embeddings. Any model from the Hugging Face sentence-transformers library is supported. |
similarity_threshold | float | 0.3 | The minimum cosine similarity score required for a tool to be considered a match. |
max_workers | int | 4 | The maximum number of worker threads for embedding generation. |
cache_embeddings | bool | true | If true, tool embeddings are cached in memory to improve performance on subsequent searches. |
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:26-42
If the sentence-transformers library is not installed, this strategy will fall back to a simple character-frequency-based embedding method as a basic text similarity measure.
Configuration and Usage
The tool search strategy is configured within the UtcpClientConfig object or a corresponding JSON configuration file when creating a UtcpClient instance.
Sources: README.md:120-123
Example: config.json
Here is an example of how to specify the search strategy in a config.json file.
{
"tool_repository": {
"tool_repository_type": "in_memory"
},
"tool_search_strategy": {
"tool_search_strategy_type": "tag_and_description_word_match"
}
}
Sources: README.md:120-126
To use the semantic search strategy, the configuration would be updated as follows:
{
"tool_repository": {
"tool_repository_type": "in_memory"
},
"tool_search_strategy": {
"tool_search_strategy_type": "in_mem_embeddings",
"model_name": "all-mpnet-base-v2",
"similarity_threshold": 0.5
}
}
Sources: plugins/tool_search/in_mem_embeddings/tests/test_integration.py:83-88
Extensibility
The system is designed to be extensible, allowing developers to create and integrate custom search strategies.
To create a new search strategy:
- Create a new class that inherits from
utcp.interfaces.tool_search_strategy.ToolSearchStrategy. - Implement the
search_toolsabstract method with the custom search logic. - Create a serializer class that inherits from
utcp.interfaces.serializer.Serializer. - Register the new strategy's serializer in the
ToolSearchStrategyConfigSerializer.tool_search_strategy_implementationsdictionary. This makes the strategy discoverable by the core system and allows it to be configured via dictionary or JSON file.
Sources: core/src/utcp/interfaces/tool_search_strategy.py:14, 64, plugins/tool_search/in_mem_embeddings/tests/test_integration.py:46-49