Views
No views yet
jina-embeddings-v2-base-code is to use Jina AI's Embedding API.jina-embeddings-v2-base-code is an multilingual embedding model speaks English and 30 widely used programming languages.
Same as other jina-embeddings-v2 series, it supports 8192 sequence length.jina-embeddings-v2-base-code is based on a Bert architecture (JinaBert) that supports the symmetric bidirectional variant of ALiBi to allow longer sequence length.
The backbone jina-bert-v2-base-code is pretrained on the github-code dataset.
The model is further trained on Jina AI's collection of more than 150 millions of coding question answer and docstring source code pairs.
These pairs were obtained from various domains and were carefully selected through a thorough cleaning process.jina-embeddings-v2-small-en: 33 million parameters.jina-embeddings-v2-base-en: 137 million parameters.jina-embeddings-v2-base-zh: Chinese-English Bilingual embeddings.jina-embeddings-v2-base-de: German-English Bilingual embeddings.jina-embeddings-v2-base-es: Spanish-English Bilingual embeddings (soon).jina-embeddings-v2-base-code: 161 million parameters code embeddings.mean poooling takes all token embeddings from model output and averaging them at sentence/paragraph level.
It has been proved to be the most effective way to produce high-quality sentence embeddings.
We offer an encode function to deal with this.encode function:1import torch
2import torch.nn.functional as F
3from transformers import AutoTokenizer, AutoModel
4
5def mean_pooling(model_output, attention_mask):
6 token_embeddings = model_output[0]
7 input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
8 return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
9
10sentences = [
11 'How do I access the index while iterating over a sequence with a for loop?',
12 '# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)',
13]
14
15tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embeddings-v2-base-code')
16model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True)
17
18encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
19
20with torch.no_grad():
21 model_output = model(**encoded_input)
22
23embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
24embeddings = F.normalize(embeddings, p=2, dim=1)1!pip install transformers
2from transformers import AutoModel
3from numpy.linalg import norm
4
5cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
6model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-code', trust_remote_code=True)
7embeddings = model.encode(
8 [
9 'How do I access the index while iterating over a sequence with a for loop?',
10 '# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)',
11 ]
12)
13print(cos_sim(embeddings[0], embeddings[1]))
14>>> tensor([[0.7282]])max_length parameter to the encode function:1embeddings = model.encode(
2 ['Very long ... code'],
3 max_length=2048
4)1!pip install -U sentence-transformers
2from sentence_transformers import SentenceTransformer
3from sentence_transformers.util import cos_sim
4
5model = SentenceTransformer(
6 "jinaai/jina-embeddings-v2-base-code",
7 trust_remote_code=True
8)
9
10# control your input sequence length up to 8192
11model.max_seq_length = 1024
12
13embeddings = model.encode([
14 'How do I access the index while iterating over a sequence with a for loop?',
15 '# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)',
16])
17print(cos_sim(embeddings[0], embeddings[1]))1// npm i @xenova/transformers
2import { pipeline, cos_sim } from '@xenova/transformers';
3
4const extractor = await pipeline('feature-extraction', 'jinaai/jina-embeddings-v2-base-code', {
5 quantized: false, // Comment out this line to use the 8-bit quantized version
6});
7
8const texts = [
9 'How do I access the index while iterating over a sequence with a for loop?',
10 '# Use the built-in enumerator\nfor idx, x in enumerate(xs):\n print(idx, x)',
11]
12const embeddings = await extractor(texts, { pooling: 'mean' });
13
14const score = cos_sim(embeddings[0].data, embeddings[1].data);
15console.log(score);
16// 0.7281748759529421