The Stella model, trained on high-performance datasets, is designed to enhance your experience with various text processing tasks like querying, semantic similarity, and classification. In this article, we’ll walk you through how to use the Stella model and troubleshoot common issues that may arise during implementation.
Overview of the Stella Model
The Stella Model, built on advanced architectures like `Alibaba-NLP/gte-large-en-v1.5`, comes with two main prompt structures for different tasks:
- Sentence to Passage (s2p): For retrieving passages based on queries.
- Sentence to Sentence (s2s): For identifying semantically similar text.
How to Use the Stella Model
Using SentenceTransformers
To start using the model with SentenceTransformers, follow these straightforward steps:
from sentence_transformers import SentenceTransformer
# Define query prompt
query_prompt_name = "s2p_query"
queries = [
"What are some ways to reduce stress?",
"What are the benefits of drinking green tea?",
]
# Define documents
docs = [
"There are many effective ways to reduce stress...",
"Green tea has been consumed for centuries...",
]
# Load the model
model = SentenceTransformer("dunzhang/stella_en_400M_v5", trust_remote_code=True).cuda()
# Encode queries and documents
query_embeddings = model.encode(queries, prompt_name=query_prompt_name)
doc_embeddings = model.encode(docs)
print(query_embeddings.shape, doc_embeddings.shape)
# (2, 1024) (2, 1024)
# Calculate similarities
similarities = model.similarity(query_embeddings, doc_embeddings)
print(similarities)
Using Transformers
Alternatively, you can also leverage the Transformers library:
import os
import torch
from transformers import AutoModel, AutoTokenizer
from sklearn.preprocessing import normalize
query_prompt = "Instruct: Given a web search query, retrieve relevant passages that answer the query.\nQuery: "
queries = ["What are some ways to reduce stress?", "What are the benefits of drinking green tea?"]
queries = [query_prompt + query for query in queries]
# Define documents
docs = [
"There are many effective ways to reduce stress...",
"Green tea has been consumed for centuries...",
]
# Load the model
model_dir = "{Your MODEL_PATH}"
vector_dim = 1024
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).cuda().eval()
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# Embed the queries
with torch.no_grad():
input_data = tokenizer(queries, padding="longest", truncation=True, max_length=512, return_tensors="pt")
input_data = {k: v.cuda() for k, v in input_data.items()}
attention_mask = input_data["attention_mask"]
last_hidden_state = model(**input_data)[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(query_vectors.cpu().numpy())
# Embed the documents
with torch.no_grad():
input_data = tokenizer(docs, padding="longest", truncation=True, max_length=512, return_tensors="pt")
input_data = {k: v.cuda() for k, v in input_data.items()}
attention_mask = input_data["attention_mask"]
last_hidden_state = model(**input_data)[0]
last_hidden = last_hidden_state.masked_fill(~attention_mask[..., None].bool(), 0.0)
docs_vectors = last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
docs_vectors = normalize(docs_vectors.cpu().numpy())
print(query_vectors.shape, docs_vectors.shape)
# (2, 1024) (2, 1024)
# Calculate similarities
similarities = query_vectors @ docs_vectors.T
print(similarities)
As this code shows, the process of retrieving data can be compared to gardening. Just as a gardener prepares soil, plants seeds, and waters them to nurture growth, you prepare your queries, embed them using the model, and nourish them with relevant documents to yield informative results.
Troubleshooting Common Issues
While using the Stella Model, you may encounter some issues. Here are potential solutions:
- Model Loading Errors: Ensure that the model’s path is correctly defined and that you have the necessary libraries installed.
- Memory Errors: If you encounter memory issues, consider reducing the vector dimensions or using a more powerful GPU.
- Unexpected Output Shapes: Check your input prompts and ensure they match the expected format outlined in the documentation.
- Low Similarity Scores: Re-evaluate the relevance of your documents relative to the queries provided.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
With the Stella Model, you’re equipped to tackle various text classification and retrieval tasks effectively. By following the steps outlined above, coupled with a gardening analogy, you’ll appreciate the art and science behind nurturing your data processing tasks.
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.

