中文 | English
为 M 系 Mac 打造的 OpenAI 兼容 API

MiniAiApi 是一个专为 M 系列芯片的 Mac (尤其是 Mac Mini )优化的 AI 能力服务,它尝试利用 Mac 生态支持的软件和框架( MLX )来提供性能最高的、和 OpenAI 兼容的 API 接口。
say 命令进行语音合成| API 端点 | 状态 | 说明 | 依赖服务 |
|---|---|---|---|
/v1/audio/speech |
✅ 可用 | TTS 语音合成 (传统 + 模型 + 克隆) | macOS say / MLX-Audio |
/v1/audio/transcriptions |
✅ 可用 | 语音转文字 | MLX Whisper |
/v1/audio/translations |
✅ 可用 | 语音翻译为英文 | MLX Whisper |
/v1/chat/completions |
✅ 可用 | 聊天对话 | LMstudio |
/v1/embeddings |
✅ 可用 | 文本嵌入 | LMstudio |
/v1/images/generations |
✅ 可用 | 图像生成 | Draw Things |
/v1/models |
✅ 可用 | 获取模型列表 | - |
/health |
✅ 可用 | 健康检查 | - |
备注: - ✅ 表示已实现并可用的 API - 部分 API 需要额外的依赖服务才能正常工作 - 所有 API 都兼容 OpenAI 的请求和响应格式
git clone <repository-url>
cd miniAiApi
npm install
# 安装 MLX Whisper
pip install mlx-whisper
# 安装 MLX-Audio (可选,用于音频克隆)
pip install mlx-audio
# 安装 FFmpeg
brew install ffmpeg
# 安装和配置 LMstudio
# 1. 从官网下载并安装 LMstudio
# 2. 启动 LMstudio,在设置中开启 API Server
# 3. 设置监听地址为 127.0.0.1:1234(默认)
# 安装和配置 Draw Things (可选)
# 1. 从 App Store 安装 Draw Things
# 2. 在设置→高级设置中启用 HTTP API Server
# 3. 设置监听地址为 127.0.0.1:7860(默认)
建议预先下载模型以避免首次使用时的等待:
# 安装 Hugging Face CLI (如果还没有安装)
pip install huggingface_hub
# 下载 whisper
hf download mlx-community/whisper-large-v3-mlx (Large模型中文识别效果较好)
# 下载推荐的中文 TTS 模型
hf download mlx-community/Spark-TTS-0.5B-fp16
hf download mlx-community/Spark-TTS-0.5B-4-6bit(备用,克隆效果较差,但更快)
# 下载 Qwen3-TTS (标准模型,不克隆)
hf download mlx-community/Qwen3-TTS-12Hz-0.6B-Base-bf16
# 下载其他可用模型 (可选)
hf download mlx-community/Kokoro-82M-bf16 (中文非常不行,其他语言可以)
注意: 下载模型可能需要几分钟到几十分钟,取决于网络速度。使用
hf download命令可以看到下载进度。
cp env.example .env
编辑 .env 文件配置你的设置:
# 服务器配置
PORT=3000
HOST=0.0.0.0
# TTS 配置
TTS_VOICE=Yue
TTS_OUTPUT_FORMAT=mp3
# TTS 标准模型配置(不克隆)
TTS_MODEL_ENABLED=false
TTS_MODEL_ALIAS=tts-1:model
TTS_MODEL_NAME=mlx-community/Qwen3-TTS-12Hz-0.6B-Base-bf16
TTS_MODEL_LOCAL_PATH=
TTS_MODEL_DEFAULT_VOICE_ZH=Vivian
TTS_MODEL_DEFAULT_VOICE_EN=Ryan
# TTS 音频克隆配置 (可选)
TTS_CLONE_ENABLED=false
TTS_CLONE_MODEL=mlx-community/Spark-TTS-0.5B-fp16
TTS_CLONE_REF_AUDIO=/path/to/reference/audio.mp3
TTS_CLONE_REF_TEXT=参考音频对应的文本内容
TTS_CLONE_LANG_CODE=z
TTS_CLONE_SPEED=1.0
# STT 配置
STT_MODEL=mlx-community/whisper-large-v3-mlx
STT_LANGUAGE=zh
# API 安全
API_KEY_REQUIRED=false
API_KEY=your-api-key-here
# LMstudio 配置
LMSTUDIO_BASE_URL=http://127.0.0.1:1234
LMSTUDIO_API_KEY=
LMSTUDIO_TIMEOUT=60000
# Draw Things 配置
DRAW_THINGS_BASE_URL=http://127.0.0.1:7860
DRAW_THINGS_ENABLED=false
DRAW_THINGS_TIMEOUT=120000
启用音频克隆功能
env
TTS_CLONE_ENABLED=true
选择并下载模型 ```bash # 推荐:高质量中文模型 hf download mlx-community/Spark-TTS-0.5B-fp16
# 或者:量化版本 (占用内存更少) hf download mlx-community/Spark-TTS-0.5B-4-6bit ```
配置模型和参考音频
env
TTS_CLONE_MODEL=mlx-community/Spark-TTS-0.5B-fp16
TTS_CLONE_REF_AUDIO=/Users/yourname/audio/reference.mp3
TTS_CLONE_REF_TEXT=这是参考音频中说话人说的完整内容,需要与音频内容完全匹配。
TTS_CLONE_LANG_CODE=z
TTS_CLONE_SPEED=1.0
准备参考音频
配置完成后,可以通过以下命令测试:
# 测试传统 TTS
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{"model": "tts-1", "input": "测试传统语音合成"}' \
--output test_normal.mp3
# 测试标准模型 TTS(不克隆)
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{"model": "tts-1:model", "input": "测试标准模型语音合成", "voice": "alloy", "response_format": "wav"}' \
--output test_model.wav
# 测试音频克隆
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{"model": "tts-1:clone", "input": "测试音频克隆功能"}' \
--output test_clone.mp3
# 生产环境
npm start
# 开发环境(自动重启)
npm run dev
服务启动后,访问 http://localhost:3000 查看 API 信息。
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "你好,这是一个测试。",
"voice": "alloy",
"response_format": "mp3"
}' \
--output speech.mp3
支持的语音:
- alloy → Yue (中文)
- echo → Ting-Ting (中文)
- fable → Sin-ji (中文)
- onyx → Li-mu (中文)
- nova → Mei-Jia (中文)
- shimmer → Yu-shu (中文)
要使用音频克隆功能,只需在模型名称后添加 :clone 后缀:
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1:clone",
"input": "其实模型的幻觉不是什么大问题,不如说,相信概率模型预训练的知识,本身就是缘木求鱼。模型的核心还是推理能力要强,然后导入可信上下文,再通过推理能力去得出答案。",
"voice": "alloy",
"response_format": "mp3",
"speed": 1.5
}' \
--output cloned_speech.mp3
注意: - 使用克隆模式前,需要在
.env中配置TTS_CLONE_ENABLED=true和相关参数 - 需要提供参考音频文件和对应的参考文本 - 克隆模式会忽略voice参数,使用配置的参考音频进行音色克隆
标准模型 TTS 不需要参考音频,不做音色克隆,直接使用 TTS 模型生成语音。
推荐用 .env 开启后,通过 TTS_MODEL_ALIAS 触发(默认 tts-1:model):
curl -X POST http://localhost:3000/v1/audio/speech \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1:model",
"input": "你好,这是标准模型语音合成测试。",
"voice": "alloy",
"response_format": "wav",
"speed": 1.0
}' \
--output speech_model.wav
TTS_MODEL_ALIAS 的作用是把“标准模型 TTS”映射成一个稳定的模型名,避免你在客户端到处硬编码具体的模型 repo id 或本地路径:
- 你可以把 TTS_MODEL_ALIAS 改成任意字符串(例如 tts-1:qwen3),然后客户端请求里使用同名 model 即可触发标准模型分支
- 当 TTS_MODEL_ENABLED=true 时:model == TTS_MODEL_ALIAS 会走标准模型
- 你也可以直接传 model == TTS_MODEL_NAME(例如 mlx-community/Qwen3-TTS-12Hz-0.6B-Base-bf16)来触发标准模型
TTS_MODEL_LOCAL_PATH(可选)用于强制只用本地已下载的 snapshot 路径,避免运行时访问 Hugging Face(适合离线环境)。
curl -X POST http://localhost:3000/v1/audio/transcriptions \
-H "Content-Type: multipart/form-data" \
-F file="@audio.mp3" \
-F model="whisper-1" \
-F language="zh"
curl -X POST http://localhost:3000/v1/audio/translations \
-H "Content-Type: multipart/form-data" \
-F file="@audio.mp3" \
-F model="whisper-1"
curl http://localhost:3000/v1/models
curl -X POST http://localhost:3000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "user", "content": "你好"}
]
}'
curl -X POST http://localhost:3000/v1/embeddings \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "text-embedding-ada-002",
"input": "这是一段需要向量化的文本"
}'
curl -X POST http://localhost:3000/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"prompt": "一只可爱的小猫在花园里玩耍",
"n": 1,
"size": "1024x1024",
"quality": "standard",
"style": "vivid"
}'
curl http://localhost:3000/health
TTS_VOICE: 默认语音 (默认: Yue)TTS_OUTPUT_FORMAT: 输出格式 (mp3/wav)TTS_TEMP_DIR: 临时文件目录TTS_MODEL_ENABLED: 是否启用标准模型 TTS (默认: false)TTS_MODEL_ALIAS: 标准模型的别名入口 (默认: tts-1:model)TTS_MODEL_NAME: 标准模型 repo id (默认: mlx-community/Qwen3-TTS-12Hz-0.6B-Base-bf16)TTS_MODEL_LOCAL_PATH: 标准模型本地 snapshot 目录 (可选,优先于 repo id)TTS_MODEL_DEFAULT_VOICE_ZH: 中文默认 speaker (默认: Vivian)TTS_MODEL_DEFAULT_VOICE_EN: 英文默认 speaker (默认: Ryan)TTS_CLONE_ENABLED: 是否启用音频克隆功能 (默认: false)TTS_CLONE_MODEL: MLX-Audio 模型名称 (默认: mlx-community/Spark-TTS-0.5B-fp16)TTS_CLONE_REF_AUDIO: 参考音频文件路径 (必须,用于克隆音色)TTS_CLONE_REF_TEXT: 参考音频对应的文本内容 (必须,用于模型对齐)TTS_CLONE_LANG_CODE: 语言代码 (默认: z,表示中文)TTS_CLONE_SPEED: 语音速度 (默认: 1.0,范围 0.5-2.0)推荐的 MLX-Audio 模型:
- mlx-community/Spark-TTS-0.5B-fp16 - 高质量中文 TTS 模型 (推荐)
- mlx-community/Spark-TTS-0.5B-4-6bit - 量化版本,占用内存更少
- mlx-community/Kokoro-82M-bf16 - 多语言支持的轻量模型
语言代码说明:
- z - 中文 (推荐用于中文文本)
- a - 美式英语
- b - 英式英语
- j - 日语
STT_MODEL: Whisper 模型 (默认: mlx-community/whisper-large-v3-mlx)STT_LANGUAGE: 识别语言 (zh/en/auto 等)STT_OUTPUT_DIR: 输出目录LMSTUDIO_BASE_URL: LMstudio 服务地址 (默认: http://127.0.0.1:1234)LMSTUDIO_API_KEY: LMstudio API 密钥 (可选)LMSTUDIO_TIMEOUT: 请求超时时间 (默认: 60000ms)DRAW_THINGS_BASE_URL: Draw Things HTTP API 地址 (默认: http://127.0.0.1:7860)DRAW_THINGS_ENABLED: 是否启用图像生成功能 (默认: false)DRAW_THINGS_TIMEOUT: 请求超时时间 (默认: 120000ms)mlx-community/whisper-tinymlx-community/whisper-basemlx-community/whisper-smallmlx-community/whisper-mediummlx-community/whisper-large-v2mlx-community/whisper-large-v3mlx-community/whisper-large-v3-mlxmlx-community/whisper-large-v3-turbominiAiApi/
├── src/
│ ├── index.js # 主服务器文件
│ ├── services/
│ │ ├── ttsService.js # TTS 服务
│ │ └── sttService.js # STT 服务
│ ├── routes/
│ │ ├── audioRoutes.js # 音频 API 路由
│ │ └── imageRoutes.js # 图像 API 路由
│ └── middleware/
│ └── auth.js # 认证中间件
├── config/
│ └── default.js # 配置管理
├── public/
├── env.example
├── package.json
└── README.md
src/services/ 中添加新的服务类src/routes/ 中添加对应的路由src/index.js 中注册路由API 使用标准的 HTTP 状态码和 OpenAI 兼容的错误格式:
{
"error": {
"message": "错误描述",
"type": "error_type",
"code": "error_code"
}
}
say -v ?确保 FFmpeg 已安装
音频克隆不工作
python -m mlx_audio.tts.generate --helpTTS_CLONE_ENABLED=truels ~/.cache/huggingface/hub/检查语言代码是否正确 (中文使用 z)
标准模型 TTS 不工作
pip show mlx-audio 版本足够新并包含 qwen3_tts(例如 mlx-audio 0.3.1+)TTS_MODEL_ENABLED=true如果使用 TTS_MODEL_LOCAL_PATH,确认该目录存在且可读
STT 不工作
which mlx_whisper确保模型已下载
文件上传失败
确保音频格式受支持
LMstudio 代理失败
检查 API Key 是否匹配
图像生成失败
DRAW_THINGS_ENABLED=true 配置服务会输出详细的日志信息,包括: - 请求处理时间 - 错误堆栈跟踪 - 服务状态检查
MIT License
欢迎提交 Issues 和 Pull Requests!
$ claude mcp add miniaiapi \
-- python -m otcore.mcp_server <graph>