Mixedbread AI Rerank
If you’re opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.
%pip install llama-index > /dev/null%pip install llama-index-postprocessor-mixedbreadai-rerank > /dev/null%pip install llama-index-llms-openai > /dev/null
from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.core.response.pprint_utils import pprint_response
Download Data
!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
--2025-07-24 19:14:25-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txtResolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 75042 (73K) [text/plain]Saving to: ‘data/paul_graham/paul_graham_essay.txt’
data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.03s
2025-07-24 19:14:25 (2.35 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]
import osfrom llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding
# You can visit https://www.mixedbread.ai/api-reference#quick-start-guide# to get an api keymixedbread_api_key = os.environ.get("MXBAI_API_KEY", "your-api-key")model_name = "mixedbread-ai/mxbai-embed-large-v1"
mixbreadai_embeddings = MixedbreadAIEmbedding( api_key=mixedbread_api_key, model_name=model_name)
# load documentsdocuments = SimpleDirectoryReader("./data/paul_graham/").load_data()
# build indexindex = VectorStoreIndex.from_documents( documents=documents, embed_model=mixbreadai_embeddings)
Retrieve top 10 most relevant nodes, then filter with MixedbreadAI Rerank
Section titled “Retrieve top 10 most relevant nodes, then filter with MixedbreadAI Rerank”from llama_index.postprocessor.mixedbreadai_rerank import MixedbreadAIRerank
mixedbreadai_rerank = MixedbreadAIRerank( api_key=mixedbread_api_key, top_n=2, model="mixedbread-ai/mxbai-rerank-large-v1",)
query_engine = index.as_query_engine( similarity_top_k=10, node_postprocessors=[mixedbreadai_rerank],)response = query_engine.query( "What did Sam Altman do in this essay?",)
pprint_response(response, show_source=True)
Final Response: Sam Altman was asked to become the president of YCombinator (YC) after the original founders decided to step back andreorganize the company to ensure its longevity. Initially hesitant dueto his interest in starting a nuclear reactor startup, Sam eventuallyagreed to take over as president starting with the winter 2014 batch.______________________________________________________________________Source Node 1/2Node ID: 9bef8795-4532-44eb-a590-45abf15b11e5Similarity: 0.109680176Text: This seemed strange advice, because YC was doing great. But ifthere was one thing rarer than Rtm offering advice, it was Rtm beingwrong. So this set me thinking. It was true that on my currenttrajectory, YC would be the last thing I did, because it was onlytaking up more of my attention. It had already eaten Arc, and was inthe process of ea...______________________________________________________________________Source Node 2/2Node ID: 3060722a-0e57-492e-9071-2148e5eec2beSimilarity: 0.041625977Text: But after Heroku got bought we had enough money to go back tobeing self-funded. [15] I've never liked the term "deal flow,"because it implies that the number of new startups at any given timeis fixed. This is not only false, but it's the purpose of YC tofalsify it, by causing startups to be founded that would not otherwisehave existed. [1...
Directly retrieve top 2 most similar nodes
Section titled “Directly retrieve top 2 most similar nodes”query_engine = index.as_query_engine( similarity_top_k=2,)response = query_engine.query( "What did Sam Altman do in this essay?",)
Retrieved context is irrelevant and response is hallucinated.
pprint_response(response, show_source=True)
Final Response: Sam Altman worked on the application builder, whileDan worked on network infrastructure, and two undergrads worked on thefirst two services (images and phone calls). Later on, Sam realized hedidn't want to run a company and decided to build a subset of theproject as an open source project.______________________________________________________________________Source Node 1/2Node ID: a42ab697-0bd1-40fc-8e23-64148e62fe6dSimilarity: 0.557881093860686Text: I started working on the application builder, Dan worked onnetwork infrastructure, and the two undergrads worked on the first twoservices (images and phone calls). But about halfway through thesummer I realized I really didn't want to run a company — especiallynot a big one, which it was looking like this would have to be. I'donly started V...______________________________________________________________________Source Node 2/2Node ID: a398b429-fad6-4284-a201-835e5c1fec3cSimilarity: 0.49815489887733433Text: But alas it was more like the Accademia than not. Betterorganized, certainly, and a lot more expensive, but it was nowbecoming clear that art school did not bear the same relationship toart that medical school bore to medicine. At least not the paintingdepartment. The textile department, which my next door neighborbelonged to, seemed to be ...