InMemoryVectorStore

Struct InMemoryVectorStore 

Source
pub struct InMemoryVectorStore { /* private fields */ }
Expand description

In-memory vector store using cosine similarity

Implementations§

Source§

impl InMemoryVectorStore

Source

pub fn new() -> Self

Create a new in-memory vector store

Examples found in repository?
examples/rag_advanced.rs (line 37)
26async fn main() -> helios_engine::Result<()> {
27    println!("🚀 Helios Engine - Advanced RAG Features");
28    println!("========================================\n");
29
30    let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| {
31        println!("⚠ Warning: OPENAI_API_KEY not set. Using placeholder.");
32        "your-api-key-here".to_string()
33    });
34
35    // Create RAG system directly (without agent)
36    let embeddings = OpenAIEmbeddings::new("https://api.openai.com/v1/embeddings", api_key);
37    let vector_store = InMemoryVectorStore::new();
38    let rag_system = RAGSystem::new(Box::new(embeddings), Box::new(vector_store));
39
40    println!("✓ RAG system created\n");
41
42    // --- Example 1: Adding documents with metadata ---
43    println!("Example 1: Documents with Metadata");
44    println!("===================================\n");
45
46    let documents = vec![
47        (
48            "Rust is a systems programming language focused on safety and performance.",
49            hashmap! {
50                "category" => "programming",
51                "language" => "rust",
52                "year" => "2010",
53                "difficulty" => "intermediate",
54            },
55        ),
56        (
57            "Python is known for its simplicity and extensive library ecosystem.",
58            hashmap! {
59                "category" => "programming",
60                "language" => "python",
61                "year" => "1991",
62                "difficulty" => "beginner",
63            },
64        ),
65        (
66            "Machine learning is a subset of AI that enables systems to learn from data.",
67            hashmap! {
68                "category" => "ai",
69                "topic" => "machine-learning",
70                "difficulty" => "advanced",
71            },
72        ),
73        (
74            "Docker is a platform for developing, shipping, and running applications in containers.",
75            hashmap! {
76                "category" => "devops",
77                "tool" => "docker",
78                "year" => "2013",
79            },
80        ),
81    ];
82
83    let mut doc_ids = Vec::new();
84    for (text, meta) in documents.iter() {
85        let metadata: HashMap<String, serde_json::Value> = meta
86            .iter()
87            .map(|(k, v)| (k.to_string(), serde_json::json!(v)))
88            .collect();
89
90        let id = rag_system.add_document(text, Some(metadata)).await?;
91        println!(
92            "Added document: {} (ID: {})",
93            &text[..50.min(text.len())],
94            id
95        );
96        doc_ids.push(id);
97    }
98    println!();
99
100    // --- Example 2: Semantic search ---
101    println!("Example 2: Semantic Search");
102    println!("==========================\n");
103
104    let queries = vec![
105        ("programming language safety", 3),
106        ("containerization technology", 2),
107        ("artificial intelligence", 2),
108    ];
109
110    for (query, limit) in queries {
111        println!("Query: '{}' (limit: {})", query, limit);
112        let results = rag_system.search(query, limit).await?;
113        print_results(&results);
114        println!();
115    }
116
117    // --- Example 3: Document count ---
118    println!("Example 3: Document Management");
119    println!("===============================\n");
120
121    let count = rag_system.count().await?;
122    println!("Total documents: {}\n", count);
123
124    // --- Example 4: Delete a document ---
125    if let Some(first_id) = doc_ids.first() {
126        println!("Deleting document: {}", first_id);
127        rag_system.delete_document(first_id).await?;
128        let new_count = rag_system.count().await?;
129        println!("Documents after deletion: {}\n", new_count);
130    }
131
132    // --- Example 5: Search after deletion ---
133    println!("Example 5: Search After Deletion");
134    println!("=================================\n");
135
136    let results = rag_system.search("programming languages", 5).await?;
137    println!("Results for 'programming languages':");
138    print_results(&results);
139    println!();
140
141    // --- Example 6: Clear all documents ---
142    println!("Example 6: Clear All Documents");
143    println!("===============================\n");
144
145    rag_system.clear().await?;
146    let final_count = rag_system.count().await?;
147    println!("Documents after clear: {}\n", final_count);
148
149    println!("✅ Example completed successfully!");
150    println!("\n💡 Key Features Demonstrated:");
151    println!("  • Direct RAG system usage (no agent required)");
152    println!("  • Documents with custom metadata");
153    println!("  • Semantic search with configurable limits");
154    println!("  • Document management (add, delete, count, clear)");
155    println!("  • Batch operations");
156
157    println!("\n📝 Advanced Use Cases:");
158    println!("  • Building custom RAG pipelines");
159    println!("  • Document management systems");
160    println!("  • Knowledge base applications");
161    println!("  • Semantic search engines");
162
163    Ok(())
164}

Trait Implementations§

Source§

impl Default for InMemoryVectorStore

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl VectorStore for InMemoryVectorStore

Source§

fn initialize<'life0, 'async_trait>( &'life0 self, _dimension: usize, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Initialize the vector store (create collections, etc.)
Source§

fn add<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, id: &'life1 str, embedding: Vec<f32>, text: &'life2 str, metadata: HashMap<String, Value>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Add a document with its embedding
Source§

fn search<'life0, 'async_trait>( &'life0 self, query_embedding: Vec<f32>, limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<SearchResult>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Search for similar documents
Source§

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete a document by ID
Source§

fn clear<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Clear all documents
Source§

fn count<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<usize>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get document count

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more