π§ PocketBrain
PocketBrain is a lightweight, 100% offline RAG (Retrieval-Augmented Generation) library for Flutter.
It allows you to embed a "brain" into your mobile application that can answer questions based on your own data (Knowledge Base) without needing an internet connection or expensive API keys (like OpenAI/GPT).
Perfect for: Offline FAQs, Smart User Manuals, Corporate Knowledge Bases, and secure on-device assistants.
β¨ Key Features
- π« 100% Offline: No internet connection required. No data leaves the device.
- πΈ Zero Cost: No recurring API fees. Runs entirely on the device's CPU.
- π High Performance: Uses ObjectBox for ultra-fast storage and TFLite for efficient on-device inference.
- π§ Smart Semantic Search: Understands meaning, not just keywords.
- Query: "Where can I get the app?"
- Matches: "You can download it from the Play Store."
- π Easy Training: "Teach" the AI simply by importing a standard
.csvfile. - π¦ Self-Contained: Comes with a pre-optimized Quantized BERT model (
all-MiniLM-L6-v2) built-in.
π Installation
Add pocket_brain to your pubspec.yaml:
dependencies:
pocket_brain: ^1.0.0
Run the command:
flutter pub get
π How to Use
- Initialize the Brain Before using the library, you must initialize it. This loads the AI model and the database into memory. You should do this once, typically when your app starts.
import 'package:pocket_brain/pocket_brain.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Create the instance
final brain = PocketBrain();
// Initialize (Loads TFLite model & ObjectBox)
await brain.init();
runApp(MyApp());
}
- Teach the AI (Import Data) PocketBrain learns from CSV files. When you import a CSV, the library automatically:
- Wipes the old memory (to prevent duplicates).
- Embeds the new questions into vectors.
- Stores everything in the offline database.
Future<void> trainBrain() async {
final brain = PocketBrain();
// Get a File reference (e.g., from FilePicker)
File csvFile = File('/path/to/your_knowledge_base.csv');
// This returns the number of items successfully learned
int count = await brain.importFromCsv(csvFile);
print("Brain updated with $count new facts!");
}
- Ask Questions (Inference) Once the brain is trained, you can ask it anything. It performs a Semantic Search, meaning it looks for the meaning of the question, not just matching keywords.
void askSomething() {
final brain = PocketBrain();
// Example Query
String query = "Where can I download the app?";
// Ask the brain
String? answer = brain.ask(query);
if (answer != null) {
print("AI Answer: $answer");
} else {
print("I don't know the answer to that.");
}
}
π CSV Data Format
To train the brain, your CSV file must follow this simple format: Column 1: The Question Column 2: The Answer The first row is ignored (treated as a header).
βοΈ Under The Hood
PocketBrain combines several powerful technologies to make offline RAG possible:
- Tokenizer: Converts text into a sequence of IDs using a BertTokenizer.
- Embedding Model: Uses a quantized all-MiniLM-L6-v2 TFLite model to convert text into a 384-dimensional vector.
- Vector Store: Stores these vectors in ObjectBox, a super-fast NoSQL edge database.
- Linear Scan (Cosine Similarity): When a user asks a question, the library compares the query vector against all stored vectors to find the closest semantic match.