ai_core_codespark 0.2.0
ai_core_codespark: ^0.2.0 copied to clipboard
Local AI model for Flutter — on-device text embeddings & vector search, fully offline, no API keys or cloud. The engine behind semantic search & RAG.
// A ~30-line preview of what `semantic_search_codespark` becomes once it sits
// on top of this core. This is the DX the whole ecosystem is selling.
import 'package:ai_core_codespark/ai_core_codespark.dart';
Future<void> main() async {
// 1. One engine, downloads + verifies the model on first run.
final engine = CodesparkEngine(model: ModelCatalog.miniLmL6V2);
await engine.initialize(
onProgress: (received, total) {
final pct = total > 0 ? (received / total * 100).toStringAsFixed(0) : '?';
// ignore: avoid_print
print('Downloading model… $pct%');
},
);
// 2. Embed a corpus once and stash it in the vector store.
const items = ['automobile', 'banana', 'vehicle', 'fruit smoothie'];
final store = VectorStore();
final vectors = await engine.embedBatch(items);
for (var i = 0; i < items.length; i++) {
store.add(VectorRecord(id: items[i], vector: vectors[i]));
}
// 3. Search by meaning, not spelling.
final query = await engine.embed('car');
for (final hit in store.search(query, k: 2, threshold: 0.3)) {
// ignore: avoid_print
print('${hit.record.id} (${hit.score.toStringAsFixed(3)})');
}
// Expected: automobile (~0.6), vehicle (~0.6) — banana/smoothie fall below.
await engine.dispose();
}