flutter_rag_chatbot 0.1.2
flutter_rag_chatbot: ^0.1.2 copied to clipboard
A Flutter package for building RAG-based chatbots with local vector storage and xAI Grok API integration.
example/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_rag_chatbot/flutter_rag_chatbot.dart';
import 'package:flutter_rag_chatbot/src/widgets/rag_chat_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'RAG Chatbot Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const ChatScreen(),
);
}
}
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
RagChatbot? _chatbot;
bool _isInitializing = true;
String? _error;
@override
void initState() {
super.initState();
_initializeChatbot();
}
Future<void> _initializeChatbot() async {
try {
final chatbot = RagChatbot();
await chatbot.initialize(
apiKey: 'your-api-key-here', // Replace with your actual API key
aiProvider:
AIProvider.chatgpt, // Choose from: grok, chatgpt, claude, gemini
knowledgeBase: [
'This is a string document that will be embedded.',
'Another text document for the knowledge base.',
],
urls: [
'https://example.com', // Will fetch and extract text from this URL
],
);
setState(() {
_chatbot = chatbot;
_isInitializing = false;
});
} catch (e) {
setState(() {
_error = e.toString();
_isInitializing = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('RAG Chatbot Demo'),
),
body: _isInitializing
? const Center(child: CircularProgressIndicator())
: _error != null
? Center(child: Text('Error: $_error'))
: _chatbot == null
? const Center(child: Text('Chatbot not initialized'))
: RagChatWidget(chatbot: _chatbot!),
);
}
@override
void dispose() {
_chatbot?.dispose();
super.dispose();
}
}