ai, development,

Setup LLM Development Environment

Cui Cui Follow Jan 01, 2024 · 5 mins read
Setup LLM Development Environment

“AI Development Environment” - A comprehensive guide to setting up your LLM development workspace

基础环境设置

Python 环境 (Conda)

# 下载 Miniconda 安装脚本
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh

# 运行安装脚本
bash Miniconda3-latest-MacOSX-arm64.sh

# 按照提示完成安装
# 1. 接受许可条款
# 2. 确认安装位置(默认是 ~/miniconda3)
# 3. 选择是否初始化 Miniconda3(建议选择 yes)

# 重新加载 shell 配置
source ~/.zshrc

# 验证安装
conda --version

# 创建新的环境
conda create -n llm-dev python=3.10
conda activate llm-dev

# 配置 conda 镜像源(可选,但推荐)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

# 安装基础包
conda install -c pytorch pytorch torchvision torchaudio
conda install -c huggingface transformers
conda install -c conda-forge accelerate
conda install -c conda-forge bitsandbytes
conda install -c conda-forge sentencepiece
conda install -c conda-forge protobuf
conda install -c conda-forge safetensors

环境管理命令

# 查看所有环境
conda env list

# 更新环境
conda update --all

# 导出环境配置
conda env export > environment.yml

# 从配置文件创建环境
conda env create -f environment.yml

# 删除环境
conda env remove -n llm-dev

安装基础 LLM 开发包

pip install torch torchvision torchaudio
pip install transformers
pip install accelerate
pip install bitsandbytes
pip install sentencepiece
pip install protobuf
pip install safetensors

LLM 开发工具

安装 LangChain

pip install langchain
pip install langchain-community
pip install langchain-core

安装向量数据库

# 安装 ChromaDB
pip install chromadb

# 安装 FAISS
pip install faiss-cpu

安装评估工具

pip install evaluate
pip install rouge_score
pip install bert-score

本地 LLM 运行环境

安装 LM Studio

# 从官网下载安装包
https://lmstudio.ai/

安装 Ollama

brew install ollama

安装 Text Generation WebUI

git clone https://github.com/oobabooga/text-generation-webui
cd text-generation-webui
./start_macos.sh

开发 IDE 和工具

VS Code 扩展

  • Python
  • Jupyter
  • Pylance
  • Python Test Explorer
  • GitLens
  • Docker

安装 Jupyter

pip install jupyter
pip install ipykernel
python -m ipykernel install --user --name=llm-dev

模型下载和管理

Hugging Face CLI

pip install huggingface_hub
huggingface-cli login

模型下载工具

pip install huggingface_hub
pip install transformers

性能优化工具

CUDA 支持(如果使用 NVIDIA GPU)

# 检查 CUDA 是否可用
python -c "import torch; print(torch.cuda.is_available())"

# 安装 CUDA 工具包
brew install cuda

量化工具

pip install optimum
pip install auto-gptq

监控和调试工具

安装 Weights & Biases

pip install wandb
wandb login

安装 MLflow

pip install mlflow

提示工程工具

安装 PromptFlow

pip install promptflow
pip install promptflow-tools

安装 PromptPerfect

pip install promptperfect

模型微调工具

安装 PEFT

pip install peft

安装 LoRA 工具

pip install loralib

模型部署工具

安装 FastAPI

pip install fastapi
pip install uvicorn

安装 Gradio

pip install gradio

数据预处理工具

安装数据集处理工具

pip install datasets
pip install nltk
pip install spacy
python -m spacy download en_core_web_sm

安装数据清洗工具

pip install pandas
pip install numpy
pip install scikit-learn

模型测试工具

安装测试框架

pip install pytest
pip install pytest-cov

安装性能测试工具

pip install locust

环境变量配置

创建 .env 文件:

# API Keys
OPENAI_API_KEY=your_openai_api_key
HUGGINGFACE_API_KEY=your_huggingface_api_key

# 模型配置
MODEL_PATH=/path/to/your/models
CACHE_DIR=/path/to/cache

# 系统配置
CUDA_VISIBLE_DEVICES=0
TOKENIZERS_PARALLELISM=true

常用模型配置

模型下载示例

# 下载 Llama 2
huggingface-cli download meta-llama/Llama-2-7b-chat-hf

# 下载 Mistral
huggingface-cli download mistralai/Mistral-7B-v0.1

# 下载 Phi-2
huggingface-cli download microsoft/phi-2

模型量化配置

# 4-bit 量化配置示例
from transformers import BitsAndBytesConfig

quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
)

开发最佳实践

  1. 使用版本控制管理模型和代码
    git lfs install
    git lfs track "*.bin"
    git lfs track "*.safetensors"
    
  2. 使用 Docker 容器化开发环境
    FROM pytorch/pytorch:2.1.0-cuda11.8-cudnn8-runtime
       
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install -r requirements.txt
       
    COPY . .
    
  3. 使用环境管理工具
    # 使用 conda
    conda create -n llm-dev python=3.10
    conda activate llm-dev
       
    # 或使用 poetry
    poetry init
    poetry add torch transformers
    

性能优化建议

  1. 使用模型量化减少内存占用
  2. 使用批处理提高推理效率
  3. 使用模型缓存减少重复计算
  4. 使用异步处理提高并发性能
  5. 使用模型蒸馏减小模型体积

安全注意事项

  1. 不要将 API 密钥提交到版本控制系统
  2. 使用环境变量管理敏感信息
  3. 定期更新依赖包以修复安全漏洞
  4. 使用模型安全扫描工具
  5. 实施适当的访问控制和认证机制

实用命令

检查 GPU 使用情况

nvidia-smi

模型量化

python -m transformers.quantization.quantize --model_name_or_path model_name

推荐的学习资源

注意事项

  1. 确保有足够的磁盘空间用于模型存储
  2. 建议使用 SSD 存储模型文件
  3. 对于大型模型,建议使用至少 16GB RAM
  4. 如果使用 GPU,建议使用 NVIDIA 显卡
  5. 定期更新依赖包以获取最新功能和修复
Join Newsletter
Get the latest news right in your inbox. We never spam!
Cui
Written by Cui Follow
Hi, I am Z, the coder for cuizhanming.com!