零基础入门:AWS EC2系统安装PrivateGPT详细步骤

云信安装大师
90
AI 质量分
10 5 月, 2025
3 分钟阅读
0 阅读

零基础入门:AWS EC2系统安装PrivateGPT详细步骤

引言

PrivateGPT是一个可以本地运行的AI对话系统,能够保护你的数据隐私。本文将手把手教你如何在AWS EC2上安装PrivateGPT,即使你是完全没有经验的新手也能轻松完成。

准备工作

在开始之前,你需要:
1. 一个AWS账号(免费套餐即可)
2. 基本的Linux命令行知识
3. 大约30分钟的时间

第一步:创建EC2实例

1.1 登录AWS控制台

访问AWS官网并登录你的账号。

1.2 启动EC2实例

  1. 在服务列表中选择”EC2″
  2. 点击”启动实例”
  3. 输入实例名称,如”PrivateGPT-Server”

1.3 选择AMI(Amazon Machine Image)

建议选择Ubuntu Server 22.04 LTS (HVM),因为:
– Ubuntu社区支持良好
– 软件包更新及时
– PrivateGPT依赖项容易安装

代码片段
# AMI ID示例(不同区域可能不同):
ami-0c55b159cbfafe1f0 (us-east-1区域)

1.4 选择实例类型

PrivateGPT需要较多内存和计算资源,建议:
– t3.xlarge (4 vCPU, 16GB内存) – 适合测试
– g4dn.xlarge (GPU实例) – 如需更好的性能

注意:GPU实例成本较高,测试阶段可以先使用CPU实例

1.5 配置存储

建议分配至少50GB的SSD存储空间:
– PrivateGPT模型文件较大(约20GB)
– /dev/sda1根卷:50GB GP3

1.6 安全组设置

添加入站规则允许SSH和自定义TCP端口:

代码片段
类型       协议  端口范围   源
SSH        TCP    22       My IP (或0.0.0.0/0)
Custom TCP TCP    8000     0.0.0.0/0 (用于PrivateGPT Web界面)

第二步:连接到EC2实例

2.1 SSH连接

使用你下载的密钥对连接:

代码片段
chmod 400 your-key-pair.pem
ssh -i "your-key-pair.pem" ubuntu@your-instance-public-dns

常见问题:如果连接失败,检查安全组设置和密钥文件权限

第三步:安装依赖项

3.1 更新系统包

代码片段
sudo apt update && sudo apt upgrade -y

3.2 安装基本工具

代码片段
sudo apt install -y git python3-pip python3-venv build-essential cmake libstdc++6 software-properties-common wget curl

3.3 Python环境设置

代码片段
python3 -m venv privategpt-env
source privategpt-env/bin/activate

第四步:安装PrivateGPT

4.1 Clone仓库

代码片段
git clone https://github.com/imartinez/privateGPT.git
cd privateGPT

4.2 Python依赖项安装

代码片段
pip install -r requirements.txt --no-cache-dir

经验提示--no-cache-dir可以避免磁盘空间不足的问题

4.3 LLM模型下载(约20GB)

代码片段
wget https://gpt4all.io/models/ggml-gpt4all-j-v1.3-groovy.bin -O models/ggml-gpt4all-j-v1.3-groovy.bin --progress=bar:force:noscroll || curl -L https://gpt4all.io/models/ggml-gpt4all-j-v1.3-groovy.bin --output models/ggml-gpt4all-j-v1.3-groovy.bin --progress-bar || echo "Download failed, please manually download from https://gpt4all.io/index.html"

注意:下载可能需要较长时间,建议使用screen/tmux保持会话

第五步:配置PrivateGPT

5.1 .env文件配置

代码片段
cp example.env .env && nano .env

修改以下关键参数:

代码片段
MODEL_TYPE=LlamaCpp # GPT模型类型 
PERSIST_DIRECTORY=db #向量数据库存储目录 
MODEL_PATH=models/ggml-gpt4all-j-v1.3-groovy.bin #模型路径 
EMBEDDINGS_MODEL_NAME=all-MiniLM-L6-v2 #嵌入模型 
MODEL_N_CTX=1000 #上下文长度 
TARGET_SOURCE_CHUNKS=4 #返回的源块数 

Sixth Step: Run PrivateGPT with PM2 (Persistent Process)

To ensure PrivateGPT keeps running after you disconnect:

代码片段
npm install pm2 -g # Install PM2 process manager  
pm2 start python --name "privategpt" -- serve  
pm2 save  
pm2 startup  
# Follow the instructions to enable PM2 on boot  

Now PrivateGPT will automatically restart if the server reboots.

Step Seven: Accessing the Web Interface

By default, PrivateGPT runs on port 8000. To access it:

代码片段
http://<your-ec2-public-ip>:8000/

If you can’t connect:
1.Check security group rules allow port 8000
2.Check PM2 logs: pm2 logs privategpt

Step Eight: Using PrivateGPT

Once running, you can:
-Upload documents to /ingest endpoint
-Chat via the web interface or API
-Manage documents through the UI

Example API call:

代码片段
curl -X POST http://localhost:8000/completions \
-H "Content-Type: application/json" \ 
-d '{"prompt": "What is PrivateGPT?"}'

Troubleshooting

Common issues and fixes:

Issue: Out of memory errors
Fix: Use a larger instance type or add swap space:

代码片段
sudo fallocate -l8G /swapfile && sudo chmod600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile && echo '/swapfile none swap sw00' | sudo tee-a /etc/fstab  

Issue: CUDA errors on GPU instances
Fix: Install NVIDIA drivers first:

代码片段
sudo apt install nvidia-driver-535 nvidia-dkms-535  
sudo reboot  
nvidia-smi # Verify installation  
pip uninstall llama-cpp-python && CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python --no-cache-dir --force-reinstall  

Conclusion

You now have a fully private AI assistant running on AWS! Key takeaways:

✅ Used EC2 to host PrivateGPT securely in the cloud
✅ Configured persistence with PM2 for reliability
✅ Can ingest and query private documents safely

Next steps could be adding authentication or connecting other data sources.

Let me know in the comments if you run into any issues!

原创 高质量