Fixes described in #29

This commit is contained in:
JHubi1 2024-07-03 17:56:54 +02:00
parent 9f4e27758e
commit 0086ec63bc
No known key found for this signature in database
GPG Key ID: 7BF82570CBBBD050
7 changed files with 96 additions and 89 deletions

View File

@ -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",

View File

@ -566,6 +566,7 @@ class _MainAppState extends State<MainApp> {
Expanded(
child: Text(jsonDecode(item)["title"],
softWrap: false,
maxLines: 1,
overflow: TextOverflow.fade,
style: const TextStyle(
fontWeight: FontWeight.w500)),

View File

@ -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,

View File

@ -66,6 +66,23 @@ class _ScreenSettingsBehaviorState extends State<ScreenSettingsBehavior> {
),
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,

View File

@ -25,9 +25,9 @@ Future<List<llama.Message>> getHistory([String? addToSystem]) async {
system += "\n$addToSystem";
}
List<llama.Message> history = [
llama.Message(role: llama.MessageRole.system, content: system)
];
List<llama.Message> history = (prefs!.getBool("useSystem") ?? true)
? [llama.Message(role: llama.MessageRole.system, content: system)]
: [];
List<llama.Message> history2 = [];
images = [];
for (var i = 0; i < messages.length; i++) {
@ -53,6 +53,52 @@ Future<List<llama.Message>> getHistory([String? addToSystem]) async {
return history;
}
Future<String> getTitleAi(List history) async {
final generated = await (llama.OllamaClient(
headers: (jsonDecode(prefs!.getString("hostHeaders") ?? "{}") as Map)
.cast<String, String>(),
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<void> 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<String> send(String value, BuildContext context, Function setState,
{void Function(String currentText, bool done)? onStream,
String? addToSystem}) async {
@ -210,37 +256,7 @@ Future<String> 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(() {});
}

View File

@ -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<String> 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<String, String>(),
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;

View File

@ -5,6 +5,8 @@
"settingsDescriptionVoice",
"settingsDescriptionExport",
"settingsDescriptionAbout",
"settingsUseSystem",
"settingsUseSystemDescription",
"settingsPreloadModels",
"settingsVoiceTtsNotSupported",
"settingsVoiceTtsNotSupportedDescription",
@ -17,6 +19,8 @@
"settingsDescriptionVoice",
"settingsDescriptionExport",
"settingsDescriptionAbout",
"settingsUseSystem",
"settingsUseSystemDescription",
"settingsPreloadModels",
"settingsVoiceTtsNotSupported",
"settingsVoiceTtsNotSupportedDescription",