基于 LangChain 1.0 和 LangGraph 1.0 的智能体平台后端服务。
- Web 框架: FastAPI
- ASGI 服务器: Uvicorn
- 数据库: PostgreSQL + SQLAlchemy 2.0 (异步)
- 数据库迁移: Alembic
- 包管理: uv
- AI 框架: LangChain 1.0 + LangGraph 1.0
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"cd backend
# 创建虚拟环境
uv venv
# 激活虚拟环境
source .venv/bin/activate # Linux/macOS
# .venv\Scripts\activate # Windows
# 安装依赖
# 默认使用清华大学镜像源 (配置在 uv.toml 中)
# Default uses Tsinghua mirror (configured in uv.toml)
uv syncPyPI 镜像源配置
项目默认使用清华大学镜像源 (https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple) 以加速依赖下载。您可以通过以下方式自定义:
-
环境变量 (优先级最高):
export UV_INDEX_URL=https://pypi.org/simple # 使用官方源 export UV_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple # 使用清华源
-
.env文件: 在.env中设置UV_INDEX_URL变量 -
配置文件:
- 编辑
pyproject.toml中的[tool.uv]部分 (推荐) - 编辑
uv.toml中的[index]部分
- 编辑
PyPI Mirror Configuration
The project uses Tsinghua mirror by default. You can customize it via:
- Environment variable:
UV_INDEX_URL(highest priority) .envfile: SetUV_INDEX_URLvariable- Configuration file:
pyproject.tomloruv.toml
cp .env.example .env
# 编辑 .env 文件,填入你的配置
# 可选的: 设置 UV_INDEX_URL 自定义 PyPI 镜像源
# Optional: Set UV_INDEX_URL to customize PyPI mirror# 创建 PostgreSQL 数据库
createdb joysafeter
# 运行迁移
alembic upgrade head必须使用 uv run 来运行,确保使用正确的虚拟环境!
#使用 uv run
uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# 使用 uv run
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4
# 或使用 uvloop (更高性能)
uv run uvicorn app.main:app \
--host 0.0.0.0 \
--port 8000 \
--workers 4 \
--loop uvloop \
--http httptools启动服务后访问:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
{
"success": true,
"code": 200,
"message": "Success",
"data": { ... },
"timestamp": "2024-12-04T00:00:00Z"
}{
"success": true,
"code": 200,
"message": "Success",
"data": {
"items": [...],
"total": 100,
"page": 1,
"page_size": 20,
"pages": 5
},
"timestamp": "2024-12-04T00:00:00Z"
}alembic revision --autogenerate -m "描述"alembic upgrade headalembic downgrade -1# 安装开发依赖
uv sync --dev
# 运行测试
pytest
# 带覆盖率
pytest --cov=app# 开发环境
docker-compose up -d postgres redis
# 生产环境 (多实例)
docker-compose --profile production up -d --scale app=4 ┌─────────┐
│ Nginx │
│ (LB) │
└────┬────┘
│
┌───────────────┼───────────────┐
│ │ │
┌────▼────┐ ┌────▼────┐ ┌────▼────┐
│ App 1 │ │ App 2 │ │ App N │
│ (8000) │ │ (8000) │ │ (8000) │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
└───────────────┼───────────────┘
│
┌──────────┴──────────┐
│ │
┌────▼────┐ ┌────▼────┐
│ Postgres │ │ Redis │
│ (状态) │ │ (缓存) │
└─────────┘ └─────────┘
Apache 2.0