-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
[Model] Add support for Jina Embeddings V4 #20802
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
Open
sigridjineth
wants to merge
24
commits into
vllm-project:main
Choose a base branch
from
sigridjineth:jina-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+655
−1
Open
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9fbc0e9
feat: jina support
sigridjineth eea8462
refactor: fail fast
sigridjineth 5e247e9
refactor: exceptions
sigridjineth 9be40b2
refactor: improve jina embeddings v4 model
sigridjineth 64c06c7
refactor: oom
sigridjineth 56b7409
refactor: Validate lengths match
sigridjineth bef3df2
refactor: normalize
sigridjineth efa8b04
refactor: normalize
sigridjineth 0fe30f8
refactor: review
sigridjineth 062a156
refactor: prehook commits
sigridjineth edfe91a
fix: Apply isort formatting to jina_embeddings_v4.py
5d12bd4
[ci skip-hooks] Formatting attempt(s)
27b28f7
fix: Resolve yapf/isort conflict with disable comments
3bdbd17
refactor: accept review
fafd668
refactor: address review feedback for Jina embeddings V4
0c3f1bd
refactor: import HAS_TRITON from triton_utils instead of local defini…
5c45015
refactor: rename example file to follow existing embedding pattern
9d34781
Merge remote-tracking branch 'origin/main' into jina-support
8e0578a
refactor: update JinaVLForEmbedding to comply with new pooling archit…
eb1497e
refactor: use pooler utility functions to avoid duplicate code
1b4f405
refactor: address maintainer review comments for JinaVLPooler
702fd16
perf: optimize vision token detection using torch.isin
5114a3c
fix: introducing dedicated VisionPooler class
6b501b2
feat: add vision pooling support for jina embeddings v4
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
examples/offline_inference/vision_language_embedding.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
""" | ||
Example of using Jina Embeddings V4 with vLLM for multimodal embeddings. | ||
|
||
This example demonstrates: | ||
1. Text-only embeddings | ||
2. Image-only embeddings | ||
3. Mixed text and image embeddings | ||
""" | ||
|
||
import torch | ||
|
||
from vllm import LLM | ||
from vllm.config import PoolerConfig | ||
from vllm.inputs.data import TextPrompt | ||
from vllm.multimodal.utils import fetch_image | ||
|
||
|
||
def get_embeddings(outputs): | ||
"""Extract and normalize embeddings from model outputs.""" | ||
VISION_START_TOKEN_ID, VISION_END_TOKEN_ID = 151652, 151653 | ||
|
||
embeddings = [] | ||
for output in outputs: | ||
if VISION_START_TOKEN_ID in output.prompt_token_ids: | ||
# For vision inputs, extract only vision token embeddings | ||
img_start_pos = output.prompt_token_ids.index(VISION_START_TOKEN_ID) | ||
img_end_pos = output.prompt_token_ids.index(VISION_END_TOKEN_ID) | ||
embeddings_tensor = output.outputs.data.detach().clone()[ | ||
img_start_pos : img_end_pos + 1 | ||
] | ||
else: | ||
# For text-only inputs, use all token embeddings | ||
embeddings_tensor = output.outputs.data.detach().clone() | ||
|
||
# Pool and normalize embeddings | ||
pooled_output = embeddings_tensor.mean(dim=0, dtype=torch.float32) | ||
embeddings.append(torch.nn.functional.normalize(pooled_output, dim=-1)) | ||
return embeddings | ||
|
||
|
||
def main(): | ||
# Initialize the model | ||
model = LLM( | ||
model="jinaai/jina-embeddings-v4-vllm-retrieval", | ||
task="embed", | ||
override_pooler_config=PoolerConfig(pooling_type="ALL", normalize=False), | ||
dtype="float16", | ||
) | ||
|
||
# Example 1: Text-only embeddings | ||
print("=== Text Embeddings ===") | ||
query = "Overview of climate change impacts on coastal cities" | ||
query_prompt = TextPrompt(prompt=f"Query: {query}") | ||
|
||
passage = """The impacts of climate change on coastal cities are significant | ||
and multifaceted. Rising sea levels threaten infrastructure, while increased | ||
storm intensity poses risks to populations and economies.""" | ||
passage_prompt = TextPrompt(prompt=f"Passage: {passage}") | ||
|
||
# Generate embeddings | ||
text_outputs = model.encode([query_prompt, passage_prompt]) | ||
text_embeddings = get_embeddings(text_outputs) | ||
|
||
# Calculate similarity | ||
similarity = torch.dot(text_embeddings[0], text_embeddings[1]).item() | ||
print(f"Query: {query[:50]}...") | ||
print(f"Passage: {passage[:50]}...") | ||
print(f"Similarity: {similarity:.4f}\n") | ||
|
||
# Example 2: Image embeddings | ||
print("=== Image Embeddings ===") | ||
# Fetch sample images | ||
image1_url = "https://raw.githubusercontent.com/jina-ai/multimodal-reranker-test/main/handelsblatt-preview.png" | ||
image2_url = "https://raw.githubusercontent.com/jina-ai/multimodal-reranker-test/main/paper-11.png" | ||
|
||
image1 = fetch_image(image1_url) | ||
image2 = fetch_image(image2_url) | ||
|
||
# Create image prompts with the required format | ||
image1_prompt = TextPrompt( | ||
prompt="<|im_start|>user\n<|vision_start|><|image_pad|>" | ||
"<|vision_end|>Describe the image.<|im_end|>\n", | ||
multi_modal_data={"image": image1}, | ||
) | ||
|
||
image2_prompt = TextPrompt( | ||
prompt="<|im_start|>user\n<|vision_start|><|image_pad|>" | ||
"<|vision_end|>Describe the image.<|im_end|>\n", | ||
multi_modal_data={"image": image2}, | ||
) | ||
|
||
# Generate embeddings | ||
image_outputs = model.encode([image1_prompt, image2_prompt]) | ||
image_embeddings = get_embeddings(image_outputs) | ||
|
||
# Calculate similarity | ||
similarity = torch.dot(image_embeddings[0], image_embeddings[1]).item() | ||
print(f"Image 1: {image1_url.split('/')[-1]}") | ||
print(f"Image 2: {image2_url.split('/')[-1]}") | ||
print(f"Similarity: {similarity:.4f}\n") | ||
|
||
# Example 3: Cross-modal similarity (text vs image) | ||
print("=== Cross-modal Similarity ===") | ||
query = "scientific paper with markdown formatting" | ||
query_prompt = TextPrompt(prompt=f"Query: {query}") | ||
|
||
# Generate embeddings for text query and second image | ||
cross_outputs = model.encode([query_prompt, image2_prompt]) | ||
cross_embeddings = get_embeddings(cross_outputs) | ||
|
||
# Calculate cross-modal similarity | ||
similarity = torch.dot(cross_embeddings[0], cross_embeddings[1]).item() | ||
print(f"Text query: {query}") | ||
print(f"Image: {image2_url.split('/')[-1]}") | ||
print(f"Cross-modal similarity: {similarity:.4f}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, my bad. I meant vision_language_pooling.py, we just renamed that file from vision_language_embedding.py recently. 😅