createSession method

  1. @override
Future<InferenceModelSession> createSession({
  1. double temperature = .8,
  2. int randomSeed = 1,
  3. int topK = 1,
  4. double? topP,
  5. String? loraPath,
})
override

Creates a new InferenceModelSession for generation.

temperature, randomSeed, topK, topP — parameters for sampling. loraPath — optional path to LoRA model.

Implementation

@override
Future<InferenceModelSession> createSession({
  double temperature = .8,
  int randomSeed = 1,
  int topK = 1,
  double? topP,
  String? loraPath,
}) async {
  if (_isClosed) {
    throw Exception('Model is closed. Create a new instance to use it again');
  }
  if (_createCompleter case Completer<InferenceModelSession> completer) {
    return completer.future;
  }
  final completer = _createCompleter = Completer<InferenceModelSession>();
  try {
    final (isLoraInstalled, File? loraFile) = await (
      modelManager.isLoraInstalled,
      modelManager._loraFile,
    ).wait;

    final resolvedLoraPath = (isLoraInstalled && loraFile != null) ? loraFile.path : loraPath;

    await _platformService.createSession(
      randomSeed: randomSeed,
      temperature: temperature,
      topK: topK,
      topP: topP,
      loraPath: resolvedLoraPath,
    );

    final session = _session = MobileInferenceModelSession(
      modelType: modelType,
      onClose: () {
        _session = null;
        _createCompleter = null;
      },
    );
    return session;
  } catch (e, st) {
    completer.completeError(e, st);
    Error.throwWithStackTrace(e, st);
  }
}