Skip to content
Merged

Dev #58

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions rust/examples/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,39 @@
//! # Usage
//!
//! ```bash
//! # First, copy the example config and edit it
//! cp config.toml ./my_vectorless.toml
//! # Edit my_vectorless.toml to customize settings
//! # Using environment variables for LLM config (overrides config file):
//! LLM_API_KEY=sk-xxx LLM_MODEL=gpt-4o cargo run --example advanced
//!
//! # Or with defaults (using config file):
//! cargo run --example advanced
//! ```

use vectorless::{EngineBuilder, IndexContext, QueryContext};

#[tokio::main]
async fn main() -> vectorless::Result<()> {
// Initialize tracing for debug output (set RUST_LOG=debug to see more)
tracing_subscriber::fmt::init();

println!("=== Vectorless Advanced Example (Config File) ===\n");

// Load all settings from the specified config file.
// The config file must include api_key and model.
let client = EngineBuilder::new()
.with_config_path("./config.toml")
// If environment variables are set, they override the config file values.
let mut builder = EngineBuilder::new().with_config_path("./config.toml");

// Override config with env vars if present
if let Ok(api_key) = std::env::var("LLM_API_KEY") {
builder = builder.with_key(&api_key);
}
if let Ok(model) = std::env::var("LLM_MODEL") {
builder = builder.with_model(&model);
}
if let Ok(endpoint) = std::env::var("LLM_ENDPOINT") {
builder = builder.with_endpoint(&endpoint);
}

let client = builder
.build()
.await
.map_err(|e: vectorless::BuildError| vectorless::Error::Config(e.to_string()))?;
Expand Down Expand Up @@ -56,4 +72,4 @@ async fn main() -> vectorless::Result<()> {

println!("\n=== Done ===");
Ok(())
}
}
112 changes: 47 additions & 65 deletions rust/examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
//! # Usage
//!
//! ```bash
//! # Using environment variables for LLM config:
//! LLM_API_KEY=sk-xxx LLM_MODEL=gpt-4o \
//! LLM_ENDPOINT=https://api.openai.com/v1 cargo run --example events
//!
//! # Or with defaults (edit the code to set your key/endpoint):
//! cargo run --example events
//! ```

Expand All @@ -22,6 +27,9 @@ use vectorless::events::{EventEmitter, IndexEvent, QueryEvent};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize tracing for debug output (set RUST_LOG=debug to see more)
tracing_subscriber::fmt::init();

println!("=== Event Callbacks Example ===\n");

// 1. Create event emitter with handlers
Expand Down Expand Up @@ -90,87 +98,61 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!(" ✓ Event handlers configured\n");

// Build engine with LLM configuration from environment or defaults.
// Adjust the defaults below to match your setup.
let api_key = std::env::var("LLM_API_KEY")
.unwrap_or_else(|_| "sk-...".to_string());
let model = std::env::var("LLM_MODEL")
.unwrap_or_else(|_| "gpt-4o".to_string());
let endpoint = std::env::var("LLM_ENDPOINT")
.unwrap_or_else(|_| "https://api.openai.com/v1".to_string());

// 2. Create engine with events
println!("Step 2: Creating engine with event emitter...");
let engine = EngineBuilder::new()
.with_workspace("./workspace_events_example")
.with_key("sk-...")
.with_model("gpt-4o")
.with_key(&api_key)
.with_model(&model)
.with_endpoint(&endpoint)
.with_events(events)
.build()
.await
.map_err(|e: vectorless::BuildError| vectorless::Error::Config(e.to_string()))?;
.await?;
println!(" ✓ Engine created\n");

// 3. Index a document (events will fire)
println!("Step 3: Indexing document (watch events)...\n");

let temp_dir = tempfile::tempdir()?;
let doc_content = r#"# Example Document

## Introduction

This is an example document for demonstrating event callbacks.

## Features

- Event monitoring for indexing
- Event monitoring for queries
- Progress tracking

## Architecture

The event system uses handlers that can be attached to the engine builder.
"#;

let doc_path = temp_dir.path().join("example.md");
tokio::fs::write(&doc_path, doc_content).await?;

let index_result = engine.index(IndexContext::from_path(&doc_path)).await?;
let doc_id = index_result.doc_id().unwrap().to_string();
println!();

// 4. Query the document (events will fire)
println!("Step 4: Querying document (watch events)...\n");

// 3. Index a document with events
println!("Step 3: Indexing document (with events)...");
let result = engine
.query(QueryContext::new("What features are available?").with_doc_id(&doc_id))
.index(IndexContext::from_path("../README.md"))
.await?;
println!();
let doc_id = result.doc_id().unwrap().to_string();
println!(" ✓ Indexed: {doc_id}\n");

// 5. Show results
println!("Step 5: Query result:");
// 4. Query with events
println!("Step 4: Querying (with events)...");
let result = engine
.query(
QueryContext::new("What is vectorless?")
.with_doc_id(&doc_id)
)
.await?;
if let Some(item) = result.single() {
println!(" - Score: {:.2}", item.score);
println!(" - Nodes: {}", item.node_ids.len());
println!(" ✓ Found result ({} chars)", item.content.len());
if !item.content.is_empty() {
let preview: String = item.content.chars().take(100).collect();
println!(" - Content: {}...", preview);
let preview: String = item.content.chars().take(200).collect();
println!(" Preview: {}...", preview);
}
}
println!();

// 6. Show statistics
println!("Step 6: Event statistics:");
println!(
" - Index events fired: {}",
index_count.load(Ordering::SeqCst)
);
println!(
" - Query events fired: {}",
query_count.load(Ordering::SeqCst)
);
println!(
" - Nodes visited: {}",
nodes_visited.load(Ordering::SeqCst)
);
println!();

// 7. Cleanup
println!("Step 7: Cleanup...");

// 5. Stats
println!("\n--- Stats ---");
println!(" Documents indexed: {}", index_count.load(Ordering::SeqCst));
println!(" Queries executed: {}", query_count.load(Ordering::SeqCst));
println!(" Nodes visited: {}", nodes_visited.load(Ordering::SeqCst));

// Cleanup
engine.remove(&doc_id).await?;
println!(" ✓ Document removed\n");
println!("\n Cleaned up");

println!("=== Example Complete ===");
println!("\n=== Done ===");
Ok(())
}
30 changes: 21 additions & 9 deletions rust/examples/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
//! # Usage
//!
//! ```bash
//! # Using environment variables for LLM config:
//! LLM_API_KEY=sk-xxx LLM_MODEL=gpt-4o \
//! LLM_ENDPOINT=https://api.openai.com/v1 cargo run --example flow
//!
//! # Or with defaults (edit the code to set your key/endpoint):
//! cargo run --example flow
//! ```

Expand Down Expand Up @@ -54,14 +59,23 @@ async fn main() -> vectorless::Result<()> {

println!("=== Vectorless Flow Example ===\n");

// Build engine with LLM configuration from environment or defaults.
// Adjust the defaults below to match your setup.
let api_key = std::env::var("LLM_API_KEY")
.unwrap_or_else(|_| "sk-...".to_string());
let model = std::env::var("LLM_MODEL")
.unwrap_or_else(|_| "gpt-4o".to_string());
let endpoint = std::env::var("LLM_ENDPOINT")
.unwrap_or_else(|_| "https://api".to_string());

// Step 1: Create a Vectorless client
println!("Step 1: Creating Vectorless client...");

let engine = EngineBuilder::new()
.with_workspace("./worksspace_flow_example")
.with_key("sk...")
.with_model("gpt-4o")
.with_endpoint("https://api")
.with_key(&api_key)
.with_model(&model)
.with_endpoint(&endpoint)
.build()
.await
.map_err(|e| vectorless::Error::Config(e.to_string()))?;
Expand Down Expand Up @@ -130,12 +144,10 @@ async fn main() -> vectorless::Result<()> {
println!();
}

// Step 5: Cleanup
println!("Step 5: Cleanup...");

// engine.remove(&doc_id).await?;
// println!(" - Document removed");
// Cleanup
for doc in engine.list().await? {
engine.remove(&doc.id).await?;
}

println!("\n=== Example Complete ===");
Ok(())
}
21 changes: 18 additions & 3 deletions rust/examples/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,35 @@
//! # Usage
//!
//! ```bash
//! # Using environment variables for LLM config:
//! LLM_API_KEY=sk-xxx LLM_MODEL=gpt-4o \
//! cargo run --example graph
//!
//! # Or with defaults (edit the code to set your key/endpoint):
//! cargo run --example graph
//! ```

use vectorless::{EngineBuilder, IndexContext};

#[tokio::main]
async fn main() -> vectorless::Result<()> {
// Initialize tracing for debug output (set RUST_LOG=debug to see more)
tracing_subscriber::fmt::init();

println!("=== Document Graph Example ===\n");

// Build engine with LLM configuration from environment or defaults.
// Adjust the defaults below to match your setup.
let api_key = std::env::var("LLM_API_KEY")
.unwrap_or_else(|_| "sk-...".to_string());
let model = std::env::var("LLM_MODEL")
.unwrap_or_else(|_| "gpt-4o".to_string());

// 1. Create engine
let engine = EngineBuilder::new()
.with_workspace("./workspace_graph_example")
.with_key("sk-...")
.with_model("gpt-4o")
.with_key(&api_key)
.with_model(&model)
.build()
.await
.map_err(|e: vectorless::BuildError| vectorless::Error::Config(e.to_string()))?;
Expand Down Expand Up @@ -91,4 +106,4 @@ async fn main() -> vectorless::Result<()> {

println!("\n=== Done ===");
Ok(())
}
}
25 changes: 21 additions & 4 deletions rust/examples/index_incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,35 @@
//! Incremental indexing example — re-index with change detection.
//!
//! ```bash
//! # Using environment variables for LLM config:
//! LLM_API_KEY=sk-xxx LLM_MODEL=google/gemini-3-flash-preview \
//! LLM_ENDPOINT=http://localhost:4000/api/v1 cargo run --example index_incremental
//!
//! # Or with defaults (edit the code to set your key/endpoint):
//! cargo run --example index_incremental
//! ```

use vectorless::{DocumentFormat, EngineBuilder, IndexContext, IndexMode};

#[tokio::main]
async fn main() -> vectorless::Result<()> {
// Initialize tracing for debug output (set RUST_LOG=debug to see more)
tracing_subscriber::fmt::init();

// Build engine with LLM configuration from environment or defaults.
// Adjust the defaults below to match your setup.
let api_key = std::env::var("LLM_API_KEY")
.unwrap_or_else(|_| "sk-or-v1-...".to_string());
let model = std::env::var("LLM_MODEL")
.unwrap_or_else(|_| "google/gemini-3-flash-preview".to_string());
let endpoint = std::env::var("LLM_ENDPOINT")
.unwrap_or_else(|_| "http://localhost:4000/api/v1".to_string());

let engine = EngineBuilder::new()
.with_workspace("./workspace_incremental_example")
.with_key("sk-or-v1-...")
.with_model("google/gemini-3-flash-preview")
.with_endpoint("http://localhost:4000/api/v1")
.with_key(&api_key)
.with_model(&model)
.with_endpoint(&endpoint)
.build()
.await
.map_err(|e| vectorless::Error::Config(e.to_string()))?;
Expand Down Expand Up @@ -93,4 +110,4 @@ Deletes a user by their unique identifier.
}

Ok(())
}
}
Loading