Repository navigation
How to run the model from project than downloading? #1021
Replies: 2 comments
Yes, you can absolutely load and run a model from within your project instead of downloading it every time. The Foundry Local SDK uses a caching mechanism to store downloaded models locally, and it provides full control over which model is loaded . The SDK's download() method is idempotent: it will skip the download if the model is already present in the local cache. You can also check the cache state explicitly using methods like get_cached_models() . For your Blazor WASM scenario, this is the recommended approach. 📖 General Pattern (Language Agnostic) The workflow across all SDKs (C#, Python, etc.) follows a consistent pattern :
🔧 Practical Example in C# Here's how you could implement this in C#, which is directly relevant for your Blazor WASM application : using Microsoft.AI.Foundry.Local;
// 1. Configure with a persistent cache path
var config = new Configuration
{
AppName = "MyApp",
ModelCacheDir = "./foundry_local_data/model_cache" // Set your cache path
};
var manager = new FoundryLocalManager(config);
await manager.StartServiceAsync();
string modelAlias = "phi-3.5-mini";
// 2. Check if the model is already cached
var cachedModels = await manager.ListCachedModelsAsync();
bool isModelCached = cachedModels.Any(m => m.Alias == modelAlias);
if (!isModelCached)
{
// 3. Download only if not cached. Subsequent calls will be skipped.
await manager.DownloadModelAsync(modelAlias);
}
// 4. Load the model from the local cache
await manager.LoadModelAsync(modelAlias);This approach ensures the model is loaded from your project's designated cache directory on disk after the initial download, avoiding repeated downloads and giving you control over the specific model version being used . |
|
I built a C#/.NET example using Microsoft Foundry Local I've been experimenting with Microsoft Foundry Local and created a small C#/.NET application to test local AI inference. I recorded the setup and walkthrough here: I'm particularly interested in feedback from other .NET developers using Foundry Local. What models are you currently using? |
Uh oh!
There was an error while loading. Please reload this page.
I came across Foundry Local AI. This is super cool. I was checking the c# samples and noticed that all samples download the model in the fly. Just curious if there is a way to load and run the model from within project so that I can avoid download everytime and control the model that I can use. This will be very helpful when we try to run foundry local ai in blazor wasm apps.
All reactions