Skip to content
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

new: Added jina embedding v3 #428

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

hh-space-invader
Copy link
Contributor

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

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?

Copy link
Member

@joein joein left a comment

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

fastembed/text/multitask_embedding.py Outdated Show resolved Hide resolved
@hh-space-invader hh-space-invader changed the title (WIP) new: Added jina embedding v3 new: Added jina embedding v3 Dec 23, 2024
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