-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.rs
More file actions
87 lines (73 loc) · 2.6 KB
/
basic.rs
File metadata and controls
87 lines (73 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use zeroentropy_community::Client;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load .env file if it exists
dotenv::dotenv().ok();
// Create client from ZEROENTROPY_API_KEY environment variable
let client = Client::from_env()?;
// Create a collection
println!("Creating collection...");
match client.collections().add("rust_example").await {
Ok(response) => println!("{}", response.message),
Err(zeroentropy_community::Error::Conflict(_)) => println!("Collection already exists"),
Err(e) => return Err(e.into()),
}
// Add some text documents
println!("\nAdding documents...");
client.documents().add_text(
"rust_example",
"doc1.txt",
"Rust is a systems programming language focused on safety and performance.",
None,
).await?;
client.documents().add_text(
"rust_example",
"doc2.txt",
"The Rust compiler prevents many common programming errors at compile time.",
None,
).await?;
// Add a document with metadata
let mut metadata = HashMap::new();
metadata.insert(
"category".to_string(),
zeroentropy_community::MetadataValue::String("tutorial".to_string()),
);
client.documents().add_text(
"rust_example",
"doc3.txt",
"Cargo is Rust's build system and package manager.",
Some(metadata),
).await?;
println!("Documents added successfully!");
// Search for documents
println!("\nSearching for 'performance'...");
let results = client.queries().top_snippets(
"rust_example",
"performance",
5,
None,
Some(true), // include metadata
None,
None,
).await?;
println!("Found {} results:", results.results.len());
for (i, result) in results.results.iter().enumerate() {
println!("\n{}. {} (score: {:.4})", i + 1, result.path, result.score);
println!(" Content: {}", result.content);
if let Some(metadata) = &result.metadata {
println!(" Metadata: {:?}", metadata);
}
}
// List all documents in collection
println!("\n\nListing all documents...");
let doc_list = client.documents().get_info_list("rust_example", Some(10), None).await?;
for doc in doc_list.documents {
println!("- {} (status: {:?})", doc.path, doc.index_status);
}
// Clean up - delete collection
println!("\nCleaning up...");
client.collections().delete("rust_example").await?;
println!("Collection deleted!");
Ok(())
}