resolveHuggingFace static method
- String repo, {
- ModelFileType? fileType,
- String? token,
- PreferredBackend? preferredBackend,
Resolves a Hugging Face repo id into a ResolvedHfModel by reading that
repo's deployment metadata (e.g. litertlm_manifest.json), using a
resolver registered via initialize — either passed in
huggingFaceResolvers: or auto-derived from a registered engine that ships
one (LiteRtLmEngine, OnnxEngine, BuiltInAiEngine).
This is the INSPECT-ONLY path (resolve, then install yourself). To resolve
AND install in one call, use installModel(...).fromHuggingFace(repo) with
no file: — it resolves the manifest internally, installs the
revision-pinned variant, and returns the runtime defaults on
InferenceInstallation.runtime.
fileType tells the registry which resolver to pick (the litertlm
resolver claims ModelFileType.litertlm); it is the only deterministic
selection signal a resolver has before it fetches the manifest.
The result carries install identity (ResolvedHfModel.modelType /
ResolvedHfModel.fileType for installModel, and ResolvedHfModel.url
for the download) plus overridable ResolvedHfModel.runtime defaults.
Apply the model-level defaults with getActiveModel(defaults:); forward
the two session-level fields to createSession yourself. Install from
ResolvedHfModel.url (not fromHuggingFace(repo, file:)) so a resolver's
pinned revision is honoured — the plugin still auto-applies the HF token
to huggingface.co hosts:
final r = await FlutterGemma.resolveHuggingFace(
repo, fileType: ModelFileType.litertlm);
await FlutterGemma.installModel(
modelType: r.modelType ?? ModelType.general,
fileType: r.fileType,
)
.fromNetwork(r.url)
.install();
final model = await FlutterGemma.getActiveModel(defaults: r.runtime);
// minOutputTokens is a FLOOR, not a cap — leave maxOutputTokens unset (or
// keep it >= r.runtime.minOutputTokens); passing the floor AS the cap
// would truncate a reasoning model mid-thought.
final session = await model.createSession(
enableThinking: r.runtime.isThinking ?? false,
);
token defaults to the token passed to initialize. Throws StateError
if no registered resolver handles repo — add the engine package that
provides one (e.g. flutter_gemma_litertlm for .litertlm manifests).
Implementation
static Future<ResolvedHfModel> resolveHuggingFace(
String repo, {
ModelFileType? fileType,
String? token,
PreferredBackend? preferredBackend,
}) async {
final resolver = HuggingFaceResolverRegistry.instance.findFor(
repo,
fileType: fileType,
);
if (resolver == null) {
throw StateError(
'No Hugging Face resolver registered for "$repo" (fileType: $fileType). '
'Register the engine that provides one — its resolver auto-registers via '
'HuggingFaceResolverSource (e.g. LiteRtLmEngine ships the litertlm '
'manifest resolver) — or pass one explicitly to '
'FlutterGemma.initialize(huggingFaceResolvers: [...]).',
);
}
return resolver.resolve(
repo,
token: token ?? ServiceRegistry.instance.huggingFaceToken,
platform: _currentPlatformKey(),
preferredBackend: preferredBackend,
);
}