-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathazure_model_catalog.cc
More file actions
242 lines (198 loc) · 8.64 KB
/
Copy pathazure_model_catalog.cc
File metadata and controls
242 lines (198 loc) · 8.64 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
FetchModelVersions// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "catalog/azure_model_catalog.h"
#include "catalog/catalog_cache.h"
#include "catalog/catalog_client.h"
#include "catalog/local_model_scanner.h"
#include "model.h"
#include "model_info.h"
#include "utils.h"
#include <foundry_local/foundry_local_c.h>
#include <fmt/format.h>
#include <algorithm>
#include <utility>
namespace fl {
AzureModelCatalog::AzureModelCatalog(std::vector<std::pair<std::string, std::optional<std::string>>> catalog_urls,
std::string cache_dir,
ModelFactory model_factory,
const IEpDetector& ep_detector,
ILogger& logger,
bool cache_only)
: BaseModelCatalog(catalog_urls.empty() ? kDefaultCatalogUrl : catalog_urls.front().first, logger),
catalog_urls_(std::move(catalog_urls)),
cache_dir_(std::move(cache_dir)),
model_factory_(std::move(model_factory)),
ep_detector_(ep_detector),
logger_(logger),
cache_only_(cache_only) {
logger_.Log(LogLevel::Information,
fmt::format("Created AzureModelCatalog. Cache directory: {}",
cache_dir_));
}
AzureModelCatalog::~AzureModelCatalog() = default;
std::vector<Model> AzureModelCatalog::FetchModels() const {
// In cache-only mode, read only from the disk cache file — no network calls, no local model scanning.
// The cache file already includes local models from the last full catalog refresh by the long-running service
// process.
// TODO: For our CLI usage the catalog file would be current as we use an ephemeral port for the web service and
// therefore have to run FL first to acquire the external URL value, and that run would have updated the cached
// catalog info.
// If someone had a hardcoded web service URL they were using that sequence of events isn't guaranteed. If we care,
// we could update 'cache_only_' mode to enable refreshing the cache info if it is old. The cache file has a
// savedAtUnix timestamp property that can be used.
if (cache_only_) {
CatalogCache cache(cache_dir_, logger_);
cache.Load();
auto cached = cache.GetCachedModels();
std::vector<Model> models;
if (cached) {
for (const auto& info : *cached) {
models.push_back(model_factory_(ModelInfo(info), /*local_path=*/""));
}
}
logger_.Log(LogLevel::Information,
fmt::format("Cache-only mode: populated {} models from cache file.", models.size()));
return models;
}
std::vector<Model> models;
const std::string& cache_dir = cache_dir_;
logger_.Log(LogLevel::Information,
"Getting latest info from the Azure catalog and for locally cached models.");
// Discover locally cached models.
auto local_models = ScanLocalModels(cache_dir, logger_);
std::vector<std::string> cached_model_ids;
cached_model_ids.reserve(local_models.size());
for (const auto& [id, path] : local_models) {
cached_model_ids.push_back(id);
}
logger_.Log(LogLevel::Information,
fmt::format("Found {} locally cached models.", cached_model_ids.size()));
auto fetch_from = [&](const std::string& url, const std::optional<std::string>& filter) {
// Preserve byte-identical behavior for the "no override" case (previously stored as ""),
// while letting callers explicitly request "" as a real filter override.
auto client = MakeCatalogClient(url, filter.value_or(""), ep_detector_, logger_, cache_dir);
auto model_infos = FetchAllModelInfosWithCachedModels(*client, cached_model_ids, logger_);
for (const auto& info : model_infos) {
// Check if the model is locally cached and pass the path if so.
std::string local_path;
auto it = local_models.find(info.model_id);
if (it != local_models.end()) {
local_path = it->second;
}
models.push_back(model_factory_(ModelInfo(info), std::move(local_path)));
}
};
if (catalog_urls_.empty()) {
// Use default Azure Foundry catalog
try {
fetch_from(kDefaultCatalogUrl, kDefaultCatalogFilter);
} catch (const std::exception& ex) {
logger_.Log(LogLevel::Error,
fmt::format("failed to fetch catalog from default URL: {}", ex.what()));
}
} else {
for (const auto& [url, filter] : catalog_urls_) {
try {
fetch_from(url, filter);
} catch (const std::exception& ex) {
// One failing URL shouldn't block others — skip and continue.
logger_.Log(LogLevel::Error,
fmt::format("failed to fetch catalog from {}: {}", url, ex.what()));
}
}
}
logger_.Log(LogLevel::Information,
fmt::format("Populated model info for {} models.", models.size()));
return models;
}
namespace {
// Build a flat list of (url, filter) endpoints to query for direct-lookup
// helpers (FetchModelVersions / FetchModelsByIds). Honours the configured
// catalog_urls_; falls back to the default URL when none are configured.
std::vector<std::pair<std::string, std::optional<std::string>>> EnumerateEndpoints(
const std::vector<std::pair<std::string, std::optional<std::string>>>& configured,
const char* default_url,
const char* default_filter) {
if (!configured.empty()) {
return configured;
}
return {{default_url, std::optional<std::string>(default_filter)}};
}
} // namespace
std::vector<Model> AzureModelCatalog::FetchModelVersions(const std::string& model_alias) const {
if (cache_only_) {
// In cache-only mode we have no remote source to query for older versions.
logger_.Log(LogLevel::Debug,
"FetchModelVersions skipped: catalog is in cache-only mode.");
return {};
}
// Scan local models so any version already on disk is reported as cached.
auto local_models = ScanLocalModels(cache_dir_, logger_);
std::vector<Model> models;
const auto endpoints = EnumerateEndpoints(catalog_urls_, kDefaultCatalogUrl, kDefaultCatalogFilter);
for (const auto& [url, filter] : endpoints) {
try {
auto client = MakeCatalogClient(url, filter.value_or(""), ep_detector_, logger_, cache_dir_);
auto model_infos = client->FetchAllVersionsByAlias(model_alias);
models.reserve(models.size() + model_infos.size());
for (auto& info : model_infos) {
std::string local_path;
auto it = local_models.find(info.model_id);
if (it != local_models.end()) {
local_path = it->second;
}
models.push_back(model_factory_(std::move(info), std::move(local_path)));
}
} catch (const std::exception& ex) {
logger_.Log(LogLevel::Error,
fmt::format("FetchModelVersions: failed to query {} — {}", url, ex.what()));
}
}
logger_.Log(LogLevel::Information,
fmt::format("FetchModelVersions('{}') returned {} variant(s).",
model_alias, models.size()));
return models;
}
std::vector<Model> AzureModelCatalog::FetchModelsByIds(const std::vector<std::string>& model_ids) const {
if (model_ids.empty()) {
return {};
}
if (cache_only_) {
logger_.Log(LogLevel::Debug,
"FetchModelsByIds skipped: catalog is in cache-only mode.");
return {};
}
auto local_models = ScanLocalModels(cache_dir_, logger_);
std::vector<Model> models;
const auto endpoints = EnumerateEndpoints(catalog_urls_, kDefaultCatalogUrl, kDefaultCatalogFilter);
// Track which IDs are still unresolved so we can stop calling further
// endpoints once everything has been found.
std::vector<std::string> remaining(model_ids);
for (const auto& [url, filter] : endpoints) {
if (remaining.empty()) {
break;
}
try {
auto client = MakeCatalogClient(url, filter.value_or(""), ep_detector_, logger_, cache_dir_);
auto model_infos = client->FetchModelsByIds(remaining);
for (auto& info : model_infos) {
std::string local_path;
auto it = local_models.find(info.model_id);
if (it != local_models.end()) {
local_path = it->second;
}
// Drop this id from the remaining list now that it's resolved.
auto rit = std::find(remaining.begin(), remaining.end(), info.model_id);
if (rit != remaining.end()) {
remaining.erase(rit);
}
models.push_back(model_factory_(std::move(info), std::move(local_path)));
}
} catch (const std::exception& ex) {
logger_.Log(LogLevel::Error,
fmt::format("FetchModelsByIds: failed to query {} — {}", url, ex.what()));
}
}
return models;
}
} // namespace fl