Views
No views yet

jina-embeddings-v4 with the code adapter merged into the base Qwen2.5-VL weights. This architecture modification enables native compatibility with vLLM without requiring custom adapter-handling code.1import torch
2from PIL import Image
3
4from vllm import LLM
5from vllm.config import PoolerConfig
6from vllm.inputs.data import TextPrompt
7
8# Initialize model
9model = LLM(
10 model="jinaai/jina-embeddings-v4-vllm-code",
11 task="embed",
12 override_pooler_config=PoolerConfig(pooling_type="ALL", normalize=False),
13 dtype="float16",
14)
15
16# Create text prompts
17query =query = "Find a function that prints a greeting message to the console"
18query_prompt = TextPrompt(
19 prompt=f"Query: {query}"
20)
21
22passage = "def hello_world():\n print('Hello, World!')"
23passage_prompt = TextPrompt(
24 prompt=f"Passage: {passage}"
25)
26
27# Create image prompt
28image = Image.open("<path_to_image>")
29image_prompt = TextPrompt(
30 prompt="<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe the image.<|im_end|>\n",
31 multi_modal_data={"image": image},
32)
33
34# Encode all prompts
35prompts = [query_prompt, passage_prompt, image_prompt]
36outputs = model.encode(prompts)
37
38
39def get_embeddings(outputs):
40 VISION_START_TOKEN_ID, VISION_END_TOKEN_ID = 151652, 151653
41
42 embeddings = []
43 for output in outputs:
44 if VISION_START_TOKEN_ID in output.prompt_token_ids:
45 # Gather only vision tokens
46 img_start_pos = torch.where(
47 torch.tensor(output.prompt_token_ids) == VISION_START_TOKEN_ID
48 )[0][0]
49 img_end_pos = torch.where(
50 torch.tensor(output.prompt_token_ids) == VISION_END_TOKEN_ID
51 )[0][0]
52 embeddings_tensor = output.outputs.data.detach().clone()[
53 img_start_pos : img_end_pos + 1
54 ]
55 else:
56 # Use all tokens for text-only prompts
57 embeddings_tensor = output.outputs.data.detach().clone()
58
59 # Pool and normalize embeddings
60 pooled_output = (
61 embeddings_tensor.sum(dim=0, dtype=torch.float32)
62 / embeddings_tensor.shape[0]
63 )
64 embeddings.append(torch.nn.functional.normalize(pooled_output, dim=-1))
65 return embeddings
66
67embeddings = get_embeddings(outputs)