Added model preloading

This commit is contained in:
JHubi1 2024-06-25 20:14:25 +02:00
parent df4879fe6e
commit 4d90f8611b
No known key found for this signature in database
GPG Key ID: 7BF82570CBBBD050
3 changed files with 79 additions and 18 deletions

View File

@ -287,6 +287,11 @@
"description": "Text displayed as description for show model tags toggle", "description": "Text displayed as description for show model tags toggle",
"context": "Visible in the settings view" "context": "Visible in the settings view"
}, },
"settingsPreloadModels": "Preload models",
"@settingsPreloadModels": {
"description": "Text displayed as description for preload models toggle",
"context": "Visible in the settings view"
},
"settingsResetOnModelChange": "Reset on model change", "settingsResetOnModelChange": "Reset on model change",
"@settingsResetOnModelChange": { "@settingsResetOnModelChange": {
"description": "Text displayed as description for reset on model change toggle", "description": "Text displayed as description for reset on model change toggle",

View File

@ -27,13 +27,11 @@ class _ScreenSettingsInterfaceState extends State<ScreenSettingsInterface> {
color: Theme.of(context).colorScheme.surface, color: Theme.of(context).colorScheme.surface,
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
title: Row(children: [ title: Row(children: [
Text(AppLocalizations.of(context)!.settingsTitleInterface), Text(AppLocalizations.of(context)!.settingsTitleInterface),
Expanded(child: SizedBox(height: 200, child: MoveWindow())) Expanded(child: SizedBox(height: 200, child: MoveWindow()))
]), ]),
actions: actions: desktopControlsActions(context)),
desktopControlsActions(context)
),
body: Padding( body: Padding(
padding: const EdgeInsets.only(left: 16, right: 16), padding: const EdgeInsets.only(left: 16, right: 16),
child: Column(children: [ child: Column(children: [
@ -48,6 +46,14 @@ class _ScreenSettingsInterfaceState extends State<ScreenSettingsInterface> {
prefs!.setBool("modelTags", value); prefs!.setBool("modelTags", value);
setState(() {}); setState(() {});
}), }),
toggle(
context,
AppLocalizations.of(context)!.settingsPreloadModels,
(prefs!.getBool("preloadModel") ?? true), (value) {
selectionHaptic();
prefs!.setBool("preloadModel", value);
setState(() {});
}),
toggle( toggle(
context, context,
AppLocalizations.of(context)! AppLocalizations.of(context)!

View File

@ -1,5 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
@ -12,6 +13,7 @@ import 'package:ollama_dart/ollama_dart.dart' as llama;
// ignore: depend_on_referenced_packages // ignore: depend_on_referenced_packages
import 'package:flutter_chat_types/flutter_chat_types.dart' as types; import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
import 'package:http/http.dart' as http;
void setModel(BuildContext context, Function setState) { void setModel(BuildContext context, Function setState) {
List<String> models = []; List<String> models = [];
@ -31,7 +33,7 @@ void setModel(BuildContext context, Function setState) {
.cast<String, String>(), .cast<String, String>(),
baseUrl: "$host/api") baseUrl: "$host/api")
.listModels() .listModels()
.timeout(const Duration(seconds: 5)); .timeout(const Duration(seconds: 10));
for (var i = 0; i < list.models!.length; i++) { for (var i = 0; i < list.models!.length; i++) {
models.add(list.models![i].model!.split(":")[0]); models.add(list.models![i].model!.split(":")[0]);
modelsReal.add(list.models![i].model!); modelsReal.add(list.models![i].model!);
@ -74,14 +76,21 @@ void setModel(BuildContext context, Function setState) {
var content = StatefulBuilder(builder: (context, setLocalState) { var content = StatefulBuilder(builder: (context, setLocalState) {
setModalState = setLocalState; setModalState = setLocalState;
return PopScope( return PopScope(
canPop: loaded, canPop: false,
onPopInvoked: (didPop) { onPopInvoked: (didPop) async {
if (!loaded) return; if (!loaded) return;
if (usedIndex >= 0 && loaded = false;
modelsReal[usedIndex] != model && if (usedIndex >= 0 && modelsReal[usedIndex] != model) {
(prefs!.getBool("resetOnModelSelect") ?? true)) { if (prefs!.getBool("resetOnModelSelect") ?? true) {
messages = []; messages = [];
chatUuid = null; chatUuid = null;
}
} else {
setState(() {
desktopTitleVisible = true;
});
Navigator.of(context).pop();
return;
} }
model = (usedIndex >= 0) ? modelsReal[usedIndex] : null; model = (usedIndex >= 0) ? modelsReal[usedIndex] : null;
chatAllowed = !(model == null); chatAllowed = !(model == null);
@ -92,9 +101,50 @@ void setModel(BuildContext context, Function setState) {
prefs?.remove("model"); prefs?.remove("model");
} }
prefs?.setBool("multimodal", multimodal); prefs?.setBool("multimodal", multimodal);
setState(() {
desktopTitleVisible = true; if (model != null &&
}); int.parse(prefs!.getString("keepAlive") ?? "300") != 0 && (prefs!.getBool("preloadModel") ?? true)) {
setLocalState(() {});
try {
// don't use llama client, package doesn't support just loading without content
await http
.post(
Uri.parse("$host/api/generate"),
headers: {
"Content-Type": "application/json",
...(jsonDecode(prefs!.getString("hostHeaders") ?? "{}")
as Map)
}.cast<String, String>(),
body: jsonEncode({
"model": model!,
"keep_alive":
int.parse(prefs!.getString("keepAlive") ?? "300")
}),
)
.timeout(const Duration(seconds: 15));
} catch (_) {
// ignore: use_build_context_synchronously
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
// ignore: use_build_context_synchronously
content: Text(AppLocalizations.of(context)!
.settingsHostInvalid("timeout")),
showCloseIcon: true));
setState(() {
model = null;
chatAllowed = false;
});
}
setState(() {
desktopTitleVisible = true;
});
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
} else {
setState(() {
desktopTitleVisible = true;
});
Navigator.of(context).pop();
}
}, },
child: Container( child: Container(
width: desktopLayout(context) ? null : double.infinity, width: desktopLayout(context) ? null : double.infinity,