createSession method
Future<InferenceModelSession>
createSession({
- double temperature = 0.8,
- int randomSeed = 1,
- int topK = 1,
- double? topP,
- String? loraPath,
- bool? enableVisionModality,
override
Creates a new InferenceModelSession for generation.
temperature
, randomSeed
, topK
, topP
— parameters for sampling.
loraPath
— optional path to LoRA model.
enableVisionModality
— enable vision modality for multimodal models.
Implementation
@override
Future<InferenceModelSession> createSession({
double temperature = 0.8,
int randomSeed = 1,
int topK = 1,
double? topP,
String? loraPath,
bool? enableVisionModality, // Enabling vision modality support
}) async {
// TODO: Implement vision modality for web
if (enableVisionModality == true) {
if (kDebugMode) {
print('Warning: Vision modality is not yet implemented for web platform');
}
}
if (_initCompleter case Completer<InferenceModelSession> completer) {
return completer.future;
}
final completer = _initCompleter = Completer<InferenceModelSession>();
try {
final fileset = await FilesetResolver.forGenAiTasks('https://cdn.jsdelivr.net/npm/@mediapipe/tasks-genai@latest/wasm'.toJS).toDart;
final loraPathToUse = loraPath ?? modelManager._loraPath;
final hasLoraParams = loraPathToUse != null && loraRanks != null;
final config = LlmInferenceOptions(
baseOptions: LlmInferenceBaseOptions(modelAssetPath: modelManager._path),
maxTokens: maxTokens,
randomSeed: randomSeed,
topK: topK,
temperature: temperature,
topP: topP,
supportedLoraRanks: !hasLoraParams ? null : Int32List.fromList(loraRanks!).toJS,
loraPath: !hasLoraParams ? null : loraPathToUse,
maxNumImages: supportImage ? (maxNumImages ?? 1) : null,
supportAudio: false, // Not implemented yet
);
final llmInference = await LlmInference.createFromOptions(fileset, config).toDart;
final session = this.session = WebModelSession(
modelType: modelType,
fileType: fileType,
llmInference: llmInference,
supportImage: supportImage, // Enabling image support
onClose: onClose,
);
completer.complete(session);
return session;
} catch (e) {
throw Exception("Failed to create session: $e");
}
}