--- task_categories: - question-answering language: - en tags: - TREC-RAG - RAG - MSMARCO - MSMARCOV2.1 - Snowflake - gte - stella-en-1.5B_v5 pretty_name: TREC-RAG-Embedding-Baseline stella-en-1.5B_v5 size_categories: - 100M= top_k: break doc_embeddings = np.asarray(doc_embeddings) vector_dim = 1024 vector_linear_directory = f"2_Dense_{vector_dim}" model = AutoModel.from_pretrained('NovaSearch/stella_en_1.5B_v5', trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained('NovaSearch/stella_en_1.5B_v5') vector_linear = torch.nn.Linear(in_features=model.config.hidden_size, out_features=vector_dim) vector_linear_dict = { k.replace("linear.", ""): v for k, v in torch.load(os.path.join(model_dir, f"{vector_linear_directory}/pytorch_model.bin")).items() } vector_linear.load_state_dict(vector_linear_dict) model.eval() # ensure that model and vector linear are on the same device query_prefix = 'Instruct: Given a web search query, retrieve relevant passages that answer the query.\nQuery: ' queries = ['how do you clean smoke off walls'] queries_with_prefix = ["{}{}".format(query_prefix, i) for i in queries] query_tokens = tokenizer(queries_with_prefix, padding=True, truncation=True, return_tensors='pt', max_length=512) # Compute token embeddings with torch.no_grad(): attention_mask = *query_token["attention_mask"] last_hidden_state = model(***query_token)[0] last_hidden = last_hidden_state.masked_fill(~attention_mask[..., None].bool(), 0.0) query_vectors = last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None] query_vectors = normalize(vector_linear(query_vectors).cpu().numpy()) doc_embeddings = torch.nn.functional.normalize(doc_embeddings, p=2, dim=1) # Compute dot score between query embedding and document embeddings dot_scores = np.matmul(query_embeddings, doc_embeddings.transpose())[0] top_k_hits = np.argpartition(dot_scores, -top_k)[-top_k:].tolist() # Sort top_k_hits by dot score top_k_hits.sort(key=lambda x: dot_scores[x], reverse=True) # Print results print("Query:", queries[0]) for doc_id in top_k_hits: print(docs[doc_id]['doc_id']) print(docs[doc_id]['text']) print(docs[doc_id]['url'], "\n") ```