How to Effectively Use OpenSearch Neural Sparse Encoding

Jul 31, 2024 | Educational

OpenSearch Neural Sparse Encoding is a powerful tool for improving search relevance and retrieval efficiency. In this guide, we will walk you through selecting the right model, encoding queries and documents, and extracting meaningful results.

Selecting the Right Model

When choosing a model, consider factors such as search relevance, inference speed, and retrieval efficiency (measured in FLOPS). The benchmarking of models is based on their zero-shot performance using the BEIR benchmark, which includes datasets like TrecCovid, NFCorpus, and others. Below is a comparison of models:


Model                                    Inference-free for Retrieval   Model Parameters   AVG NDCG@10   AVG FLOPS
---------------------------------------------------------------------------------------------------------
[opensearch-neural-sparse-encoding-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v1)          133M                         0.524               11.4
[opensearch-neural-sparse-encoding-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v2-distill)    67M                          0.528               8.3
[opensearch-neural-sparse-encoding-doc-v1](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v1)   133M                         0.490               2.3
[opensearch-neural-sparse-encoding-doc-v2-distill](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v2-distill)  67M                      0.504               1.8
[opensearch-neural-sparse-encoding-doc-v2-mini](https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-doc-v2-mini)   23M                          0.497               1.7

From the comparison, it is evident that the v2 series of models outperform their predecessors in search relevance and efficiency.

Understanding Sparse Vectors

The learned sparse retrieval model encodes queries and documents as 30,522-dimensional sparse vectors. This means that only non-zero dimensions, which represent important tokens in the vocabulary, are stored. Imagine a library: instead of storing every book (every token), you only maintain a list of the most important references (non-zero tokens) for quick retrieval.

How to Use the Model

To run the model, you typically need to operate within an OpenSearch cluster. However, it can also be used outside the cluster with the HuggingFace models API. Below is a step-by-step implementation:


import itertools
import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer

# Function to get sparse vector from dense vectors
def get_sparse_vector(feature, output):
    values, _ = torch.max(output * feature['attention_mask'].unsqueeze(-1), dim=1)
    return torch.log(1 + torch.relu(values))

# Load the model
model = AutoModelForMaskedLM.from_pretrained('opensearch-project/opensearch-neural-sparse-encoding-v1')
tokenizer = AutoTokenizer.from_pretrained('opensearch-project/opensearch-neural-sparse-encoding-v1')

# Example query and document
query = "Whats the weather in ny now?"
document = "Currently New York is rainy."

# Encode the query and document
feature = tokenizer([query, document], padding=True, truncation=True, return_tensors='pt')
output = model(**feature)[0]
sparse_vector = get_sparse_vector(feature, output)

# Get similarity score
sim_score = torch.matmul(sparse_vector[0], sparse_vector[1])
print("Similarity Score:", sim_score.item())

In this example, the model effectively matches the query and document, calculating a similarity score even with differing token usage.

Troubleshooting

If you encounter issues while using the OpenSearch Neural Sparse Encoding model, here are some common troubleshooting tips:

  • Ensure the model is properly loaded and that your environment supports the necessary libraries and dependencies.
  • Double-check the format of your input data, making sure it meets the expected requirements.
  • If the similarity scores seem off, review your tokenization settings and ensure that the queries and documents are accurately represented.

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

Conclusion

Using OpenSearch Neural Sparse Encoding effectively can greatly improve your search relevance and retrieval accuracy. By choosing the right model and understanding how to encode and compare texts, you’ll be well on your way to leveraging this technology.

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