Voyage
Document Reranking with Voyage Rerank API
This example demonstrates how to use the Rerank API of Voyage with PhpMistral
to rank a list of documents based on their relevance to a query.
The Rerank API takes a query and a list of documents and returns the documents sorted by relevance score, using a reranking model.
Tip
Reranking is useful for improving search results, question answering, and information retrieval systems by prioritizing the most relevant documents.
Example Code
use Partitech\PhpMistral\Clients\Voyage\VoyageClient;
use Partitech\PhpMistral\Exceptions\MistralClientException;
$apiKey = getenv('VOYAGE_API_KEY');
$client = new VoyageClient(apiKey: (string) $apiKey);
$documents = [
"Yoga improves flexibility and reduces stress through breathing exercises and meditation.",
"Regular yoga practice can help strengthen muscles and improve balance.",
"A recent study has shown that yoga can lower cortisol levels, the stress hormone.",
"Yoga and meditation are ancient practices originating from India.",
"Certain types of yoga, such as Vinyasa, are more dynamic and can improve cardiovascular health.",
"Yoga helps in improving posture and reducing back pain.",
"Practicing yoga daily can enhance lung capacity and respiratory function.",
"Yoga has been linked to better sleep quality and reduced insomnia.",
"Many people practice yoga to enhance mindfulness and concentration.",
"Hatha yoga is a gentle form of yoga that is beneficial for beginners.",
"Some research suggests that yoga can help lower blood pressure.",
"Yoga is often used as a complementary therapy for anxiety and depression.",
"Power yoga is an intense workout that can help with weight loss.",
"Regular yoga practice may improve digestion and gut health.",
"Certain yoga poses are known to relieve headaches and migraines.",
"Hot yoga, performed in a heated room, promotes detoxification through sweating.",
"Prenatal yoga is specifically designed to support pregnant women.",
"Restorative yoga focuses on deep relaxation and stress relief.",
"Yoga can improve overall body awareness and coordination.",
"Studies indicate that yoga may help boost the immune system.",
"Practicing yoga in the morning can enhance energy levels throughout the day.",
"Chair yoga is an accessible option for seniors and people with mobility issues.",
"Some elite athletes incorporate yoga into their training for injury prevention.",
"Yoga philosophy includes principles of self-discipline and inner peace.",
"Practicing yoga outdoors can increase vitamin D exposure and mental well-being.",
"Yoga retreats offer an immersive experience in mindfulness and relaxation.",
"A well-balanced diet combined with yoga can lead to optimal health benefits.",
"Mindfulness meditation, often part of yoga, helps develop emotional resilience.",
"Certain breathing techniques in yoga, like Pranayama, improve focus and calmness.",
"Yoga promotes a sense of community and connection when practiced in groups.",
"Practicing yoga regularly can slow down the effects of aging on the body.",
"Combining yoga with strength training can enhance physical performance.",
"Yoga has been found to reduce chronic pain in conditions like arthritis and fibromyalgia.",
];
try {
$rerank = $client->rerank(
model: 'rerank-2.5-lite',
query: "What are the health benefits of yoga?",
documents: $documents,
top: 3,
returnDocuments: true);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}
print_r($rerank);
Example Output
Array
(
[object] => list
[data] => Array
(
[0] => Array
(
[relevance_score] => 0.875
[index] => 0
[document] => Yoga improves flexibility and reduces stress through breathing exercises and meditation.
)
[1] => Array
(
[relevance_score] => 0.8515625
[index] => 1
[document] => Regular yoga practice can help strengthen muscles and improve balance.
)
[2] => Array
(
[relevance_score] => 0.8515625
[index] => 19
[document] => Studies indicate that yoga may help boost the immune system.
)
)
[model] => rerank-2.5-lite
[usage] => Array
(
[total_tokens] => 559
)