Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed the unnecessary asterisk #283

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions lib/ChatBotPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,53 @@ import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/material.dart';
import 'package:google_generative_ai/google_generative_ai.dart';


const apiKey = 'put-gemini-api-key';


class ChatBotPage extends StatefulWidget {
const ChatBotPage({Key? key}) : super(key: key);


@override
_ChatBotPageState createState() => _ChatBotPageState();
}


class _ChatBotPageState extends State<ChatBotPage> {
final TextEditingController _controller = TextEditingController();
final List<Map<String, String>> _messages = [];
GenerativeModel? _model;


@override
void initState() {
super.initState();
_initializeModel();
}


void _initializeModel() async {
_model = GenerativeModel(
model: 'gemini-1.5-flash-latest',
apiKey: apiKey,
);
}


void _sendMessage() async {
if (_controller.text.isEmpty) return;




setState(() {
_messages.add({"sender": "user", "text": "${_controller.text}"});
_messages.add({"sender": "user", "text": _controller.text});
});


final prompt = _controller.text + "don't give answer in markdown format";
final prompt = _controller.text + " don't give answer in markdown format";
_controller.clear();


if (_model != null) {
final content = [Content.text(prompt)];
final response = await _model!.generateContent(content);
setState(() {
_messages.add({"sender": "bot", "text": response.text!});
final botText = response.text!.replaceAll('*', ''); // Remove asterisks
_messages.add({"sender": "bot", "text": botText});
});
}
}


@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -84,7 +72,7 @@ class _ChatBotPageState extends State<ChatBotPage> {
margin: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color:isUser
color: isUser
? (AdaptiveTheme.of(context).mode.isDark ? Color.fromARGB(255, 31, 49, 70) : Colors.blue[100])
: (AdaptiveTheme.of(context).mode.isDark ? Colors.grey[700] : Colors.grey[300]),
borderRadius: BorderRadius.circular(10.0),
Expand Down Expand Up @@ -136,18 +124,15 @@ class _ChatBotPageState extends State<ChatBotPage> {
);
}


@override
void dispose() {
_controller.dispose();
super.dispose();
}
}


void main() {
runApp(const MaterialApp(
home: ChatBotPage(),
));
}

}
Loading