Instruction Following Reranking
advanced_techniquesagentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag
Export
Using Instruction-following Reranking in your AI Applications
This notebook shows you how to implement instruction-following reranking in your AI applications using Voyage AI's reranker-2.5 model.
Step 1: Install required libraries
- voyageai: Python library to interact with Voyage AI's APIs
Step 2: Setup prerequisites
Set the Voyage API key as an environment variable, and initialize the Voyage AI client.
Steps to obtain a Voyage AI API Key can be found here.
[ ]
[ ]
Scenario 1: Incorporating implicit business logic
[27]
[28]
With instruction-following
[ ]
[30]
Document: Source: peer_reviewed_journal, A randomized controlled trial of 600 patients found topiramate reduced migraine frequency by 50% compared to placebo, with common side effects including cognitive slowing. Score: 0.8203125 Document: Source: peer_reviewed_journal, Systematic review of 42 studies shows CGRP monoclonal antibodies reduce monthly migraine days by 4-6 days on average, with better tolerability profiles than traditional preventives. Score: 0.8203125 Document: Source: peer_reviewed_journal, Meta-analysis of acupuncture trials demonstrates modest benefit for migraine prevention, with effect size comparable to some pharmacological interventions and minimal adverse events. Score: 0.77734375
Without instruction-following
[31]
Document: Source: forum, I started taking magnesium and B2 supplements for my migraines and they've totally disappeared! Haven't needed my prescription in months. Score: 0.75390625 Document: Source: general_website, 10 natural remedies for migraines that actually work! From essential oils to ice packs, these home treatments can help you avoid medication side effects. Score: 0.75 Document: Source: healthcare_provider, Preventive migraine medications work by reducing the frequency and severity of attacks. Options include daily pills, monthly injections, or quarterly infusions depending on your needs. Score: 0.75
Scenario 2: Handling different types of queries
[32]
[33]
With instruction-following
[ ]
[35]
Document: Query not using an index? Use explain() to diagnose: ```javascript
db.users.find({ email: 'user@example.com' }).explain('executionStats')``` Look for 'IXSCAN' (good) vs 'COLLSCAN' (bad). If you see COLLSCAN, create an index on the queried field.
Score: 0.75
Document: Compound index not being used? Verify your query matches the index prefix. An index on {a: 1, b: 1, c: 1} supports queries on 'a', 'a+b', and 'a+b+c', but NOT 'b' or 'c' alone. Reorder index fields or create additional indexes for different query patterns.
Score: 0.67578125
Document: Index build failing or taking too long? Quick fixes: Use {background: true} option to avoid blocking writes. For large collections, build indexes during low-traffic periods. On replica sets, use rolling index builds: build on secondaries first, then step down primary and build there.
Score: 0.546875
Without instruction-following
[36]
Document: Query not using an index? Use explain() to diagnose: ```javascript
db.users.find({ email: 'user@example.com' }).explain('executionStats')``` Look for 'IXSCAN' (good) vs 'COLLSCAN' (bad). If you see COLLSCAN, create an index on the queried field.
Score: 0.80078125
Document: Compound index not being used? Verify your query matches the index prefix. An index on {a: 1, b: 1, c: 1} supports queries on 'a', 'a+b', and 'a+b+c', but NOT 'b' or 'c' alone. Reorder index fields or create additional indexes for different query patterns.
Score: 0.76953125
Document: Too many indexes slowing down writes? Solution: Audit your indexes with db.collection.getIndexes() and remove unused ones. Use MongoDB's index usage stats: ```javascript
db.collection.aggregate([{ $indexStats: {} }])``` Drop indexes with zero or minimal 'ops' count.
Score: 0.6171875
Scenario 3: Managing long-term memories and state
[37]
[38]
With instruction-following
[39]
[40]
Document: Date: 2025-08-05, Inquired about beach destinations in Mexico and Caribbean for March 2025. Budget: up to $1000/night. Relevance Score: 0.5078125 Document: Date: 2025-06-01, Customer has severe shellfish allergy. Avoid destinations where seafood is primary cuisine. Relevance Score: 0.404296875 Document: Date: 2025-09-15, Booked Ritz-Carlton Maui, oceanfront suite, $850/night. Anniversary trip with husband David. Relevance Score: 0.404296875
Without instruction-following
[41]
Document: Date: 2025-08-05, Inquired about beach destinations in Mexico and Caribbean for March 2025. Budget: up to $1000/night. Relevance Score: 0.6796875 Document: Date: 2024-01-15, Prefers warm beach destinations year-round. Enjoys water activities and relaxation. Relevance Score: 0.4765625 Document: Date: 2025-09-15, Booked Ritz-Carlton Maui, oceanfront suite, $850/night. Anniversary trip with husband David. Relevance Score: 0.47265625