Skip to content

Commit 4fee821

Browse files
committed
update rust sample for first run resilience
- uses a smaller model so download is faster - adds catalog check if model exists - adds local cache check and downloads as needed
1 parent 593991a commit 4fee821

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

samples/rust/hello-foundry-local/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cargo run
2020

2121
1. Creates a FoundryLocalManager instance
2222
2. Starts the Foundry Local service if it's not already running
23-
3. Downloads and loads the phi-3.5-mini model
23+
3. Downloads and loads the phi-3-mini-4k model
2424
4. Sends a prompt to the model using the OpenAI-compatible API
2525
5. Displays the response from the model
2626

samples/rust/hello-foundry-local/src/main.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,50 @@ async fn main() -> Result<()> {
1111
println!("Hello Foundry Local!");
1212
println!("===================");
1313

14+
// For this example, we will use the "phi-3-mini-4k" model which is 2.181 GB in size.
15+
let model_to_use: &str = "phi-3-mini-4k";
16+
1417
// Create a FoundryLocalManager instance using the builder pattern
1518
println!("\nInitializing Foundry Local manager...");
1619
let mut manager = FoundryLocalManager::builder()
20+
// Alternatively to the checks below, you can specify the model to use directly during bootstrapping
21+
// .alias_or_model_id(model_to_use)
1722
.bootstrap(true) // Start the service if not running
1823
.build()
1924
.await?;
2025

2126
// List all the models in the catalog
2227
println!("\nAvailable models in catalog:");
2328
let models = manager.list_catalog_models().await?;
29+
let model_in_catalog = models.iter().any(|m| m.alias == model_to_use);
2430
for model in models {
2531
println!("- {model}");
2632
}
33+
// Check if the model is in the catalog
34+
if !model_in_catalog {
35+
println!("Model '{model_to_use}' not found in catalog. Exiting.");
36+
return Ok(());
37+
}
2738

2839
// List available models in the local cache
2940
println!("\nAvailable models in local cache:");
3041
let models = manager.list_cached_models().await?;
42+
let model_in_cache = models.iter().any(|m| m.alias == model_to_use);
3143
for model in models {
3244
println!("- {model}");
3345
}
3446

47+
// Check if the model is already cached and download if not
48+
if !model_in_cache {
49+
println!("Model '{model_to_use}' not found in local cache. Downloading...");
50+
// Download the model if not in cache
51+
// NOTE if you've bootstrapped with `alias_or_model_id`, you can use that directly and skip this check
52+
manager.download_model(model_to_use, None, false).await?;
53+
println!("Model '{model_to_use}' downloaded successfully.");
54+
}
55+
3556
// Get the model information
36-
let model_info = manager.get_model_info("phi-4-mini", true).await?;
57+
let model_info = manager.get_model_info(model_to_use, true).await?;
3758
println!("\nUsing model: {model_info}");
3859

3960
// Build the prompt

0 commit comments

Comments
 (0)