-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathModelVariant.cs
More file actions
217 lines (181 loc) · 8.59 KB
/
ModelVariant.cs
File metadata and controls
217 lines (181 loc) · 8.59 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.AI.Foundry.Local;
using Microsoft.AI.Foundry.Local.Detail;
using Microsoft.Extensions.Logging;
public class ModelVariant : IModel
{
private readonly IModelLoadManager _modelLoadManager;
private readonly ICoreInterop _coreInterop;
private readonly ILogger _logger;
public ModelInfo Info { get; } // expose the full info record
// expose a few common properties directly
public string Id => Info.Id;
public string Alias => Info.Alias;
public int Version { get; init; } // parsed from Info.Version if possible, else 0
internal ModelVariant(ModelInfo modelInfo, IModelLoadManager modelLoadManager, ICoreInterop coreInterop,
ILogger logger)
{
Info = modelInfo;
Version = modelInfo.Version;
_modelLoadManager = modelLoadManager;
_coreInterop = coreInterop;
_logger = logger;
}
// simpler and always correct to check if loaded from the model load manager
// this allows for multiple instances of ModelVariant to exist
public async Task<bool> IsLoadedAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => IsLoadedImplAsync(ct),
"Error checking if model is loaded", _logger)
.ConfigureAwait(false);
}
public async Task<bool> IsCachedAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => IsCachedImplAsync(ct),
"Error checking if model is cached", _logger)
.ConfigureAwait(false);
}
public async Task<string> GetPathAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => GetPathImplAsync(ct),
"Error getting path for model", _logger)
.ConfigureAwait(false);
}
public async Task DownloadAsync(Action<float>? downloadProgress = null,
CancellationToken? ct = null)
{
await Utils.CallWithExceptionHandling(() => DownloadImplAsync(downloadProgress, ct),
$"Error downloading model {Id}", _logger)
.ConfigureAwait(false);
}
public async Task LoadAsync(CancellationToken? ct = null)
{
await Utils.CallWithExceptionHandling(() => _modelLoadManager.LoadAsync(Id, ct),
"Error loading model", _logger)
.ConfigureAwait(false);
}
public async Task UnloadAsync(CancellationToken? ct = null)
{
await Utils.CallWithExceptionHandling(() => _modelLoadManager.UnloadAsync(Id, ct),
"Error unloading model", _logger)
.ConfigureAwait(false);
}
public async Task RemoveFromCacheAsync(CancellationToken? ct = null)
{
await Utils.CallWithExceptionHandling(() => RemoveFromCacheImplAsync(ct),
$"Error removing model {Id} from cache", _logger)
.ConfigureAwait(false);
}
public async Task<OpenAIChatClient> GetChatClientAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => GetChatClientImplAsync(ct),
"Error getting chat client for model", _logger)
.ConfigureAwait(false);
}
public async Task<OpenAIAudioClient> GetAudioClientAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => GetAudioClientImplAsync(ct),
"Error getting audio client for model", _logger)
.ConfigureAwait(false);
}
public async Task<OpenAIResponsesClient> GetResponsesClientAsync(CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(() => GetResponsesClientImplAsync(ct),
"Error getting responses client for model", _logger)
.ConfigureAwait(false);
}
private async Task<bool> IsLoadedImplAsync(CancellationToken? ct = null)
{
var loadedModels = await _modelLoadManager.ListLoadedModelsAsync(ct).ConfigureAwait(false);
return loadedModels.Contains(Id);
}
private async Task<bool> IsCachedImplAsync(CancellationToken? ct = null)
{
var cachedModelIds = await Utils.GetCachedModelIdsAsync(_coreInterop, ct).ConfigureAwait(false);
return cachedModelIds.Contains(Id);
}
private async Task<string> GetPathImplAsync(CancellationToken? ct = null)
{
var request = new CoreInteropRequest { Params = new Dictionary<string, string> { { "Model", Id } } };
var result = await _coreInterop.ExecuteCommandAsync("get_model_path", request, ct).ConfigureAwait(false);
if (result.Error != null)
{
throw new FoundryLocalException(
$"Error getting path for model {Id}: {result.Error}. Has it been downloaded?");
}
var path = result.Data!;
return path;
}
private async Task DownloadImplAsync(Action<float>? downloadProgress = null,
CancellationToken? ct = null)
{
var request = new CoreInteropRequest
{
Params = new() { { "Model", Id } }
};
ICoreInterop.Response? response;
if (downloadProgress == null)
{
response = await _coreInterop.ExecuteCommandAsync("download_model", request, ct).ConfigureAwait(false);
}
else
{
var callback = new ICoreInterop.CallbackFn(progressString =>
{
if (float.TryParse(progressString, out var progress))
{
downloadProgress(progress);
}
});
response = await _coreInterop.ExecuteCommandWithCallbackAsync("download_model", request,
callback, ct).ConfigureAwait(false);
}
if (response.Error != null)
{
throw new FoundryLocalException($"Error downloading model {Id}: {response.Error}");
}
}
private async Task RemoveFromCacheImplAsync(CancellationToken? ct = null)
{
var request = new CoreInteropRequest { Params = new Dictionary<string, string> { { "Model", Id } } };
var result = await _coreInterop.ExecuteCommandAsync("remove_cached_model", request, ct).ConfigureAwait(false);
if (result.Error != null)
{
throw new FoundryLocalException($"Error removing model {Id} from cache: {result.Error}");
}
}
private async Task<OpenAIChatClient> GetChatClientImplAsync(CancellationToken? ct = null)
{
if (!await IsLoadedAsync(ct))
{
throw new FoundryLocalException($"Model {Id} is not loaded. Call LoadAsync first.");
}
return new OpenAIChatClient(Id);
}
private async Task<OpenAIAudioClient> GetAudioClientImplAsync(CancellationToken? ct = null)
{
if (!await IsLoadedAsync(ct))
{
throw new FoundryLocalException($"Model {Id} is not loaded. Call LoadAsync first.");
}
return new OpenAIAudioClient(Id);
}
private async Task<OpenAIResponsesClient> GetResponsesClientImplAsync(CancellationToken? ct = null)
{
if (!await IsLoadedAsync(ct))
{
throw new FoundryLocalException($"Model {Id} is not loaded. Call LoadAsync first.");
}
var urls = FoundryLocalManager.Instance.Urls;
if (urls == null || urls.Length == 0)
{
throw new FoundryLocalException(
"Web service is not running. Call StartWebServiceAsync before creating a ResponsesClient.");
}
return new OpenAIResponsesClient(urls[0], Id);
}
}