Mixedbread

Python SDK

Get started with the Mixedbread Python SDK. Learn how to install, configure, and use the SDK for reranking and embedding tasks.

Installation
pip install mixedbread-ai

Quick Start

Here's a quick example to get you started with the API:

Client creation
from mixedbread_ai import MixedbreadAIClient
 
mxbai = MixedbreadAIClient(api_key="YOUR_API_KEY", max_retries=3)

Reranking documents

Reranking example
model = "mixedbread-ai/mxbai-rerank-large-v1"
query = "What's the best way to make sourdough bread?"
documents = [
    "Sourdough bread is made using a fermented dough starter instead of commercial yeast. The process typically takes 24-48 hours and results in a tangy, chewy bread with a crisp crust.",
    "Croissants are a French pastry made from layered, buttery, yeast-leavened dough. They are known for their flaky texture and crescent shape.",
    "To make sourdough bread, you'll need a mature sourdough starter, bread flour, water, and salt. The key is to allow enough time for fermentation and proper shaping techniques.",
    "Baguettes are a long, thin loaf of French bread known for their crispy crust and chewy interior. They're typically made with just flour, water, yeast, and salt.",
    "The best way to make sourdough bread involves maintaining a healthy starter, using a high-quality bread flour, and mastering the techniques of folding and shaping the dough. Proper fermentation time and baking in a Dutch oven can greatly improve results.",
    "Rye bread is a type of bread made with various proportions of rye flour. It has a stronger, more distinctive flavor than bread made from wheat flour and is popular in many parts of Europe."
]
 
reranked_docs = mxbai.reranking(
    model=model,
    query=query,
    input=documents
)
 
print(reranked_docs)

Embedding documents

Embedding example
model = "mixedbread-ai/mxbai-embed-large-v1"
 
# Query embedding (with prompt)
query_embedding = mxbai.embeddings(
    model=model,
    input=["What's the secret to making fluffy croissants?"],
    prompt="Represent this question for searching relevant bakery passages:"
)
 
# Document embeddings (without prompt)
documents = [
    "Croissants are made by layering butter and dough through a process called lamination.",
    "The key to fluffy croissants is using cold butter and maintaining a cool temperature throughout the folding process.",
    "Sourdough bread requires a starter culture made from flour and water.",
    "Baguettes are best baked in a steam-filled oven for a crispy crust.",
    "Fluffy croissants need multiple rounds of folding and chilling to create many layers."
]
 
document_embeddings = mxbai.embeddings(
    model=model,
    input=documents
)
 
print({"query_embedding": query_embedding, "document_embeddings": document_embeddings})

Configuration

The MixedbreadAIClient constructor accepts the following parameters:

class MixedbreadAIClient:
    def __init__(
        self,
        api_key: str,
        max_retries: int = 3,
        timeout: float = 30.0,
        base_url: str = "https://api.mixedbread.ai"
    ):
        # ...
  • api_key: Your Mixedbread API key (required)
  • max_retries: Maximum number of retries for failed requests (default: 3)
  • timeout: Request timeout in seconds (default: 30.0)
  • base_url: Custom base URL for API requests (optional)

Error Handling

Use try/except blocks to handle errors:

from mixedbread_ai.exceptions import MxbaiApiError
 
try:
    embeddings = mxbai.embeddings(
        model="mixedbread-ai/mxbai-embed-large-v1",
        input=["Example text"]
    )
except MxbaiApiError as e:
    print(f"API Error: {e}")
    print(f"Status Code: {e.status_code}")
except Exception as e:
    print(f"Unexpected error: {e}")

Next Steps

Happy baking! 🍞🚀

On this page