intfloat commited on
Commit
07163b7
1 Parent(s): 4111085

Simplify usage; integrate Sentence Transformers (+ LlamaIndex/LangChain, etc.) (#39)

Browse files

- Add "add_eos_token" to tokenizer config; simplify usage (a5e1612ae784f73eba634b00acf64db7d99ad7e9)
- Integrate with Sentence Transformers + 3rd parties (90cddb89718db232ed6257f69b36e9e601120883)
- Reduce sentence-transformers version to prevent "Upgrade your ST" warning (84b20972705a9d8c825bdf39a81acac66a10d701)

1_Pooling/config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 4096,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": false,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false,
7
+ "pooling_mode_weightedmean_tokens": false,
8
+ "pooling_mode_lasttoken": true,
9
+ "include_prompt": true
10
+ }
README.md CHANGED
@@ -1,6 +1,8 @@
1
  ---
2
  tags:
3
  - mteb
 
 
4
  model-index:
5
  - name: e5-mistral-7b-instruct
6
  results:
@@ -6851,6 +6853,36 @@ This model has 32 layers and the embedding size is 4096.
6851
 
6852
  Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
6853
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6854
  ```python
6855
  import torch
6856
  import torch.nn.functional as F
@@ -6892,10 +6924,7 @@ model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct')
6892
 
6893
  max_length = 4096
6894
  # Tokenize the input texts
6895
- batch_dict = tokenizer(input_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
6896
- # append eos_token_id to every input_ids
6897
- batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
6898
- batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
6899
 
6900
  outputs = model(**batch_dict)
6901
  embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
 
1
  ---
2
  tags:
3
  - mteb
4
+ - sentence-transformers
5
+ - transformers
6
  model-index:
7
  - name: e5-mistral-7b-instruct
8
  results:
 
6853
 
6854
  Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
6855
 
6856
+ ### Sentence Transformers
6857
+
6858
+ ```python
6859
+ from sentence_transformers import SentenceTransformer
6860
+
6861
+ model = SentenceTransformer("intfloat/e5-mistral-7b-instruct")
6862
+ # In case you want to reduce the maximum sequence length:
6863
+ model.max_seq_length = 4096
6864
+
6865
+ queries = [
6866
+ "how much protein should a female eat",
6867
+ "summit define",
6868
+ ]
6869
+ documents = [
6870
+ "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.",
6871
+ "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."
6872
+ ]
6873
+
6874
+ query_embeddings = model.encode(queries, prompt_name="web_search_query")
6875
+ document_embeddings = model.encode(documents)
6876
+
6877
+ scores = (query_embeddings @ document_embeddings.T) * 100
6878
+ print(scores.tolist())
6879
+ ```
6880
+
6881
+ Have a look at [config_sentence_transformers.json](config_sentence_transformers.json) for the prompts that are pre-configured, such as `web_search_query`, `sts_query`, and `summarization_query`. Additionally, check out [unilm/e5/utils.py](https://github.com/microsoft/unilm/blob/9c0f1ff7ca53431fe47d2637dfe253643d94185b/e5/utils.py#L106) for prompts we used for evaluation. You can use these via e.g. `model.encode(queries, prompt="Instruct: Given a claim, find documents that refute the claim\nQuery: ")`.
6882
+
6883
+
6884
+ ### Transformers
6885
+
6886
  ```python
6887
  import torch
6888
  import torch.nn.functional as F
 
6924
 
6925
  max_length = 4096
6926
  # Tokenize the input texts
6927
+ batch_dict = tokenizer(input_texts, max_length=max_length, padding=True, truncation=True, return_tensors='pt')
 
 
 
6928
 
6929
  outputs = model(**batch_dict)
6930
  embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
config_sentence_transformers.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "__version__": {
3
+ "sentence_transformers": "2.7.0",
4
+ "transformers": "4.39.3",
5
+ "pytorch": "2.1.0+cu121"
6
+ },
7
+ "prompts": {
8
+ "web_search_query": "Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: ",
9
+ "sts_query": "Instruct: Retrieve semantically similar text.\nQuery: ",
10
+ "summarization_query": "Instruct: Given a news summary, retrieve other semantically similar summaries\nQuery: ",
11
+ "bitext_query": "Instruct: Retrieve parallel sentences.\nQuery: "
12
+ },
13
+ "default_prompt_name": null
14
+ }
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.models.Normalize"
19
+ }
20
+ ]
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 32768,
3
+ "do_lower_case": false
4
+ }
tokenizer_config.json CHANGED
@@ -1,4 +1,5 @@
1
  {
 
2
  "added_tokens_decoder": {
3
  "0": {
4
  "content": "<unk>",
 
1
  {
2
+ "add_eos_token": true,
3
  "added_tokens_decoder": {
4
  "0": {
5
  "content": "<unk>",