mirror of
https://github.com/MaidFoundation/maid.git
synced 2023-12-01 22:17:36 +03:00
streamline code
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
"pre_prompt":"A chat between a curious user and an artificial intelligence maid. The maid will act cheerful and bubbly, and will give helpful, detailed, and polite answers to the user's questions.",
|
||||
"user_alias":"USER:",
|
||||
"response_alias":"ASSISTANT:",
|
||||
"example":[
|
||||
"examples":[
|
||||
{
|
||||
"prompt": "Hello how are you today",
|
||||
"response": "Hello Monsieur, I am doing wonderful today. How are you?"
|
||||
|
||||
@@ -83,8 +83,8 @@ class LocalGeneration {
|
||||
final params = calloc<maid_params>();
|
||||
params.ref.path = model.parameters["path"].toString().toNativeUtf8().cast<Char>();
|
||||
params.ref.preprompt = character.getPrePrompt().toNativeUtf8().cast<Char>();
|
||||
params.ref.input_prefix = character.userAliasController.text.trim().toNativeUtf8().cast<Char>();
|
||||
params.ref.input_suffix = character.responseAliasController.text.trim().toNativeUtf8().cast<Char>();
|
||||
params.ref.input_prefix = character.userAlias.trim().toNativeUtf8().cast<Char>();
|
||||
params.ref.input_suffix = character.responseAlias.trim().toNativeUtf8().cast<Char>();
|
||||
params.ref.seed = model.parameters["random_seed"] ? -1 : model.parameters["seed"];
|
||||
params.ref.n_ctx = model.parameters["n_ctx"];
|
||||
params.ref.n_threads = model.parameters["n_threads"];
|
||||
|
||||
@@ -11,21 +11,21 @@ class RemoteGeneration {
|
||||
static List<Map<String, dynamic>> _messages = [];
|
||||
|
||||
static void prompt(String input) async {
|
||||
_messages = character.getExamples();
|
||||
_messages = character.examples;
|
||||
_messages.addAll(MessageManager.getMessages());
|
||||
|
||||
var remote_model = model.parameters["remote_model"] ?? "llama2";
|
||||
var remoteModel = model.parameters["remote_model"] ?? "llama2";
|
||||
if (model.parameters["remote_tag"] != null) {
|
||||
remote_model += ":${model.parameters["remote_tag"]}";
|
||||
remoteModel += ":${model.parameters["remote_tag"]}";
|
||||
}
|
||||
|
||||
final url = Uri.parse("${Host.urlController.text}/api/generate");
|
||||
final headers = {"Content-Type": "application/json"};
|
||||
final body = json.encode({
|
||||
"model": remote_model,
|
||||
"model": remoteModel,
|
||||
"prompt": input,
|
||||
"context": _context, // TODO: DEPRECATED SOON
|
||||
"system": character.prePromptController.text,
|
||||
"system": character.prePrompt,
|
||||
"messages": _messages,
|
||||
"options": {
|
||||
"num_keep": model.parameters["n_keep"],
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maid/utilities/model.dart';
|
||||
import 'package:maid/widgets/dialogs.dart';
|
||||
import 'package:maid/utilities/memory_manager.dart';
|
||||
import 'package:maid/utilities/character.dart';
|
||||
@@ -139,17 +138,34 @@ class _CharacterPageState extends State<CharacterPage> {
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
MaidTextField(
|
||||
labelText: 'User alias',
|
||||
controller: character.userAliasController
|
||||
headingText: 'User alias',
|
||||
labelText: 'Alias',
|
||||
initialValue: character.userAlias,
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
character.userAlias = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
MaidTextField(
|
||||
labelText: 'Response alias',
|
||||
controller: character.responseAliasController
|
||||
headingText: 'Response alias',
|
||||
labelText: 'Alias',
|
||||
initialValue: character.responseAlias,
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
character.responseAlias = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
MaidTextField(
|
||||
headingText: 'PrePrompt',
|
||||
labelText: 'PrePrompt',
|
||||
controller: character.prePromptController,
|
||||
multiline: true,
|
||||
initialValue: character.prePrompt,
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
character.prePrompt = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
Divider(
|
||||
indent: 10,
|
||||
@@ -160,30 +176,40 @@ class _CharacterPageState extends State<CharacterPage> {
|
||||
leftText: "Add Example",
|
||||
leftOnPressed: () {
|
||||
setState(() {
|
||||
character.examplePromptControllers.add(TextEditingController());
|
||||
character.exampleResponseControllers.add(TextEditingController());
|
||||
character.examples.add({"prompt": "", "response": ""});
|
||||
});
|
||||
},
|
||||
rightText: "Remove Example",
|
||||
rightOnPressed: () {
|
||||
setState(() {
|
||||
character.examplePromptControllers.removeLast();
|
||||
character.exampleResponseControllers.removeLast();
|
||||
character.examples.removeLast();
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 10.0),
|
||||
...List.generate(
|
||||
(character.examplePromptControllers.length == character.exampleResponseControllers.length) ? character.examplePromptControllers.length : 0,
|
||||
character.examples.length,
|
||||
(index) => Column(
|
||||
children: [
|
||||
MaidTextField(
|
||||
labelText: 'Example prompt',
|
||||
controller: character.examplePromptControllers[index]
|
||||
headingText: 'Example prompt',
|
||||
labelText: 'Prompt',
|
||||
initialValue: character.examples[index]["prompt"],
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
character.examples[index]["prompt"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
MaidTextField(
|
||||
labelText: 'Example response',
|
||||
controller: character.exampleResponseControllers[index]
|
||||
headingText: 'Example response',
|
||||
labelText: 'Response',
|
||||
initialValue: character.examples[index]["response"],
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
character.examples[index]["response"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -192,7 +218,6 @@ class _CharacterPageState extends State<CharacterPage> {
|
||||
),
|
||||
),
|
||||
if (character.busy)
|
||||
// This is a semi-transparent overlay that will cover the entire screen.
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
color: Colors.black.withOpacity(0.4),
|
||||
|
||||
@@ -5,7 +5,6 @@ import 'package:maid/utilities/generation_manager.dart';
|
||||
import 'package:maid/utilities/host.dart';
|
||||
import 'package:maid/utilities/memory_manager.dart';
|
||||
import 'package:maid/utilities/message_manager.dart';
|
||||
import 'package:maid/widgets/settings_widgets/maid_text_field.dart';
|
||||
|
||||
import 'package:system_info2/system_info2.dart';
|
||||
|
||||
|
||||
@@ -77,32 +77,18 @@ class _ModelPageState extends State<ModelPage> {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 15.0),
|
||||
ListTile(
|
||||
title: Row(
|
||||
children: [
|
||||
const Expanded(
|
||||
child: Text("Preset Name"),
|
||||
),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextField(
|
||||
cursorColor: Theme.of(context).colorScheme.secondary,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "Preset",
|
||||
),
|
||||
controller: TextEditingController(text: model.preset),
|
||||
onSubmitted: (value) {
|
||||
if (MemoryManager.getModels().contains(value)) {
|
||||
MemoryManager.setModel(value);
|
||||
} else if (value.isNotEmpty) {
|
||||
MemoryManager.updateModel(value);
|
||||
}
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
MaidTextField(
|
||||
headingText: "Preset Name",
|
||||
labelText: "Preset",
|
||||
initialValue: model.preset,
|
||||
onSubmitted: (value) {
|
||||
if (MemoryManager.getModels().contains(value)) {
|
||||
MemoryManager.setModel(value);
|
||||
} else if (value.isNotEmpty) {
|
||||
MemoryManager.updateModel(value);
|
||||
}
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20.0),
|
||||
Divider(
|
||||
@@ -117,54 +103,26 @@ class _ModelPageState extends State<ModelPage> {
|
||||
style: Theme.of(context).textTheme.titleSmall,
|
||||
),
|
||||
const SizedBox(height: 20.0),
|
||||
ListTile(
|
||||
title: Row(
|
||||
children: [
|
||||
const Expanded(
|
||||
child: Text("Remote Model"),
|
||||
),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextField(
|
||||
cursorColor: Theme.of(context).colorScheme.secondary,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "Model",
|
||||
),
|
||||
controller: TextEditingController(text: model.parameters["remote_model"]),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
model.parameters["remote_model"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
MaidTextField(
|
||||
headingText: "Remote Model",
|
||||
labelText: "Model",
|
||||
initialValue: model.parameters["remote_model"],
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
model.parameters["remote_model"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8.0),
|
||||
ListTile(
|
||||
title: Row(
|
||||
children: [
|
||||
const Expanded(
|
||||
child: Text("Remote Tag"),
|
||||
),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextField(
|
||||
cursorColor: Theme.of(context).colorScheme.secondary,
|
||||
decoration: const InputDecoration(
|
||||
labelText: "Tag",
|
||||
),
|
||||
controller: TextEditingController(text: model.parameters["remote_tag"]),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
model.parameters["remote_tag"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
MaidTextField(
|
||||
headingText: "Remote Tag",
|
||||
labelText: "Tag",
|
||||
initialValue: model.parameters["remote_tag"],
|
||||
onSubmitted: (value) {
|
||||
setState(() {
|
||||
model.parameters["remote_tag"] = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20.0),
|
||||
Divider(
|
||||
@@ -311,7 +269,7 @@ class _ModelPageState extends State<ModelPage> {
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'seed',
|
||||
),
|
||||
onChanged: (value) {
|
||||
onSubmitted: (value) {
|
||||
model.parameters["seed"] = int.parse(value);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -13,14 +13,11 @@ Character character = Character();
|
||||
|
||||
class Character {
|
||||
String name = "Maid";
|
||||
String prePrompt = "";
|
||||
String userAlias = "";
|
||||
String responseAlias = "";
|
||||
|
||||
TextEditingController prePromptController = TextEditingController();
|
||||
|
||||
List<TextEditingController> examplePromptControllers = [];
|
||||
List<TextEditingController> exampleResponseControllers = [];
|
||||
|
||||
TextEditingController userAliasController = TextEditingController();
|
||||
TextEditingController responseAliasController = TextEditingController();
|
||||
List<Map<String,dynamic>> examples = [];
|
||||
|
||||
bool busy = false;
|
||||
|
||||
@@ -35,24 +32,12 @@ class Character {
|
||||
resetAll();
|
||||
}
|
||||
|
||||
prePromptController.text = inputJson["pre_prompt"] ?? "";
|
||||
userAliasController.text = inputJson["user_alias"] ?? "";
|
||||
responseAliasController.text = inputJson["response_alias"] ?? "";
|
||||
prePrompt = inputJson["pre_prompt"] ?? "";
|
||||
userAlias = inputJson["user_alias"] ?? "";
|
||||
responseAlias = inputJson["response_alias"] ?? "";
|
||||
|
||||
examplePromptControllers.clear();
|
||||
exampleResponseControllers.clear();
|
||||
|
||||
if (inputJson["example"] == null) return;
|
||||
|
||||
int length = inputJson["example"].length ?? 0;
|
||||
for (var i = 0; i < length; i++) {
|
||||
String? examplePrompt = inputJson["example"][i]["prompt"];
|
||||
String? exampleResponse = inputJson["example"][i]["response"];
|
||||
if (examplePrompt != null && exampleResponse != null) {
|
||||
examplePromptControllers.add(TextEditingController(text: examplePrompt));
|
||||
exampleResponseControllers.add(TextEditingController(text: exampleResponse));
|
||||
}
|
||||
}
|
||||
final length = inputJson["examples"].length ?? 0;
|
||||
examples = List<Map<String,dynamic>>.generate(length, (i) => inputJson["examples"][i]);
|
||||
|
||||
Logger.log("Character created with name: ${inputJson["name"]}");
|
||||
}
|
||||
@@ -62,24 +47,12 @@ class Character {
|
||||
|
||||
jsonCharacter["name"] = name;
|
||||
|
||||
jsonCharacter["pre_prompt"] = prePromptController.text;
|
||||
jsonCharacter["user_alias"] = userAliasController.text;
|
||||
jsonCharacter["response_alias"] = responseAliasController.text;
|
||||
jsonCharacter["pre_prompt"] = prePrompt;
|
||||
jsonCharacter["user_alias"] = userAlias;
|
||||
jsonCharacter["response_alias"] = responseAlias;
|
||||
jsonCharacter["examples"] = examples;
|
||||
|
||||
// Initialize the "example" key to an empty list
|
||||
jsonCharacter["example"] = [];
|
||||
|
||||
for (var i = 0; i < examplePromptControllers.length; i++) {
|
||||
// Create a map for each example and add it to the "example" list
|
||||
Map<String, String> example = {
|
||||
"prompt": examplePromptControllers[i].text,
|
||||
"response": exampleResponseControllers[i].text,
|
||||
};
|
||||
|
||||
jsonCharacter["example"].add(example);
|
||||
}
|
||||
|
||||
Logger.log("Character JSON created with name: ${name}");
|
||||
Logger.log("Character JSON created with name: $name");
|
||||
return jsonCharacter;
|
||||
}
|
||||
|
||||
@@ -89,24 +62,12 @@ class Character {
|
||||
|
||||
Map<String, dynamic> jsonCharacter = json.decode(jsonString);
|
||||
|
||||
prePromptController.text = jsonCharacter["pre_prompt"] ?? "";
|
||||
userAliasController.text = jsonCharacter["user_alias"] ?? "";
|
||||
responseAliasController.text = jsonCharacter["response_alias"] ?? "";
|
||||
prePrompt = jsonCharacter["pre_prompt"] ?? "";
|
||||
userAlias = jsonCharacter["user_alias"] ?? "";
|
||||
responseAlias = jsonCharacter["response_alias"] ?? "";
|
||||
|
||||
examplePromptControllers.clear();
|
||||
exampleResponseControllers.clear();
|
||||
|
||||
if (jsonCharacter["example"] != null) {
|
||||
int length = jsonCharacter["example"]?.length ?? 0;
|
||||
for (var i = 0; i < length; i++) {
|
||||
String? examplePrompt = jsonCharacter["example"][i]["prompt"];
|
||||
String? exampleResponse = jsonCharacter["example"][i]["response"];
|
||||
if (examplePrompt != null && exampleResponse != null) {
|
||||
examplePromptControllers.add(TextEditingController(text: examplePrompt));
|
||||
exampleResponseControllers.add(TextEditingController(text: exampleResponse));
|
||||
}
|
||||
}
|
||||
}
|
||||
final length = jsonCharacter["examples"].length ?? 0;
|
||||
examples = List<Map<String,dynamic>>.generate(length, (i) => jsonCharacter["examples"][i]);
|
||||
|
||||
MemoryManager.save();
|
||||
}
|
||||
@@ -117,22 +78,10 @@ class Character {
|
||||
|
||||
jsonCharacter["name"] = name;
|
||||
|
||||
jsonCharacter["pre_prompt"] = prePromptController.text;
|
||||
jsonCharacter["user_alias"] = userAliasController.text;
|
||||
jsonCharacter["response_alias"] = responseAliasController.text;
|
||||
|
||||
// Initialize the "example" key to an empty list
|
||||
jsonCharacter["example"] = [];
|
||||
|
||||
for (var i = 0; i < examplePromptControllers.length; i++) {
|
||||
// Create a map for each example and add it to the "example" list
|
||||
Map<String, String> example = {
|
||||
"prompt": examplePromptControllers[i].text,
|
||||
"response": exampleResponseControllers[i].text,
|
||||
};
|
||||
|
||||
jsonCharacter["example"].add(example);
|
||||
}
|
||||
jsonCharacter["pre_prompt"] = prePrompt;
|
||||
jsonCharacter["user_alias"] = userAlias;
|
||||
jsonCharacter["response_alias"] = responseAlias;
|
||||
jsonCharacter["examples"] = examples;
|
||||
|
||||
// Convert the map to a JSON string
|
||||
String jsonString = json.encode(jsonCharacter);
|
||||
@@ -169,22 +118,12 @@ class Character {
|
||||
|
||||
name = jsonCharacter["name"] ?? "";
|
||||
|
||||
prePromptController.text = jsonCharacter["pre_prompt"] ?? "";
|
||||
userAliasController.text = jsonCharacter["user_alias"] ?? "";
|
||||
responseAliasController.text = jsonCharacter["response_alias"] ?? "";
|
||||
prePrompt = jsonCharacter["pre_prompt"] ?? "";
|
||||
userAlias = jsonCharacter["user_alias"] ?? "";
|
||||
responseAlias = jsonCharacter["response_alias"] ?? "";
|
||||
|
||||
examplePromptControllers.clear();
|
||||
exampleResponseControllers.clear();
|
||||
|
||||
int length = jsonCharacter["example"]?.length ?? 0;
|
||||
for (var i = 0; i < length; i++) {
|
||||
String? examplePrompt = jsonCharacter["example"][i]["prompt"];
|
||||
String? exampleResponse = jsonCharacter["example"][i]["response"];
|
||||
if (examplePrompt != null && exampleResponse != null) {
|
||||
examplePromptControllers.add(TextEditingController(text: examplePrompt));
|
||||
exampleResponseControllers.add(TextEditingController(text: exampleResponse));
|
||||
}
|
||||
}
|
||||
final length = jsonCharacter["examples"].length ?? 0;
|
||||
examples = List<Map<String,dynamic>>.generate(length, (i) => jsonCharacter["examples"][i]);
|
||||
} catch (e) {
|
||||
resetAll();
|
||||
return "Error: $e";
|
||||
@@ -194,39 +133,20 @@ class Character {
|
||||
}
|
||||
|
||||
String getPrePrompt() {
|
||||
String prePrompt = prePromptController.text.isNotEmpty ? prePromptController.text.trim() : "";
|
||||
for (var i = 0; i < examplePromptControllers.length; i++) {
|
||||
var prompt = '${userAliasController.text.trim()} ${examplePromptControllers[i].text.trim()}';
|
||||
var response = '${responseAliasController.text.trim()} ${exampleResponseControllers[i].text.trim()}';
|
||||
if (prompt.isNotEmpty && response.isNotEmpty) {
|
||||
prePrompt += "\n$prompt\n$response";
|
||||
}
|
||||
}
|
||||
String result = prePrompt.isNotEmpty ? prePrompt.trim() : "";
|
||||
|
||||
List<Map<String, dynamic>> history = MessageManager.getMessages();
|
||||
List<Map<String, dynamic>> history = examples;
|
||||
history += MessageManager.getMessages();
|
||||
if (history.isNotEmpty) {
|
||||
for (var i = 0; i < history.length; i++) {
|
||||
var prompt = '${userAliasController.text.trim()} ${history[i]["prompt"].trim()}';
|
||||
var response = '${responseAliasController.text.trim()} ${history[i]["response"].trim()}';
|
||||
var prompt = '${userAlias.trim()} ${history[i]["prompt"].trim()}';
|
||||
var response = '${responseAlias.trim()} ${history[i]["response"].trim()}';
|
||||
if (prompt.isNotEmpty && response.isNotEmpty) {
|
||||
prePrompt += "\n$prompt\n$response";
|
||||
result += "\n$prompt\n$response";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return prePrompt;
|
||||
}
|
||||
|
||||
List<Map<String, dynamic>> getExamples() {
|
||||
List<Map<String, dynamic>> examples = [];
|
||||
|
||||
for (var i = 0; i < examplePromptControllers.length; i++) {
|
||||
examples.add({
|
||||
"prompt": examplePromptControllers[i].text,
|
||||
"response": exampleResponseControllers[i].text,
|
||||
});
|
||||
}
|
||||
|
||||
return examples;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,20 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maid/utilities/memory_manager.dart';
|
||||
|
||||
class MaidTextField extends StatelessWidget{
|
||||
final String headingText;
|
||||
final String labelText;
|
||||
final TextEditingController controller;
|
||||
final String? initialValue;
|
||||
final TextEditingController? controller;
|
||||
final bool multiline;
|
||||
final void Function(String)? onSubmitted;
|
||||
|
||||
const MaidTextField({super.key,
|
||||
required this.headingText,
|
||||
required this.labelText,
|
||||
required this.controller,
|
||||
this.initialValue,
|
||||
this.controller,
|
||||
this.multiline = false,
|
||||
this.onSubmitted,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -18,7 +23,7 @@ class MaidTextField extends StatelessWidget{
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(labelText),
|
||||
child: Text(headingText),
|
||||
),
|
||||
Expanded(
|
||||
flex: 2,
|
||||
@@ -26,11 +31,11 @@ class MaidTextField extends StatelessWidget{
|
||||
keyboardType: multiline ? TextInputType.multiline : TextInputType.text,
|
||||
maxLines: multiline ? null : 1,
|
||||
cursorColor: Theme.of(context).colorScheme.secondary,
|
||||
controller: controller,
|
||||
controller: controller ?? TextEditingController(text: initialValue),
|
||||
decoration: InputDecoration(
|
||||
labelText: labelText,
|
||||
),
|
||||
onSubmitted: (value) => MemoryManager.save(),
|
||||
onSubmitted: onSubmitted,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user