Update README.md
Browse files
README.md
CHANGED
@@ -5280,4 +5280,105 @@ model-index:
|
|
5280 |
value: 87.78807479093082
|
5281 |
- type: max_f1
|
5282 |
value: 80.3221673073403
|
|
|
|
|
|
|
5283 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5280 |
value: 87.78807479093082
|
5281 |
- type: max_f1
|
5282 |
value: 80.3221673073403
|
5283 |
+
language:
|
5284 |
+
- en
|
5285 |
+
license: mit
|
5286 |
---
|
5287 |
+
|
5288 |
+
## E5-mistral-7b-instruct
|
5289 |
+
|
5290 |
+
**[TODO] Technical details on the model training and evaluation will be available before 2024-01-01.**
|
5291 |
+
|
5292 |
+
Some highlights for preview:
|
5293 |
+
* This model is only fine-tuned for less than 1000 steps, no contrastive pre-training is used.
|
5294 |
+
* For a large part of training data, the query, positive documents and negative documents are all generated by LLMs.
|
5295 |
+
|
5296 |
+
This model has 32 layers and the embedding size is 4096.
|
5297 |
+
|
5298 |
+
## Usage
|
5299 |
+
|
5300 |
+
Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
|
5301 |
+
|
5302 |
+
```python
|
5303 |
+
import torch
|
5304 |
+
import torch.nn.functional as F
|
5305 |
+
|
5306 |
+
from torch import Tensor
|
5307 |
+
from transformers import AutoTokenizer, AutoModel
|
5308 |
+
from transformers.file_utils import PaddingStrategy
|
5309 |
+
|
5310 |
+
|
5311 |
+
def last_token_pool(last_hidden_states: Tensor,
|
5312 |
+
attention_mask: Tensor) -> Tensor:
|
5313 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
5314 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
5315 |
+
if left_padding:
|
5316 |
+
return last_hidden[:, -1]
|
5317 |
+
else:
|
5318 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
5319 |
+
batch_size = last_hidden.shape[0]
|
5320 |
+
return last_hidden[torch.arange(batch_size, device=last_hidden.device), sequence_lengths]
|
5321 |
+
|
5322 |
+
|
5323 |
+
def get_detailed_instruct(task_description: str, query: str) -> str:
|
5324 |
+
return f'Instruct: {task_description}\nQuery: {query}'
|
5325 |
+
|
5326 |
+
|
5327 |
+
# Each query must come with a one-sentence instruction that describes the task
|
5328 |
+
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
5329 |
+
input_texts = [get_detailed_instruct(task, 'how much protein should a female eat'),
|
5330 |
+
get_detailed_instruct(task, 'summit define'),
|
5331 |
+
"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.",
|
5332 |
+
"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."]
|
5333 |
+
|
5334 |
+
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
5335 |
+
model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct')
|
5336 |
+
|
5337 |
+
max_length = 4096
|
5338 |
+
# Tokenize the input texts
|
5339 |
+
batch_dict = tokenizer(input_texts, max_length=max_length - 1, return_attention_mask=False, padding=PaddingStrategy.DO_NOT_PAD, truncation=True)
|
5340 |
+
# append eos_token_id to every input_ids
|
5341 |
+
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
5342 |
+
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
5343 |
+
|
5344 |
+
outputs = model(**batch_dict)
|
5345 |
+
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
5346 |
+
|
5347 |
+
# normalize embeddings
|
5348 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
5349 |
+
scores = (embeddings[:2] @ embeddings[2:].T) * 100
|
5350 |
+
print(scores.tolist())
|
5351 |
+
```
|
5352 |
+
|
5353 |
+
## Supported Languages
|
5354 |
+
|
5355 |
+
This model is initialized from [Mistral-7B-v0.1](https://huggingface.co/mistralai/Mistral-7B-v0.1)
|
5356 |
+
and fine-tuned on a mixture of multilingual datasets.
|
5357 |
+
As a result, it has some multilingual capability.
|
5358 |
+
However, since Mistral-7B-v0.1 is mainly trained on English data, we recommend using this model for English only.
|
5359 |
+
For multilingual use cases, please refer to [multilingual-e5-large](https://huggingface.co/intfloat/multilingual-e5-large).
|
5360 |
+
|
5361 |
+
## MTEB Benchmark Evaluation
|
5362 |
+
|
5363 |
+
Check out [unilm/e5](https://github.com/microsoft/unilm/tree/master/e5) to reproduce evaluation results
|
5364 |
+
on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).
|
5365 |
+
|
5366 |
+
## FAQ
|
5367 |
+
|
5368 |
+
**1. Do I need to add instructions to the query?**
|
5369 |
+
|
5370 |
+
Yes, this is how the model is trained, otherwise you will see a performance degradation.
|
5371 |
+
The task definition should be a one-sentence instruction that describes the task.
|
5372 |
+
This is a way to customize text embeddings for different scenarios through natural language instructions.
|
5373 |
+
|
5374 |
+
On the other hand, there is no need to add instructions to the document side.
|
5375 |
+
|
5376 |
+
**2. Why are my reproduced results slightly different from reported in the model card?**
|
5377 |
+
|
5378 |
+
Different versions of `transformers` and `pytorch` could cause negligible but non-zero performance differences.
|
5379 |
+
|
5380 |
+
## Limitations
|
5381 |
+
|
5382 |
+
Using this model for inputs longer than 4096 tokens is not recommended.
|
5383 |
+
|
5384 |
+
This model's multilingual capability is still inferior to [multilingual-e5-large](https://huggingface.co/intfloat/multilingual-e5-large) for most cases.
|