createModel method

  1. @override
Future<InferenceModel> createModel({
  1. required ModelType modelType,
  2. ModelFileType fileType = ModelFileType.task,
  3. int maxTokens = 1024,
  4. PreferredBackend? preferredBackend,
  5. List<int>? loraRanks,
  6. int? maxNumImages,
  7. bool supportImage = false,
})
override

Creates and returns a new InferenceModel instance.

modelType — model type to create. maxTokens — maximum context length for the model. preferredBackend — backend preference (e.g., CPU, GPU). loraRanks — optional supported LoRA ranks. maxNumImages — maximum number of images (for multimodal models). supportImage — whether the model supports images.

Implementation

@override
Future<InferenceModel> createModel({
  required ModelType modelType,
  ModelFileType fileType = ModelFileType.task,
  int maxTokens = 1024,
  PreferredBackend? preferredBackend,
  List<int>? loraRanks,
  int? maxNumImages,
  bool supportImage = false, // Enabling image support
}) async {
  // TODO: Implement multimodal support for web
  if (supportImage || maxNumImages != null) {
    if (kDebugMode) {
      debugPrint('Warning: Image support is not yet implemented for web platform');
    }
  }

  // Check if model already exists with different parameters
  if (_initializedModel != null) {
    final existing = _initializedModel! as WebInferenceModel;

    // Check if parameters match
    final bool parametersChanged =
      existing.modelType != modelType ||
      existing.maxTokens != maxTokens ||
      existing.supportImage != supportImage ||
      (existing.maxNumImages ?? 0) != (maxNumImages ?? 0);

    if (parametersChanged) {
      if (kDebugMode) {
        debugPrint('[FlutterGemmaWeb] Model parameters changed, closing existing model');
      }
      await existing.close();
      _initializedModel = null;
    }
  }

  final model = _initializedModel ??= WebInferenceModel(
    modelType: modelType,
    fileType: fileType,
    maxTokens: maxTokens,
    loraRanks: loraRanks,
    modelManager:
        modelManager as WebModelManager, // Use the same instance from FlutterGemmaPlugin.instance
    supportImage: supportImage, // Passing the flag
    maxNumImages: maxNumImages,
    onClose: () {
      _initializedModel = null;
    },
  );
  return model;
}