跳转至

LocalAI 本地部署配置

兼容 OpenAI API 的本地推理服务,支持多种后端,Go 语言实现


系统要求

项目 最低要求 推荐
Docker 20.x+ 最新版
内存 8 GB 16 GB+
GPU 可选(NVIDIA CUDA) NVIDIA RTX 3060+
磁盘 10 GB SSD 50 GB+

安装

Docker(推荐)

Bash
# CPU
docker run -d \
  -p 8080:8080 \
  -v $PWD/models:/models \
  --name localai \
  localai/localai:latest

# GPU (CUDA)
docker run -d \
  -p 8080:8080 \
  -v $PWD/models:/models \
  --gpus all \
  --name localai \
  localai/localai:latest-cublas-cuda12

二进制安装

Bash
1
2
3
4
# 下载最新 release
wget https://github.com/mudler/LocalAI/releases/latest/download/local-ai-linux-amd64
chmod +x local-ai-linux-amd64
./local-ai-linux-amd64

模型配置

YAML 配置文件

models/ 目录下创建 YAML 配置:

models/qwen2.5-7b.yaml:

YAML
name: qwen2.5-7b
parameters:
  model: qwen2.5-7b-instruct-q4_k_m.gguf
  temperature: 0.7
  top_p: 0.9
  max_tokens: 4096
context_size: 4096
gpu_layers: 99
threads: 8
template:
  chat: |
    {{- if .System }}<|im_start|>system
    {{ .System }}<|im_end|>
    {{- end }}
    {{- range .Messages }}
    {{- if eq .Role "user" }}<|im_start|>user
    {{ .Content }}<|im_end|>
    {{- else if eq .Role "assistant" }}<|im_start|>assistant
    {{ .Content }}<|im_end|>
    {{- end }}
    {{- end }}<|im_start|>assistant

下载模型

Bash
# 将 GGUF 模型放入 models/ 目录
wget -P models/ https://huggingface.co/.../qwen2.5-7b-instruct-q4_k_m.gguf

API 调用

LocalAI 完全兼容 OpenAI API:

Bash
# Chat Completions
curl http://localhost:8080/v1/chat/completions -d '{
  "model": "qwen2.5-7b",
  "messages": [{"role": "user", "content": "你好"}],
  "stream": true
}'

# Completions
curl http://localhost:8080/v1/completions -d '{
  "model": "qwen2.5-7b",
  "prompt": "你好世界",
  "max_tokens": 512
}'

# Embeddings
curl http://localhost:8080/v1/embeddings -d '{
  "model": "text-embedding",
  "input": "你好世界"
}'

支持的后端

后端 说明 配置
llama.cpp 默认,GGUF 模型 backend: llama
stable-diffusion 图像生成 backend: stablediffusion
whisper 语音识别 backend: whisper
petals P2P 分布式推理 backend: petals

练习清单

  • Docker 启动 LocalAI
  • 配置一个 GGUF 模型的 YAML
  • 用 OpenAI 兼容 API 调用
  • 配置 GPU 加速