Files
2026-07-13 13:33:03 +08:00

218 lines
6.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MNNLLM iOS Application
[View English Documentation](./README.md)
## 介绍
本项目是一个基于MNN引擎,支持本地大模型多模态对话的iOS应用。
纯本地运行,隐私性强。当模型下载到本地之后,所有的对话都将在本地进行,不会有任何网络上传处理。
## 功能:
1. 本地模型
- 本地已下载模式展示
- 支持自定义置顶
2. 模型市场
- 获取 MNN 支持的模型列表
- 模型管理,支持下载和删除模型
- 支持切换 Hugging Face、 ModelScope 和 Modeler 下载源
- 模型搜索,支持关键词搜索、标签搜索
3. 基准测试
- 支持自动化基准测试,输出Prefill speed、 Decode Speed 和 Memory Usage等信息
- 支持文本、图片和音频的批量测试
4. 多模态聊天对话:支持完整的Markdown格式输出
- 文本到文本
- 语音到文本(支持 Omni 模型的语音输出)
- 图片到文本,图片可以拍摄输入或从图库中选择
- 视频到文本,支持视频输入处理
- Sana Diffusion:支持动漫风格迁移(比如吉卜力风格)
5. 模型配置
- 支持配置 mmap
- 支持配置 sampling strategy
- 支持配置 diffusion 设置
- 支持配置后端类型(CPU/Metal)
- 支持配置精度(低/普通/高)
- 支持配置线程数
- 支持配置多模态 Prompt API
- 支持配置 Omni 模型的音频输出
6. 对话历史
- 支持模型对话历史列表,还原历史对话场景
### 视频介绍
<img width="200" alt="image" src="./assets/introduction.gif" />
[点击这里下载原分辨率介绍视频](https://github.com/Yogayu/MNN/blob/master/project/MNNLLMForiOS/assets/introduction.mov)
### 应用预览图
| | | | |
|--|--|--|--|
| **Text To Text** | **Image To Text** | **Audio To Text** | **Model Fliter** |
| ![Text To Text](./assets/text.PNG) | ![Image To Text](./assets/image.PNG) | ![Audio To Text](./assets/audio.jpg) | ![Audio To Text](./assets/fliter.PNG) |
| **Local Model** | **Model Market** | **Benckmark** | **History** |
| ![Model List](./assets/localModel.PNG) | ![History](./assets/modelMarket.PNG) | ![History](./assets/benchmark.jpeg) | ![History](./assets/history2.PNG) |
<p></p>
此外,本应用支持 DeepSeek 带think模式端侧使用:
<img src="./assets/deepseek.jpg" alt="deepThink" width="200" />
## 如何构建并使用
1. 下载仓库代码:
```shell
git clone https://github.com/alibaba/MNN.git
```
2. 编译 MNN.framework:
```shell
sh package_scripts/ios/buildiOS.sh "
-DMNN_ARM82=ON
-DMNN_LOW_MEMORY=ON
-DMNN_SUPPORT_TRANSFORMER_FUSE=ON
-DMNN_BUILD_LLM=ON
-DMNN_CPU_WEIGHT_DEQUANT_GEMM=ON
-DMNN_METAL=ON
-DMNN_BUILD_DIFFUSION=ON
-DMNN_OPENCL=OFF
-DMNN_SEP_BUILD=OFF
-DLLM_SUPPORT_AUDIO=ON
-DMNN_BUILD_AUDIO=ON
-DLLM_SUPPORT_VISION=ON
-DMNN_BUILD_OPENCV=ON
-DMNN_IMGCODECS=ON
-DMNN_BUILD_LLM_OMNI=ON
"
```
3. 拷贝 framework 到 iOS 项目中
```shell
mv MNN-iOS-CPU-GPU/Static/MNN.framework apps/iOS/MNNLLMChat
```
确保 Link Binary With Libraried 中包含 MNN.framework,和其他三个 Framework。
<img src="./assets/framework.png" alt="deepThink" width="400" />
如果没有包含,可以手动添加 MNN.framework:
<img src="./assets/addFramework.png" alt="deepThink" width="200" />
<img src="./assets/addFramework2.png" alt="deepThink" width="200" />
4. 修改 iOS 签名并编译项目
```shell
cd apps/iOS/MNNLLMChat
open MNNLLMiOS.xcodeproj
```
在 Xcode 项目属性中 Signing & Capabilities > Team 输入自己的账号和Bundle Identifier
![signing](./assets/signing.png)
等待 Swift Package 下载完成之后,进行编译使用。
## 注意
iPhone 因为内存有限,建议使用7B以及以下的模型,避免内存不足导致的崩溃。
## 本地调试
本地调试模型非常简单,只需要将模型文件拖动到LocalModel文件夹下,然后运行项目即可:
1. 首先在 [huggingface](https://huggingface.co/taobao-mnn) 或者 [modelscope](https://modelscope.cn/organization/MNN) 下载 MNN 相关的模型
<img width="400" alt="image" src="./assets/copyLocalModel.png" />
2. 将下载之后的模型文件夹拖动到项目中 LocalModel 文件夹下
3. 如果是根目录模型,可以进行配置:
进入ModelListViewModel.swift的配置,比如是否支持思考:
```swift
// MARK: Config the Local Model here
let modelName = "Qwen3-0.6B-MNN-Inside" // 模型名称
let localModel = ModelInfo(
modelName: modelName,
tags: [
// MARK: if you know that model support think, uncomment the line
// NSLocalizedString("tag.deepThinking", comment: "Deep thinking tag for local model"), // 是否支持 think
NSLocalizedString("tag.localModel", comment: "Local model inside the app")],
categories: ["Local Models"],
vendor: "Local",
sources: ["local": "bundle_root/\(modelName)"],
isDownloaded: true
)
localModels.append(localModel)
ModelStorageManager.shared.markModelAsDownloaded(modelName)
```
5. 运行项目,点击进入聊天对话页面,进行模型对话和调试。
应用会自动检测并加载LocalModel文件夹中的模型,无需额外配置。
## Release Notes
### Version 0.5
- 新增 Sana Diffusion 图片风格迁移支持(如吉卜力风格)
- 新增 Omni 模型语音输出支持
- 新增视频输入支持,用于多模态对话
- 新增多模态 Prompt API 配置
- 新增后端类型配置(CPU/Metal)
- 新增精度配置(低/普通/高)
- 新增线程数配置
- 新增文本、图片和音频批量测试支持
### Version 0.4
- 新增项目三个大模块:本地模型,模型市场和基准测试
- 新增基准测试,可以测试不同模型效果
- 新增设置页面,可以从历史侧边蓝进入
- 新增Ali CDN获取模型列表
- 新增模型市场筛选功能
### Version 0.3.1
- 支持模型参数配置
| <img width="200" alt="image" src="./assets/SamplingStrategy1.PNG" /> | <img width="200" alt="image" src="./assets/SamplingStrategy2.PNG" /> | <img width="200" alt="image" src="./assets/SamplingStrategy3.PNG" />
### Version 0.3
新增:
- 支持 Modeler 源进行下载
- 支持 Stable Diffusion 文生图
| <img width="200" alt="image" src="./assets/diffusion.JPG" /> | <img width="200" alt="image" src="./assets/diffusionSettings.PNG" /> |
### Version 0.2
新增:
- 支持配置mmap和手动缓存清理
- 支持使用 ModelScope 源进行模型下载
| <img width="200" alt="image" src="./assets/usemmap.PNG" /> | <img width="200" alt="image" src="./assets/downloadSource.PNG" /> |
## 引用
- [Exyte/Chat](https://github.com/exyte/Chat)
- [stephencelis/CSQLite](https://github.com/stephencelis/SQLite.swift)
- [swift-transformers](https://github.com/huggingface/swift-transformers/)