File size: 10,603 Bytes
a453c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e97ac87
 
 
 
 
 
 
 
 
 
a453c29
 
e97ac87
a453c29
e97ac87
 
 
 
a453c29
e97ac87
a453c29
 
 
 
 
 
 
 
e97ac87
 
 
 
a453c29
 
 
e97ac87
a453c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e97ac87
 
 
a453c29
e97ac87
a453c29
e97ac87
a453c29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""Service for finding similar topics using Google Generative AI embeddings"""

import logging
import json
import hashlib
from pathlib import Path
from typing import Optional, List, Dict
from datetime import datetime
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from google import genai
from google.genai import types

from config import GOOGLE_API_KEY, PROJECT_ROOT

logger = logging.getLogger(__name__)

# Paths for topics and cache files
TOPICS_FILE = PROJECT_ROOT / "data" / "topics.json"
EMBEDDINGS_CACHE_FILE = PROJECT_ROOT / "data" / "topic_embeddings_cache.json"


class TopicSimilarityService:
    """Service for finding the most similar topic from a predefined list using embeddings"""
    
    def __init__(self):
        self.client = None
        self.topics = []
        self.topic_embeddings = None
        self.initialized = False
        self.model_name = "models/text-embedding-004"
        
    def initialize(self):
        """Initialize the Google Generative AI client and load topic embeddings"""
        if self.initialized:
            logger.info("Topic similarity service already initialized")
            return
            
        if not GOOGLE_API_KEY:
            raise ValueError("GOOGLE_API_KEY not found in environment variables")
        
        try:
            logger.info("Initializing topic similarity service with Google Generative AI")
            
            # Create Google Generative AI client
            self.client = genai.Client(api_key=GOOGLE_API_KEY)
            
            # Load topics
            self.topics = self._load_topics()
            logger.info(f"Loaded {len(self.topics)} topics from {TOPICS_FILE}")
            
            # Load or generate topic embeddings
            self.topic_embeddings = self._get_topic_embeddings()
            logger.info(f"Loaded {len(self.topic_embeddings)} topic embeddings")
            
            self.initialized = True
            logger.info("✓ Topic similarity service initialized successfully")
            
        except Exception as e:
            logger.error(f"Error initializing topic similarity service: {str(e)}")
            raise RuntimeError(f"Failed to initialize topic similarity service: {str(e)}")
    
    def _load_topics(self) -> List[str]:
        """Load topics from topics.json file"""
        # Ensure path is absolute
        topics_file = Path(TOPICS_FILE).resolve()
        
        if not topics_file.exists():
            raise FileNotFoundError(
                f"Topics file not found: {topics_file}\n"
                f"Current working directory: {Path.cwd()}\n"
                f"PROJECT_ROOT: {PROJECT_ROOT}\n"
                f"TOPICS_FILE path: {TOPICS_FILE}"
            )
        
        try:
            with open(topics_file, 'r', encoding='utf-8') as f:
                data = json.load(f)
            topics = data.get("topics", [])
            if not topics:
                raise ValueError(f"No topics found in {topics_file}")
            return topics
        except (json.JSONDecodeError, KeyError) as e:
            raise ValueError(f"Error loading topics from {topics_file}: {str(e)}")
    
    def _get_topics_hash(self, topics: List[str]) -> str:
        """Generate a hash of the topics list to verify cache validity"""
        topics_str = json.dumps(topics, sort_keys=True)
        return hashlib.md5(topics_str.encode('utf-8')).hexdigest()
    
    def _load_cached_embeddings(self) -> Optional[np.ndarray]:
        """Load cached topic embeddings if they exist and are valid"""
        # Ensure path is absolute
        cache_file = Path(EMBEDDINGS_CACHE_FILE).resolve()
        
        if not cache_file.exists():
            return None
        
        try:
            with open(cache_file, 'r', encoding='utf-8') as f:
                cache_data = json.load(f)
            
            # Verify cache is valid by checking topics hash
            current_hash = self._get_topics_hash(self.topics)
            
            if cache_data.get("topics_hash") == current_hash:
                # Convert list embeddings back to numpy arrays
                embeddings = [np.array(emb) for emb in cache_data.get("embeddings", [])]
                logger.info(f"Loaded {len(embeddings)} topic embeddings from cache")
                return np.array(embeddings)
            else:
                # Topics have changed, cache is invalid
                logger.info("Topics have changed, cache is invalid")
                return None
        except (json.JSONDecodeError, KeyError, ValueError) as e:
            logger.warning(f"Could not load cached embeddings: {e}")
            return None
    
    def _save_cached_embeddings(self, embeddings: np.ndarray):
        """Save topic embeddings to cache file"""
        topics_hash = self._get_topics_hash(self.topics)
        
        # Convert numpy arrays to lists for JSON serialization
        embeddings_list = [emb.tolist() for emb in embeddings]
        
        cache_data = {
            "topics_hash": topics_hash,
            "embeddings": embeddings_list,
            "model": self.model_name,
            "cached_at": datetime.now().isoformat()
        }
        
        try:
            # Ensure path is absolute and directory exists
            cache_file = Path(EMBEDDINGS_CACHE_FILE).resolve()
            cache_file.parent.mkdir(parents=True, exist_ok=True)
            
            with open(cache_file, 'w', encoding='utf-8') as f:
                json.dump(cache_data, f, indent=2)
            logger.info(f"Cached {len(embeddings)} topic embeddings to {cache_file}")
        except Exception as e:
            logger.warning(f"Could not save cached embeddings: {e}")
    
    def _get_topic_embeddings(self) -> np.ndarray:
        """
        Get topic embeddings, loading from cache if available, otherwise generating and caching them
        
        Returns:
            numpy.ndarray: Array of topic embeddings
        """
        # Try to load from cache first
        cached_embeddings = self._load_cached_embeddings()
        if cached_embeddings is not None:
            return cached_embeddings
        
        # Cache miss or invalid - generate embeddings
        logger.info(f"Generating embeddings for {len(self.topics)} topics (this may take a moment)...")
        
        try:
            embedding_response = self.client.models.embed_content(
                model=self.model_name,
                contents=self.topics,
                config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY")
            )
            
            if not hasattr(embedding_response, "embeddings") or embedding_response.embeddings is None:
                raise RuntimeError("Embedding API did not return embeddings")
            
            embeddings = [np.array(e.values) for e in embedding_response.embeddings]
            embeddings_array = np.array(embeddings)
            
            # Save to cache for future use
            self._save_cached_embeddings(embeddings_array)
            
            return embeddings_array
            
        except Exception as e:
            logger.error(f"Error generating topic embeddings: {str(e)}")
            raise RuntimeError(f"Failed to generate topic embeddings: {str(e)}")
    
    def find_most_similar_topic(self, input_text: str) -> Dict[str, any]:
        """
        Compare a single input text to all topics and return the highest cosine similarity
        
        Args:
            input_text: The text to compare against topics
            
        Returns:
            dict: Contains 'topic', 'similarity', and 'index' of the most similar topic
        """
        if not self.initialized:
            self.initialize()
        
        if not input_text or not isinstance(input_text, str):
            raise ValueError("Input text must be a non-empty string")
        
        input_text = input_text.strip()
        if len(input_text) == 0:
            raise ValueError("Input text cannot be empty")
        
        if not self.topics:
            raise ValueError("No topics found in topics.json")
        
        try:
            # Embed the input text
            embedding_response = self.client.models.embed_content(
                model=self.model_name,
                contents=[input_text],
                config=types.EmbedContentConfig(task_type="SEMANTIC_SIMILARITY")
            )
            
            if not hasattr(embedding_response, "embeddings") or embedding_response.embeddings is None:
                raise RuntimeError("Embedding API did not return embeddings")
            
            # Extract input embedding
            input_embedding = np.array(embedding_response.embeddings[0].values).reshape(1, -1)
            
            # Calculate cosine similarity between input and each topic
            similarities = cosine_similarity(input_embedding, self.topic_embeddings)[0]
            
            # Find the highest similarity
            max_index = np.argmax(similarities)
            max_similarity = similarities[max_index]
            most_similar_topic = self.topics[max_index]
            
            return {
                "topic": most_similar_topic,
                "similarity": float(max_similarity),
                "index": int(max_index)
            }
            
        except Exception as e:
            logger.error(f"Error finding similar topic: {str(e)}")
            raise RuntimeError(f"Failed to find similar topic: {str(e)}")
    
    def batch_find_similar_topics(self, input_texts: List[str]) -> List[str]:
        """
        Find the most similar topic for each input text
        
        Args:
            input_texts: List of input texts to compare against topics
            
        Returns:
            List of most similar topics (one per input text)
        """
        if not self.initialized:
            self.initialize()
        
        if not input_texts or not isinstance(input_texts, list):
            raise ValueError("Input texts must be a non-empty list")
        
        results = []
        for text in input_texts:
            try:
                result = self.find_most_similar_topic(text)
                results.append(result["topic"])
            except Exception as e:
                logger.error(f"Error finding similar topic for text '{text[:50]}...': {str(e)}")
                results.append(None)  # Or raise, depending on desired behavior
        
        return results


# Initialize singleton instance
topic_similarity_service = TopicSimilarityService()