本文档提供了对 Google AI Edge Gallery 代码库的全面介绍,这是一个实验性的 Android 应用程序,可实现设备端生成式 AI 体验。概览涵盖了系统的架构、核心组件和关键工作流,以建立探索特定子系统的基础知识。
有关模型配置和注册管理的详细信息,请参见模型注册系统。有关 Android 特定的构建配置和依赖项,请参见 Android 应用程序架构。有关用户界面组件和聊天功能,请参见聊天界面系统。
系统目的与架构
Google AI Edge 画廊是一款 Android 应用程序,使用 MediaPipe 和 TensorFlow Lite 在设备端运行大型语言模型。该系统使用户能够在下载模型后,通过聊天界面、图像分析和提示实验与 AI 模型进行交互,无需网络连接。
高层系统架构
vbnet
External_Services
User_Interaction_Features
AI_Processing_Pipeline
Android_Application
Model_Registry
model_allowlist.json
Central Model Registry
Model Definitions
• modelId & modelFile
• defaultConfig
• taskTypes
MainActivity
Main UI Host
GalleryApplication
App Entry Point
ChatView
Primary Chat Interface
ModelManagerViewModel
Model State Management
MediaPipe LLM API
Inference Engine
TensorFlow Lite
Runtime Execution
.task Files
Quantized Models
llm_chat
Multi-turn Conversations
llm_ask_image
Vision + Text Queries
llm_prompt_lab
Single-turn Experiments
Hugging Face
Model Discovery
OAuth Authentication
User Accounts
来源:model_allowlist.json1-70README.md1-62
核心组件概述
该系统由五个主要子系统组成,这些子系统协同工作以提供设备端的 AI 能力:
组件 | 主要文件 | 目的 |
---|---|---|
模型注册表 | model_allowlist.json |
定义可用的模型、配置和功能 |
Android 应用程序 | 构建文件,Manifest | 处理应用生命周期、依赖关系和权限 |
聊天界面 | ChatView, MessageInput 组件 | 提供用户交互和模型切换功能 |
AI 处理 | MediaPipe,TensorFlow Lite 集成 | 在设备上执行模型推理 |
外部集成 | Hugging Face, OAuth 服务 | 模型发现与认证 |
模型注册系统
model_allowlist.json
文件作为中央注册表,定义了所有可用的 AI 模型及其部署配置:
css
model_allowlist.json
models[]
Model Configuration
Basic Information
• name
• modelId
• modelFile
• description
Runtime Configuration
• sizeInBytes
• version
• llmSupportImage
Inference Parameters
• topK, topP
• temperature
• maxTokens
• accelerators
Supported Tasks
• llm_chat
• llm_ask_image
• llm_prompt_lab
来源:[model_allowlist.json1-70]
目前注册表定义了四种生产模型,包括带有视觉支持的 Gemma 3n 变体和针对不同应用场景优化的 Qwen2.5 模型。每个模型条目指定了其支持的任务类型,使应用程序能够将用户交互路由到相应的模型能力。
应用流程和模型生命周期
vbnet
GalleryApplication
Application Start
Load model_allowlist.json
Parse Model Definitions
MainActivity
Launch UI
ChatView
Primary Interface
Model Selection
HorizontalPager
ModelManagerViewModel
Initialize Selected Model
Load .task File
via MediaPipe LLM API
TensorFlow Lite
Runtime Initialization
Model Ready
for Inference
Route by Task Type
llm_chat
Multi-turn Conversation
llm_ask_image
Vision Analysis
llm_prompt_lab
Single-turn Prompts
Model Inference
Display Response
in ChatPanel
Await Next Input
MessageInputText
Cleanup Previous Model
来源:model_allowlist.json1-70README.md22-31
关键技术集成
该应用集成了多项关键技术,以实现设备端 AI:
MediaPipe 和 TensorFlow Lite 集成
- MediaPipe LLM 推理 API: 提供高级接口用于模型加载和文本生成
- TensorFlow Lite 运行时 : 负责低级模型执行,并支持 GPU 加速
- 量化模型支持 : 处理 4 位和 8 位量化模型以实现最佳设备性能
Android 平台特性
- Jetpack Compose: 全界面组件的现代声明式 UI 框架
- CameraX 集成 : 使基于视觉的 AI 交互能够进行图像捕捉
- Work Manager: 负责后台模型下载和同步
- 数据存储偏好设置:维护用户设置和模型配置
外部服务集成
- Hugging Face 发现: enables 模型浏览和从 LiteRT 社区下载模型
- OAuth 认证:提供安全的用户账户管理以增强功能
系统架构优先考虑离线功能,同时保持与网络的连接以进行模型发现和更新。所有 AI 处理都在可用时使用设备硬件加速在本地进行,从而确保隐私并减少对网络连接的依赖。