Embed a single text.
(self, text: str)
| 30 | raise NotImplementedError |
| 31 | |
| 32 | def _embed_single(self, text: str) -> List[float]: |
| 33 | """Embed a single text.""" |
| 34 | inputs = self.tokenizer( |
| 35 | text, padding=True, truncation=True, return_tensors='pt' |
| 36 | ).to(self.model.device) |
| 37 | |
| 38 | with self.torch.no_grad(): |
| 39 | outputs = self.model(**inputs) |
| 40 | |
| 41 | embedding = self._get_embedding(outputs, inputs['attention_mask']) |
| 42 | embedding = self.F.normalize(embedding, p=2, dim=1) |
| 43 | return embedding.cpu().numpy()[0].tolist() |
| 44 | |
| 45 | def embed_documents(self, texts: List[str]) -> List[List[float]]: |
| 46 | """Batch embed documents.""" |
no test coverage detected