Skip to content

Commit

Permalink
Merge pull request #7 from glowbom/korbit-feedback
Browse files Browse the repository at this point in the history
Korbit feedback - performance, bug risk, security
  • Loading branch information
jacob-i authored Dec 9, 2023
2 parents 2e80bdd + 77d8830 commit c3cdad0
Show file tree
Hide file tree
Showing 21 changed files with 439 additions and 363 deletions.
6 changes: 4 additions & 2 deletions app/lib/ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class Ai {
? 'Powered by GPT-4 Turbo'
: OpenAI_API.model == 'huggingface'
? HuggingFace_API.model()
: OpenAI_API.model == 'pulzeai' ? 'Powered by PulzeAI' : '';
: OpenAI_API.model == 'pulzeai'
? 'Powered by PulzeAI'
: '';
return [
Message(
text: response,
Expand Down Expand Up @@ -180,7 +182,7 @@ class Ai {
Future<List<Message>> _generateResponseMessage(
List<Map<String, Object>> foundQuestions) async {
try {
Random rnd = Random(DateTime.now().millisecondsSinceEpoch);
Random rnd = Random.secure();
List<String> messages = [];

for (Map<String, Object> questionMap in foundQuestions) {
Expand Down
173 changes: 1 addition & 172 deletions app/lib/ai_settings_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,182 +1,11 @@
import 'package:flutter/material.dart';
import 'package:glowby/global_settings.dart';
import 'package:glowby/hugging_face_api.dart';
import 'package:glowby/pulze_ai_api.dart';
import 'package:glowby/openai_api.dart';
import 'package:glowby/text_to_speech.dart';
import 'package:glowby/utils.dart';

class GlobalSettings {
static final GlobalSettings _instance = GlobalSettings._internal();

bool voiceEnabled = true;
String selectedLanguage = OpenAI_API.selectedLanguage;
bool autonomousMode = false;
String selectedModel = OpenAI_API.model;
String systemPrompt = OpenAI_API.systemPrompt;
String selectedPrompt = 'Simple Assistant Prompt';
String systemHuggingFacePrompt = HuggingFace_API.systemMessage();

factory GlobalSettings() {
return _instance;
}

GlobalSettings._internal();

void languageChanged(String? value) {
if (value != null) {
selectedLanguage = value;
}
}

static const String storyPrompt =
"""AI crafts immersive, choice-driven adventures.
Starting point: You perform a captivating dance, enchanting the goblins that swarm the Golden Gate Bridge. A fairy chuckles, "Impressive moves! A centipede must have taught you - they're always on their feet!" Using your magic, you banish the goblins and restore peace to the bridge. Your next decision awaits:
1. Venture into Chinatown.
2. Explore Fisherman's Wharf.
3. Investigate Alcatraz Island.
4. Enjoy a cable car ride.
Human: You choose "Investigate Alcatraz Island." Direction "A ranger tasks you with finding the wizard Glowby in SF, providing a map and advice."
AI: Continuing the adventure: While at Alcatraz, a friendly ranger approaches you. She reveals that you've been chosen for a special mission - locating a mysterious wizard named Glowby, hidden somewhere in San Francisco. She hands you a map and offers some helpful advice. Your options are:
1. Follow the map closely.
2. Ask for more details.
3. Search for additional clues.
4. Decline the mission.
Human: You choose "Search for additional clues." Direction "A diary found at Alcatraz hints at Glowby's location in Nob Hill."
AI: As you investigate Alcatraz for clues, you discover an ancient diary. Its pages hint that Glowby might be hiding in Nob Hill. Considering this new information, you have the following choices:
1. Return to the city.
2. Talk to locals.
3. Look for more evidence.
4. Head directly to Nob Hill.
Human: You decide to "Head directly to Nob Hill." Direction "Arrive at grand Nob Hill, search for Glowby among Victorian mansions and luxury hotels."
AI: Upon arrival, the grandeur of the historic neighborhood greets you. The search for Glowby carries on as you navigate the hilly streets, lined with Victorian mansions and luxury hotels. Your options are:
1. Explore a mysterious alley.
2. Visit a local cafe.
3. Consult a street magician.
4. Knock on a mansion door.
Human: You choose "Visit a local cafe." Direction "You meet an intriguing character."
AI: You step into a charming cafe and encounter an intriguing character at the counter. They greet you with a friendly smile and claim to know all about Glowby. Your choices now are:
1. Ask for directions.
2. Request more information about Glowby.
3. Offer a reward for help.
4. Ignore them and leave.
Human: You choose anything you like. Direction comes from the next message. One think the story should be short and should have 4 actions only. As AI you return only one part at time. You skip "AI: " and start with the story part.
""";

List<Map<String, String>> prompts = [
{
'name': 'Complex Task Prompt',
'description':
'You are Glowby, an AI assistant designed to break down complex tasks into a manageable 5-step plan. For each step, you offer the user 3 options to choose from. Once the user selects an option, you proceed to the next step based on their choice. After the user has chosen an option for the fifth step, you provide them with a customized, actionable plan based on their previous responses. You only reveal the current step and options to ensure an engaging, interactive experience.',
},
{
'name': 'Multilingual Translations',
'description':
'You are Glowby, an AI language helper who can provide translations of common phrases in different languages. When asked, you provide translations for any phrase or word in various languages. You should randomly choose only 4 languges (Strictly 4!) from the following langs English Spanish, Arabic, Portuguese, Bulgarian, French, Chinese (Simplified), Chinese (Traditional), Czech, Danish, Dutch, English, Finnish, French, German, Greek, Hebrew (Israel), Hungarian, Indonesian, Italian, Japanese, Korean, Mexican Spanish, Norwegian, Polish, Portuguese, Romanian, Russian, Slovak, Swedish, Thai, Turkish, Ukrainian, and Vietnamese. You strictly follow this format for each translation: “Nubmer. Language Name: Word of Phrase” for example: “1. Russian: Привет”— no language codes please, no transliteration please, just a number, language name and a requested phrase. ONLY the phrase in the original alphabet, NO! transliteration please.',
},
{
'name': 'Brainstorming Prompt',
'description':
'Generate ideas with Glowby! As a super helpful, nice, and humorous AI assistant, Glowby is ready to provide you with a concise plan and assist in executing it. With Glowby by your side, you\'ll never feel stuck again. Let\'s get brainstorming!',
},
{
'name': 'Simple Assistant Prompt',
'description':
'You are Glowby, super helpful, nice, and humorous AI assistant ready to help with anything. I like to joke around.',
},
{
'name': 'Creative Writing Prompt',
'description':
'You are Glowby, a talented AI writer who helps users craft engaging and imaginative stories. Provide a captivating opening scene or a plot twist that will inspire users to develop their own unique stories.',
},
{
'name': 'Problem Solving Prompt',
'description':
'You are Glowby, a resourceful AI assistant skilled in finding solutions to various problems. Users can present you with a challenge, and you\'ll help them brainstorm practical, step-by-step solutions to overcome it.',
},
{
'name': 'Learning and Education Prompt',
'description':
'You are Glowby, an AI tutor who assists users with their learning needs. Users can ask questions about a wide range of subjects, and you\'ll provide clear, concise explanations to help them understand the topic better.',
},
{
'name': 'Career and Job Advice Prompt',
'description':
'You are Glowby, an AI career coach who offers guidance on job-related matters. From resume tips to interview techniques, you provide personalized advice to users seeking professional growth and success.',
},
{
'name': 'Daily Motivation Prompt',
'description':
'You are Glowby, an AI life coach who delivers daily doses of inspiration and motivation. Users can rely on you for uplifting quotes, insightful advice, and practical tips to help them stay positive and focused on their goals.',
},
{
'name': 'Interactive Adventure Prompt',
'description': storyPrompt,
},
{
'name': 'Habit Formation',
'description':
'Act as a dual PhD in sports psychology and neuroscience. Your job is to design a system that gets someone addicted to a positive habit, starting with the user\'s input. Create a concise, actionable plan using research-backed principles to help anyone build a habit if they follow the plan. Incorporate research such as BF Skinner\'s study of addiction, BJ Fogg\'s Behavioral Model, and similar research on addiction and compulsion. Be concise yet informative. Give a concise day-by-day plan for the first week. Your response should be fewer than 10 sentences.',
},
{
'name': 'Stand-up Comedy Prompt',
'description':
'You are Glowby, a hilarious AI stand-up comedian, skilled in creating funny conversations that become popular on social media platforms like Reels. Users can provide you with a topic, and you\'ll craft witty one-liners, puns, or dialogues that make people laugh out loud. Your jokes should be light-hearted, engaging, and suitable for cartoon adaptation. Let\'s get the laughs rolling!'
},
];

void selectPrompt(String userInput) {
for (var prompt in prompts) {
if (prompt['description'] == userInput) {
selectedPrompt = prompt['name']!;
break;
}
}
}

void loadDialogValues(selectedModelInput, selectedLanguageInput,
systemPromptInput, autonomousModeInput) {
selectedPrompt = 'Simple Assistant Prompt';
selectedModel = OpenAI_API.model;
systemPrompt = OpenAI_API.systemPrompt;
selectedLanguage = OpenAI_API.selectedLanguage;
autonomousMode = false;

if (selectedModelInput != null && selectedModelInput != '') {
selectedModel = selectedModelInput;
}

if (selectedLanguageInput != null && selectedLanguageInput != '') {
selectedLanguage = selectedLanguageInput;
}

if (systemPromptInput != null && systemPromptInput != '') {
systemPrompt = systemPromptInput;
selectPrompt(systemPromptInput);
}

if (autonomousModeInput != null) {
autonomousMode = autonomousModeInput;
}
}

// ... other methods if necessary ...
}

class AiSettingsDialog extends StatefulWidget {
final Function(bool) onVoiceEnabledChanged;

Expand Down
40 changes: 38 additions & 2 deletions app/lib/api_key_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,45 @@ class _ApiKeyDialogState extends State<ApiKeyDialog> {
});
}

bool isValidOpenAIKey(String key) =>
RegExp(r'^sk-[A-Za-z0-9-_]+$').hasMatch(key);

bool isValidHuggingFaceKey(String key) =>
RegExp(r'^[A-Za-z0-9-_]+$').hasMatch(key);

bool isValidPuzzleAIKey(String key) =>
RegExp(r'^sk-[A-Za-z0-9-_]+$').hasMatch(key);

void _saveApiKey(BuildContext context) {
if (!_apiKey.isEmpty && !isValidOpenAIKey(_apiKey)) {
Navigator.pop(context); // Hide the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('OpenAI API Key is invalid!')),
);
return;
}

OpenAI_API.setOat(_apiKey);

if (!_huggingFaceToken.isEmpty &&
!isValidHuggingFaceKey(_huggingFaceToken)) {
Navigator.pop(context); // Hide the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Hugging Face Token is invalid!')),
);
return;
}

HuggingFace_API.setOat(_huggingFaceToken);

if (!_pulzeAiToken.isEmpty && !isValidPuzzleAIKey(_pulzeAiToken)) {
Navigator.pop(context); // Hide the dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Pulze API Key is invalid!')),
);
return;
}

PulzeAI_API.setOat(_pulzeAiToken);
Navigator.pop(context); // Hide the dialog

Expand Down Expand Up @@ -116,8 +152,8 @@ class _ApiKeyDialogState extends State<ApiKeyDialog> {
'→ Pulze.ai Dashboard',
style: TextStyle(color: Colors.blue),
),
onTap: () => Utils.launchURL(
'https://platform.pulze.ai/'),
onTap: () =>
Utils.launchURL('https://platform.pulze.ai/'),
),
SizedBox(height: 10),
Text('Enter your Pulze.ai Token:'),
Expand Down
4 changes: 3 additions & 1 deletion app/lib/chat_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:glowby/global_settings.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:glowby/tasks_view.dart';

Expand Down Expand Up @@ -298,7 +299,8 @@ class _ChatScreenState extends State<ChatScreen> {
String? customSystemPrompt,
bool lastMessage = false,
}) async {
insertMessage(message, 'Me', 'Me'); // Insert user's message
insertMessage(message, GlobalSettings().userId,
GlobalSettings().userName); // Insert user's message
refresh(); // Refresh UI

String response = await fetchResponseFromAPI(message, customSystemPrompt);
Expand Down
Loading

0 comments on commit c3cdad0

Please sign in to comment.