How to Use the Crisp Reranker Models with Transformers.js

Category :

The Crisp Reranker Models are a part of the powerful family of reranker models provided by MixedBread AI. This guide will walk you through the process of implementing these models using both Python and JavaScript. We aim to make it as user-friendly as possible, ensuring you can set up and get results quickly!

1. Quickstart: Setup Requirements

  • Ensure you have Python and JavaScript environments ready.
  • Install sentence-transformers for Python:
  • pip install -U sentence-transformers
  • For JavaScript, install the necessary package:
  • npm i @xenovatransformers

2. Reranking with Python

Once you have installed the requirements, you can start reranking documents from a query using just a few lines of code. Here’s how:

from sentence_transformers import CrossEncoder

# Load the model
model = CrossEncoder('mixedbread-aimxbai-rerank-base-v1')

# Example query and documents
query = "Who wrote To Kill a Mockingbird?"
documents = [
    "To Kill a Mockingbird is a novel by Harper Lee published in 1960. It was immediately successful, winning the Pulitzer Prize, and has become a classic of modern American literature.",
    "The novel Moby-Dick was written by Herman Melville and first published in 1851. It is considered a masterpiece of American literature and deals with complex themes of obsession, revenge, and the conflict between good and evil.",
    "Harper Lee, an American novelist widely known for her novel To Kill a Mockingbird, was born in 1926 in Monroeville, Alabama. She received the Pulitzer Prize for Fiction in 1961.",
    "Jane Austen was an English novelist known primarily for her six major novels, which interpret, critique and comment upon the British landed gentry at the end of the 18th century.",
    "The Harry Potter series, which consists of seven fantasy novels written by British author J.K. Rowling, is among the most popular and critically acclaimed books of the modern era.",
    "The Great Gatsby, a novel written by American author F. Scott Fitzgerald, was published in 1925. The story is set in the Jazz Age and follows the life of millionaire Jay Gatsby and his pursuit of Daisy Buchanan."
]

# Let's get the scores!
results = model.rank(query, documents, return_documents=True, top_k=3)
print(results)

3. Reranking with JavaScript

If you prefer JavaScript, the setup is just as straightforward. You’ll need to import the necessary classes as shown below:

import AutoTokenizer, AutoModelForSequenceClassification from '@xenovatransformers';

const model_id = 'mixedbread-aimxbai-rerank-base-v1';
const model = await AutoModelForSequenceClassification.from_pretrained(model_id);
const tokenizer = await AutoTokenizer.from_pretrained(model_id);

async function rank(query, documents, top_k = undefined, return_documents = false) {
    const inputs = tokenizer(
        new Array(documents.length).fill(query),
        {
            text_pair: documents,
            padding: true,
            truncation: true,
        }
    )
    const logits = await model(inputs);
    return logits
        .sigmoid()
        .tolist()
        .map(([score], i) => ({
            corpus_id: i,
            score,
            ...(return_documents ? { text: documents[i] } : {})
        }))
        .sort((a, b) => b.score - a.score)
        .slice(0, top_k);
}

4. Using API

If you prefer using the large model via the API, here’s how to do it:

from mixedbread_ai.client import MixedbreadAI

mxbai = MixedbreadAI(api_key='MIXEDBREAD_API_KEY')
res = mxbai.reranking(
    model='mixedbread-aimxbai-rerank-large-v1',
    query='Who is the author of To Kill a Mockingbird?',
    input=[
        "To Kill a Mockingbird is a novel by Harper Lee published in 1960. It was immediately successful, winning the Pulitzer Prize, and has become a classic of modern American literature.",
        "The novel Moby-Dick was written by Herman Melville and first published in 1851. It is considered a masterpiece of American literature and deals with complex themes of obsession, revenge, and the conflict between good and evil.",
        "Harper Lee, an American novelist widely known for her novel To Kill a Mockingbird, was born in 1926 in Monroeville, Alabama. She received the Pulitzer Prize for Fiction in 1961.",
        "Jane Austen was an English novelist known primarily for her six major novels, which interpret, critique and comment upon the British landed gentry at the end of the 18th century.",
        "The Harry Potter series, which consists of seven fantasy novels written by British author J.K. Rowling, is among the most popular and critically acclaimed books of the modern era.",
        "The Great Gatsby, a novel written by American author F. Scott Fitzgerald, was published in 1925. The story is set in the Jazz Age and follows the life of millionaire Jay Gatsby and his pursuit of Daisy Buchanan."
    ],
    top_k=3,
    return_input=False
)
print(res.data)

5. Troubleshooting

If you encounter any issues while implementing the models, here are some troubleshooting tips:

  • Ensure all required dependencies are properly installed.
  • Double-check the API key if you are using the API version; an invalid API key will prevent successful calls.
  • Make sure that the model ID is correctly specified.
  • If the models are not returning expected results, verify the format of your query and documents.

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

Analogy for Understanding

Think of the Crisp Reranker models as a sophisticated librarian at a vast library filled with countless books. When you ask for a specific book or information (your query), the librarian quickly scans through all the available resources and brings the top few selections based on your request (the documents). They don’t just search the titles; they understand the context and nuances of the literature, effectively providing you with the most relevant information — much like how these models analyze text and document relevance for a user query.

Conclusion

By following the steps outlined in this guide, you should be well-equipped to leverage the Crisp Reranker models, enhancing your document retrieval processes efficiently. Remember, these models are powerful tools for improving accuracy and relevance in search applications.

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

Latest Insights

© 2024 All Rights Reserved

×