vaibhavad commited on
Commit
69703eb
1 Parent(s): 5641294

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +41 -4
README.md CHANGED
@@ -11,10 +11,6 @@ tags: []
11
  - **Paper:**
12
 
13
 
14
-
15
- ## Quick start
16
- <hr />
17
-
18
  ## Installation
19
  ```bash
20
  pip install llm2vec
@@ -22,6 +18,47 @@ pip install llm2vec
22
 
23
  ## Usage
24
  ```python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  ```
27
 
 
11
  - **Paper:**
12
 
13
 
 
 
 
 
14
  ## Installation
15
  ```bash
16
  pip install llm2vec
 
18
 
19
  ## Usage
20
  ```python
21
+ from llm2vec import LLM2Vec
22
+
23
+ import torch
24
+ from transformers import AutoTokenizer, AutoModel, AutoConfig
25
+ from peft import PeftModel
26
+
27
+ # Loading base MNTP model, along with custom code that enables bidirectional connections in decoder-only LLMs
28
+ tokenizer = AutoTokenizer.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp")
29
+ config = AutoConfig.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp", trust_remote_code=True)
30
+ model = AutoModel.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp", trust_remote_code=True, config=config, torch_dtype=torch.bfloat16)
31
+ model = PeftModel.from_pretrained(model, "McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp")
32
+ model = model.merge_and_unload() # This can take several minutes
33
+
34
+ # Loading supervised model
35
+ model = PeftModel.from_pretrained(model, "McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-supervised")
36
+
37
+ # Wrapper for encoding and pooling operations
38
+ l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
39
+
40
+ # Encoding queries using instructions
41
+ instruction = 'Given a web search query, retrieve relevant passages that answer the query:'
42
+ queries = [
43
+ [instruction, 'how much protein should a female eat'],
44
+ [instruction, 'summit define']
45
+ ]
46
+ q_reps = l2v.encode(queries)
47
+
48
+ # Encoding documents. Instruction are not required for documents
49
+ documents = [
50
+ "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.",
51
+ "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."
52
+ ]
53
+ d_reps = l2v.encode(documents)
54
+
55
+
56
+ # Compute cosine similarity
57
+ q_reps_norm = torch.nn.functional.normalize(q_reps, p=2, dim=1)
58
+ d_reps_norm = torch.nn.functional.normalize(d_reps, p=2, dim=1)
59
+ cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
60
+
61
+ print(cos_sim.tolist())
62
 
63
  ```
64