docs/examples/node_postprocessor/VoyageAIRerank.ipynb
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/node_postprocessor/VoyageAIRerank.ipynb" target="_parent"></a>
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-voyageai-rerank > /dev/null
%pip install llama-index-embeddings-voyageai > /dev/null
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from 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/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
import os
from llama_index.embeddings.voyageai import VoyageEmbedding
api_key = os.environ["VOYAGE_API_KEY"]
voyageai_embeddings = VoyageEmbedding(
voyage_api_key=api_key, model_name="voyage-3"
)
# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()
# build index
index = VectorStoreIndex.from_documents(
documents=documents, embed_model=voyageai_embeddings
)
from llama_index.postprocessor.voyageai_rerank import VoyageAIRerank
voyageai_rerank = VoyageAIRerank(
api_key=api_key, top_k=2, model="rerank-2", truncation=True
)
query_engine = index.as_query_engine(
similarity_top_k=10,
node_postprocessors=[voyageai_rerank],
)
response = query_engine.query(
"What did Sam Altman do in this essay?",
)
pprint_response(response, show_source=True)
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)