python3 -m venv venv
source venv/bin/activate # 创建虚拟环境
安装依赖
langchain==0.2.*
langchain-community # 包含 DashScope 支持
dashscope # 阿里云官方 SDK
faiss-cpu
unstructured[pdf,docx]
tiktoken
fastapi
uvicorn
python-dotenv
pypdf
python-docx
获取DashScope API Key
.env
DASHSCOPE_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx
chmod 600 .env #权限
目录结构
rag-qwen-lamp/
├── .env.example # 环境变量模板
├── requirements.txt # Python 依赖
├── README.md # 部署说明
├── knowledge_base/ # 上传的文档存放目录(自动创建)
├── vectorstore/ # FAISS 向量库存储(自动创建)
├── main.py # FastAPI 入口
├── rag_manager.py # RAG + Qwen 核心逻辑
└── php_admin/ # PHP 管理前端(LAMP 用)
├── index.php
└── upload.php
测试 FastAPI 服务
source venv/bin/activate # 确保在虚拟环境中
uvicorn main:app --host 127.0.0.1 --port 8000 # 启动服务(仅本地访问)
用 systemd 托管服务
sudo nano /etc/systemd/system/rag-qwen.service
[Unit]
Description=RAG Qwen FastAPI Service
After=network.target
[Service]
User=apache
Group=apache
WorkingDirectory=/var/www/rag-qwen
EnvironmentFile=/var/www/rag-qwen/.env
ExecStart=/var/www/rag-qwen/venv/bin/uvicorn main:app --host 127.0.0.1 --port 8000 Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
启动服务
sudo chown -R www-data:www-data /var/www/rag-qwen # 设置目录权限
systemd sudo systemctl daemon-reload # 重载
sudo systemctl enable rag-qwen # 开机自启
sudo systemctl start rag-qwen # 启动服务
sudo systemctl status rag-qwen # 查看状态
配置 Apache 反向代理
sudo nano rag-qwen.conf
# 反向代理 API
ProxyPreserveHost On
ProxyPass /api/ http://127.0.0.1:8000/
ProxyPassReverse /api/ http://127.0.0.1:8000/
使用 Chroma 替代 FAISS
from langchain_community.vectorstores import Chroma
# 替换 FAISS 为 Chroma
vectorstore = Chroma.from_documents(
documents=splits,
embedding=embeddings,
persist_directory=VECTORSTORE_PATH
)
vectorstore.persist()
609



被折叠的 条评论
为什么被折叠?



