From 0b04e89cc95858fb954788d035b9b226e0a8d084 Mon Sep 17 00:00:00 2001 From: JHubi1 Date: Sat, 8 Jun 2024 22:00:17 +0200 Subject: [PATCH] Added keep_alive --- lib/l10n/app_de.arb | 26 ++++++++ lib/l10n/app_en.arb | 26 ++++++++ lib/main.dart | 11 +++- lib/settings/interface.dart | 123 +++++++++++++++++++++++++++++++++++- lib/worker/setter.dart | 29 +++++---- pubspec.lock | 8 +++ pubspec.yaml | 1 + 7 files changed, 207 insertions(+), 17 deletions(-) diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index 6e0b9b4..84500ba 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -262,6 +262,32 @@ "description": "Text displayed as description for show tips toggle", "context": "Visible in the settings view" }, + "settingsKeepModelLoadedAlways": "Modell immer geladen lassen", + "@settingsKeepModelLoadedAlways": { + "description": "Text displayed as description for keep model loaded always toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedNever": "Modell nicht dauerhaft laden", + "@settingsKeepModelLoadedNever": { + "description": "Text displayed as description for don't keep model loaded toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedFor": "Bestimmte Modell-Ladedauer festlegen", + "@settingsKeepModelLoadedFor": { + "description": "Text displayed as description for keep model loaded for toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedSet": "Modell für {minutes} Minuten geladen behalten", + "@settingsKeepModelLoadedSet": { + "description": "Text displayed as description for keep model loaded for set time toggle", + "context": "Visible in the settings view", + "placeholders": { + "minutes": { + "type": "String", + "description": "Minutes the model should be kept loaded" + } + } + }, "settingsEnableHapticFeedback": "Haptisches Feedback aktivieren", "@settingsEnableHapticFeedback": { "description": "Text displayed as description for enable haptic feedback toggle", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 1d3cff9..47e4821 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -262,6 +262,32 @@ "description": "Text displayed as description for show tips toggle", "context": "Visible in the settings view" }, + "settingsKeepModelLoadedAlways": "Keep model always loaded", + "@settingsKeepModelLoadedAlways": { + "description": "Text displayed as description for keep model loaded always toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedNever": "Don't keep model loaded", + "@settingsKeepModelLoadedNever": { + "description": "Text displayed as description for don't keep model loaded toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedFor": "Set specific time to keep model loaded", + "@settingsKeepModelLoadedFor": { + "description": "Text displayed as description for keep model loaded for toggle", + "context": "Visible in the settings view" + }, + "settingsKeepModelLoadedSet": "Keep model loaded for {minutes} minutes", + "@settingsKeepModelLoadedSet": { + "description": "Text displayed as description for keep model loaded for set time toggle", + "context": "Visible in the settings view", + "placeholders": { + "minutes": { + "type": "String", + "description": "Minutes the model should be kept loaded" + } + } + }, "settingsEnableHapticFeedback": "Enable haptic feedback", "@settingsEnableHapticFeedback": { "description": "Text displayed as description for enable haptic feedback toggle", diff --git a/lib/main.dart b/lib/main.dart index e516175..ecabcc0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1187,7 +1187,9 @@ class _MainAppState extends State { request: llama.GenerateChatCompletionRequest( model: model!, messages: history, - keepAlive: 1, + keepAlive: int.parse(prefs! + .getString("keepAlive") ?? + "300") ), ) .timeout(const Duration(seconds: 15)); @@ -1221,7 +1223,9 @@ class _MainAppState extends State { request: llama.GenerateChatCompletionRequest( model: model!, messages: history, - keepAlive: 1, + keepAlive: int.parse(prefs! + .getString("keepAlive") ?? + "300") ), ) .timeout(const Duration(seconds: 15)); @@ -1298,6 +1302,9 @@ class _MainAppState extends State { model: model!, prompt: "You must not use markdown or any other formatting language! Create a short title for the subject of the conversation described in the following json object. It is not allowed to be too general; no 'Assistance', 'Help' or similar!\n\n```json\n${jsonEncode(history)}\n```", + keepAlive: int.parse(prefs! + .getString("keepAlive") ?? + "300") ), ); var title = generated.response! diff --git a/lib/settings/interface.dart b/lib/settings/interface.dart index 214883f..fbe2e0c 100644 --- a/lib/settings/interface.dart +++ b/lib/settings/interface.dart @@ -10,6 +10,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:bitsdojo_window/bitsdojo_window.dart'; import 'package:restart_app/restart_app.dart'; +import 'package:duration_picker/duration_picker.dart'; class ScreenSettingsInterface extends StatefulWidget { const ScreenSettingsInterface({super.key}); @@ -149,6 +150,124 @@ class _ScreenSettingsInterfaceState extends State { setState(() {}); }), titleDivider(), + toggle( + context, + AppLocalizations.of(context)! + .settingsKeepModelLoadedAlways, + int.parse(prefs!.getString("keepAlive") ?? "300") == -1, + (value) { + setState(() { + if (value) { + prefs!.setString("keepAlive", "-1"); + } else { + prefs!.setString("keepAlive", "300"); + } + }); + }), + toggle( + context, + AppLocalizations.of(context)! + .settingsKeepModelLoadedNever, + int.parse(prefs!.getString("keepAlive") ?? "300") == 0, + (value) { + setState(() { + if (value) { + prefs!.setString("keepAlive", "0"); + } else { + prefs!.setString("keepAlive", "300"); + } + }); + }), + button( + (int.parse(prefs!.getString("keepAlive") ?? "300") > 0) + ? AppLocalizations.of(context)! + .settingsKeepModelLoadedSet((int.parse( + prefs!.getString("keepAlive") ?? + "300") ~/ + 60) + .toString()) + : AppLocalizations.of(context)! + .settingsKeepModelLoadedFor, + Icons.snooze_rounded, () async { + bool loaded = false; + await showDialog( + context: context, + builder: (context) { + return Dialog( + alignment: (Platform.isWindows || + Platform.isLinux || + Platform.isMacOS) + ? null + : Alignment.bottomRight, + child: StatefulBuilder( + builder: (context, setLocalState) { + if (int.parse(prefs!.getString("keepAlive") ?? + "0") <= + 0 && + loaded == false) { + prefs!.setString("keepAlive", "0"); + WidgetsBinding.instance + .addPostFrameCallback((timeStamp) { + setLocalState(() {}); + void load() async { + try { + while (int.parse(prefs! + .getString("keepAlive")!) < + 300) { + await Future.delayed(const Duration( + milliseconds: 5)); + prefs!.setString( + "keepAlive", + (int.parse(prefs!.getString( + "keepAlive")!) + + 30) + .toString()); + setLocalState(() {}); + setState(() {}); + } + prefs!.setString("keepAlive", "300"); + loaded = true; + } catch (_) { + prefs!.setString("keepAlive", "300"); + loaded = true; + } + } + + load(); + }); + } else { + loaded = true; + } + return Padding( + padding: const EdgeInsets.all(16), + child: Theme( + data: ThemeData.from( + colorScheme: ColorScheme.fromSeed( + seedColor: Colors.black)), + child: DurationPicker( + duration: Duration( + seconds: int.parse(prefs! + .getString("keepAlive") ?? + "300")), + baseUnit: BaseUnit.minute, + lowerBound: + const Duration(minutes: 1), + upperBound: + const Duration(minutes: 60), + onChange: (value) { + if (!loaded) return; + if (value.inSeconds == 0) return; + prefs!.setString("keepAlive", + value.inSeconds.toString()); + setLocalState(() {}); + setState(() {}); + }), + ), + ); + })); + }); + }), + titleDivider(), toggle( context, AppLocalizations.of(context)! @@ -170,7 +289,7 @@ class _ScreenSettingsInterfaceState extends State { setState(() {}); }) : const SizedBox.shrink(), - const SizedBox(height: 16), + const SizedBox(height: 8), SegmentedButton( segments: [ ButtonSegment( @@ -250,7 +369,7 @@ class _ScreenSettingsInterfaceState extends State { }); }); }), - const SizedBox(height: 16) + const SizedBox(height: 16) ]), ) ]))), diff --git a/lib/worker/setter.dart b/lib/worker/setter.dart index 86c4cd7..7387b86 100644 --- a/lib/worker/setter.dart +++ b/lib/worker/setter.dart @@ -462,21 +462,24 @@ Future prompt(BuildContext context, return; } - final generated = await llama.OllamaClient( - headers: (jsonDecode(prefs! - .getString( - "hostHeaders") ?? - "{}") as Map) - .cast(), - baseUrl: "$host/api") - .generateCompletion( + final generated = + await llama.OllamaClient( + headers: (jsonDecode(prefs! + .getString( + "hostHeaders") ?? + "{}") as Map) + .cast(), + baseUrl: "$host/api", + ).generateCompletion( request: llama .GenerateCompletionRequest( - model: model!, - prompt: - "You must not use markdown or any other formatting language! Create a short title for the subject of the conversation described in the following json object. It is not allowed to be too general; no 'Assistance', 'Help' or similar!\n\n```json\n${jsonEncode(history)}\n```", - ), + model: model!, + prompt: + "You must not use markdown or any other formatting language! Create a short title for the subject of the conversation described in the following json object. It is not allowed to be too general; no 'Assistance', 'Help' or similar!\n\n```json\n${jsonEncode(history)}\n```", + keepAlive: int.parse( + prefs!.getString( + "keepAlive") ?? + "300")), ); var title = generated.response! .replaceAll("*", "") diff --git a/pubspec.lock b/pubspec.lock index 8e3dd9c..532f821 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -137,6 +137,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.1" + duration_picker: + dependency: "direct main" + description: + name: duration_picker + sha256: e505a749c93f3218aa4194d339e5d5480d927df23a81f075b5282511f6ac11ab + url: "https://pub.dev" + source: hosted + version: "1.2.0" equatable: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 56ccd49..8c5fbe5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: package_info_plus: ^8.0.0 version: ^3.0.2 flutter_displaymode: ^0.6.0 + duration_picker: ^1.2.0 dev_dependencies: flutter_test: