-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathmain.rs
More file actions
123 lines (106 loc) · 4.5 KB
/
main.rs
File metadata and controls
123 lines (106 loc) · 4.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// <complete_code>
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// <imports>
use std::io::{self, Write};
use foundry_local_sdk::{FoundryLocalConfig, FoundryLocalManager};
// </imports>
const ALIAS: &str = "qwen3-embedding-0.6b";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Native Embeddings");
println!("=================\n");
// ── 1. Initialise the manager ────────────────────────────────────────
// <init>
let manager = FoundryLocalManager::create(FoundryLocalConfig::new("foundry_local_samples"))?;
// </init>
// Discover available execution providers and their registration status.
let eps = manager.discover_eps()?;
let max_name_len = 30;
println!("Available execution providers:");
println!(" {:<width$} Registered", "Name", width = max_name_len);
println!(" {:─<width$} ──────────", "", width = max_name_len);
for ep in &eps {
println!(" {:<width$} {}", ep.name, ep.is_registered, width = max_name_len);
}
// Download and register all execution providers.
println!("\nDownloading execution providers:");
if !eps.is_empty() {
manager
.download_and_register_eps_with_progress(None, {
let mut current_ep = String::new();
move |ep_name: &str, percent: f64| {
if ep_name != current_ep {
if !current_ep.is_empty() {
println!();
}
current_ep = ep_name.to_string();
}
print!("\r {:<width$} {:5.1}%", ep_name, percent, width = max_name_len);
io::stdout().flush().ok();
}
})
.await?;
println!();
} else {
println!("No execution providers to download.");
}
// ── 2. Pick a model and ensure it is downloaded ─────────────────────
// <model_setup>
let model = manager.catalog().get_model(ALIAS).await?;
println!("Model: {} (id: {})", model.alias(), model.id());
if !model.is_cached().await? {
println!("Downloading model...");
model
.download(Some(|progress: f64| {
print!("\r {progress:.1}%");
std::io::Write::flush(&mut std::io::stdout()).ok();
}))
.await?;
println!();
}
println!("Loading model...");
model.load().await?;
println!("✓ Model loaded\n");
// </model_setup>
// ── 3. Create an embedding client ───────────────────────────────────
// <embedding_client>
let client = model.create_embedding_client();
// </embedding_client>
// ── 4. Single embedding ─────────────────────────────────────────────
// <single_embedding>
println!("--- Single Embedding ---");
let response = client
.generate_embedding("The quick brown fox jumps over the lazy dog")
.await?;
let embedding = &response.data[0].embedding;
println!("Dimensions: {}", embedding.len());
println!(
"First 5 values: {:?}",
&embedding[..5]
);
// </single_embedding>
// ── 5. Batch embeddings ─────────────────────────────────────────────
// <batch_embedding>
println!("\n--- Batch Embeddings ---");
let batch_response = client
.generate_embeddings(&[
"Machine learning is a subset of artificial intelligence",
"The capital of France is Paris",
"Rust is a systems programming language",
])
.await?;
println!("Number of embeddings: {}", batch_response.data.len());
for (i, data) in batch_response.data.iter().enumerate() {
println!(" [{i}] Dimensions: {}", data.embedding.len());
}
// </batch_embedding>
// ── 6. Unload the model ─────────────────────────────────────────────
// <cleanup>
println!("\nUnloading model...");
model.unload().await?;
println!("Done.");
// </cleanup>
Ok(())
}
// </complete_code>