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.
flutter_rag_chatbot #
A Flutter package designed to help developers easily build and integrate RAG-based chatbots into mobile apps. This package leverages Retrieval-Augmented Generation to retrieve relevant documents from a local vector store and then augment queries to a Language Model (LLM) like xAI's Grok API.
Features #
- Retrieval-Augmented Generation (RAG): Combines information retrieval with text generation.
- Local Vector Store: Basic in-memory vector store for document retrieval using cosine similarity.
- LLM Integration: Designed to integrate with LLMs (e.g., xAI Grok API).
- Chat UI Widget: A pre-built Flutter widget for displaying chat conversations.
- Offline Fallback: (Planned) Cache responses for offline access.
- Multi-turn History: Maintains conversation context.
- Markdown Parsing: Displays LLM responses formatted with Markdown.
- Extensibility: Hooks for custom retrievers and LLMs.
- Future-Proofing: Includes placeholders for image inputs, query logging, and hybrid edge/cloud modes.
- Multi-Content Support: Support for URLs, PDFs, images, and strings for embedding and retrieval.
Installation #
Add the following to your pubspec.yaml
file:
dependencies:
flutter_rag_chatbot:
git:
url: https://github.com/your-username/flutter_rag_chatbot.git # Replace with your repository URL
ref: main # Or the specific branch/tag
# Or, if published to pub.flutter-io.cn:
# flutter_rag_chatbot: ^0.1.0
Then run flutter pub get
.
Usage #
1. Initialize the RagChatbot #
First, initialize the RagChatbot
with your API key and a knowledge base (list of documents).
import 'package:flutter/material.dart';
import 'package:flutter_rag_chatbot/flutter_rag_chatbot.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late final RagChatbot _chatbot;
final List<String> _knowledgeBase = [
"The capital of France is Paris.",
"Mount Everest is the highest mountain in the world.",
"The Earth revolves around the Sun.",
"Flutter is a UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase.",
];
@override
void initState() {
super.initState();
_chatbot = RagChatbot();
_initializeChatbot();
}
Future<void> _initializeChatbot() async {
await _chatbot.initialize(
apiKey: 'YOUR_AI_API_KEY', // Replace with your actual API key
aiProvider: AIProvider.grok, // Choose from: grok, chatgpt, claude, gemini
knowledgeBase: _knowledgeBase,
urls: ['https://example.com'], // Optional: URLs to fetch and include
pdfPaths: ['/path/to/document.pdf'], // Optional: PDF files to include
imagePaths: ['/path/to/image.jpg'], // Optional: Images to include
);
setState(() {}); // Rebuild to show the chat widget after initialization
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'RAG Chatbot Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('RAG Chatbot Demo'),
),
body: _chatbot == null
? const Center(child: CircularProgressIndicator())
: RagChatWidget(chatbot: _chatbot),
),
);
}
}
2. Use the RagChatWidget #
Integrate the RagChatWidget
into your Flutter application:
// See the example above for full integration.
// In your widget tree:
RagChatWidget(chatbot: _chatbot)
AI Provider Selection #
The package now supports multiple AI providers for text generation:
Supported AI Providers: #
- Grok (xAI):
AIProvider.grok
- ChatGPT (OpenAI):
AIProvider.chatgpt
- Claude (Anthropic):
AIProvider.claude
- Gemini (Google):
AIProvider.gemini
Usage Example: #
await chatbot.initialize(
apiKey: 'your-api-key',
aiProvider: AIProvider.chatgpt, // Choose your preferred AI provider
knowledgeBase: ['Your knowledge documents'],
// ... other content types
);
API Key Requirements: #
- Grok: xAI API key
- ChatGPT: OpenAI API key
- Claude: Anthropic API key
- Gemini: Google API key (may require different authentication)
Multi-Content Type Support #
The package now supports multiple content types for embedding and retrieval:
Supported Content Types: #
- Strings: Direct text documents
- URLs: Web pages (fetches and extracts text content)
- PDFs: PDF documents (placeholder for text extraction)
- Images: Image files (placeholder for image analysis/description)
Usage Example: #
await chatbot.initialize(
apiKey: 'your-api-key',
knowledgeBase: [
'String document 1',
'String document 2',
],
urls: [
'https://example.com',
'https://flutter.cn',
],
pdfPaths: [
'/path/to/document.pdf',
'/path/to/another.pdf',
],
imagePaths: [
'/path/to/image.jpg',
'/path/to/photo.png',
],
);
Implementation Notes: #
- URLs: Automatically fetches web content and extracts text from HTML
- PDFs: Currently placeholder - requires PDF parsing library integration
- Images: Currently placeholder - requires image recognition integration
- Resource Management: Remember to call
chatbot.dispose()
when done to release resources
Contributing #
Contributions are welcome! Please feel free to open an issue or submit a pull request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.