|
| 1 | +"""In-memory embedding-based semantic search strategy for UTCP tools. |
| 2 | +
|
| 3 | +This module provides a semantic search implementation that uses sentence embeddings |
| 4 | +to find tools based on meaning similarity rather than just keyword matching. |
| 5 | +Embeddings are cached in memory for improved performance. |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import logging |
| 10 | +from typing import List, Tuple, Optional, Literal, Dict, Any |
| 11 | +from concurrent.futures import ThreadPoolExecutor |
| 12 | +import numpy as np |
| 13 | +from pydantic import BaseModel, Field, PrivateAttr |
| 14 | + |
| 15 | +from utcp.interfaces.tool_search_strategy import ToolSearchStrategy |
| 16 | +from utcp.data.tool import Tool |
| 17 | +from utcp.interfaces.concurrent_tool_repository import ConcurrentToolRepository |
| 18 | +from utcp.interfaces.serializer import Serializer |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +class InMemEmbeddingsSearchStrategy(ToolSearchStrategy): |
| 23 | + """In-memory semantic search strategy using sentence embeddings. |
| 24 | + |
| 25 | + This strategy converts tool descriptions and search queries into numerical |
| 26 | + embeddings and finds the most semantically similar tools using cosine similarity. |
| 27 | + Embeddings are cached in memory for improved performance during repeated searches. |
| 28 | + """ |
| 29 | + |
| 30 | + tool_search_strategy_type: Literal["in_mem_embeddings"] = "in_mem_embeddings" |
| 31 | + |
| 32 | + # Configuration parameters |
| 33 | + model_name: str = Field( |
| 34 | + default="all-MiniLM-L6-v2", |
| 35 | + description="Sentence transformer model name to use for embeddings. " |
| 36 | + "Accepts any model from Hugging Face sentence-transformers library. " |
| 37 | + "Popular options: 'all-MiniLM-L6-v2' (fast, good quality), " |
| 38 | + "'all-mpnet-base-v2' (slower, higher quality), " |
| 39 | + "'paraphrase-MiniLM-L6-v2' (paraphrase detection). " |
| 40 | + "See https://huggingface.co/sentence-transformers for full list." |
| 41 | + ) |
| 42 | + similarity_threshold: float = Field(default=0.3, description="Minimum similarity score to consider a match") |
| 43 | + max_workers: int = Field(default=4, description="Maximum number of worker threads for embedding generation") |
| 44 | + cache_embeddings: bool = Field(default=True, description="Whether to cache tool embeddings for performance") |
| 45 | + |
| 46 | + # Private attributes |
| 47 | + _embedding_model: Optional[Any] = PrivateAttr(default=None) |
| 48 | + _tool_embeddings_cache: Dict[str, np.ndarray] = PrivateAttr(default_factory=dict) |
| 49 | + _executor: Optional[ThreadPoolExecutor] = PrivateAttr(default=None) |
| 50 | + _model_loaded: bool = PrivateAttr(default=False) |
| 51 | + |
| 52 | + def __init__(self, **data): |
| 53 | + super().__init__(**data) |
| 54 | + self._executor = ThreadPoolExecutor(max_workers=self.max_workers) |
| 55 | + |
| 56 | + async def _ensure_model_loaded(self): |
| 57 | + """Ensure the embedding model is loaded.""" |
| 58 | + if self._model_loaded: |
| 59 | + return |
| 60 | + |
| 61 | + try: |
| 62 | + # Import sentence-transformers here to avoid dependency issues |
| 63 | + from sentence_transformers import SentenceTransformer |
| 64 | + |
| 65 | + # Load the model in a thread to avoid blocking |
| 66 | + loop = asyncio.get_running_loop() |
| 67 | + self._embedding_model = await loop.run_in_executor( |
| 68 | + self._executor, |
| 69 | + SentenceTransformer, |
| 70 | + self.model_name |
| 71 | + ) |
| 72 | + self._model_loaded = True |
| 73 | + logger.info(f"Loaded embedding model: {self.model_name}") |
| 74 | + |
| 75 | + except ImportError: |
| 76 | + logger.warning("sentence-transformers not available, falling back to simple text similarity") |
| 77 | + self._embedding_model = None |
| 78 | + self._model_loaded = True |
| 79 | + except Exception as e: |
| 80 | + logger.error(f"Failed to load embedding model: {e}") |
| 81 | + self._embedding_model = None |
| 82 | + self._model_loaded = True |
| 83 | + |
| 84 | + async def _get_text_embedding(self, text: str) -> np.ndarray: |
| 85 | + """Generate embedding for given text.""" |
| 86 | + if not text: |
| 87 | + return np.zeros(384) # Default dimension for all-MiniLM-L6-v2 |
| 88 | + |
| 89 | + if self._embedding_model is None: |
| 90 | + # Fallback to simple text similarity |
| 91 | + return self._simple_text_embedding(text) |
| 92 | + |
| 93 | + try: |
| 94 | + loop = asyncio.get_event_loop() |
| 95 | + embedding = await loop.run_in_executor( |
| 96 | + self._executor, |
| 97 | + self._embedding_model.encode, |
| 98 | + text |
| 99 | + ) |
| 100 | + return embedding |
| 101 | + except Exception as e: |
| 102 | + logger.warning(f"Failed to generate embedding for text: {e}") |
| 103 | + return self._simple_text_embedding(text) |
| 104 | + |
| 105 | + def _simple_text_embedding(self, text: str) -> np.ndarray: |
| 106 | + """Simple fallback embedding using character frequency.""" |
| 107 | + # Create a simple embedding based on character frequency |
| 108 | + # This is a fallback when sentence-transformers is not available |
| 109 | + embedding = np.zeros(384) |
| 110 | + text_lower = text.lower() |
| 111 | + |
| 112 | + # Simple character frequency-based embedding |
| 113 | + for i, char in enumerate(text_lower): |
| 114 | + embedding[i % 384] += ord(char) / 1000.0 |
| 115 | + |
| 116 | + # Normalize |
| 117 | + norm = np.linalg.norm(embedding) |
| 118 | + if norm > 0: |
| 119 | + embedding = embedding / norm |
| 120 | + |
| 121 | + return embedding |
| 122 | + |
| 123 | + async def _get_tool_embedding(self, tool: Tool) -> np.ndarray: |
| 124 | + """Get or generate embedding for a tool.""" |
| 125 | + if not self.cache_embeddings or tool.name not in self._tool_embeddings_cache: |
| 126 | + # Create text representation of the tool |
| 127 | + tool_text = f"{tool.name} {tool.description} {' '.join(tool.tags)}" |
| 128 | + embedding = await self._get_text_embedding(tool_text) |
| 129 | + |
| 130 | + if self.cache_embeddings: |
| 131 | + self._tool_embeddings_cache[tool.name] = embedding |
| 132 | + |
| 133 | + return embedding |
| 134 | + |
| 135 | + return self._tool_embeddings_cache[tool.name] |
| 136 | + |
| 137 | + def _cosine_similarity(self, a: np.ndarray, b: np.ndarray) -> float: |
| 138 | + """Calculate cosine similarity between two vectors.""" |
| 139 | + try: |
| 140 | + dot_product = np.dot(a, b) |
| 141 | + norm_a = np.linalg.norm(a) |
| 142 | + norm_b = np.linalg.norm(b) |
| 143 | + |
| 144 | + if norm_a == 0 or norm_b == 0: |
| 145 | + return 0.0 |
| 146 | + |
| 147 | + return dot_product / (norm_a * norm_b) |
| 148 | + except Exception as e: |
| 149 | + logger.warning(f"Error calculating cosine similarity: {e}") |
| 150 | + return 0.0 |
| 151 | + |
| 152 | + async def search_tools( |
| 153 | + self, |
| 154 | + tool_repository: ConcurrentToolRepository, |
| 155 | + query: str, |
| 156 | + limit: int = 10, |
| 157 | + any_of_tags_required: Optional[List[str]] = None |
| 158 | + ) -> List[Tool]: |
| 159 | + """Search for tools using semantic similarity. |
| 160 | + |
| 161 | + Args: |
| 162 | + tool_repository: The tool repository to search within. |
| 163 | + query: The search query string. |
| 164 | + limit: Maximum number of tools to return. |
| 165 | + any_of_tags_required: Optional list of tags where one of them must be present. |
| 166 | + |
| 167 | + Returns: |
| 168 | + List of Tool objects ranked by semantic similarity. |
| 169 | + """ |
| 170 | + if limit < 0: |
| 171 | + raise ValueError("limit must be non-negative") |
| 172 | + |
| 173 | + # Ensure the embedding model is loaded |
| 174 | + await self._ensure_model_loaded() |
| 175 | + |
| 176 | + # Get all tools |
| 177 | + tools: List[Tool] = await tool_repository.get_tools() |
| 178 | + |
| 179 | + # Filter by required tags if specified |
| 180 | + if any_of_tags_required and len(any_of_tags_required) > 0: |
| 181 | + any_of_tags_required = [tag.lower() for tag in any_of_tags_required] |
| 182 | + tools = [ |
| 183 | + tool for tool in tools |
| 184 | + if any(tag.lower() in any_of_tags_required for tag in tool.tags) |
| 185 | + ] |
| 186 | + |
| 187 | + if not tools: |
| 188 | + return [] |
| 189 | + |
| 190 | + # Generate query embedding |
| 191 | + query_embedding = await self._get_text_embedding(query) |
| 192 | + |
| 193 | + # Calculate similarity scores for all tools |
| 194 | + tool_scores: List[Tuple[Tool, float]] = [] |
| 195 | + |
| 196 | + for tool in tools: |
| 197 | + try: |
| 198 | + tool_embedding = await self._get_tool_embedding(tool) |
| 199 | + similarity = self._cosine_similarity(query_embedding, tool_embedding) |
| 200 | + |
| 201 | + if similarity >= self.similarity_threshold: |
| 202 | + tool_scores.append((tool, similarity)) |
| 203 | + |
| 204 | + except Exception as e: |
| 205 | + logger.warning(f"Error processing tool {tool.name}: {e}") |
| 206 | + continue |
| 207 | + |
| 208 | + # Sort by similarity score (descending) |
| 209 | + sorted_tools = [ |
| 210 | + tool for tool, score in sorted( |
| 211 | + tool_scores, |
| 212 | + key=lambda x: x[1], |
| 213 | + reverse=True |
| 214 | + ) |
| 215 | + ] |
| 216 | + |
| 217 | + # Return up to 'limit' tools |
| 218 | + return sorted_tools[:limit] if limit > 0 else sorted_tools |
| 219 | + |
| 220 | + async def __aenter__(self): |
| 221 | + """Async context manager entry.""" |
| 222 | + await self._ensure_model_loaded() |
| 223 | + return self |
| 224 | + |
| 225 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 226 | + """Async context manager exit.""" |
| 227 | + if self._executor: |
| 228 | + self._executor.shutdown(wait=False) |
| 229 | + |
| 230 | + |
| 231 | +class InMemEmbeddingsSearchStrategyConfigSerializer(Serializer[InMemEmbeddingsSearchStrategy]): |
| 232 | + """Serializer for InMemEmbeddingsSearchStrategy configuration.""" |
| 233 | + |
| 234 | + def to_dict(self, obj: InMemEmbeddingsSearchStrategy) -> dict: |
| 235 | + return obj.model_dump() |
| 236 | + |
| 237 | + def validate_dict(self, data: dict) -> InMemEmbeddingsSearchStrategy: |
| 238 | + try: |
| 239 | + return InMemEmbeddingsSearchStrategy.model_validate(data) |
| 240 | + except Exception as e: |
| 241 | + raise ValueError(f"Invalid configuration: {e}") from e |
0 commit comments