-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Add Jina-Embeddings-V3 Model
#44251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Jina-Embeddings-V3 Model
#44251
Changes from 65 commits
05e2cb3
331f3b4
1c32317
4014ee3
efe5a39
2524630
3a33e40
2dafe59
baa7d91
5dcd47f
6fb7d95
a9575dd
1ba2c54
632820f
b2a4ec5
284ffc6
caee0ba
bad2fc2
ec81999
ba3390f
e0a90bf
ade3631
c6ed80f
a199bf6
b44f90b
6cf10ed
fe0c7cb
00b93db
af6f908
05d4190
8bc7dfa
95e05f6
c92493a
e3a7c79
d5e49ce
081af3d
32a1722
5dee40f
c3a26e9
486aae1
74922d3
0bb18c9
45844f4
03f4375
901d7a9
b0cba60
8988ceb
66b3b2e
a0d4011
ca3a9d5
f95b90d
457e75e
d561f86
76a2807
a8fabb2
75ffec8
9c53b0f
d31585f
1b7540c
94e3b2d
c4f7cc6
0da360e
0d36657
c4072d8
b71bda5
5bce77a
ae8ee84
b0df7cd
3c2efc2
f71b4ba
3d38dbe
6a1c9dc
0f37b73
e5c6f35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| <!--Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
| rendered properly in your Markdown viewer. | ||
| --> | ||
|
|
||
| *This model was released on 2024-09-16 and added to Hugging Face Transformers on 2026-03-18.* | ||
|
|
||
| <div style="float: right;"> | ||
| <div class="flex flex-wrap space-x-1"> | ||
| <img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-DE3412?style=flat&logo=pytorch&logoColor=white" > | ||
| <img alt="FlashAttention" src="https://img.shields.io/badge/%E2%9A%A1%EF%B8%8E%20FlashAttention-eae0c8?style=flat"> | ||
| <img alt="SDPA" src="https://img.shields.io/badge/SDPA-DE3412?style=flat&logo=pytorch&logoColor=white"> | ||
| </div> | ||
| </div> | ||
|
|
||
|
|
||
| # JinaEmbeddingsV3 | ||
|
|
||
| The [Jina-Embeddings-v3](https://huggingface.co/papers/2409.10173) is a multilingual, multi-task text embedding model designed for a variety of NLP applications. Based on the XLM-RoBERTa architecture, this model supports **Rotary Position Embeddings (RoPE)** replacing absolute position embeddings to support long input sequences up to 8192 tokens. Additionally, it features 5 built-in **Task-Specific LoRA Adapters:** that allow the model to generate task-specific embeddings (e.g., for retrieval vs. classification) without increasing inference latency significantly. | ||
|
|
||
|
|
||
| You can find the original Jina Embeddings v3 checkpoints under the [Jina AI](https://huggingface.co/jinaai) organization. | ||
|
|
||
|
|
||
| > [!TIP] | ||
| > Click on the Jina Embeddings v3 models in the right sidebar for more examples of how to apply the model to different language tasks. | ||
|
|
||
| The example below demonstrates how to extract features (embeddings) with [`Pipeline`], [`AutoModel`], and from the command line. | ||
|
|
||
| <hfoptions id="usage"> | ||
| <hfoption id="Pipeline"> | ||
|
|
||
| ```py | ||
| import torch | ||
| from transformers import pipeline | ||
|
|
||
| pipeline = pipeline( | ||
|
vasqu marked this conversation as resolved.
|
||
| task="feature-extraction", | ||
| model="jinaai/jina-embeddings-v3", | ||
| revision="refs/pr/137", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw I've talked internally with people from Jina AI, we will create another repo with the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. You can ping me once you create a new repo with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Sai-Suraj-27 thanks for the effort, we created an hf repository here with your PR: https://huggingface.co/jinaai/jina-embeddings-v3-hf |
||
| ) | ||
| # Returns a list of lists containing the embeddings for each token | ||
| embeddings = pipeline("Jina Embeddings V3 is great for semantic search.") | ||
| ``` | ||
|
|
||
|
|
||
| </hfoption> | ||
| <hfoption id="AutoModel"> | ||
|
|
||
|
|
||
| ```py | ||
| import torch | ||
| from transformers import AutoModel, AutoTokenizer | ||
|
|
||
| tokenizer = AutoTokenizer.from_pretrained("jinaai/jina-embeddings-v3") | ||
| model = AutoModel.from_pretrained("jinaai/jina-embeddings-v3", device_map="auto", revision="refs/pr/137") | ||
|
|
||
| prompt = "Jina Embeddings V3 is great for semantic search." | ||
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | ||
|
|
||
| with torch.no_grad(): | ||
| outputs = model(**inputs) | ||
| # The base AutoModel returns the raw hidden states for all tokens | ||
| last_hidden_states = outputs.last_hidden_state | ||
|
|
||
| print(f"Features shape: {last_hidden_states.shape}") | ||
| ``` | ||
|
|
||
| </hfoptions> | ||
| </hfoptions> | ||
|
|
||
| ## Task-Specific LoRA Adapters | ||
|
|
||
| A key feature of `JinaEmbeddingsV3` is it's LoRA adapters, which allow you to tailor the output embeddings to specific useful use cases without the overhead of loading entirely different models. | ||
|
|
||
| The following tasks are supported: | ||
|
|
||
| * **`retrieval.query`**: Used for query embeddings in asymmetric retrieval tasks (e.g., search queries). | ||
| * **`retrieval.passage`**: Used for passage embeddings in asymmetric retrieval tasks (e.g., the documents being searched). | ||
| * **`separation`**: Used for embeddings in clustering and re-ranking applications. | ||
| * **`classification`**: Used for embeddings in classification tasks. | ||
| * **`text-matching`**: Used for embeddings in tasks that quantify similarity between two texts, such as Semantic Textual Similarity (STS) or symmetric retrieval tasks. | ||
|
|
||
|
|
||
| To generate high-quality sentence or paragraph embeddings, you need to apply **mean pooling** to the model's token embeddings. Mean pooling takes all token embeddings from the model's output and averages them, masking out the padding tokens. | ||
|
|
||
| Here is how you can generate sentence embeddings tailored for a retrieval query task using the `AutoModel` API. | ||
|
|
||
| ```python | ||
| import torch | ||
| import torch.nn.functional as F | ||
| from transformers import AutoTokenizer, AutoModel | ||
|
|
||
| def mean_pooling(model_output, attention_mask): | ||
| # First element of model_output contains all token embeddings | ||
| token_embeddings = model_output[0] | ||
| input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() | ||
|
|
||
| # Sum the embeddings and divide by the number of non-padding tokens | ||
| sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1) | ||
| sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9) | ||
| return sum_embeddings / sum_mask | ||
|
Sai-Suraj-27 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| sentences = [ | ||
| "How is the weather today?", | ||
| "What is the current weather like today?" | ||
| ] | ||
|
|
||
| tokenizer = AutoTokenizer.from_pretrained("jinaai/jina-embeddings-v3") | ||
| model = AutoModel.from_pretrained("jinaai/jina-embeddings-v3", revision="refs/pr/137") | ||
|
|
||
| encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors="pt").to(model.device) | ||
|
|
||
| # Set up the adapter mask for your specific task | ||
| task = 'retrieval_query' # Can be any of (retrieval_passage, separation, classification, text_matching) depending on the use-case. | ||
|
|
||
| model.load_adapter("jinaai/jina-embeddings-v3", adapter_name=task, adapter_kwargs={"subfolder": task, "revision": "refs/pr/137"}) | ||
|
Sai-Suraj-27 marked this conversation as resolved.
Outdated
|
||
|
|
||
| model.set_adapter(task) | ||
|
|
||
| with torch.no_grad(): | ||
| model_output = model(**encoded_input) | ||
|
|
||
| embeddings = mean_pooling(model_output, encoded_input["attention_mask"]) | ||
| embeddings = F.normalize(embeddings, p=2, dim=1) | ||
|
|
||
| print(embeddings.shape) | ||
| # Output: torch.Size([2, 1024]) | ||
| ``` | ||
|
|
||
|
|
||
| ## JinaEmbeddingsV3Config | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3Config | ||
|
|
||
| ## JinaEmbeddingsV3Model | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3Model | ||
| - forward | ||
|
|
||
| ## JinaEmbeddingsV3ForMaskedLM | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3ForMaskedLM | ||
| - forward | ||
|
|
||
| ## JinaEmbeddingsV3ForSequenceClassification | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3ForSequenceClassification | ||
| - forward | ||
|
|
||
| ## JinaEmbeddingsV3ForTokenClassification | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3ForTokenClassification | ||
| - forward | ||
|
|
||
| ## JinaEmbeddingsV3ForQuestionAnswering | ||
|
|
||
| [[autodoc]] JinaEmbeddingsV3ForQuestionAnswering | ||
| - forward | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Copyright 2026 The HuggingFace Team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
|
|
||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils import _LazyModule | ||
| from ...utils.import_utils import define_import_structure | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration_jina_embeddings_v3 import * | ||
| from .modeling_jina_embeddings_v3 import * | ||
| else: | ||
| import sys | ||
|
|
||
| _file = globals()["__file__"] | ||
| sys.modules[__name__] = _LazyModule(__name__, _file, define_import_structure(_file), module_spec=__spec__) |
Uh oh!
There was an error while loading. Please reload this page.