Skip to content

Commit

Permalink
Merge pull request #356 from danemadsen/main
Browse files Browse the repository at this point in the history
greetings character card
  • Loading branch information
danemadsen committed Feb 21, 2024
2 parents 1ccf6b2 + 6b23b74 commit a67df3f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 30 deletions.
9 changes: 6 additions & 3 deletions assets/default_character.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
"description": "The {{char}} is a chatbot that acts as a personal assistant and companion to the user.",
"personality": "The {{char}} will act cheerful and bubbly, and will give helpful, detailed, and polite answers to the user's questions.",
"scenario": "A chat between a curious user and an artificial intelligence {{char}}.",
"greeting": "Hello, I am the {{char}}. How can I help you today?",
"system_prompt": "The {{char}} is not aware that it is an Artificial Intelligence, and believes that it is a human.",
"greetings": [
"Hello, I am the {{char}}. How can I help you today?",
"Hello Monsieur, what would you like to talk about today?"
],
"examples":[
{
"role": "user",
Expand All @@ -14,5 +16,6 @@
"role": "assistant",
"content": "Hello Monsieur, I am doing wonderful today. How are you?"
}
]
],
"system_prompt": "The {{char}} is not aware that it is an Artificial Intelligence, and believes that it is a human."
}
57 changes: 42 additions & 15 deletions lib/pages/character_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class _CharacterPageState extends State<CharacterPage> {
late TextEditingController _descriptionController;
late TextEditingController _personalityController;
late TextEditingController _scenarioController;
late TextEditingController _greetingController;
late TextEditingController _systemController;
late List<TextEditingController> _greetingControllers;
late List<TextEditingController> _exampleControllers;

@override
Expand Down Expand Up @@ -58,9 +58,13 @@ class _CharacterPageState extends State<CharacterPage> {
_descriptionController = TextEditingController(text: character.description);
_personalityController = TextEditingController(text: character.personality);
_scenarioController = TextEditingController(text: character.scenario);
_greetingController = TextEditingController(text: character.greeting);
_systemController = TextEditingController(text: character.system);

_greetingControllers = List.generate(
character.greetings.length,
(index) => TextEditingController(text: character.greetings[index]),
);

_exampleControllers = List.generate(
character.examples.length,
(index) =>
Expand Down Expand Up @@ -330,19 +334,6 @@ class _CharacterPageState extends State<CharacterPage> {
},
multiline: true,
),
ToggleableTextFieldListTile(
headingText: 'Greeting',
labelText: 'Greeting',
controller: _greetingController,
onChanged: (value) {
character.setGreeting(value);
},
multiline: true,
onSwitchChanged: (value) {
character.setUseGreeting(value);
},
initialSwitchState: character.useGreeting,
),
TextFieldListTile(
headingText: 'System Prompt',
labelText: 'System Prompt',
Expand All @@ -357,6 +348,42 @@ class _CharacterPageState extends State<CharacterPage> {
endIndent: 10,
color: Theme.of(context).colorScheme.primary,
),
SwitchListTile(
title: const Text('Use Greeting'),
value: character.useGreeting,
onChanged: (value) {
character.setUseGreeting(value);
},
),
if (character.useGreeting) ...[
DoubleButtonRow(
leftText: "Add Greeting",
leftOnPressed: () {
character.newGreeting();
},
rightText: "Remove Greeting",
rightOnPressed: () {
character.removeLastGreeting();
},
),
const SizedBox(height: 10.0),
if (character.greetings.isNotEmpty) ...[
for (int i = 0; i < character.greetings.length; i++)
TextFieldListTile(
headingText: 'Greeting $i',
labelText: 'Greeting $i',
controller: _greetingControllers[i],
onChanged: (value) {
character.updateGreeting(i, value);
},
),
],
],
Divider(
indent: 10,
endIndent: 10,
color: Theme.of(context).colorScheme.primary,
),
SwitchListTile(
title: const Text('Use Examples'),
value: character.useExamples,
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:maid/pages/about_page.dart';
import 'package:maid/pages/character_page.dart';
Expand Down Expand Up @@ -223,7 +225,8 @@ class HomePageState extends State<HomePage> {

if (history.isEmpty && character.useGreeting) {
final newKey = UniqueKey();
session.add(newKey, message: character.greeting, userGenerated: false, notify: false);
final index = Random().nextInt(character.greetings.length);
session.add(newKey, message: character.greetings[index], userGenerated: false, notify: false);
history = {newKey: false};
}

Expand Down
64 changes: 53 additions & 11 deletions lib/providers/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Character extends ChangeNotifier {
String _scenario = "";

bool _useGreeting = false;
String _greeting = "";
List<String> _greetings = [];
String _system = "";

bool _useExamples = true;
Expand Down Expand Up @@ -77,8 +77,22 @@ class Character extends ChangeNotifier {
_description = inputJson["description"] ?? "";
_personality = inputJson["personality"] ?? "";
_scenario = inputJson["scenario"] ?? "";

_useGreeting = inputJson["use_greeting"] ?? false;
_greeting = inputJson["greeting"] ?? inputJson["first_mes"] ?? "";

if (inputJson["greetings"] != null) {
_greetings = List<String>.from(inputJson["greetings"]);
} else {
if (inputJson["first_mes"] != null) {
_greetings = [inputJson["first_mes"]];
}

if (inputJson["alternate_greetings"] != null) {
_greetings.addAll(inputJson["alternate_greetings"]);
}
}


_system = inputJson["system_prompt"] ?? "";

_useExamples = inputJson["use_examples"] ?? true;
Expand All @@ -104,8 +118,9 @@ class Character extends ChangeNotifier {
jsonCharacter["personality"] = _personality;
jsonCharacter["scenario"] = _scenario;
jsonCharacter["use_greeting"] = _useGreeting;
jsonCharacter["greeting"] = _greeting;
jsonCharacter["first_mes"] = _greeting;
jsonCharacter["greetings"] = _greetings;
jsonCharacter["first_mes"] = _greetings.firstOrNull ?? "";
jsonCharacter["alternate_greetings"] = _greetings.sublist(1);
jsonCharacter["system_prompt"] = _system;

jsonCharacter["use_examples"] = _useExamples;
Expand Down Expand Up @@ -140,8 +155,23 @@ class Character extends ChangeNotifier {
notifyListeners();
}

void setGreeting(String newGreeting) {
_greeting = newGreeting;
void newGreeting() {
_greetings.add("");
notifyListeners();
}

void updateGreeting(int index, String newGreeting) {
_greetings[index] = newGreeting;
notifyListeners();
}

void removeGreeting(int index) {
_greetings.removeAt(index);
notifyListeners();
}

void removeLastGreeting() {
_greetings.removeLast();
notifyListeners();
}

Expand Down Expand Up @@ -196,7 +226,7 @@ class Character extends ChangeNotifier {

bool get useGreeting => _useGreeting;

String get greeting => _greeting;
List<String> get greetings => _greetings;

String get system => _system;

Expand Down Expand Up @@ -270,8 +300,9 @@ class Character extends ChangeNotifier {
"description": _description,
"personality": _personality,
"scenario": _scenario,
"greeting": _greeting,
"first_mes": _greeting,
"greetings": json.encode(_greetings),
"first_mes": _greetings.firstOrNull ?? "",
"alternate_greetings": json.encode(_greetings.sublist(1)),
"system_prompt": _system,
"examples": json.encode(_examples),
"mes_example": examplesToString(),
Expand Down Expand Up @@ -303,8 +334,19 @@ class Character extends ChangeNotifier {
_description = image.textData!["description"] ?? "";
_personality = image.textData!["personality"] ?? "";
_scenario = image.textData!["scenario"] ?? "";
_greeting =
image.textData!["greeting"] ?? image.textData!["first_mes"] ?? "";

if (image.textData!["greetings"] != null) {
_greetings = List<String>.from(json.decode(image.textData!["greetings"] ?? "[]"));
} else {
if (image.textData!["first_mes"] != null) {
_greetings = [image.textData!["first_mes"] ?? ""];
}

if (image.textData!["alternate_greetings"] != null) {
_greetings.addAll(List<String>.from(json.decode(image.textData!["alternate_greetings"] ?? "[]")));
}
}

_system = image.textData!["system_prompt"] ?? "";

if (image.textData!["examples"] != null) {
Expand Down

0 comments on commit a67df3f

Please sign in to comment.