--- tags: - feature-extraction - sentence-similarity - sentence-transformers - transformers - mteb license: gemma --- # BAAI-Multilingual-Large **BAAI-Multilingual-Large** is a LLM-based multilingual text embedding model. It is trained on a diverse range of languages and tasks based on [google/gemma-2-9b](https://huggingface.co/google/gemma-2-9b). BAAI-Multilingual-Large's training data spans a broad range of languages, including English, Chinese, Japanese, Korean, French, and more. Additionally, the data covers a variety of task types, such as retrieval, classification, and clustering. The model exhibits state-of-the-art (SOTA) results on several multilingual benchmarks. ## Usage ### Using FlagEmbedding Install: ``` pip install -U FlagEmbedding ``` ```python from FlagEmbedding import FlagLLMModel queries = ["how much protein should a female eat", "summit define"] documents = [ "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.", "Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments." ] model = FlagLLMModel('hanhainebula/baai-multilingual-large', query_instruction_for_retrieval="Given a web search query, retrieve relevant passages that answer the query.", query_instruction_format="{}\n{}", use_fp16=True) # Setting use_fp16 to True speeds up computation with a slight performance degradation embeddings_1 = model.encode_queries(queries) embeddings_2 = model.encode_corpus(documents) similarity = embeddings_1 @ embeddings_2.T print(similarity) # [[ 0.559 0.01646 ] # [-0.002686 0.4995 ]] ``` ### Using Sentence Transformers ```python from sentence_transformers import SentenceTransformer import torch # Load the model, optionally in float16 precision for faster inference model = SentenceTransformer("hanhainebula/baai-multilingual-large", model_kwargs={"torch_dtype": torch.float16}) # Prepare a prompt given an instruction instruction = 'Given a web search query, retrieve relevant passages that answer the query.' prompt = f'{instruction}\n' # Prepare queries and documents queries = [ 'how much protein should a female eat', 'summit define', ] documents = [ "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.", "Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments." ] # Compute the query and document embeddings query_embeddings = model.encode(queries, prompt=prompt) document_embeddings = model.encode(documents) # Compute the cosine similarity between the query and document embeddings similarities = model.similarity(query_embeddings, document_embeddings) print(similarities) # tensor([[ 0.5591, 0.0164], # [-0.0026, 0.4993]], dtype=torch.float16) ``` ### Using HuggingFace Transformers ```python import torch import torch.nn.functional as F from torch import Tensor from transformers import AutoTokenizer, AutoModel def last_token_pool(last_hidden_states: Tensor, attention_mask: Tensor) -> Tensor: left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0]) if left_padding: return last_hidden_states[:, -1] else: sequence_lengths = attention_mask.sum(dim=1) - 1 batch_size = last_hidden_states.shape[0] return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths] def get_detailed_instruct(task_description: str, query: str) -> str: return f'{task_description}\n{query}' task = 'Given a web search query, retrieve relevant passages that answer the query.' queries = [ get_detailed_instruct(task, 'how much protein should a female eat'), get_detailed_instruct(task, 'summit define') ] # No need to add instructions for documents documents = [ "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.", "Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments." ] input_texts = queries + documents tokenizer = AutoTokenizer.from_pretrained('hanhainebula/baai-multilingual-large') model = AutoModel.from_pretrained('hanhainebula/baai-multilingual-large') model.eval() max_length = 4096 # Tokenize the input texts batch_dict = tokenizer(input_texts, max_length=max_length, padding=True, truncation=True, return_tensors='pt', pad_to_multiple_of=8) with torch.no_grad(): outputs = model(**batch_dict) embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask']) # normalize embeddings embeddings = F.normalize(embeddings, p=2, dim=1) scores = (embeddings[:2] @ embeddings[2:].T) * 100 print(scores.tolist()) # [[55.92064666748047, 1.6549524068832397], [-0.2698777914047241, 49.95653533935547]] ```