Add `text-embeddings-inference` tag & snippet (#4)
Browse files- Add `text-embeddings-inference` tag & snippet (c99f96f79723ad601d3fa7f73a4d57c40d86f4a0)
- Update README.md (645fd273cf0144000b28ba5c2a64ce27c77520b5)
- embeddings models -> embedding models (799acf92da6b8e71dc136ad470dfe4eb89b4726e)
Co-authored-by: Alvaro Bartolome <[email protected]>
README.md
CHANGED
|
@@ -6,6 +6,7 @@ tags:
|
|
| 6 |
- feature-extraction
|
| 7 |
- sentence-similarity
|
| 8 |
- transformers
|
|
|
|
| 9 |
pipeline_tag: sentence-similarity
|
| 10 |
---
|
| 11 |
|
|
@@ -44,9 +45,9 @@ from transformers import AutoTokenizer, AutoModel
|
|
| 44 |
import torch
|
| 45 |
|
| 46 |
|
| 47 |
-
#Mean Pooling - Take attention mask into account for correct averaging
|
| 48 |
def mean_pooling(model_output, attention_mask):
|
| 49 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 50 |
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 51 |
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 52 |
|
|
@@ -65,14 +66,35 @@ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tenso
|
|
| 65 |
with torch.no_grad():
|
| 66 |
model_output = model(**encoded_input)
|
| 67 |
|
| 68 |
-
# Perform pooling. In this case,
|
| 69 |
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 70 |
|
| 71 |
print("Sentence embeddings:")
|
| 72 |
print(sentence_embeddings)
|
| 73 |
```
|
| 74 |
|
|
|
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
## Full Model Architecture
|
| 78 |
```
|
|
|
|
| 6 |
- feature-extraction
|
| 7 |
- sentence-similarity
|
| 8 |
- transformers
|
| 9 |
+
- text-embeddings-inference
|
| 10 |
pipeline_tag: sentence-similarity
|
| 11 |
---
|
| 12 |
|
|
|
|
| 45 |
import torch
|
| 46 |
|
| 47 |
|
| 48 |
+
# Mean Pooling - Take attention mask into account for correct averaging
|
| 49 |
def mean_pooling(model_output, attention_mask):
|
| 50 |
+
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
|
| 51 |
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 52 |
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 53 |
|
|
|
|
| 66 |
with torch.no_grad():
|
| 67 |
model_output = model(**encoded_input)
|
| 68 |
|
| 69 |
+
# Perform pooling. In this case, mean pooling.
|
| 70 |
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 71 |
|
| 72 |
print("Sentence embeddings:")
|
| 73 |
print(sentence_embeddings)
|
| 74 |
```
|
| 75 |
|
| 76 |
+
## Usage (Text Embeddings Inference (TEI))
|
| 77 |
|
| 78 |
+
[Text Embeddings Inference (TEI)](https://github.com/huggingface/text-embeddings-inference) is a blazing fast inference solution for text embedding models.
|
| 79 |
+
|
| 80 |
+
- CPU:
|
| 81 |
+
```bash
|
| 82 |
+
docker run -p 8080:80 -v hf_cache:/data --pull always ghcr.io/huggingface/text-embeddings-inference:cpu-latest --model-id sentence-transformers/nli-mpnet-base-v2 --pooling mean --dtype float16
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
- NVIDIA GPU:
|
| 86 |
+
```bash
|
| 87 |
+
docker run --gpus all -p 8080:80 -v hf_cache:/data --pull always ghcr.io/huggingface/text-embeddings-inference:cuda-latest --model-id sentence-transformers/nli-mpnet-base-v2 --pooling mean --dtype float16
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
Send a request to `/v1/embeddings` to generate embeddings via the [OpenAI Embeddings API](https://platform.openai.com/docs/api-reference/embeddings/create):
|
| 91 |
+
```bash
|
| 92 |
+
curl http://localhost:8080/v1/embeddings \
|
| 93 |
+
-H "Content-Type: application/json" \
|
| 94 |
+
-d '{"model":"sentence-transformers/nli-mpnet-base-v2","input":"This is an example sentence"}'
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
Or check the [Text Embeddings Inference API specification](https://huggingface.github.io/text-embeddings-inference/) instead.
|
| 98 |
|
| 99 |
## Full Model Architecture
|
| 100 |
```
|