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

Implemented chatbot feature for open source queries using Gemini #203

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
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
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
Loading