vectus 0.1.38

A vector database implemented in Rust for learning purposes.
Documentation
use std::env;
use vectus::{document::DocBuilder, metric::Metric, StorageType, Vectus};

#[tokio::main]
async fn main() {
    let docs = vec![
        DocBuilder::new()
            .with_page_content("This is my journal, I'm Alice. Today I went for a walk in the park and saw a beautiful sunset. The colors were amazing - deep oranges and purples streaked across the sky.")
            .add_metadata("date", "2021-09-01")
            .add_metadata("author", "Alice")
            .add_metadata("title", "Journal")
            .build(),
        DocBuilder::new()
            .with_page_content("Bob's document on quantum computing. The principles of superposition and entanglement are fundamental to understanding quantum mechanics and its applications in computing.")
            .add_metadata("date", "2021-09-01")
            .add_metadata("author", "Bob")
            .add_metadata("title", "Quantum Computing Basics")
            .build(),
        DocBuilder::new()
            .with_page_content("Charlie's research on climate change. Global temperatures have risen by 1.1°C since the pre-industrial era, with significant impacts on weather patterns and ecosystems worldwide.")
            .add_metadata("date", "2021-09-01")
            .add_metadata("author", "Charlie")
            .add_metadata("title", "Climate Change Impact")
            .build(),
        DocBuilder::new()
            .with_page_content("Alice's document on machine learning algorithms. Supervised learning techniques like linear regression and decision trees are widely used for predictive modeling in various industries.")
            .add_metadata("date", "2021-09-01")
            .add_metadata("author", "Alice")
            .add_metadata("title", "Machine Learning Basics")
            .build(),
        DocBuilder::new()
            .with_page_content("David's guide to organic gardening. Composting is an essential practice in organic gardening, providing nutrient-rich soil amendments and reducing waste.")
            .add_metadata("date", "2021-09-02")
            .add_metadata("author", "David")
            .add_metadata("title", "Organic Gardening 101")
            .build(),
        DocBuilder::new()
            .with_page_content("Eve's analysis of Shakespeare's sonnets. Sonnet 18, 'Shall I compare thee to a summer's day?', is one of Shakespeare's most famous and beloved poems, exploring themes of beauty and immortality.")
            .add_metadata("date", "2021-09-02")
            .add_metadata("author", "Eve")
            .add_metadata("title", "Shakespeare's Sonnets")
            .build(),
        DocBuilder::new()
            .with_page_content("Frank's cookbook of Italian recipes. The key to a perfect risotto is slowly adding warm broth and constantly stirring to release the starches from the rice, creating a creamy texture.")
            .add_metadata("date", "2021-09-03")
            .add_metadata("author", "Frank")
            .add_metadata("title", "Italian Cuisine")
            .build(),
        DocBuilder::new()
            .with_page_content("Grace's report on renewable energy. Solar panel efficiency has improved significantly in recent years, with some models now reaching over 22% efficiency in converting sunlight to electricity.")
            .add_metadata("date", "2021-09-03")
            .add_metadata("author", "Grace")
            .add_metadata("title", "Renewable Energy Progress")
            .build(),
        DocBuilder::new()
            .with_page_content("Henry's guide to mindfulness meditation. The body scan technique involves systematically focusing attention on different parts of the body, promoting relaxation and self-awareness.")
            .add_metadata("date", "2021-09-04")
            .add_metadata("author", "Henry")
            .add_metadata("title", "Mindfulness Techniques")
            .build(),
        DocBuilder::new()
            .with_page_content("Ivy's analysis of global economic trends. The rise of digital currencies and blockchain technology is reshaping the financial landscape, challenging traditional banking systems.")
            .add_metadata("date", "2021-09-04")
            .add_metadata("author", "Ivy")
            .add_metadata("title", "Economic Trends 2021")
            .build(),
        DocBuilder::new()
            .with_page_content("Jack's exploration of deep sea ecosystems. Hydrothermal vents support unique communities of organisms that thrive in extreme conditions, offering insights into the origins of life on Earth.")
            .add_metadata("date", "2021-09-05")
            .add_metadata("author", "Jack")
            .add_metadata("title", "Deep Sea Wonders")
            .build(),
        DocBuilder::new()
            .with_page_content("Karen's guide to urban planning. Green spaces in cities not only improve air quality but also provide important social and psychological benefits to residents.")
            .add_metadata("date", "2021-09-05")
            .add_metadata("author", "Karen")
            .add_metadata("title", "Sustainable Urban Design")
            .build(),
        DocBuilder::new()
            .with_page_content("Liam's history of ancient civilizations. The Indus Valley Civilization, one of the world's oldest urban cultures, had advanced city planning and sophisticated water management systems.")
            .add_metadata("date", "2021-09-06")
            .add_metadata("author", "Liam")
            .add_metadata("title", "Ancient Civilizations")
            .build(),
        DocBuilder::new()
            .with_page_content("Mia's research on artificial intelligence ethics. As AI systems become more advanced, questions of accountability and transparency in decision-making algorithms become increasingly important.")
            .add_metadata("date", "2021-09-06")
            .add_metadata("author", "Mia")
            .add_metadata("title", "AI Ethics")
            .build(),
        DocBuilder::new()
            .with_page_content("Noah's guide to astrophotography. Long exposure techniques and image stacking can reveal stunning details in night sky photography, capturing the beauty of distant galaxies and nebulae.")
            .add_metadata("date", "2021-09-07")
            .add_metadata("author", "Noah")
            .add_metadata("title", "Astrophotography Basics")
            .build(),
        DocBuilder::new()
            .with_page_content("Olivia's analysis of modern art movements. Abstract expressionism, pioneered by artists like Jackson Pollock, emphasized spontaneous, intuitive creation and the expression of the artist's psyche.")
            .add_metadata("date", "2021-09-07")
            .add_metadata("author", "Olivia")
            .add_metadata("title", "Modern Art Movements")
            .build(),
        DocBuilder::new()
            .with_page_content("Paul's guide to cybersecurity. Multi-factor authentication significantly enhances account security by requiring multiple forms of verification before granting access.")
            .add_metadata("date", "2021-09-08")
            .add_metadata("author", "Paul")
            .add_metadata("title", "Cybersecurity Essentials")
            .build(),
        DocBuilder::new()
            .with_page_content("Quinn's exploration of neuroscience. The concept of neuroplasticity reveals the brain's remarkable ability to reorganize itself by forming new neural connections throughout life.")
            .add_metadata("date", "2021-09-08")
            .add_metadata("author", "Quinn")
            .add_metadata("title", "Brain Plasticity")
            .build(),
        DocBuilder::new()
            .with_page_content("Rachel's guide to sustainable fashion. Upcycling and circular fashion concepts are gaining traction, promoting the reuse and transformation of existing garments to reduce waste in the fashion industry.")
            .add_metadata("date", "2021-09-09")
            .add_metadata("author", "Rachel")
            .add_metadata("title", "Sustainable Fashion")
            .build(),
        DocBuilder::new()
            .with_page_content("Sam's analysis of space exploration. The development of reusable rocket technology has significantly reduced the cost of space launches, opening up new possibilities for commercial space ventures.")
            .add_metadata("date", "2021-09-09")
            .add_metadata("author", "Sam")
            .add_metadata("title", "Space Exploration Trends")
            .build(),
        DocBuilder::new()
            .with_page_content("Sam's latest analysis of space exploration goes in depth with the discovery of new planets and the potential for life on other planets.")
            .add_metadata("author", "Sam")
            .add_metadata("date", "2021-09-09")
            .add_metadata("title", "Space Exploration Trends")
            .build()
    ];

    let mut vectus = Vectus::new(StorageType::InMemory, Metric::Euclidean);
    let api_key = env::var("OPENAI_API_KEY").expect("API key not found");

    let time_adding_before = std::time::Instant::now();
    vectus
        .add_documents(&api_key, &docs)
        .await
        .expect("Error adding documents");
    let time_adding_after = std::time::Instant::now();

    let query = "space exploration.".to_string();

    let time_searching_before = std::time::Instant::now();
    let relevant_docs = vectus.get_k_relevant_documents(&api_key, &query, 2).await;
    let time_searching_after = std::time::Instant::now();

    for doc in relevant_docs {
        println!("Document: {}", doc.page_content);
    }

    println!(
        "Time taken to add documents in seconds: {:?}",
        time_adding_after.duration_since(time_adding_before)
    );

    println!(
        "Time taken to search for relevant documents: {:?}",
        time_searching_after.duration_since(time_searching_before)
    );
}