Skip to content

new: Added jina embedding v3#428

Merged
joein merged 23 commits into
mainfrom
support-jina-embedding-v3
Jan 27, 2025
Merged

new: Added jina embedding v3#428
joein merged 23 commits into
mainfrom
support-jina-embedding-v3

Conversation

@hh-space-invader

@hh-space-invader hh-space-invader commented Dec 19, 2024

Copy link
Copy Markdown
Contributor

To compute canonical vectors:

import onnxruntime
import numpy as np
from transformers import AutoTokenizer, PretrainedConfig


def onnx_embed(session: onnxruntime.InferenceSession, task_id: int):
	inputs = {
		'input_ids': input_text['input_ids'],
		'attention_mask': input_text['attention_mask'],
		'task_id': np.array(task_id, dtype=np.int64)
	}
	outputs = session.run(None, inputs)[0]
	embeddings = mean_pooling(outputs, input_text["attention_mask"])
	embeddings = embeddings / np.linalg.norm(embeddings, ord=2, axis=1, keepdims=True)
	return embeddings

def mean_pooling(model_output: np.ndarray, attention_mask: np.ndarray):
    token_embeddings = model_output
    input_mask_expanded = np.expand_dims(attention_mask, axis=-1)
    input_mask_expanded = np.broadcast_to(input_mask_expanded, token_embeddings.shape)
    sum_embeddings = np.sum(token_embeddings * input_mask_expanded, axis=1)
    sum_mask = np.clip(np.sum(input_mask_expanded, axis=1), a_min=1e-9, a_max=None)
    return sum_embeddings / sum_mask

docs = [
    "Hello World",
    "Follow the white rabbit."
]
model_name = 'jinaai/jina-embeddings-v3'
tokenizer = AutoTokenizer.from_pretrained(model_name)

config = PretrainedConfig.from_pretrained(model_name)

input_text = tokenizer(docs, return_tensors='np', padding=True, truncation=True)
input_ids = input_text['input_ids']
attention_mask = input_text['attention_mask']

model_path = 'models/models--jinaai--jina-embeddings-v3/snapshots/62a81741b58448ed8f691764cec7aa5d3c045e4c/onnx/model.onnx'
session = onnxruntime.InferenceSession(model_path)

for task_id in range(5):
	embeddings = onnx_embed(session, task_id)
	print([[round(value, 4) for value in e.tolist()[:5]] for e in embeddings])

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  • Does your submission pass the existing tests?
  • Have you added tests for your feature?
  • Have you installed pre-commit with pip3 install pre-commit and set up hooks with pre-commit install?

New models submission:

  • Have you added an explanation of why it's important to include this model?
  • Have you added tests for the new model? Were canonical values for tests computed via the original model?
  • Have you added the code snippet for how canonical values were computed?
  • Have you successfully ran tests with your changes locally?

@joein joein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. tests
  2. license
  3. users can't change task id in this setting, since we are not propagating kwargs to onnx_embed, they are always hard-coded

Comment thread fastembed/text/multitask_embedding.py Outdated
@hh-space-invader hh-space-invader changed the title (WIP) new: Added jina embedding v3 new: Added jina embedding v3 Dec 23, 2024
Comment thread fastembed/text/multitask_embedding.py Outdated
Comment thread fastembed/text/multitask_embedding.py Outdated
Comment thread fastembed/text/multitask_embedding.py Outdated
Comment thread tests/test_text_multitask_embeddings.py
Comment thread tests/test_text_multitask_embeddings.py

@joein joein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lacks tests of propagation task ids in parallel processing

Comment thread fastembed/text/multitask_embedding.py Outdated
@hh-space-invader hh-space-invader requested a review from joein January 5, 2025 23:12
Comment thread tests/test_text_multitask_embeddings.py
Comment thread fastembed/text/multitask_embedding.py Outdated
@hh-space-invader hh-space-invader requested a review from joein January 8, 2025 06:53

@joein joein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please upload scripts for canonical vectors computation to the dedicated repo?

Comment thread fastembed/text/multitask_embedding.py Outdated
@hh-space-invader hh-space-invader force-pushed the support-jina-embedding-v3 branch from 7a4acc7 to f91ad9b Compare January 20, 2025 03:36
@joein joein force-pushed the support-jina-embedding-v3 branch from c84ef3a to fee54d0 Compare January 27, 2025 21:42
@joein joein merged commit b05877d into main Jan 27, 2025
@joein joein deleted the support-jina-embedding-v3 branch January 27, 2025 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants