Skip to content

feat: implement ONNX Runtime + ObjectBox local AI stack (方案 C)#41

Draft
Copilot wants to merge 15 commits intomainfrom
copilot/implement-onnx-runtime-objectbox
Draft

feat: implement ONNX Runtime + ObjectBox local AI stack (方案 C)#41
Copilot wants to merge 15 commits intomainfrom
copilot/implement-onnx-runtime-objectbox

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 28, 2025

Implements on-device AI capabilities using ONNX Runtime for inference and ObjectBox for vector search, enabling offline semantic search, embedding generation, OCR, and ASR.

Dependencies

  • onnxruntime: ^1.16.0 - ONNX inference (MIT)
  • objectbox: ^4.0.0 + objectbox_flutter_libs + objectbox_generator - Vector DB (Apache-2.0)
  • sherpa_onnx: ^1.11.3 - ASR (Apache-2.0)
  • google_mlkit_text_recognition: ^0.13.0 - OCR (MIT)
  • record: ^5.1.0, camera: ^0.11.0 - Input capture

New Services (lib/services/local_ai/)

  • ONNXInferenceService - Model loading, caching, inference execution
  • TokenizerService - WordPiece tokenization with CJK support
  • EmbeddingService - Text→vector using paraphrase-multilingual-MiniLM-L12-v2 (384d)
  • VectorSearchService - HNSW-indexed search via ObjectBox, similarity caching
  • ASRService - Whisper/Paraformer speech recognition (placeholder, needs model files)
  • OCRService - ML Kit text recognition with multi-script support

ObjectBox Entities (lib/models/objectbox/)

  • NoteVector - Note embeddings with HNSW index
  • SearchHistory - Query tracking
  • SimilarNotesCache - Precomputed similarity relationships

Usage

// Generate embedding
final embeddingService = EmbeddingService();
await embeddingService.initialize();
final result = await embeddingService.embed("Hello world");

// Vector search
final vectorSearch = VectorSearchService();
await vectorSearch.initialize();
final results = await vectorSearch.search("find notes about AI", topK: 10);

Setup Required

  1. dart run build_runner build - Generate ObjectBox code
  2. Download model files to assets/models/
Original prompt

方案 C:ONNX Runtime + ObjectBox

技术栈

  • 嵌入: onnxruntime (MIT) - paraphrase-multilingual-MiniLM-L12-v2 (多语言)
  • ASR: sherpa_onnx (Apache-2.0) - Whisper/Paraformer
  • OCR: google_mlkit_text_recognition (MIT)
  • 向量存储: ObjectBox Vector Search (Apache-2.0)

核心实现

1. 多语言嵌入模型

推荐: paraphrase-multilingual-MiniLM-L12-v2

备选: text2vec-base-multilingual (中文优化, 110MB, 768维)

2. ONNX 推理引擎

class ONNXInferenceService {
  Future<void> loadModel(String modelId, String path);
  Future<Map<String, OrtValue>> runInference(String modelId, inputs);
}

3. Tokenizer

class TokenizerService {
  Future<void> loadVocab(String vocabPath);
  TokenizedInput encode(String text);
}

4. ObjectBox 向量搜索

@Entity()
class NoteVector {
  @HnswIndex(dimensions: 384)
  List<double> embedding;
}

// 搜索
vectorBox.query()
  .nearestNeighborsF32('embedding', queryVector, topK)
  .find();

5. ASR/OCR

  • ASR: sherpa_onnx (Whisper tiny 39MB 或 Paraformer)
  • OCR: ML Kit 自动下载中文模型

依赖配置

dependencies:
  onnxruntime: ^1.16.0
  objectbox: ^4.0.0
  sherpa_onnx: ^1.11.3
  google_mlkit_text_recognition: ^0.13.0
  record: ^5.1.0
  camera: ^0.11.0

生成 ObjectBox 代码:

dart run build_runner build

优势

  • 模型灵活可替换(HuggingFace 生态丰富)
  • ObjectBox 向量搜索性能优于暴力搜索(适合大规模笔记)
  • 全部开源许可,商业安全
  • 多语言嵌入模型统一处理中英文

注意事项

  • 需要自行实现 Tokenizer(提供 WordPiece 示例代码)
  • 无 LLM,不支持 AI 纠错和来源识别
  • 适合对模型选择有特定需求的团队

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel
Copy link
Copy Markdown

vercel bot commented Dec 28, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
thought-echo Ready Ready Preview, Comment Jan 3, 2026 1:39am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 28, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

- Add ONNX, ObjectBox, sherpa_onnx, ML Kit dependencies
- Create ONNXInferenceService for model loading and inference
- Create TokenizerService with WordPiece tokenization
- Create EmbeddingService for text embedding generation
- Create VectorSearchService for ObjectBox vector search
- Create ASRService for speech-to-text using sherpa_onnx
- Create OCRService for text recognition using Google ML Kit
- Add NoteVector ObjectBox entity with HNSW index

Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
- Add null safety checks for note.id in indexNote method
- Add better documentation for ObjectBox initialization steps
- Improve embedding extraction with type safety validation
- Add batch processing optimization for search method
- Replace Map return type with strongly-typed QuoteInfo class
- Add clear documentation for ASR placeholder implementation
- Make embedding dimension configurable with documentation

Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement ONNX Runtime and ObjectBox integration feat: implement ONNX Runtime + ObjectBox local AI stack (方案 C) Dec 28, 2025
Copilot AI requested a review from Shangjin-Xiao December 28, 2025 03:13
Copilot AI and others added 6 commits December 28, 2025 08:19
…rors

Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
Co-authored-by: Shangjin-Xiao <84136399+Shangjin-Xiao@users.noreply.github.com>
…endency-issue

Fix onnxruntime dependency version, resolve analyzer errors, and enable local AI features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants