Generate text embeddings for a list of text strings. Args: text (list[str]): A list of text strings. model: The model used for generating embeddings. batchsize (int): The batch size for processing text. Returns: list: A list of text embeddings.
(
text: list[str], model: BGEM3FlagModel, batchsize: int = 32
)
| 141 | |
| 142 | |
| 143 | def get_text_embedding( |
| 144 | text: list[str], model: BGEM3FlagModel, batchsize: int = 32 |
| 145 | ) -> list[torch.Tensor]: |
| 146 | """ |
| 147 | Generate text embeddings for a list of text strings. |
| 148 | |
| 149 | Args: |
| 150 | text (list[str]): A list of text strings. |
| 151 | model: The model used for generating embeddings. |
| 152 | batchsize (int): The batch size for processing text. |
| 153 | |
| 154 | Returns: |
| 155 | list: A list of text embeddings. |
| 156 | """ |
| 157 | if isinstance(text, str): |
| 158 | return torch.tensor(model.encode(text)["dense_vecs"]).to(model.device) |
| 159 | result = [] |
| 160 | for i in range(0, len(text), batchsize): |
| 161 | result.extend( |
| 162 | torch.tensor(model.encode(text[i : i + batchsize])["dense_vecs"]).to( |
| 163 | model.device |
| 164 | ) |
| 165 | ) |
| 166 | return result |
| 167 | |
| 168 | |
| 169 | def get_image_embedding( |
no outgoing calls
no test coverage detected