In-Memory Embeddings Search Plugin
Relevant source files
The following files were used as context for generating this wiki page:- plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py
- plugins/tool_search/in_mem_embeddings/README.md
- plugins/tool_search/in_mem_embeddings/pyproject.toml
- plugins/tool_search/in_mem_embeddings/tests/test_in_mem_embeddings_search.py
- plugins/tool_search/in_mem_embeddings/tests/test_integration.py
- plugins/tool_search/in_mem_embeddings/tests/test_plugin.py
- plugins/tool_search/in_mem_embeddings/tests/test_performance.py
- core/README.md
The In-Memory Embeddings Search Plugin provides a semantic search strategy for UTCP. It allows for finding tools based on the conceptual meaning of a search query, rather than simple keyword matching. This is achieved by converting tool metadata and search queries into numerical vector embeddings and calculating their similarity.
This plugin is designed for performance, featuring in-memory caching of tool embeddings to accelerate repeated searches. It integrates with the UTCP core system via Python's entry points mechanism, allowing for auto-discovery and registration. If high-quality embedding libraries are not installed, it gracefully falls back to a simpler, less accurate character-frequency-based method.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:1-17, plugins/tool_search/in_mem_embeddings/README.md
Installation and Dependencies
The plugin can be installed via pip. It has optional dependencies for enabling high-quality semantic embeddings.
# Base installation
pip install utcp-in-mem-embeddings
# Installation with high-quality embedding models
pip install "utcp-in-mem-embeddings[embedding]"
Sources: plugins/tool_search/in_mem_embeddings/README.md:5-9
Dependencies
| Package | Version | Required For | Description |
|---|---|---|---|
utcp | >=1.0 | Core | The core UTCP framework. |
numpy | >=2.3 | Core | Used for numerical operations on embeddings. |
sentence-transformers | >=2.2.0 | Optional (embedding) | Provides pre-trained models for high-quality text embeddings. |
torch | >=1.9.0 | Optional (embedding) | A dependency for sentence-transformers. |
Without the optional embedding dependencies, the plugin uses a fallback mechanism with reduced accuracy.
Sources: plugins/tool_search/in_mem_embeddings/pyproject.toml:10-18, plugins/tool_search/in_mem_embeddings/README.md:19-27
Architecture and Core Components
The plugin's functionality is encapsulated within the InMemEmbeddingsSearchStrategy class, which implements the ToolSearchStrategy interface from the UTCP core.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:19-21
InMemEmbeddingsSearchStrategy Class
This class manages the entire semantic search process, from loading embedding models to calculating similarity scores.
The following diagram illustrates the structure of the InMemEmbeddingsSearchStrategy class.
Configuration
The strategy is configured through Pydantic fields, which can be set when initializing the UtcpClient.
| Parameter | Type | Default | Description |
|---|---|---|---|
tool_search_strategy_type | Literal["in_mem_embeddings"] | "in_mem_embeddings" | The unique identifier for this search strategy. |
model_name | str | "all-MiniLM-L6-v2" | The sentence-transformer model to use. Can be any model from the Hugging Face library. |
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 threads for the ThreadPoolExecutor used 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:25-38
Search Process Flow
The search_tools method orchestrates the search. The process involves several steps, from fetching tools to ranking them by semantic similarity.
The diagram below shows the data flow for a search request.
Embedding Generation
High-Quality vs. Fallback Embeddings
The plugin dynamically chooses the embedding method based on installed dependencies.
-
High-Quality (Default): If
sentence-transformersis installed, the_get_text_embeddingmethod uses the specifiedmodel_nameto generate high-dimensional vector embeddings. This is done in aThreadPoolExecutorto avoid blocking the asyncio event loop.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:43-66, 79-93 -
Fallback: If
sentence-transformersis not found, the_simple_text_embeddingmethod is used. This creates a basic embedding by mapping character ordinals to a fixed-size vector. This method is less accurate but ensures the plugin remains functional.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:95-109, plugins/tool_search/in_mem_embeddings/tests/test_in_mem_embeddings_search.py:68-79
Tool Embedding
A tool's embedding is generated from a concatenated string of its name, description, and tags. This composite text is then passed to the embedding generation function.
# plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:115-116
# Create text representation of the tool
tool_text = f"{tool.name} {tool.description} {' '.join(tool.tags)}"
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:111-124
Caching
When cache_embeddings is True, generated tool embeddings are stored in a dictionary _tool_embeddings_cache with the tool name as the key. Subsequent searches for the same tool retrieve the embedding from this cache, avoiding the computational cost of regeneration.
Sources: plugins/tool_search/in_mem_embeddings/src/utcp_in_mem_embeddings/in_mem_embeddings_search.py:34, 112-124, plugins/tool_search/in_mem_embeddings/tests/test_in_mem_embeddings_search.py:101-114
Integration with UTCP Core
The plugin is designed to be automatically discovered by the UTCP core library.
Entry Point Registration
The pyproject.toml file defines an entry point under the utcp.plugins group. This tells the UTCP core that the in_mem_embeddings key maps to a registration function within this package.
# plugins/tool_search/in_mem_embeddings/pyproject.toml:29-30
[project.entry-points."utcp.plugins"]
in_mem_embeddings = "utcp_in_mem_embeddings:register"
When the UTCP client initializes, it scans for these entry points, calls the register function, and makes the InMemEmbeddingsSearchStrategy available for use in the client configuration.
Sources: plugins/tool_search/in_mem_embeddings/pyproject.toml:29-30, plugins/tool_search/in_mem_embeddings/tests/test_integration.py:47-52
Discovery and Instantiation
The following diagram shows how the UTCP client discovers and uses the plugin.
Testing
The plugin includes a comprehensive test suite to ensure correctness and performance.
- Unit Tests (
test_in_mem_embeddings_search.py): Verify individual components like cosine similarity calculation, the embedding fallback mechanism, and caching logic. - Integration Tests (
test_integration.py): Confirm that the plugin registers correctly with the UTCP core and that the search strategy can be instantiated and used through the standardToolSearchStrategyConfigSerializer. - Performance Tests (
test_performance.py): Measure the performance of "cold start" (model loading) and "warm cache" searches to ensure efficiency.
Sources: plugins/tool_search/in_mem_embeddings/tests/test_in_mem_embeddings_search.py, plugins/tool_search/in_mem_embeddings/tests/test_integration.py, plugins/tool_search/in_mem_embeddings/tests/test_performance.py
Summary
The In-Memory Embeddings Search Plugin is a powerful extension for UTCP that enables intelligent, meaning-based tool discovery. Its key strengths are its use of state-of-the-art sentence-transformer models, performance optimization through in-memory caching, and a robust fallback mechanism that ensures functionality even with a minimal installation. Its seamless integration via entry points makes it a simple but effective upgrade for any UTCP application requiring advanced tool search capabilities.