Unlocking Semantic Search with the Multi-QA MiniLM Model

Oct 28, 2024 | Educational

Welcome to our guide on utilizing the multi-qa-MiniLM-L6-dot-v1 model for semantic search. This powerful model, built on the principles of the sentence-transformers library, is designed for feature extraction and sentence similarity. It has been specifically trained on an extensive dataset of 215 million question-answer pairs, making it a robust tool for mapping text into a 384-dimensional dense vector space.

Getting Started with the Multi-QA MiniLM Model

To leverage the capabilities of this model for semantic search, you first need to ensure that the sentence-transformers library is installed on your system. Here’s a step-by-step breakdown:

Step 1: Installation

pip install -U sentence-transformers

Step 2: Import the Dependencies

Begin by importing the necessary packages in Python.

from sentence_transformers import SentenceTransformer, util

Step 3: Set Up Your Query and Documents

Next, specify the query and your collection of documents. Think of your query as a person asking for directions, while the documents are various streets providing potential routes.

query = "How many people live in London?"
docs = ["Around 9 Million people live in London.", "London is known for its financial district."]

Step 4: Load the Model

model = SentenceTransformer('sentence-transformers/multi-qa-MiniLM-L6-dot-v1')

Step 5: Encode the Query and Documents

Using the model, encode both the query and the documents into embeddings. This process transforms the written text into numerical vectors, making them computable.

query_emb = model.encode(query)
doc_emb = model.encode(docs)

Step 6: Calculate Document Scores

Next, compute the similarity scores between the query embedding and each document embedding.

scores = util.dot_score(query_emb, doc_emb)[0].cpu().tolist()

Step 7: Pair and Sort Scores

Pair the documents with their respective scores and sort them in descending order, akin to ranking the best routes to take.

doc_score_pairs = list(zip(docs, scores))
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)

Step 8: Output the Results

Finally, print the documents along with their scores to see which documents are the most relevant to your query.

for doc, score in doc_score_pairs:
    print(score, doc)

Advanced Usage with Hugging Face Transformers

If you prefer not to use the sentence-transformers library, you can utilize the Hugging Face Transformers library instead. Follow the setup but with changes in how embeddings are computed:

from transformers import AutoTokenizer, AutoModel
import torch

Pooling and Encoding

You will need to define a pooling method and an encode function to process your input:

# CLS Pooling - Take output from first token
def cls_pooling(model_output):
    return model_output.last_hidden_state[:,0]

# Encode text function
def encode(texts):
    encoded_input = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
    with torch.no_grad():
        model_output = model(**encoded_input, return_dict=True)
    embeddings = cls_pooling(model_output)
    return embeddings

Troubleshooting Tips

  • Error with model loading: Ensure that the model path is correctly specified and you are connected to the internet if loading from Hugging Face.
  • Dimensionality issues: Verify that your input sentences are within the limit of 512 word pieces to prevent truncation.
  • Performance issues: If responses are slow, consider running the code on a GPU or TPU for enhanced computational speed.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Technical Specifications

The model encapsulates a few important technical specifications:

  • Dimensions: 384
  • Produces normalized embeddings: No
  • Pooling Method: CLS pooling
  • Suitable score functions: dot-product (e.g., util.dot_score)

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox