Skip to content

Commit

Permalink
Merge pull request #203 from Saumya-28/main
Browse files Browse the repository at this point in the history
Implemented chatbot feature for open source queries using Gemini
  • Loading branch information
andoriyaprashant authored Jun 23, 2024
2 parents 11a929c + a7a35d5 commit fa4f459
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 19 deletions.
150 changes: 150 additions & 0 deletions lib/ChatBotPage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
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}"});
});


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!});
});
}
}


@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ChatBot'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _messages.length,
itemBuilder: (context, index) {
final message = _messages[index];
final isUser = message["sender"] == "user";
return Align(
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
child: Container(
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context).size.width * 0.75,
),
margin: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: isUser ? Colors.blue[100] : Colors.grey[300],
borderRadius: BorderRadius.circular(10.0),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (!isUser) const CircleAvatar(child: Icon(Icons.android)),
if (!isUser) const SizedBox(width: 5.0),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(message["text"] ?? ''),
],
),
),
if (isUser) const SizedBox(width: 5.0),
if (isUser) const CircleAvatar(child: Icon(Icons.person)),
],
),
),
);
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _controller,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter your message',
),
),
),
IconButton(
icon: const Icon(Icons.send),
onPressed: _sendMessage,
),
],
),
),
],
),
);
}


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


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

Loading

0 comments on commit fa4f459

Please sign in to comment.