QOL: Mzansi AI Chat Look and Feel pt1
This commit is contained in:
@@ -1,12 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart';
|
||||
import 'package:mzansi_innovation_hub/main.dart';
|
||||
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/mih_icons.dart';
|
||||
import 'package:mzansi_innovation_hub/mih_components/mih_providers/ollama_provider.dart';
|
||||
import 'package:mzansi_innovation_hub/mih_config/mih_colors.dart';
|
||||
import 'package:mzansi_innovation_hub/mih_config/mih_env.dart';
|
||||
|
||||
class MzansiAiProvider extends ChangeNotifier {
|
||||
int toolIndex;
|
||||
String? startUpQuestion;
|
||||
late OllamaProvider ollamaProvider;
|
||||
|
||||
MzansiAiProvider({
|
||||
this.toolIndex = 0,
|
||||
});
|
||||
}) {
|
||||
ollamaProvider = OllamaProvider(
|
||||
baseUrl: "${AppEnviroment.baseAiUrl}/api",
|
||||
model: AppEnviroment.getEnv() == "Prod" ? 'gemma3n:e4b' : "gemma3:1b",
|
||||
systemPrompt: "You are Mzansi AI, a helpful and friendly AI assistant running on the 'MIH App'.\n" +
|
||||
"The MIH App was created by 'Mzansi Innovation Hub', a South African-based startup company." +
|
||||
"Your primary purpose is to assist users by answering general questions and helping with creative writing tasks or any other task a user might have for you.\n" +
|
||||
"Maintain a casual and friendly tone, but always remain professional.\n" +
|
||||
"Strive for a balance between being empathetic and delivering factual information accurately.\n" +
|
||||
"You may use lighthearted or playful language if the context is appropriate and enhances the user experience.\n" +
|
||||
"You operate within the knowledge domain of the 'MIH App'.\n" +
|
||||
"Here is a description of the MIH App and its features:\n" +
|
||||
"MIH App Description: MIH is the first super app of Mzansi, designed to streamline both personal and business life. It's an all-in-one platform for managing professional profiles, teams, appointments, and quick calculations. \n" +
|
||||
"Key Features:\n" +
|
||||
"- Mzansi Profile: Central hub for managing personal and business information, including business team details." +
|
||||
"- Mzansi Wallet: Digitally store loyalty cards.\n" +
|
||||
"- Patient Manager (For Medical Practices): Seamless patient appointment scheduling and data management.\n" +
|
||||
"- Mzansi AI: Your friendly AI assistant for quick answers and support (that's you!).\n" +
|
||||
"- Mzansi Directory: A place to search and find out more about the people and businesses across Mzansi.\n" +
|
||||
"- Calendar: Integrated calendar for managing personal and business appointments.\n" +
|
||||
"- Calculator: Simple calculator with tip and forex calculation functionality.\n" +
|
||||
"- MIH Access: Manage and view profile access security.\n" +
|
||||
"**Core Rules and Guidelines:**\n" +
|
||||
"- **Accuracy First:** Always prioritize providing correct information.\n" +
|
||||
"- **Uncertainty Handling:** If you are unsure about an answer, politely respond with: 'Please bear with us as we are still learning and do not have all the answers.'\n" +
|
||||
"- **Response Length:** Aim to keep responses under 250 words. If a more comprehensive answer is required, exceed this limit but offer to elaborate further (e.g., 'Would you like me to elaborate on this topic?').\n" +
|
||||
"- **Language & Safety:** Never use offensive language or generate harmful content. If a user presses for information that is inappropriate or out of bounds, clearly state why you cannot provide it (e.g., 'I cannot assist with that request as it goes against my safety guidelines.').\n" +
|
||||
"- **Out-of-Scope Questions:** - If a question is unclear, ask the user to rephrase or clarify it. - If a question is entirely out of your scope and you cannot provide a useful answer, admit you don't know. - If a user is unhappy with your response or needs further assistance beyond your capabilities, suggest they visit the 'Mzansi Innovation Hub Social Media Pages' for more direct support. Do not provide specific links, just refer to the pages generally.\n" +
|
||||
"- **Target Audience:** Adapt your explanations to beginners and intermediate users, but be prepared for more complex questions from expert users. Ensure your language is clear and easy to understand.\n",
|
||||
)..addListener(() {
|
||||
notifyListeners(); // Forward OllamaProvider notifications
|
||||
});
|
||||
}
|
||||
|
||||
void reset() {
|
||||
toolIndex = 0;
|
||||
@@ -23,4 +62,186 @@ class MzansiAiProvider extends ChangeNotifier {
|
||||
startUpQuestion = question;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
LlmChatViewStyle? getChatStyle(BuildContext context) {
|
||||
return LlmChatViewStyle(
|
||||
backgroundColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
progressIndicatorColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
menuColor: Colors.black,
|
||||
// MihColors.getGreenColor(
|
||||
// MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
disabledButtonStyle: ActionButtonStyle(
|
||||
icon: MihIcons.mzansiAi,
|
||||
iconColor: MihColors.getSecondaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
),
|
||||
recordButtonStyle: ActionButtonStyle(
|
||||
iconColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getGreenColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
),
|
||||
submitButtonStyle: ActionButtonStyle(
|
||||
icon: Icons.send,
|
||||
iconColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getGreenColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
),
|
||||
stopButtonStyle: ActionButtonStyle(
|
||||
iconColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getRedColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
),
|
||||
actionButtonBarDecoration: BoxDecoration(
|
||||
color: MihColors.getSecondaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
// Mzansi AI Chat Style
|
||||
llmMessageStyle: LlmMessageStyle(
|
||||
icon: MihIcons.mzansiAi,
|
||||
iconColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getSecondaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.only(
|
||||
topRight: Radius.circular(25),
|
||||
bottomLeft: Radius.circular(25),
|
||||
bottomRight: Radius.circular(25),
|
||||
),
|
||||
color: MihColors.getSecondaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(76),
|
||||
blurRadius: 8,
|
||||
offset: Offset(2, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// User Chat Style
|
||||
userMessageStyle: UserMessageStyle(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(25),
|
||||
bottomLeft: Radius.circular(25),
|
||||
bottomRight: Radius.circular(25),
|
||||
),
|
||||
color: MihColors.getGreenColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(76),
|
||||
blurRadius: 8,
|
||||
offset: Offset(2, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
),
|
||||
// User Input Style
|
||||
chatInputStyle: ChatInputStyle(
|
||||
backgroundColor: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
decoration: BoxDecoration(
|
||||
color: MihColors.getSecondaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(76),
|
||||
blurRadius: 8,
|
||||
offset: Offset(2, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
hintStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
hintText: "Ask Mzansi AI...",
|
||||
),
|
||||
// Suggestions Style
|
||||
suggestionStyle: SuggestionStyle(
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(25),
|
||||
bottomLeft: Radius.circular(25),
|
||||
bottomRight: Radius.circular(25),
|
||||
),
|
||||
color: MihColors.getGreenColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(76),
|
||||
blurRadius: 8,
|
||||
offset: Offset(2, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
),
|
||||
copyButtonStyle: ActionButtonStyle(
|
||||
iconColor: MihColors.getSecondaryInvertedColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
editButtonStyle: ActionButtonStyle(
|
||||
iconColor: MihColors.getSecondaryInvertedColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
),
|
||||
cancelButtonStyle: ActionButtonStyle(
|
||||
iconDecoration: BoxDecoration(
|
||||
color: MihColors.getRedColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
borderRadius: BorderRadius.circular(25),
|
||||
),
|
||||
iconColor: MihColors.getSecondaryInvertedColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
textStyle: TextStyle(
|
||||
color: MihColors.getPrimaryColor(
|
||||
MzansiInnovationHub.of(context)!.theme.mode == "Dark"),
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
136
Frontend/lib/mih_components/mih_providers/ollama_provider.dart
Normal file
136
Frontend/lib/mih_components/mih_providers/ollama_provider.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_ai_toolkit/flutter_ai_toolkit.dart';
|
||||
import 'package:ollama_dart/ollama_dart.dart';
|
||||
|
||||
class OllamaProvider extends LlmProvider with ChangeNotifier {
|
||||
OllamaProvider({
|
||||
String? baseUrl,
|
||||
Map<String, String>? headers,
|
||||
Map<String, String>? queryParams,
|
||||
required String model,
|
||||
String? systemPrompt,
|
||||
}) : _client = OllamaClient(
|
||||
baseUrl: baseUrl,
|
||||
headers: headers,
|
||||
queryParams: queryParams,
|
||||
),
|
||||
_model = model,
|
||||
_systemPrompt = systemPrompt,
|
||||
_history = [];
|
||||
final OllamaClient _client;
|
||||
final String _model;
|
||||
final List<ChatMessage> _history;
|
||||
final String? _systemPrompt;
|
||||
|
||||
@override
|
||||
Stream<String> generateStream(
|
||||
String prompt, {
|
||||
Iterable<Attachment> attachments = const [],
|
||||
}) async* {
|
||||
final messages = _mapToOllamaMessages([
|
||||
ChatMessage.user(prompt, attachments),
|
||||
]);
|
||||
yield* _generateStream(messages);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<String> sendMessageStream(
|
||||
String prompt, {
|
||||
Iterable<Attachment> attachments = const [],
|
||||
}) async* {
|
||||
final userMessage = ChatMessage.user(prompt, attachments);
|
||||
final llmMessage = ChatMessage.llm();
|
||||
_history.addAll([userMessage, llmMessage]);
|
||||
notifyListeners();
|
||||
final messages = _mapToOllamaMessages(_history);
|
||||
final stream = _generateStream(messages);
|
||||
yield* stream.map((chunk) {
|
||||
llmMessage.append(chunk);
|
||||
return chunk;
|
||||
});
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
Iterable<ChatMessage> get history => _history;
|
||||
|
||||
void resetChat() {
|
||||
_history.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
set history(Iterable<ChatMessage> history) {
|
||||
_history.clear();
|
||||
_history.addAll(history);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Stream<String> _generateStream(List<Message> messages) async* {
|
||||
final allMessages = <Message>[];
|
||||
if (_systemPrompt != null && _systemPrompt.isNotEmpty) {
|
||||
allMessages.add(Message(
|
||||
role: MessageRole.system,
|
||||
content: _systemPrompt,
|
||||
));
|
||||
}
|
||||
allMessages.addAll(messages);
|
||||
|
||||
final stream = _client.generateChatCompletionStream(
|
||||
request: GenerateChatCompletionRequest(
|
||||
model: _model,
|
||||
messages: allMessages,
|
||||
),
|
||||
);
|
||||
// final stream = _client.generateChatCompletionStream(
|
||||
// request: GenerateChatCompletionRequest(
|
||||
// model: _model,
|
||||
// messages: messages,
|
||||
// ),
|
||||
// );
|
||||
|
||||
yield* stream.map((res) => res.message.content);
|
||||
}
|
||||
|
||||
List<Message> _mapToOllamaMessages(List<ChatMessage> messages) {
|
||||
return messages.map((message) {
|
||||
switch (message.origin) {
|
||||
case MessageOrigin.user:
|
||||
if (message.attachments.isEmpty) {
|
||||
return Message(
|
||||
role: MessageRole.user,
|
||||
content: message.text ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
return Message(
|
||||
role: MessageRole.user,
|
||||
content: message.text ?? '',
|
||||
images: [
|
||||
for (final attachment in message.attachments)
|
||||
if (attachment is ImageFileAttachment)
|
||||
base64Encode(attachment.bytes)
|
||||
else
|
||||
throw LlmFailureException(
|
||||
'Unsupported attachment type: $attachment',
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
case MessageOrigin.llm:
|
||||
return Message(
|
||||
role: MessageRole.assistant,
|
||||
content: message.text ?? '',
|
||||
);
|
||||
}
|
||||
}).toList(growable: false);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_client.endSession();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user