使用 PrivateGPT 建立完全私有的文件問答系統,資料不離開本地,保護敏感資訊
專案簡介
PrivateGPT 是一個完全私有的 RAG(檢索增強生成)系統,讓你與文件對話而不需要網路連線。所有資料處理都在本地進行,適合處理敏感或機密文件。
GitHub Stars: 57K+
主要功能
- 完全離線 - 無需網路連線
- 隱私優先 - 資料不離開本地
- 多格式支援 - PDF、Word、Markdown、程式碼
- 多模型 - Ollama、llama.cpp、HuggingFace
- API 相容 - OpenAI 格式 API
安裝
Poetry 安裝
1
2
3
4
5
6
7
8
| git clone https://github.com/zylon-ai/private-gpt
cd private-gpt
# 安裝依賴
poetry install --extras "ui llms-llama-cpp embeddings-huggingface vector-stores-qdrant"
# 下載模型
poetry run python scripts/setup
|
快速安裝
1
2
| # 使用 Ollama
poetry install --extras "ui llms-ollama embeddings-ollama vector-stores-qdrant"
|
啟動
啟動服務
1
2
3
4
5
| # 啟動 API 和 UI
PGPT_PROFILES=local make run
# 僅 API
poetry run python -m private_gpt
|
訪問 http://localhost:8001
設定檔
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # settings-local.yaml
server:
env_name: local
port: 8001
llm:
mode: ollama
ollama:
model: llama3.3
api_base: http://localhost:11434
embedding:
mode: ollama
ollama:
model: nomic-embed-text
|
上傳文件
支援格式
- PDF
- Word (.docx)
- PowerPoint (.pptx)
- Markdown
- 純文字
- 程式碼檔案
透過 UI 上傳
- 開啟網頁介面
- 點擊「Upload」
- 選擇檔案
- 等待處理完成
透過 API 上傳
1
2
3
| curl -X POST "http://localhost:8001/v1/ingest/file" \
-H "Content-Type: multipart/form-data" \
-F "file=@document.pdf"
|
Python 上傳
1
2
3
4
5
6
7
| import requests
with open("document.pdf", "rb") as f:
response = requests.post(
"http://localhost:8001/v1/ingest/file",
files={"file": f}
)
|
對話查詢
UI 查詢
直接在網頁介面輸入問題。
API 查詢
1
2
3
4
5
6
7
8
| curl -X POST "http://localhost:8001/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "這份文件的主要內容是什麼?"}
],
"use_context": true
}'
|
Python 整合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| from openai import OpenAI
client = OpenAI(
base_url="http://localhost:8001/v1",
api_key="not-needed"
)
# 使用上傳的文件作為上下文
response = client.chat.completions.create(
model="private-gpt",
messages=[
{"role": "user", "content": "摘要這份報告的重點"}
],
extra_body={"use_context": True}
)
print(response.choices[0].message.content)
|
模型設定
使用 Ollama
1
2
3
4
5
6
7
8
9
10
11
12
| # settings-ollama.yaml
llm:
mode: ollama
ollama:
model: llama3.3
api_base: http://localhost:11434
request_timeout: 120.0
embedding:
mode: ollama
ollama:
model: nomic-embed-text
|
使用 llama.cpp
1
2
3
4
5
6
7
8
9
10
11
12
| # settings-local.yaml
llm:
mode: llamacpp
llamacpp:
model_path: models/llama-2-7b-chat.Q4_K_M.gguf
n_ctx: 4096
n_gpu_layers: 35
embedding:
mode: huggingface
huggingface:
model_name: BAAI/bge-small-en-v1.5
|
使用 OpenAI
1
2
3
4
5
6
7
8
9
10
11
| # settings-openai.yaml
llm:
mode: openai
openai:
model: gpt-4o
api_key: ${OPENAI_API_KEY}
embedding:
mode: openai
openai:
model: text-embedding-3-small
|
向量資料庫
Qdrant(預設)
1
2
3
4
| vectorstore:
database: qdrant
qdrant:
path: ./local_data/qdrant
|
Chroma
1
2
3
4
| vectorstore:
database: chroma
chroma:
persist_directory: ./local_data/chroma
|
Docker 部署
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| version: '3.8'
services:
private-gpt:
image: zylonai/private-gpt:latest
ports:
- "8001:8001"
environment:
- PGPT_PROFILES=docker
volumes:
- ./local_data:/app/local_data
- ./settings-docker.yaml:/app/settings-docker.yaml
ollama:
image: ollama/ollama
volumes:
- ollama-data:/root/.ollama
volumes:
ollama-data:
|
安全考量
資料隔離
1
2
3
| # 限制文件目錄
data:
local_data_folder: ./secure_data
|
存取控制
1
2
3
4
| server:
auth:
enabled: true
secret: your-secret-key
|
稽核日誌
1
2
3
| logging:
level: INFO
file: ./logs/private-gpt.log
|
效能優化
GPU 加速
1
2
3
| llm:
llamacpp:
n_gpu_layers: 99 # 所有層到 GPU
|
Embedding 快取
1
2
3
4
5
| embedding:
cache:
enabled: true
type: file
path: ./cache/embeddings
|
相關連結
延伸閱讀