To effectively generate embeddings for PDF content using Ollama, the mxbai-embed-large and nomic-embed-text models are recommended due to their balance of performance and efficiency. These models are specifically designed for semantic understanding and retrieval tasks, making them well-suited for processing PDF text.
Recommended Models
-
mxbai-embed-large (334M parameters)
- Optimized for high accuracy in semantic search and retrieval-augmented generation (RAG) workflows[1][8].
- Demonstrates strong performance in capturing nuanced relationships between text chunks, which is critical for multi-page PDFs[1][6].
-
nomic-embed-text (137M parameters)
- Efficient for smaller hardware setups while maintaining competitive performance[1][8].
- Supports longer text sequences, useful for processing larger PDF chunks[8].
Key Considerations for PDFs
- Chunking Strategy: Split PDF text into logical sections (e.g., paragraphs or pages) before embedding to maintain context[2][6]. Explicitly include page numbers in the text to mitigate hallucination risks[2].
- Metadata Handling: Store page numbers and document structure separately alongside embeddings for accurate retrieval[2][7].
- Avoid General-Purpose Models: Models like
llama3 are less effective for embeddings compared to specialized options[6][8].
Example Workflow
# Using mxbai-embed-large with ChromaDB (from Search Result 1)
import ollama
import chromadb
# Load and chunk PDF text (hypothetical PDF extraction step)
pdf_chunks = ["Page 1: Llamas are camelids...", "Page 2: Domesticated 4,000 years ago..."]
# Generate embeddings
client = chromadb.Client()
collection = client.create_collection(name="pdf_docs")
for i, chunk in enumerate(pdf_chunks):
embedding = ollama.embed(model="mxbai-embed-large", input=chunk)["embeddings"]
collection.add(ids=[str(i)], embeddings=embedding, documents=[chunk])
# Query example
query_embedding = ollama.embed(model="mxbai-embed-large", input="When were llamas domesticated?")["embeddings"]
results = collection.query(query_embeddings=[query_embedding], n_results=1)
print(results["documents"]) # Returns "Page 2: Domesticated 4,000 years ago..."
For complex PDFs with mixed content (text/images), preprocess with OCR tools before embedding. The mxbai-embed-large model generally outperforms smaller alternatives in accuracy but requires more computational resources[1][8].