Multi-document RAG with Page Search API
Page Search API: given a query and a collection of documents, automatically find which documents are relevant and get the pages that answer.
In the single-document cookbook you searched one PDF. Here you upload three 10-K filings, ask a comparison question about two of them, and the API automatically picks the right documents.
- Upload three 10-K filings into a collection.
- Stage 1: find which documents are relevant.
- Stage 2: search pages within each relevant document.
- Generate a comparison from the multi-document context.
Set up
Get a PageIndex API key, then install the SDK:
pip install -U pageindex openai
import os
from pageindex import PageSearchClient
os.environ["PAGEINDEX_API_KEY"] = "Your PageIndex API key here"
client = PageSearchClient()
Upload documents into a collection
Create a collection and upload three annual reports, each tagged with its company name:
filings = client.documents.create_collection("SEC Filings")["collection"]["id"]
client.upload("apple-10k-2024.pdf", collection_id=filings, metadata={"company": "Apple", "year": 2024})
client.upload("google-10k-2024.pdf", collection_id=filings, metadata={"company": "Google", "year": 2024})
client.upload("meta-10k-2024.pdf", collection_id=filings, metadata={"company": "Meta", "year": 2024})
Stage 1: find the relevant documents
Use search_documents to find which filings are likely to answer the question, without searching their pages yet:
question = "Compare the R&D spending of Apple and Google in fiscal year 2024"
docs_result = client.search_documents(question, collection_id=filings)
relevant_docs = docs_result["results"][0]["documents"]
for doc in relevant_docs:
print(f" {doc['name']}, relevance {doc['relevance']:.2f}")
The API picks only the two relevant filings out of the three:
apple-10k-2024.pdf, relevance 0.93
google-10k-2024.pdf, relevance 0.91
Stage 2: search pages within each document
For each relevant document, search for the pages that answer:
context_parts = []
for doc in relevant_docs:
page_result = client.search(question, doc_id=doc["doc_id"], include_text=True)
pages = page_result["results"][0]["documents"][0]["pages"]
for page in pages:
context_parts.append(f"[{doc['name']}, Page {page['page']}]\n{page['text']}")
context = "\n\n---\n\n".join(context_parts)
Now context contains the relevant pages from each filing, tagged with the document name and page number.
Generate a comparison
Pass the multi-document context to any vision-language model. Here we use OpenAI as an example:
from openai import OpenAI
os.environ["OPENAI_API_KEY"] = "Your OpenAI API key here"
llm = OpenAI()
SYSTEM_PROMPT = (
"You are a document assistant. Answer the user's question based only on "
"the provided document pages. Cite the document name and page number as "
"[document, Page X] for every claim. If the answer is not in the provided "
"pages, say so."
)
answer = llm.responses.create(
model="gpt-6.1-terra", # or any vision-language model
input=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": question + "\n\n" + context},
],
)
print(answer.output_text)
In fiscal year 2024:
- Apple spent $29.9 billion on R&D [apple-10k-2024.pdf, Page 31]
- Google (Alphabet) spent $45.4 billion on R&D [google-10k-2024.pdf, Page 44]
Google spent about 52% more on R&D than Apple.
Each figure traces back to a specific document and page.
Result
Three filings uploaded, but only two matched the query. Two stages — find the documents, then find the pages — keep the search precise and the cost low.
Previous: Vectorless RAG | Back to Cookbook