-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathFoundryLocalManager.cs
More file actions
649 lines (558 loc) · 23.5 KB
/
FoundryLocalManager.cs
File metadata and controls
649 lines (558 loc) · 23.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.AI.Foundry.Local;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net.Http.Json;
using System.Net.Mime;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
[JsonConverter(typeof(JsonStringEnumConverter<DeviceType>))]
public enum DeviceType
{
CPU,
GPU,
NPU,
Invalid
}
public partial class FoundryLocalManager : IDisposable, IAsyncDisposable
{
private Uri? _serviceUri;
private HttpClient? _serviceClient;
private List<ModelInfo>? _catalogModels;
private static readonly string AssemblyVersion = typeof(FoundryLocalManager).Assembly.GetName().Version?.ToString() ?? "unknown";
// Gets the service URI
public Uri ServiceUri => _serviceUri ?? throw new InvalidOperationException("Service URI is not set. Call StartServiceAsync() first.");
// Get the endpoint for model control
public Uri Endpoint => new(ServiceUri, "/v1");
// Retrieves the current API key to use
public string ApiKey { get; internal set; } = "OPENAI_API_KEY";
// Sees if the service is already running
public bool IsServiceRunning => _serviceUri != null;
public static async Task<FoundryLocalManager> StartModelAsync(string aliasOrModelId, DeviceType? device = null, CancellationToken ct = default)
{
var manager = new FoundryLocalManager();
try
{
await manager.StartServiceAsync(ct);
var modelInfo = await manager.GetModelInfoAsync(aliasOrModelId, device, ct)
?? throw new InvalidOperationException($"Model {aliasOrModelId} not found in catalog.");
await manager.DownloadModelAsync(modelInfo.ModelId, device: device, token: null, force: false,ct: ct);
await manager.LoadModelAsync(aliasOrModelId, device: device, ct: ct);
return manager;
}
catch
{
manager.Dispose();
throw;
}
}
public async Task StartServiceAsync(CancellationToken ct = default)
{
if (_serviceUri == null)
{
_serviceUri = await EnsureServiceRunning(ct);
_serviceClient = new HttpClient
{
BaseAddress = _serviceUri,
// set the timeout to 2 hours (for downloading large models)
Timeout = TimeSpan.FromSeconds(7200)
};
_serviceClient.DefaultRequestHeaders.UserAgent.ParseAdd($"foundry-local-cs-sdk/{AssemblyVersion}");
}
}
public async Task StopServiceAsync(CancellationToken ct = default)
{
if (_serviceUri != null)
{
await InvokeFoundry("service stop", ct);
_serviceUri = null;
_serviceClient = null;
}
}
public void Dispose()
{
_serviceClient?.Dispose();
GC.SuppressFinalize(this); // Ensures that the finalizer is not called for this object.
}
public async ValueTask DisposeAsync()
{
if (_serviceClient is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else
{
_serviceClient?.Dispose();
}
GC.SuppressFinalize(this); // Ensures that the finalizer is not called for this object.
}
public async Task<List<ModelInfo>> ListCatalogModelsAsync(CancellationToken ct = default)
{
if (_catalogModels == null)
{
await StartServiceAsync(ct);
var results = await _serviceClient!.GetAsync("/foundry/list", ct);
var jsonResponse = await results.Content.ReadAsStringAsync(ct);
var models = JsonSerializer.Deserialize(jsonResponse, ModelGenerationContext.Default.ListModelInfo);
_catalogModels = models ?? [];
// override ep to cuda for generic-gpu models if cuda is available
bool hasCuda = _catalogModels.Any(m => m.Runtime.ExecutionProvider == "CUDAExecutionProvider");
if (hasCuda)
{
foreach (var m in _catalogModels)
{
if (m.ModelId.Contains("-generic-gpu:", StringComparison.OrdinalIgnoreCase))
{
m.EpOverride = "cuda";
}
}
}
}
return _catalogModels;
}
public void RefreshCatalog()
{
_catalogModels = null;
}
public async Task<ModelInfo?> GetModelInfoAsync(string aliasOrModelId, DeviceType? device = null, CancellationToken ct = default)
{
var catalog = await ListCatalogModelsAsync(ct);
if (catalog.Count == 0 || string.IsNullOrWhiteSpace(aliasOrModelId))
{
return null;
}
// 1) Try to match by full ID exactly (with or without ':' for backwards compatibility)
var exact = catalog.FirstOrDefault(m =>
m.ModelId.Equals(aliasOrModelId, StringComparison.OrdinalIgnoreCase));
if (exact != null)
{
return exact;
}
// 2) Try to match by ID prefix "<id>:" and pick the highest version
string prefix = aliasOrModelId + ":";
int bestVersion = -1;
ModelInfo? best = null;
foreach (var m in catalog)
{
if (m.ModelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
var v = GetVersion(m.ModelId);
if (v > bestVersion)
{
bestVersion = v;
best = m;
}
}
}
if (best != null) return best;
// 3) Match by alias, optionally filtered by device
var aliasMatches = catalog.Where(m =>
!string.IsNullOrEmpty(m.Alias) &&
m.Alias.Equals(aliasOrModelId, StringComparison.OrdinalIgnoreCase));
if (device is not null)
{
aliasMatches = aliasMatches.Where(m => m.Runtime.DeviceType == device);
}
// Catalog/list is assumed pre-sorted by service:
// NPU → non-generic-GPU → generic-GPU → non-generic-CPU → CPU
var candidate = aliasMatches.FirstOrDefault();
if (candidate == null) return null;
// Windows-only fallback: if candidate is generic-GPU and has NO EpOverride, prefer CPU alias if available
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
candidate.ModelId.Contains("-generic-gpu:", StringComparison.OrdinalIgnoreCase) &&
string.IsNullOrEmpty(candidate.EpOverride))
{
var cpuFallback = catalog.FirstOrDefault(m =>
!string.IsNullOrEmpty(m.Alias) &&
m.Alias.Equals(aliasOrModelId, StringComparison.OrdinalIgnoreCase) &&
m.Runtime.DeviceType == DeviceType.CPU);
if (cpuFallback != null)
{
candidate = cpuFallback;
}
}
return candidate;
}
public async Task<string> GetCacheLocationAsync(CancellationToken ct = default)
{
await StartServiceAsync(ct);
var response = await _serviceClient!.GetAsync("/openai/status", ct);
var json = await response.Content.ReadAsStringAsync(ct);
var jsonDocument = JsonDocument.Parse(json);
return jsonDocument.RootElement.GetProperty("modelDirPath").GetString()
?? throw new InvalidOperationException("Model directory path not found in response.");
}
public async Task<List<ModelInfo>> ListCachedModelsAsync(CancellationToken ct = default)
{
await StartServiceAsync(ct);
var results = await _serviceClient!.GetAsync("/openai/models", ct);
var jsonResponse = await results.Content.ReadAsStringAsync(ct);
var modelIds = JsonSerializer.Deserialize<string[]>(jsonResponse) ?? [];
return await FetchModelInfosAsync(modelIds, ct);
}
public async Task<ModelInfo?> DownloadModelAsync(string aliasOrModelId, DeviceType? device = null, string? token = null, bool? force = false, CancellationToken ct = default)
{
var modelInfo = await GetModelInfoAsync(aliasOrModelId, device, ct)
?? throw new InvalidOperationException($"Model {aliasOrModelId} not found in catalog.");
var localModels = await ListCachedModelsAsync(ct);
if (localModels.Any(MatchAliasOrId(aliasOrModelId)) && !force.GetValueOrDefault(false))
{
return modelInfo;
}
var request = new DownloadRequest
{
Model = new DownloadRequest.ModelInfo
{
Name = modelInfo.ModelId,
Uri = modelInfo.Uri,
Publisher = modelInfo.Publisher,
ProviderType = modelInfo.ProviderType + "Local",
PromptTemplate = modelInfo.PromptTemplate
},
Token = token ?? "",
IgnorePipeReport = true
};
var response = await _serviceClient!.PostAsJsonAsync("/openai/download", request, ct);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync(ct);
// Find the last '{' to get the start of the JSON object
var jsonStart = responseBody.LastIndexOf('{');
if (jsonStart == -1)
{
throw new InvalidOperationException("No JSON object found in response.");
}
var jsonPart = responseBody[jsonStart..];
// Parse the JSON part
using var jsonDoc = JsonDocument.Parse(jsonPart);
var success = jsonDoc.RootElement.GetProperty("success").GetBoolean();
var errorMessage = jsonDoc.RootElement.GetProperty("errorMessage").GetString();
if (!success)
{
throw new InvalidOperationException($"Failed to download model: {errorMessage}");
}
return modelInfo;
}
public async Task<bool> IsModelUpgradeableAsync(string aliasOrModelId, DeviceType? device = null, CancellationToken ct = default)
{
var modelInfo = await GetLatestModelInfoAsync(aliasOrModelId, device, ct);
if (modelInfo == null)
{
return false; // Model not found in the catalog
}
var latestVersion = GetVersion(modelInfo.ModelId);
if (latestVersion == -1)
{
return false; // Invalid version format in model ID
}
var cachedModels = await ListCachedModelsAsync(ct);
foreach (var cachedModel in cachedModels)
{
if (cachedModel.ModelId.Equals(modelInfo.ModelId, StringComparison.OrdinalIgnoreCase) &&
GetVersion(cachedModel.ModelId) == latestVersion)
{
// Cached model is already at latest version
return false;
}
}
// Latest version not in cache - upgrade available
return true;
}
public async Task<ModelInfo?> UpgradeModelAsync(string aliasOrModelId, DeviceType? device = null, string? token = null, CancellationToken ct = default)
{
// Get the latest model info; throw if not found
var modelInfo = await GetLatestModelInfoAsync(aliasOrModelId, device, ct)
?? throw new ArgumentException($"Model '{aliasOrModelId}' was not found in the catalog.");
// Attempt to download the model
try
{
return await DownloadModelAsync(modelInfo.ModelId, device, token, false, ct);
}
catch (Exception ex)
{
throw new InvalidOperationException($"Failed to upgrade model '{aliasOrModelId}'.", ex);
}
}
public async Task<ModelInfo> LoadModelAsync(string aliasOrModelId, DeviceType? device = null, TimeSpan? timeout = null, CancellationToken ct = default)
{
var modelInfo = await GetModelInfoAsync(aliasOrModelId, device, ct)
?? throw new InvalidOperationException($"Model {aliasOrModelId} not found in catalog.");
var localModelInfo = await ListCachedModelsAsync(ct);
if (!localModelInfo.Any(MatchAliasOrId(aliasOrModelId)))
{
throw new InvalidOperationException($"Model {aliasOrModelId} not found in local models. Please download it first.");
}
var queryParams = new Dictionary<string, string>
{
{ "timeout", (timeout ?? TimeSpan.FromMinutes(10)).TotalSeconds.ToString(CultureInfo.InvariantCulture) }
};
// Prefer EpOverride if present (Python behavior)
if (!string.IsNullOrEmpty(modelInfo.EpOverride))
{
queryParams["ep"] = modelInfo.EpOverride!;
}
var uriBuilder = new UriBuilder(ServiceUri)
{
Path = $"/openai/load/{modelInfo.ModelId}",
Query = string.Join("&", queryParams.Select(kvp => $"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"))
};
var response = await _serviceClient!.GetAsync(uriBuilder.Uri, ct);
response.EnsureSuccessStatusCode();
return modelInfo;
}
public async IAsyncEnumerable<ModelDownloadProgress> DownloadModelWithProgressAsync(
string aliasOrModelId,
DeviceType? device = null,
string? token = null,
bool? force = false,
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken ct = default)
{
if (_serviceClient is null)
{
yield return ModelDownloadProgress.Error("Service not started");
yield break;
}
var modelInfo = await GetModelInfoAsync(aliasOrModelId, device, ct).ConfigureAwait(false);
if (modelInfo is null)
{
yield return ModelDownloadProgress.Error($"Model '{aliasOrModelId}' was not found in the catalogue");
yield break;
}
var localModels = await ListCachedModelsAsync(ct);
if (localModels.Any(MatchAliasOrId(aliasOrModelId)) && !force.GetValueOrDefault(false))
{
yield return ModelDownloadProgress.Completed(modelInfo);
yield break;
}
var payload = new DownloadRequest
{
Model = new DownloadRequest.ModelInfo
{
Name = modelInfo.ModelId,
Uri = modelInfo.Uri,
Publisher = modelInfo.Publisher,
ProviderType = modelInfo.ProviderType + "Local",
PromptTemplate = modelInfo.PromptTemplate
},
Token = token ?? "",
IgnorePipeReport = true
};
var uriBuilder = new UriBuilder(
scheme: ServiceUri.Scheme,
host: ServiceUri.Host,
port: ServiceUri.Port,
pathValue: "/openai/download");
using var request = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri);
request.Content = new StringContent(
JsonSerializer.Serialize(payload),
Encoding.UTF8,
MediaTypeNames.Application.Json);
using var response = await _serviceClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct);
response.EnsureSuccessStatusCode();
using var stream = await response.Content.ReadAsStreamAsync(ct);
using var reader = new StreamReader(stream);
string? line;
var completed = false;
StringBuilder jsonBuilder = new();
var collectingJson = false;
while (!completed && (line = await reader.ReadLineAsync(ct)) is not null)
{
// Check for cancellation at the start of each iteration
ct.ThrowIfCancellationRequested();
// Check if this line contains download percentage
if (line.StartsWith("Total", StringComparison.CurrentCultureIgnoreCase) && line.Contains("Downloading") && line.Contains('%'))
{
// Parse percentage from line like "Total 45.67% Downloading model.onnx.data"
var percentStr = line.Split('%')[0].Split(' ').Last();
if (double.TryParse(percentStr, out var percentage))
{
yield return ModelDownloadProgress.Progress(percentage);
// Check cancellation after yielding progress to ensure timely response
ct.ThrowIfCancellationRequested();
}
}
else if (line.Contains("[DONE]") || line.Contains("All Completed"))
{
// Start collecting JSON after we see the completion marker
collectingJson = true;
}
else if (collectingJson && line.Trim().StartsWith("{", StringComparison.CurrentCultureIgnoreCase))
{
// Start of JSON object
jsonBuilder.AppendLine(line);
}
else if (collectingJson && jsonBuilder.Length > 0)
{
// Continue collecting JSON
jsonBuilder.AppendLine(line);
// Check if we have a complete JSON object by looking for ending brace
if (line.Trim() == "}")
{
completed = true;
}
}
}
if (jsonBuilder.Length > 0)
{
var jsonPart = jsonBuilder.ToString();
ModelDownloadProgress result;
try
{
using var jsonDoc = JsonDocument.Parse(jsonPart);
var success = jsonDoc.RootElement.GetProperty("success").GetBoolean();
var errorMessage = jsonDoc.RootElement.GetProperty("errorMessage").GetString();
result = success
? ModelDownloadProgress.Completed(modelInfo)
: ModelDownloadProgress.Error(errorMessage ?? "Unknown error");
}
catch (JsonException ex)
{
result = ModelDownloadProgress.Error($"Failed to parse JSON response: {ex.Message}");
}
yield return result;
}
else
{
yield return ModelDownloadProgress.Error("No completion response received");
}
}
public async Task<List<ModelInfo>> ListLoadedModelsAsync(CancellationToken ct = default)
{
var response = await _serviceClient!.GetAsync(new Uri(ServiceUri, "/openai/loadedmodels"), ct);
response.EnsureSuccessStatusCode();
var names = await response.Content.ReadFromJsonAsync<string[]>(ct)
?? throw new InvalidOperationException("Failed to read loaded models.");
return await FetchModelInfosAsync(names, CancellationToken.None);
}
public async Task UnloadModelAsync(string aliasOrModelId, DeviceType? device = null, bool force = false, CancellationToken ct = default)
{
var modelInfo = await GetModelInfoAsync(aliasOrModelId, device, ct)
?? throw new InvalidOperationException($"Model {aliasOrModelId} not found in catalog.");
var response = await _serviceClient!.GetAsync($"/openai/unload/{modelInfo.ModelId}?force={force.ToString().ToLowerInvariant()}", ct);
response.EnsureSuccessStatusCode();
}
private async Task<List<ModelInfo>> FetchModelInfosAsync(IEnumerable<string> aliasesOrModelIds, CancellationToken ct = default)
{
var modelInfos = new List<ModelInfo>();
foreach (var idOrAlias in aliasesOrModelIds)
{
var model = await GetModelInfoAsync(idOrAlias, null, ct);
if (model != null)
{
modelInfos.Add(model);
}
}
return modelInfos;
}
public async Task<ModelInfo?> GetLatestModelInfoAsync(string aliasOrModelId, DeviceType? device = null, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(aliasOrModelId))
{
return null;
}
var withoutVersion = aliasOrModelId.Split(':')[0];
return await GetModelInfoAsync(withoutVersion, device, ct);
}
/// <summary>
/// Extracts the numeric version from a model ID string (e.g. "model-x:3" → 3).
/// </summary>
/// <param name="modelId">The model ID string.</param>
/// <returns>The numeric version, or -1 if not found.</returns>
public static int GetVersion(string modelId)
{
if (string.IsNullOrEmpty(modelId))
{
return -1;
}
var parts = modelId.Split(':');
if (parts.Length == 0)
{
return -1;
}
var versionPart = parts[^1]; // last element
if (int.TryParse(versionPart, out var version))
{
return version;
}
return -1;
}
private static async Task<Uri?> EnsureServiceRunning(CancellationToken ct = default)
{
var startInfo = new ProcessStartInfo
{
FileName = @"foundry",
Arguments = "service start",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
// This is a workaround for the issue where the
// environment variable DOTNET_ENVIRONMENT is set to "Development" by default.
startInfo.Environment["DOTNET_ENVIRONMENT"] = null;
using var process = new Process
{
StartInfo = startInfo
};
process.Start();
await process.WaitForExitAsync(ct);
return await StatusEndpoint(ct);
}
private static async Task<Uri?> StatusEndpoint(CancellationToken ct = default)
{
var statusResult = await InvokeFoundry("service status", ct);
var status = TestIsRunning().Match(statusResult);
if (status.Success)
{
var uri = new Uri(status.Groups[1].Value);
var builder = new UriBuilder() { Scheme = uri.Scheme, Host = uri.Host, Port = uri.Port };
return builder.Uri;
}
return status.Success ? new Uri(status.Groups[1].Value) : null;
}
private static async Task<string> InvokeFoundry(string args, CancellationToken ct = default)
{
var startInfo = new ProcessStartInfo
{
FileName = @"foundry",
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
// This is a workaround for the issue where the
// environment variable DOTNET_ENVIRONMENT is set to "Development" by default.
startInfo.Environment["DOTNET_ENVIRONMENT"] = null;
using Process process = Process.Start(startInfo)
?? throw new InvalidOperationException("Failed to start the foundry process");
var output = new StringBuilder();
process.OutputDataReceived += (sender, e) =>
{
if (e.Data is not null)
{
output.AppendLine(e.Data);
}
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync(ct);
return output.ToString();
}
private static Func<ModelInfo, bool> MatchAliasOrId(string aliasOrModelId)
=> modelInfo => modelInfo.ModelId.Equals(aliasOrModelId, StringComparison.OrdinalIgnoreCase) || modelInfo.Alias.Equals(aliasOrModelId, StringComparison.OrdinalIgnoreCase);
[GeneratedRegex("is running on (http://.*)\\s+")]
private static partial Regex TestIsRunning();
}