Sentence Similarity
Transformers
Safetensors
English
mistral
feature-extraction
text-embedding
embeddings
information-retrieval
beir
text-classification
language-model
text-clustering
text-semantic-similarity
text-evaluation
text-reranking
Sentence Similarity
natural_questions
ms_marco
fever
hotpot_qa
mteb
custom_code
text-generation-inference
text-embeddings-inference
Inference Endpoints
Update README.md
Browse files
README.md
CHANGED
@@ -44,31 +44,44 @@ import torch
|
|
44 |
from transformers import AutoTokenizer, AutoModel, AutoConfig
|
45 |
from peft import PeftModel
|
46 |
|
47 |
-
# Loading base Mistral model, along with custom code that enables bidirectional connections in decoder-only LLMs.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
# Loading
|
55 |
-
model = PeftModel.from_pretrained(
|
|
|
|
|
|
|
56 |
|
57 |
# Wrapper for encoding and pooling operations
|
58 |
l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
|
59 |
|
60 |
# Encoding queries using instructions
|
61 |
-
instruction =
|
|
|
|
|
62 |
queries = [
|
63 |
-
[instruction,
|
64 |
-
[instruction,
|
65 |
]
|
66 |
q_reps = l2v.encode(queries)
|
67 |
|
68 |
# Encoding documents. Instruction are not required for documents
|
69 |
documents = [
|
70 |
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
|
71 |
-
"Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."
|
72 |
]
|
73 |
d_reps = l2v.encode(documents)
|
74 |
|
@@ -79,8 +92,8 @@ cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
|
|
79 |
|
80 |
print(cos_sim)
|
81 |
"""
|
82 |
-
tensor([[0.
|
83 |
-
[0.
|
84 |
"""
|
85 |
```
|
86 |
|
|
|
44 |
from transformers import AutoTokenizer, AutoModel, AutoConfig
|
45 |
from peft import PeftModel
|
46 |
|
47 |
+
# Loading base Mistral model, along with custom code that enables bidirectional connections in decoder-only LLMs.
|
48 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
49 |
+
"McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp"
|
50 |
+
)
|
51 |
+
config = AutoConfig.from_pretrained(
|
52 |
+
"McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp", trust_remote_code=True
|
53 |
+
)
|
54 |
+
model = AutoModel.from_pretrained(
|
55 |
+
"McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp",
|
56 |
+
trust_remote_code=True,
|
57 |
+
config=config,
|
58 |
+
torch_dtype=torch.bfloat16,
|
59 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
60 |
+
)
|
61 |
|
62 |
+
# Loading MNTP (Masked Next Token Prediction) model.
|
63 |
+
model = PeftModel.from_pretrained(
|
64 |
+
model,
|
65 |
+
"McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp",
|
66 |
+
)
|
67 |
|
68 |
# Wrapper for encoding and pooling operations
|
69 |
l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
|
70 |
|
71 |
# Encoding queries using instructions
|
72 |
+
instruction = (
|
73 |
+
"Given a web search query, retrieve relevant passages that answer the query:"
|
74 |
+
)
|
75 |
queries = [
|
76 |
+
[instruction, "how much protein should a female eat"],
|
77 |
+
[instruction, "summit define"],
|
78 |
]
|
79 |
q_reps = l2v.encode(queries)
|
80 |
|
81 |
# Encoding documents. Instruction are not required for documents
|
82 |
documents = [
|
83 |
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
|
84 |
+
"Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments.",
|
85 |
]
|
86 |
d_reps = l2v.encode(documents)
|
87 |
|
|
|
92 |
|
93 |
print(cos_sim)
|
94 |
"""
|
95 |
+
tensor([[0.6266, 0.4199],
|
96 |
+
[0.3429, 0.5240]])
|
97 |
"""
|
98 |
```
|
99 |
|