Encode texts into embedding vectors. Returns: np.ndarray of shape (len(texts), dim)
(self, texts: List[str], **kwargs)
| 212 | timeout=60, |
| 213 | ) |
| 214 | logger.info(f"Embedding API client initialized, model={self.model_name}") |
| 215 | |
| 216 | def encode(self, texts: List[str], **kwargs) -> np.ndarray: |
| 217 | """Encode texts into embedding vectors. |
| 218 | |
| 219 | Returns: |
| 220 | np.ndarray of shape (len(texts), dim) |
| 221 | """ |
| 222 | if isinstance(texts, str): |
| 223 | texts = [texts] |
| 224 | |
| 225 | texts = [t if t.strip() else "empty" for t in texts] |
| 226 | |
| 227 | # ReMem uses instruction prefix to distinguish queries from documents |
| 228 | instruction = kwargs.get("instruction", "") |
| 229 | if instruction: |
| 230 | texts = [f"{instruction}{t}" for t in texts] |
| 231 | |
| 232 | if self.provider == "local": |
| 233 | embeddings = self._encode_local(texts) |
| 234 | else: |
| 235 | embeddings = self._encode_api(texts) |
| 236 | |
| 237 | # Normalize |
| 238 | if self.normalize and kwargs.get("norm", True): |
| 239 | norms = np.linalg.norm(embeddings, axis=1, keepdims=True) |
| 240 | norms = np.where(norms == 0, 1, norms) |
| 241 | embeddings = embeddings / norms |
| 242 | |
| 243 | return embeddings |
| 244 |
no test coverage detected