Update README.md
Browse files
README.md
CHANGED
|
@@ -57,38 +57,7 @@ The following Python packages are required:
|
|
| 57 |
</details>
|
| 58 |
|
| 59 |
<details>
|
| 60 |
-
<summary>via <a href="https://huggingface.co/docs/transformers/en/index">transformers</a
|
| 61 |
-
|
| 62 |
-
```python
|
| 63 |
-
# !pip install transformers>=4.53.0 torch>=2.7.1
|
| 64 |
-
|
| 65 |
-
from transformers import AutoModel
|
| 66 |
-
import torch
|
| 67 |
-
|
| 68 |
-
# Initialize the model
|
| 69 |
-
model = AutoModel.from_pretrained("jinaai/jina-code-embeddings-1.5b", trust_remote_code=True)
|
| 70 |
-
model.to("cuda")
|
| 71 |
-
|
| 72 |
-
# Configure truncate_dim, max_length, batch_size in the encode function if needed
|
| 73 |
-
|
| 74 |
-
# Encode query
|
| 75 |
-
query_embeddings = model.encode(
|
| 76 |
-
["print hello world in python"],
|
| 77 |
-
task="nl2code",
|
| 78 |
-
prompt_name="query",
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
# Encode passage
|
| 82 |
-
passage_embeddings = model.encode(
|
| 83 |
-
["print('Hello World!')"],
|
| 84 |
-
task="nl2code",
|
| 85 |
-
prompt_name="passage",
|
| 86 |
-
)
|
| 87 |
-
```
|
| 88 |
-
</details>
|
| 89 |
-
|
| 90 |
-
<details>
|
| 91 |
-
<summary> via <a href="https://huggingface.co/docs/transformers/en/index">transformers</a> (using Qwen2Model without trust_remote_code)</summary>
|
| 92 |
|
| 93 |
```python
|
| 94 |
# !pip install transformers>=4.53.0 torch>=2.7.1
|
|
@@ -96,8 +65,7 @@ passage_embeddings = model.encode(
|
|
| 96 |
import torch
|
| 97 |
import torch.nn.functional as F
|
| 98 |
|
| 99 |
-
from transformers
|
| 100 |
-
from transformers.models.qwen2.tokenization_qwen2_fast import Qwen2TokenizerFast
|
| 101 |
|
| 102 |
INSTRUCTION_CONFIG = {
|
| 103 |
"nl2code": {
|
|
@@ -152,8 +120,8 @@ documents = [
|
|
| 152 |
]
|
| 153 |
all_inputs = queries + documents
|
| 154 |
|
| 155 |
-
tokenizer =
|
| 156 |
-
model =
|
| 157 |
|
| 158 |
batch_dict = tokenizer(
|
| 159 |
all_inputs,
|
|
@@ -170,6 +138,9 @@ passage_embeddings = embeddings[2:]
|
|
| 170 |
|
| 171 |
# Compute the (cosine) similarity between the query and document embeddings
|
| 172 |
scores = cosine_similarity(query_embeddings, passage_embeddings)
|
|
|
|
|
|
|
|
|
|
| 173 |
```
|
| 174 |
</details>
|
| 175 |
|
|
@@ -189,7 +160,8 @@ model = SentenceTransformer(
|
|
| 189 |
"torch_dtype": torch.bfloat16,
|
| 190 |
"attn_implementation": "flash_attention_2",
|
| 191 |
"device_map": "cuda"
|
| 192 |
-
}
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
# The queries and documents to embed
|
|
@@ -208,8 +180,8 @@ document_embeddings = model.encode(documents, prompt_name="nl2code_document")
|
|
| 208 |
# Compute the (cosine) similarity between the query and document embeddings
|
| 209 |
similarity = model.similarity(query_embeddings, document_embeddings)
|
| 210 |
print(similarity)
|
| 211 |
-
# tensor([[0.
|
| 212 |
-
# [0.
|
| 213 |
```
|
| 214 |
</details>
|
| 215 |
|
|
@@ -267,7 +239,6 @@ all_inputs = queries + documents
|
|
| 267 |
# vLLM embedding model
|
| 268 |
llm = LLM(
|
| 269 |
model="jinaai/jina-code-embeddings-1.5b",
|
| 270 |
-
hf_overrides={"architectures": ["Qwen2ForCausalLM"]},
|
| 271 |
task="embed"
|
| 272 |
)
|
| 273 |
|
|
@@ -288,6 +259,9 @@ passage_embeddings = embeddings[n_q:]
|
|
| 288 |
|
| 289 |
# Cosine similarity matrix (queries x documents)
|
| 290 |
scores = cosine_similarity(query_embeddings, passage_embeddings)
|
|
|
|
|
|
|
|
|
|
| 291 |
```
|
| 292 |
|
| 293 |
</details>
|
|
|
|
| 57 |
</details>
|
| 58 |
|
| 59 |
<details>
|
| 60 |
+
<summary>via <a href="https://huggingface.co/docs/transformers/en/index">transformers</a></summary>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
```python
|
| 63 |
# !pip install transformers>=4.53.0 torch>=2.7.1
|
|
|
|
| 65 |
import torch
|
| 66 |
import torch.nn.functional as F
|
| 67 |
|
| 68 |
+
from transformers import AutoModel, AutoTokenizer
|
|
|
|
| 69 |
|
| 70 |
INSTRUCTION_CONFIG = {
|
| 71 |
"nl2code": {
|
|
|
|
| 120 |
]
|
| 121 |
all_inputs = queries + documents
|
| 122 |
|
| 123 |
+
tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-code-embeddings-1.5b')
|
| 124 |
+
model = AutoModel.from_pretrained('jinaai/jina-code-embeddings-1.5b')
|
| 125 |
|
| 126 |
batch_dict = tokenizer(
|
| 127 |
all_inputs,
|
|
|
|
| 138 |
|
| 139 |
# Compute the (cosine) similarity between the query and document embeddings
|
| 140 |
scores = cosine_similarity(query_embeddings, passage_embeddings)
|
| 141 |
+
print(scores)
|
| 142 |
+
# tensor([[0.8168, 0.1236],
|
| 143 |
+
# [0.1204, 0.5525]], grad_fn=<MmBackward0>)
|
| 144 |
```
|
| 145 |
</details>
|
| 146 |
|
|
|
|
| 160 |
"torch_dtype": torch.bfloat16,
|
| 161 |
"attn_implementation": "flash_attention_2",
|
| 162 |
"device_map": "cuda"
|
| 163 |
+
},
|
| 164 |
+
tokenizer_kwargs={"padding_side": "left"},
|
| 165 |
)
|
| 166 |
|
| 167 |
# The queries and documents to embed
|
|
|
|
| 180 |
# Compute the (cosine) similarity between the query and document embeddings
|
| 181 |
similarity = model.similarity(query_embeddings, document_embeddings)
|
| 182 |
print(similarity)
|
| 183 |
+
# tensor([[0.8169, 0.1214],
|
| 184 |
+
# [0.1190, 0.5500]])
|
| 185 |
```
|
| 186 |
</details>
|
| 187 |
|
|
|
|
| 239 |
# vLLM embedding model
|
| 240 |
llm = LLM(
|
| 241 |
model="jinaai/jina-code-embeddings-1.5b",
|
|
|
|
| 242 |
task="embed"
|
| 243 |
)
|
| 244 |
|
|
|
|
| 259 |
|
| 260 |
# Cosine similarity matrix (queries x documents)
|
| 261 |
scores = cosine_similarity(query_embeddings, passage_embeddings)
|
| 262 |
+
print(scores)
|
| 263 |
+
# tensor([[0.8171, 0.1230],
|
| 264 |
+
# [0.1207, 0.5513]])
|
| 265 |
```
|
| 266 |
|
| 267 |
</details>
|