# Requires transformers>=4.51.0import torchimport torch.nn.functional as F from torch import Tensorfrom 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'Instruct: {task_description}\nQuery:{query}' # Each query must come with a one-sentence instruction that describes the tasktask = 'Given a web search query, retrieve relevant passages that answer the query' queries = [ get_detailed_instruct(task, 'What is the capital of China?'), get_detailed_instruct(task, 'Explain gravity')]# No need to add instructions for retrieval documentsdocuments = [ "The capital of China is Beijing.", "Gravity is a force that attracts two bodies towards each other. It gives weight to physical objects and is responsible for the movement of planets around the sun."]input_texts = queries + documents tokenizer = AutoTokenizer.from_pretrained('AQ-MedAI/Diver-Retriever-4B-1020', padding_side='left')model = AutoModel.from_pretrained('AQ-MedAI/Diver-Retriever-4B-1020') max_length = 8192 # Tokenize the input textsbatch_dict = tokenizer( input_texts, padding=True, truncation=True, max_length=max_length, return_tensors="pt",)batch_dict.to(model.device)outputs = model(**batch_dict)embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask']) # normalize embeddingsembeddings = F.normalize(embeddings, p=2, dim=1)scores = (embeddings[:2] @ embeddings[2:].T)print(scores.tolist())# [[0.9319270849227905, 0.5878604054450989], [0.639923095703125, 0.7950234413146973]]