Melange
API ReferenceFlutter

ZeticMLangeLLMModel

API reference for running LLM inference in Flutter.

This page reflects zetic_mlange 1.9.1.

ZeticMLangeLLMModel loads an on-device LLM and supports text generation, token streaming, function calling, and image response for LFM-VL models.

Import

import 'package:zetic_mlange/zetic_mlange.dart';

create

static Future<ZeticMLangeLLMModel> create({
  required String personalKey,
  required String name,
  int? version,
  LLMModelMode modelMode = LLMModelMode.runAuto,
  APType? apType,
  LLMQuantType? quantType,
  ModelCacheHandlingPolicy cacheHandlingPolicy =
      CacheHandlingPolicy.removeOverlapping,
  LLMInitOption? initOption,
  LLMKVCacheCleanupPolicy kvCacheCleanupPolicy =
      LLMKVCacheCleanupPolicy.cleanUpOnFull,
  MlangeProgressCallback? onDownload,
})
ParameterTypeDefaultDescription
personalKeyString-Personal key for accessing the model.
nameString-Model name in account_name/project_name format.
versionint?nullModel version. null loads the latest version.
modelModeLLMModelModerunAutoBackend selection strategy.
apTypeAPType?nullOptional processor filter.
quantTypeLLMQuantType?nullOptional quantization filter.
cacheHandlingPolicyModelCacheHandlingPolicyremoveOverlappingManaged artifact cache cleanup policy.
initOptionLLMInitOption?nullLLM initialization options.
kvCacheCleanupPolicyLLMKVCacheCleanupPolicycleanUpOnFullConvenience default used when initOption is omitted.
onDownloadMlangeProgressCallback?nullDownload progress callback from 0.0 to 1.0.
final model = await ZeticMLangeLLMModel.create(
  personalKey: personalKey,
  name: 'account_name/project_name',
  initOption: const LLMInitOption(nCtx: 4096),
);

Text Generation

run

Starts generation for a prompt.

LLMRunResult run(String text)
final result = model.run('Explain on-device AI in one paragraph.');

waitForNextToken

Waits for the next generated token.

LLMNextTokenResult waitForNextToken()
while (true) {
  final next = model.waitForNextToken();
  if (next.isFinished) break;
  append(next.token);
}

Vision-Language Response

Use respond(...) with an LFM-VL-capable model.

Future<String> respond({
  String systemPrompt = '',
  required String userText,
  required ZeticMLangeLLMImage image,
})
final image = ZeticMLangeLLMImage(
  rgb: rgbBytes,
  width: width,
  height: height,
);

final response = await model.respond(
  systemPrompt: 'Answer briefly.',
  userText: 'What is in this image?',
  image: image,
);

Function Calling

String? functionCallingSystemPrompt

void registerTool(LLMToolSpec spec, LLMToolExecutor executor)
bool unregisterTool(String name)
void clearTools()
List<LLMToolSpec> registeredTools()
model.registerTool(
  const LLMToolSpec(
    name: 'lookup',
    description: 'Look up local app data.',
    parametersJson: '{"type":"object","properties":{"query":{"type":"string"}}}',
  ),
  (call) => const LLMToolResult(content: '{"result":"Found"}'),
);

model.run('Use lookup to answer the question.');

Lifecycle

bool get isClosed
void cleanUp()
Future<void> resetSession()
Future<void> close()
@Deprecated('Use close() instead.')
Future<void> deinit()

Call cleanUp() or resetSession() before starting a fresh conversation. Call close() when the model is no longer needed.

On this page