riccardomusmeci
commited on
Commit
•
348a989
1
Parent(s):
370c79d
Update README
Browse files
README.md
CHANGED
@@ -1,3 +1,108 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
pipeline_tag: feature-extraction
|
6 |
+
tags:
|
7 |
+
- e5-mistral-7b-instruct
|
8 |
+
- mlx-llm
|
9 |
+
- mlx
|
10 |
+
- feature-extraction
|
11 |
+
- embeddings
|
12 |
+
library_name: mlx-llm
|
13 |
---
|
14 |
+
|
15 |
+
|
16 |
+
# E5-mistral-7b-instruct
|
17 |
+
|
18 |
+
[Improving Text Embeddings with Large Language Models](https://arxiv.org/pdf/2401.00368.pdf). Liang Wang, Nan Yang, Xiaolong Huang, Linjun Yang, Rangan Majumder, Furu Wei, arXiv 2024
|
19 |
+
|
20 |
+
This model has 32 layers and the embedding size is 4096.
|
21 |
+
|
22 |
+
## Model description
|
23 |
+
|
24 |
+
Please, refer to the [original model card](https://huggingface.co/intfloat/e5-mistral-7b-instruct) for more details on E5-mistral-7b-instruct.
|
25 |
+
|
26 |
+
## Use with mlx-llm
|
27 |
+
|
28 |
+
Download weights from files section and install `mlx-llm` from GitHub.
|
29 |
+
```bash
|
30 |
+
git clone https://github.com/riccardomusmeci/mlx-llm
|
31 |
+
cd mlx-llm
|
32 |
+
pip install .
|
33 |
+
```
|
34 |
+
|
35 |
+
Run
|
36 |
+
|
37 |
+
```python
|
38 |
+
import mlx.core as mx
|
39 |
+
import numpy as np
|
40 |
+
from mlx_llm.model import create_model
|
41 |
+
from transformers import AutoTokenizer
|
42 |
+
|
43 |
+
model = create_model(
|
44 |
+
"e5-mistral-7b-instruct",
|
45 |
+
weights_path="path/to/weights.npz",
|
46 |
+
strict=False
|
47 |
+
)
|
48 |
+
|
49 |
+
def get_detailed_instruct(task_description: str, query: str) -> str:
|
50 |
+
return f'Instruct: {task_description}\nQuery: {query}'
|
51 |
+
|
52 |
+
def last_token_pool(embeds: mx.array, attn_mask: mx.array) -> mx.array:
|
53 |
+
left_padding = (attn_mask[:, -1].sum() == attn_mask.shape[0])
|
54 |
+
if left_padding:
|
55 |
+
return embeds[:, -1]
|
56 |
+
else:
|
57 |
+
sequence_lengths = attn_mask.sum(axis=1) - 1
|
58 |
+
batch_size = embeds.shape[0]
|
59 |
+
return embeds[mx.arange(batch_size), sequence_lengths]
|
60 |
+
|
61 |
+
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
62 |
+
|
63 |
+
input_texts = [
|
64 |
+
get_detailed_instruct(task, 'how much protein should a female eat'),
|
65 |
+
# get_detailed_instruct(task, 'summit define'),
|
66 |
+
"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.",
|
67 |
+
"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."
|
68 |
+
]
|
69 |
+
|
70 |
+
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
71 |
+
|
72 |
+
# prepare input and attn_mask
|
73 |
+
max_length = 4096
|
74 |
+
batch_dict = tokenizer(
|
75 |
+
input_texts,
|
76 |
+
max_length=max_length - 1,
|
77 |
+
return_attention_mask=False,
|
78 |
+
padding=False,
|
79 |
+
truncation=True
|
80 |
+
)
|
81 |
+
|
82 |
+
batch_dict['input_ids'] = [
|
83 |
+
input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']
|
84 |
+
]
|
85 |
+
|
86 |
+
batch_dict = tokenizer.pad(
|
87 |
+
batch_dict,
|
88 |
+
padding=True,
|
89 |
+
return_attention_mask=True,
|
90 |
+
return_tensors='np'
|
91 |
+
)
|
92 |
+
|
93 |
+
x = mx.array(batch_dict["input_ids"].tolist())
|
94 |
+
attn_mask = mx.array(batch_dict["attention_mask"].tolist())
|
95 |
+
|
96 |
+
# compute embed
|
97 |
+
embeds = model.embed(x)
|
98 |
+
mx.eval(embeds)
|
99 |
+
|
100 |
+
embeds = np.array(last_token_pool(embeds, attn_mask))
|
101 |
+
|
102 |
+
# Normalize embeds
|
103 |
+
norm_den = np.linalg.norm(embeds, axis=-1)
|
104 |
+
norm_embeds = embeds / norm_den[:, None]
|
105 |
+
|
106 |
+
scores = (norm_embeds @ norm_embeds.T) * 100
|
107 |
+
print(scores)
|
108 |
+
```
|