|
| 1 | +""" |
| 2 | +Semantic Searcher using Pinecone vector database. |
| 3 | +
|
| 4 | +Coordinates text embedding and vector search to find semantically |
| 5 | +similar content. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +from typing import List, Dict, Any |
| 10 | + |
| 11 | +from database.pinecone_connector import PineconeConnector |
| 12 | +from search.embedder import TextEmbedder |
| 13 | + |
| 14 | +logging.basicConfig(level=logging.INFO) |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +class Searcher: |
| 19 | + """ |
| 20 | + High-level semantic search coordinator. |
| 21 | + |
| 22 | + Combines text embedding and Pinecone vector search to provide |
| 23 | + an easy-to-use interface for semantic similarity search. |
| 24 | + |
| 25 | + Usage: |
| 26 | + searcher = Searcher(api_key="...", index_name="chunks-index") |
| 27 | + results = searcher.search("woman on a train", top_k=3) |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + api_key: str, |
| 33 | + index_name: str, |
| 34 | + namespace: str = "__default__" |
| 35 | + ): |
| 36 | + """ |
| 37 | + Initialize searcher with Pinecone connection. |
| 38 | + |
| 39 | + Args: |
| 40 | + api_key: Pinecone API key |
| 41 | + index_name: Name of Pinecone index to search |
| 42 | + namespace: Optional namespace for partitioning data |
| 43 | + """ |
| 44 | + self.embedder = TextEmbedder() |
| 45 | + self.connector = PineconeConnector(api_key=api_key, index_name=index_name) |
| 46 | + self.namespace = namespace |
| 47 | + |
| 48 | + logger.info( |
| 49 | + f"Searcher initialized (index={index_name}, namespace='{namespace}')" |
| 50 | + ) |
| 51 | + |
| 52 | + @property |
| 53 | + def device(self) -> str: |
| 54 | + """Get the device being used for embeddings (cpu/cuda).""" |
| 55 | + return self.embedder.device |
| 56 | + |
| 57 | + def search( |
| 58 | + self, |
| 59 | + query: str, |
| 60 | + top_k: int = 5 |
| 61 | + ) -> List[Dict[str, Any]]: |
| 62 | + """ |
| 63 | + Search for semantically similar content. |
| 64 | +
|
| 65 | + Args: |
| 66 | + query: Natural language search query |
| 67 | + top_k: Number of results to return (default: 5) |
| 68 | +
|
| 69 | + Returns: |
| 70 | + List of matches with scores and metadata, sorted by similarity |
| 71 | +
|
| 72 | + Example: |
| 73 | + results = searcher.search("cooking in kitchen", top_k=3) |
| 74 | + for result in results: |
| 75 | + print(f"Score: {result['score']}") |
| 76 | + print(f"Metadata: {result['metadata']}") |
| 77 | + """ |
| 78 | + logger.info(f"Searching for: '{query}' (top_k={top_k})") |
| 79 | + |
| 80 | + # Generate query embedding |
| 81 | + query_embedding = self.embedder.embed_text(query) |
| 82 | + |
| 83 | + # Search Pinecone with optional filters |
| 84 | + matches = self.connector.query_chunks( |
| 85 | + query_embedding=query_embedding, |
| 86 | + namespace=self.namespace, |
| 87 | + top_k=top_k |
| 88 | + ) |
| 89 | + |
| 90 | + # Format results |
| 91 | + results = [] |
| 92 | + for match in matches: |
| 93 | + result = { |
| 94 | + 'id': match.get('id'), |
| 95 | + 'score': match.get('score', 0.0), |
| 96 | + 'metadata': match.get('metadata', {}) |
| 97 | + } |
| 98 | + results.append(result) |
| 99 | + |
| 100 | + logger.info(f"Found {len(results)} results") |
| 101 | + return results |
0 commit comments