From 0086ec63bcdd663a37110a77b32e2f716785328a Mon Sep 17 00:00:00 2001 From: JHubi1 Date: Wed, 3 Jul 2024 17:56:54 +0200 Subject: [PATCH] Fixes described in #29 --- lib/l10n/app_en.arb | 10 +++++ lib/main.dart | 1 + lib/screen_settings.dart | 12 +++++- lib/settings/behavior.dart | 17 ++++++++ lib/worker/sender.dart | 84 +++++++++++++++++++++++--------------- lib/worker/setter.dart | 57 ++------------------------ untranslated_messages.json | 4 ++ 7 files changed, 96 insertions(+), 89 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 493ba80..afa5d0a 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -297,6 +297,16 @@ "description": "Text displayed as description for system message input", "context": "Visible in the settings view" }, + "settingsUseSystem": "Use system message", + "@settingsUseSystem": { + "description": "Text displayed as description for use system message toggle", + "context": "Visible in the settings view" + }, + "settingsUseSystemDescription": "Disables setting the system message above and use the one of the model instead. Can be useful for models with model files", + "@settingsUseSystemDescription": { + "description": "Description of the use system message toggle", + "context": "Visible in the settings view by long pressing the toggle" + }, "settingsDisableMarkdown": "Disable markdown", "@settingsDisableMarkdown": { "description": "Text displayed as description for disable markdown toggle", diff --git a/lib/main.dart b/lib/main.dart index 57e3201..f79be95 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -566,6 +566,7 @@ class _MainAppState extends State { Expanded( child: Text(jsonDecode(item)["title"], softWrap: false, + maxLines: 1, overflow: TextOverflow.fade, style: const TextStyle( fontWeight: FontWeight.w500)), diff --git a/lib/screen_settings.dart b/lib/screen_settings.dart index 6e82540..3c7f1c8 100644 --- a/lib/screen_settings.dart +++ b/lib/screen_settings.dart @@ -26,7 +26,8 @@ Widget toggle(BuildContext context, String text, bool value, {bool disabled = false, void Function()? onDisabledTap, void Function()? onLongTap, - void Function()? onDoubleTap}) { + void Function()? onDoubleTap, + Widget? icon}) { var space = "⁣"; // Invisible character: U+2063 var spacePlus = " $space"; return InkWell( @@ -49,12 +50,19 @@ Widget toggle(BuildContext context, String text, bool value, padding: const EdgeInsets.only(top: 4, bottom: 4), child: Stack(children: [ Padding( - padding: const EdgeInsets.only(left: 16, right: 16, top: 12), + padding: EdgeInsets.only( + left: (icon == null) ? 16 : 32, right: 16, top: 12), child: Divider( color: (Theme.of(context).brightness == Brightness.light) ? Colors.grey[300] : Colors.grey[900])), Row(mainAxisSize: MainAxisSize.max, children: [ + (icon != null) + ? Padding( + padding: const EdgeInsets.only(right: 8), + child: icon, + ) + : const SizedBox.shrink(), Expanded( child: Text(text + spacePlus, overflow: TextOverflow.ellipsis, diff --git a/lib/settings/behavior.dart b/lib/settings/behavior.dart index de76eff..aa5220d 100644 --- a/lib/settings/behavior.dart +++ b/lib/settings/behavior.dart @@ -66,6 +66,23 @@ class _ScreenSettingsBehaviorState extends State { ), border: const OutlineInputBorder())), const SizedBox(height: 16), + toggle( + context, + AppLocalizations.of(context)!.settingsUseSystem, + (prefs!.getBool("useSystem") ?? true), + (value) { + selectionHaptic(); + prefs!.setBool("useSystem", value); + setState(() {}); + }, + icon: const Icon(Icons.info_outline_rounded), + onLongTap: () { + selectionHaptic(); + ScaffoldMessenger.of(context).showSnackBar(SnackBar( + content: Text(AppLocalizations.of(context)! + .settingsUseSystemDescription), + showCloseIcon: true)); + }), toggle( context, AppLocalizations.of(context)!.settingsDisableMarkdown, diff --git a/lib/worker/sender.dart b/lib/worker/sender.dart index 68178ad..2dcefbf 100644 --- a/lib/worker/sender.dart +++ b/lib/worker/sender.dart @@ -25,9 +25,9 @@ Future> getHistory([String? addToSystem]) async { system += "\n$addToSystem"; } - List history = [ - llama.Message(role: llama.MessageRole.system, content: system) - ]; + List history = (prefs!.getBool("useSystem") ?? true) + ? [llama.Message(role: llama.MessageRole.system, content: system)] + : []; List history2 = []; images = []; for (var i = 0; i < messages.length; i++) { @@ -53,6 +53,52 @@ Future> getHistory([String? addToSystem]) async { return history; } +Future getTitleAi(List history) async { + final generated = await (llama.OllamaClient( + headers: (jsonDecode(prefs!.getString("hostHeaders") ?? "{}") as Map) + .cast(), + baseUrl: "$host/api")) + .generateChatCompletion( + request: llama.GenerateChatCompletionRequest( + model: model!, + messages: [ + const llama.Message( + role: llama.MessageRole.system, + content: + "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!"), + llama.Message( + role: llama.MessageRole.user, content: jsonEncode(history)) + ], + keepAlive: int.parse(prefs!.getString("keepAlive") ?? "300")), + ) + .timeout(const Duration(seconds: 10)); + var title = generated.message!.content + .replaceAll("\"", "") + .replaceAll("'", "") + .replaceAll("*", "") + .replaceAll("_", "") + .replaceAll("\n", " ") + .trim(); + return title; +} + +Future setTitleAi(List history) async { + try { + var title = await getTitleAi(history); + var tmp = (prefs!.getStringList("chats") ?? []); + for (var i = 0; i < tmp.length; i++) { + if (jsonDecode((prefs!.getStringList("chats") ?? [])[i])["uuid"] == + chatUuid) { + var tmp2 = jsonDecode(tmp[i]); + tmp2["title"] = title; + tmp[i] = jsonEncode(tmp2); + break; + } + } + prefs!.setStringList("chats", tmp); + } catch (_) {} +} + Future send(String value, BuildContext context, Function setState, {void Function(String currentText, bool done)? onStream, String? addToSystem}) async { @@ -210,37 +256,7 @@ Future send(String value, BuildContext context, Function setState, if (newChat && (prefs!.getBool("generateTitles") ?? true)) { void setTitle() async { - history = await getHistory(); - - try { - final generated = await client - .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```", - keepAlive: int.parse(prefs!.getString("keepAlive") ?? "300")), - ) - .timeout(const Duration(seconds: 10)); - var title = generated.response! - .replaceAll("\"", "") - .replaceAll("'", "") - .replaceAll("*", "") - .replaceAll("_", "") - .trim(); - var tmp = (prefs!.getStringList("chats") ?? []); - for (var i = 0; i < tmp.length; i++) { - if (jsonDecode((prefs!.getStringList("chats") ?? [])[i])["uuid"] == - chatUuid) { - var tmp2 = jsonDecode(tmp[i]); - tmp2["title"] = title; - tmp[i] = jsonEncode(tmp2); - break; - } - } - prefs!.setStringList("chats", tmp); - } catch (_) {} - + await setTitleAi(await getHistory()); setState(() {}); } diff --git a/lib/worker/setter.dart b/lib/worker/setter.dart index 320f169..c7ba0ea 100644 --- a/lib/worker/setter.dart +++ b/lib/worker/setter.dart @@ -4,9 +4,10 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; -import 'package:ollama_app/worker/desktop.dart'; +import 'desktop.dart'; import 'haptic.dart'; import '../main.dart'; +import 'sender.dart'; import 'package:dartx/dartx.dart'; import 'package:ollama_dart/ollama_dart.dart' as llama; @@ -518,58 +519,8 @@ Future prompt(BuildContext context, [])[i])["uuid"] == uuid) { try { - List history = []; - var tmp = jsonDecode(jsonDecode( - (prefs!.getStringList( - "chats") ?? - [])[i])["messages"]); - for (var j = 0; - j < tmp.length; - j++) { - if (tmp[j]["type"] != null) { - continue; - } - history - .add(tmp[j]["content"]); - } - if (history.isEmpty) { - controller - .text = AppLocalizations - .of(context)! - .imageOnlyConversation; - setLocalState(() { - loading = false; - }); - return; - } - - 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```", - keepAlive: int.parse( - prefs!.getString( - "keepAlive") ?? - "300")), - ) - .timeout(const Duration( - seconds: 10)); - var title = generated.response! - .replaceAll("\"", "") - .replaceAll("'", "") - .replaceAll("*", "") - .replaceAll("_", "") - .trim(); + var title = await getTitleAi( + await getHistory()); controller.text = title; setLocalState(() { loading = false; diff --git a/untranslated_messages.json b/untranslated_messages.json index f79c7bc..3b4a0d9 100644 --- a/untranslated_messages.json +++ b/untranslated_messages.json @@ -5,6 +5,8 @@ "settingsDescriptionVoice", "settingsDescriptionExport", "settingsDescriptionAbout", + "settingsUseSystem", + "settingsUseSystemDescription", "settingsPreloadModels", "settingsVoiceTtsNotSupported", "settingsVoiceTtsNotSupportedDescription", @@ -17,6 +19,8 @@ "settingsDescriptionVoice", "settingsDescriptionExport", "settingsDescriptionAbout", + "settingsUseSystem", + "settingsUseSystemDescription", "settingsPreloadModels", "settingsVoiceTtsNotSupported", "settingsVoiceTtsNotSupportedDescription",