Add `text-embeddings-inference` tag & snippet (#3)
Browse files- Add `text-embeddings-inference` tag & snippet (7dbeedb9dc39d001dc7d9a59ebc274b24f4ce72a)
- move Usage, embeddings models -> embedding models (e64d277e11c669c330425e2a03fb24568f633dbb)
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,38 @@ 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 |
```
|
@@ -97,4 +122,4 @@ If you find this model helpful, feel free to cite our publication [Sentence-BERT
|
|
97 |
publisher = "Association for Computational Linguistics",
|
98 |
url = "http://arxiv.org/abs/1908.10084",
|
99 |
}
|
100 |
-
```
|
|
|
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/stsb-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/stsb-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 '{
|
95 |
+
"model": "sentence-transformers/stsb-mpnet-base-v2",
|
96 |
+
"input": ["This is an example sentence", "Each sentence is converted"]
|
97 |
+
}'
|
98 |
+
```
|
99 |
+
|
100 |
+
Or check the [Text Embeddings Inference API specification](https://huggingface.github.io/text-embeddings-inference/) instead.
|
101 |
|
102 |
## Full Model Architecture
|
103 |
```
|
|
|
122 |
publisher = "Association for Computational Linguistics",
|
123 |
url = "http://arxiv.org/abs/1908.10084",
|
124 |
}
|
125 |
+
```
|