bwang0911 commited on
Commit
0e973a2
1 Parent(s): 452d9cc

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +124 -0
README.md ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ pipeline_tag: feature-extraction
6
+ tags:
7
+ - code
8
+ ---
9
+
10
+ <br><br>
11
+
12
+ <p align="center">
13
+ <img src="https://github.com/jina-ai/finetuner/blob/main/docs/_static/finetuner-logo-ani.svg?raw=true" alt="Finetuner logo: Finetuner helps you to create experiments in order to improve embeddings on search tasks. It accompanies you to deliver the last mile of performance-tuning for neural search applications." width="150px">
14
+ </p>
15
+
16
+
17
+ <p align="center">
18
+ <b>The text embedding set trained by <a href="https://jina.ai/"><b>Jina AI</b></a>, <a href="https://github.com/jina-ai/finetuner"><b>Finetuner</b></a> team.</b>
19
+ </p>
20
+
21
+
22
+ ## Intended Usage & Model Info
23
+
24
+ `jina-embeddings-v2-base-code` is an multilingual **embedding model** speaks English and 29 widely used programming languages supporting **8192 sequence length**.
25
+ It is based on a Bert architecture (JinaBert) that supports the symmetric bidirectional variant of [ALiBi](https://arxiv.org/abs/2108.12409) to allow longer sequence length.
26
+ The backbone `jina-bert-v2-base-code` is pretrained on the [github-code](https://huggingface.co/datasets/codeparrot/github-code) dataset.
27
+ The model is further trained on Jina AI's collection of more than 150 millions of coding question answer and docstring source code pairs.
28
+ These pairs were obtained from various domains and were carefully selected through a thorough cleaning process.
29
+
30
+ The embedding model was trained using 512 sequence length, but extrapolates to 8k sequence length (or even longer) thanks to ALiBi.
31
+ This makes our model useful for a range of use cases, especially when processing long documents is needed, including technical question answering and code search.
32
+
33
+ This model has 137 million parameters, which enables fast and memory efficient inference, while delivering impressive performance.
34
+ Additionally, we provide the following embedding models:
35
+
36
+ **V1 (Based on T5, 512 Seq)**
37
+
38
+ - [`jina-embeddings-v1-small-en`](https://huggingface.co/jinaai/jina-embedding-s-en-v1): 35 million parameters.
39
+ - [`jina-embeddings-v1-base-en`](https://huggingface.co/jinaai/jina-embedding-b-en-v1): 110 million parameters.
40
+ - [`jina-embeddings-v1-large-en`](https://huggingface.co/jinaai/jina-embedding-l-en-v1): 330 million parameters.
41
+
42
+ **V2 (Based on JinaBert, 8k Seq)**
43
+
44
+ - [`jina-embeddings-v2-small-en`](https://huggingface.co/jinaai/jina-embeddings-v2-small-en): 33 million parameters.
45
+ - [`jina-embeddings-v2-base-en`](https://huggingface.co/jinaai/jina-embeddings-v2-base-en): 137 million parameters.
46
+ - [`jina-embeddings-v2-base-code`](https://huggingface.co/jinaai/jina-embeddings-v2-base-code): 137 million parameters **(you are here)**.
47
+
48
+ ## Data & Parameters
49
+
50
+ Jina Embeddings V2 [technical report](https://arxiv.org/abs/2310.19923)
51
+
52
+ ## Usage
53
+
54
+ **<details><summary>Please apply mean pooling when integrating the model.</summary>**
55
+ <p>
56
+
57
+ ### Why mean pooling?
58
+
59
+ `mean poooling` takes all token embeddings from model output and averaging them at sentence/paragraph level.
60
+ It has been proved to be the most effective way to produce high-quality sentence embeddings.
61
+ We offer an `encode` function to deal with this.
62
+
63
+ However, if you would like to do it without using the default `encode` function:
64
+
65
+ ```python
66
+ import torch
67
+ import torch.nn.functional as F
68
+ from transformers import AutoTokenizer, AutoModel
69
+
70
+ def mean_pooling(model_output, attention_mask):
71
+ token_embeddings = model_output[0]
72
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
73
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
74
+
75
+ sentences = ['How is the weather today?', 'What is the current weather like today?']
76
+
77
+ tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-small-en')
78
+ model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-small-en', trust_remote_code=True)
79
+
80
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
81
+
82
+ with torch.no_grad():
83
+ model_output = model(**encoded_input)
84
+
85
+ embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
86
+ embeddings = F.normalize(embeddings, p=2, dim=1)
87
+ ```
88
+
89
+ </p>
90
+ </details>
91
+
92
+ You can use Jina Embedding models directly from transformers package:
93
+ ```python
94
+ !pip install transformers
95
+ from transformers import AutoModel
96
+ from numpy.linalg import norm
97
+
98
+ cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
99
+ model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True) # trust_remote_code is needed to use the encode method
100
+ embeddings = model.encode(['How is the weather today?', 'What is the current weather like today?'])
101
+ print(cos_sim(embeddings[0], embeddings[1]))
102
+ ```
103
+
104
+ If you only want to handle shorter sequence, such as 2k, pass the `max_length` parameter to the `encode` function:
105
+
106
+ ```python
107
+ embeddings = model.encode(
108
+ ['Very long ... document'],
109
+ max_length=2048
110
+ )
111
+ ```
112
+
113
+ ## Fully-managed Embeddings Service
114
+
115
+ Alternatively, you can use Jina AI's [Embeddings platform](https://jina.ai/embeddings/) for fully-managed access to Jina Embeddings models.
116
+
117
+ ## Plans
118
+
119
+ The development of new bilingual models is currently underway. We will be targeting mainly the German and Spanish languages.
120
+ The upcoming models will be called `jina-embeddings-v2-small-de/es`.
121
+
122
+ ## Contact
123
+
124
+ Join our [Discord community](https://discord.jina.ai) and chat with other community members about ideas.