add sentence transformer usage
Browse files
README.md
CHANGED
|
@@ -2622,7 +2622,7 @@ Follow us on:
|
|
| 2622 |
|
| 2623 |
|
| 2624 |
# Usage
|
| 2625 |
-
|
| 2626 |
|
| 2627 |
```bash
|
| 2628 |
python -m pip install -U angle-emb
|
|
@@ -2630,31 +2630,66 @@ python -m pip install -U angle-emb
|
|
| 2630 |
|
| 2631 |
1) Non-Retrieval Tasks
|
| 2632 |
|
|
|
|
|
|
|
| 2633 |
```python
|
| 2634 |
from angle_emb import AnglE
|
|
|
|
| 2635 |
|
| 2636 |
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
|
| 2637 |
-
|
| 2638 |
-
|
| 2639 |
-
|
| 2640 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2641 |
```
|
| 2642 |
|
| 2643 |
2) Retrieval Tasks
|
| 2644 |
|
| 2645 |
-
For retrieval purposes, please use the prompt `Prompts.C
|
| 2646 |
|
| 2647 |
```python
|
| 2648 |
from angle_emb import AnglE, Prompts
|
|
|
|
| 2649 |
|
| 2650 |
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
|
| 2651 |
-
angle.
|
| 2652 |
-
|
| 2653 |
-
|
| 2654 |
-
|
| 2655 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2656 |
```
|
| 2657 |
|
|
|
|
|
|
|
| 2658 |
# Citation
|
| 2659 |
|
| 2660 |
If you use our pre-trained models, welcome to support us by citing our work:
|
|
|
|
| 2622 |
|
| 2623 |
|
| 2624 |
# Usage
|
| 2625 |
+
## 1. angle_emb
|
| 2626 |
|
| 2627 |
```bash
|
| 2628 |
python -m pip install -U angle-emb
|
|
|
|
| 2630 |
|
| 2631 |
1) Non-Retrieval Tasks
|
| 2632 |
|
| 2633 |
+
There is no need to specify any prompts.
|
| 2634 |
+
|
| 2635 |
```python
|
| 2636 |
from angle_emb import AnglE
|
| 2637 |
+
from scipy import spatial
|
| 2638 |
|
| 2639 |
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
|
| 2640 |
+
doc_vecs = angle.encode([
|
| 2641 |
+
'The weather is great!',
|
| 2642 |
+
'The weather is very good!',
|
| 2643 |
+
'i am going to bed'
|
| 2644 |
+
])
|
| 2645 |
+
|
| 2646 |
+
for i, dv1 in enumerate(doc_vecs):
|
| 2647 |
+
for dv2 in doc_vecs[i+1:]:
|
| 2648 |
+
print(1 - spatial.distance.cosine(dv1, dv2))
|
| 2649 |
```
|
| 2650 |
|
| 2651 |
2) Retrieval Tasks
|
| 2652 |
|
| 2653 |
+
For retrieval purposes, please use the prompt `Prompts.C` for query (not for document).
|
| 2654 |
|
| 2655 |
```python
|
| 2656 |
from angle_emb import AnglE, Prompts
|
| 2657 |
+
from scipy import spatial
|
| 2658 |
|
| 2659 |
angle = AnglE.from_pretrained('WhereIsAI/UAE-Large-V1', pooling_strategy='cls').cuda()
|
| 2660 |
+
qv = angle.encode(Prompts.C.format(text='what is the weather?'))
|
| 2661 |
+
doc_vecs = angle.encode([
|
| 2662 |
+
'The weather is great!',
|
| 2663 |
+
'it is rainy today.',
|
| 2664 |
+
'i am going to bed'
|
| 2665 |
+
])
|
| 2666 |
+
|
| 2667 |
+
for dv in doc_vecs:
|
| 2668 |
+
print(1 - spatial.distance.cosine(qv[0], dv))
|
| 2669 |
+
```
|
| 2670 |
+
|
| 2671 |
+
## 2. sentence transformer
|
| 2672 |
+
|
| 2673 |
+
|
| 2674 |
+
```python
|
| 2675 |
+
from angle_emb import Prompts
|
| 2676 |
+
from sentence_transformers import SentenceTransformer
|
| 2677 |
+
|
| 2678 |
+
model = SentenceTransformer("WhereIsAI/UAE-Large-V1").cuda()
|
| 2679 |
+
|
| 2680 |
+
qv = model.encode(Prompts.C.format(text='what is the weather?'))
|
| 2681 |
+
doc_vecs = model.encode([
|
| 2682 |
+
'The weather is great!',
|
| 2683 |
+
'it is rainy today.',
|
| 2684 |
+
'i am going to bed'
|
| 2685 |
+
])
|
| 2686 |
+
|
| 2687 |
+
for dv in doc_vecs:
|
| 2688 |
+
print(1 - spatial.distance.cosine(qv, dv))
|
| 2689 |
```
|
| 2690 |
|
| 2691 |
+
|
| 2692 |
+
|
| 2693 |
# Citation
|
| 2694 |
|
| 2695 |
If you use our pre-trained models, welcome to support us by citing our work:
|