使用说明
婚纱推荐API
基于AI的婚纱推荐服务
利用OpenAI GPT-4,推荐适合新娘体型的最佳礼服。
🏗️ 体系结构
系统结构
┌──────────────┐
│ Client │ (HTTP)
└──────┬───────┘
│
↓
┌─────────────────────────────────────┐
│ API Layer (FastAPI) │
│ └─ Routes (recommend, stats) │
└──────┬──────────────────────────────┘
│
↓
┌─────────────────────────────────────┐
│ Services Layer │
│ ├─ Recommender (AI Logic) │
│ └─ Schemas (Pydantic) │
└──────┬──────────────────────────────┘
│
┌──────┴──────┬──────────────┐
│ Database │ Config │
│ Layer │ Layer │
├─────────────┼──────────────┤
│ • Session │ • Settings │
│ • Models │ • Redis │
│ • Repos │ │
└─────────────┴──────────────┘
│
↓
┌──────────────┐ ┌──────────────┐
│ MySQL │ │ Redis │
│ (L2 Cache) │ │ (L1 Cache) │
└──────────────┘ └──────────────┘
层说明
API层 (src/api/)
- 基于FastAPI的REST API
- 验证请求并格式化响应
- 路线:推荐,统计数据
服务层 (src/services/)
- 业务逻辑(AI推荐引擎)
- 定义Pydantic模式
- 域模型
数据库层 (src/database/)
- SQLAlchemy会话管理
- 定义数据库模型
- Repository模式(数据访问)
配置层 (src/config/)
- 管理首选参数
- Redis客户端
📁 项目结构
wedding-dress-api/
├── src/
│ ├── api/ # FastAPI REST API
│ │ ├── main.py # FastAPI app
│ │ └── routes/ # API routes
│ │ ├── recommend.py # 드레스 추천 엔드포인트
│ │ └── stats.py # 통계 엔드포인트
│ │
│ ├── services/ # 비즈니스 로직
│ │ ├── schemas.py # Pydantic 스키마
│ │ └── recommender.py # AI 추천 엔진
│ │
│ ├── database/ # 데이터베이스
│ │ ├── session.py # DB 세션
│ │ ├── models.py # SQLAlchemy 모델
│ │ └── repositories/ # Repository 패턴
│ │ └── recommendation.py
│ │
│ └── config/ # 설정
│ ├── settings.py # 환경 설정
│ └── redis.py # Redis 클라이언트
│
├── scripts/ # 유틸리티
│ └── init.sql # DB 초기화 스크립트
│
├── Dockerfile # Docker 이미지
├── docker-compose.yaml # 로컬 개발 환경
├── requirements.txt # Python 의존성
└── README.md
前提条件
- Python 3.11+
- Docker&Docker编写
- OpenAI API密钥
# 1. 환경 변수 설정
cp .env.example .env
# .env 파일에서 OPENAI_API_KEY와 MYSQL_PASSWORD 설정
# 2. 서비스 시작
docker-compose up -d
# 3. 로그 확인
docker-compose logs -f api
# 4. 테스트
curl http://localhost:18000/health
📡 用法
REST API
推荐连衣裙(基本3件)
curl -X POST http://localhost:18000/recommend \
-H "Content-Type: application/json" \
-d '{
"arm_length": "보통",
"leg_length": "긴",
"neck_length": "보통",
"face_shape": "달걀"
}'
更改推荐数量(1-5个)
# 1개만 추천받기
curl -X POST http://localhost:18000/recommend \
-H "Content-Type: application/json" \
-d '{
"arm_length": "보통",
"leg_length": "긴",
"neck_length": "보통",
"face_shape": "달걀",
"num_recommendations": 1
}'
# 5개 추천받기
curl -X POST http://localhost:18000/recommend \
-H "Content-Type: application/json" \
-d '{
"arm_length": "보통",
"leg_length": "긴",
"neck_length": "보통",
"face_shape": "달걀",
"num_recommendations": 5
}'
响应示例
{
"request_params": {
"arm_length": "보통",
"leg_length": "긴",
"neck_length": "보통",
"face_shape": "달걀"
},
"recommendations": [
{
"style_name": "A라인 드레스",
"description": "상체는 슬림하게, 하체는 풍성하게...",
"why_recommended": "긴 다리를 돋보이게 하며...",
"styling_tips": [
"하이힐을 신어 다리 라인을 더욱 강조",
"심플한 목걸이로 얼굴형을 살림"
]
}
],
"overall_advice": "달걀형 얼굴과 긴 다리를 가진 신부님께는...",
"cached": false,
"source": "ai_generated"
}
API端点
| 方法 | 路径 | 说明 |
|---|---|---|
| 得到 | /health |
详细健康检查 |
| 职位 | /recommend |
推荐礼服(主要) |
输入参数
| 参数 | 选项 | 组合 |
|---|---|---|
| arm_length | 短,中,长3 | |
| leg_length | 短,中,长3 | |
| neck_length短、中、长3 | ||
| face_shape | 鸡蛋,宽,棱角,长4 | |
| num_recommensions | 1,2,3,4,5(默认值:3) | 5 |
总组合: 3 × 3 × 3 × 4 × 5 = 540种
2层缓存
级别存储速度用途 |------|--------|------|------| L1Redis~1ms超高速响应 |L2|MYS|~10ms永久存储+统计|
流:
- Redis Hit→立即返回
- Redis Miss→Mysql Hit→保存Redis→返回
- SQL Miss→AI创建→两边保存→返回