pocket_brain 1.0.1 copy "pocket_brain: ^1.0.1" to clipboard
pocket_brain: ^1.0.1 copied to clipboard

An Offline RAG (Retrieval-Augmented Generation) AI Library for Flutter. Embeds knowledge base from CSV and performs semantic search on-device.

🧠 PocketBrain #

Pub Version Platform License

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 .csv file.
  • πŸ“¦ 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 #

  1. 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());
}
  1. 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!");
}
  1. 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).

Screenshot 2026-01-29 at 21 41 54

βš™οΈ 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.
5
likes
150
points
9
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

An Offline RAG (Retrieval-Augmented Generation) AI Library for Flutter. Embeds knowledge base from CSV and performs semantic search on-device.

Homepage

License

MIT (license)

Dependencies

csv, flat_buffers, flutter, objectbox, objectbox_flutter_libs, path, path_provider, tflite_flutter

More

Packages that depend on pocket_brain