MinAI - Về trang chủ
Dự án
12/1360 phút
Đang tải...

Capstone Project - Full Deployment

Deploy AI application hoan chinh voi FastAPI, Docker, monitoring va security

0

🎯 Mục tiêu bài học

TB5 min

Tổng hợp tất cả kiến thức từ course: deploy một AI application production-ready hoàn chỉnh.

Sau bài này, bạn sẽ:

✅ Xây dựng FastAPI backend với AI endpoints hoàn chỉnh ✅ Dockerize toàn bộ stack với Docker Compose ✅ Setup monitoring với Prometheus và Grafana ✅ Implement security với auth, rate limiting, input validation ✅ Viết tests và documentation cho project

1

🔍 Project Overview

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

Bạn đã hiểu kiến trúc tổng thể của capstone project chưa?

2

📝 Project Requirements

TB5 min
Yêu cầu Project

Core Features:

  • FastAPI backend với AI endpoints
  • Chat endpoint với streaming response
  • Document Q and A với RAG
  • Response caching với Redis

Infrastructure:

  • Dockerized với docker-compose
  • Nginx reverse proxy với SSL
  • Health checks và auto-restart

Monitoring:

  • Prometheus metrics collection
  • Grafana dashboard
  • LangSmith tracing
  • Cost tracking

Security:

  • API key authentication
  • Rate limiting
  • Input validation va sanitization
  • CORS configuration

Checkpoint

Bạn đã nắm được đầy đủ requirements cho project chưa?

3

🛠️ Project Structure

TB5 min
Bash
1ai-deployment-capstone/
2├── docker-compose.yml
3├── .env.example
4├── nginx/
5│ └── nginx.conf
6├── app/
7│ ├── Dockerfile
8│ ├── requirements.txt
9│ ├── main.py
10│ ├── config.py
11│ ├── auth.py
12│ ├── middleware.py
13│ ├── routes/
14│ │ ├── chat.py
15│ │ └── health.py
16│ └── services/
17│ ├── ai_service.py
18│ └── cache_service.py
19├── monitoring/
20│ ├── prometheus.yml
21│ └── grafana/
22│ └── dashboard.json
23└── tests/
24 ├── test_chat.py
25 └── test_health.py

Checkpoint

Bạn đã tạo được project structure hoàn chỉnh chưa?

4

💻 FastAPI Application

TB5 min
python.py
1# app/main.py
2from fastapi import FastAPI
3from fastapi.middleware.cors import CORSMiddleware
4from contextlib import asynccontextmanager
5from app.middleware import RateLimitMiddleware, MetricsMiddleware
6from app.routes import chat, health
7from app.config import settings
8
9@asynccontextmanager
10async def lifespan(app: FastAPI):
11 # Startup
12 print("Starting AI service...")
13 yield
14 # Shutdown
15 print("Shutting down...")
16
17app = FastAPI(
18 title="AI Deployment Capstone",
19 version="1.0.0",
20 lifespan=lifespan
21)
22
23app.add_middleware(
24 CORSMiddleware,
25 allow_origins=settings.CORS_ORIGINS,
26 allow_methods=["*"],
27 allow_headers=["*"],
28)
29app.add_middleware(RateLimitMiddleware, max_requests=60)
30app.add_middleware(MetricsMiddleware)
31
32app.include_router(health.router, tags=["health"])
33app.include_router(chat.router, prefix="/api/v1", tags=["chat"])
python.py
1# app/config.py
2from pydantic_settings import BaseSettings
3
4class Settings(BaseSettings):
5 OPENAI_API_KEY: str
6 REDIS_URL: str = "redis://redis:6379"
7 LANGSMITH_API_KEY: str = ""
8 API_KEYS: list[str] = []
9 CORS_ORIGINS: list[str] = ["http://localhost:3000"]
10 RATE_LIMIT: int = 60
11 DAILY_BUDGET: float = 10.0
12
13 class Config:
14 env_file = ".env"
15
16settings = Settings()

Checkpoint

Bạn đã setup được FastAPI application với config và middleware chưa?

5

💻 AI Service

TB5 min
python.py
1# app/services/ai_service.py
2from langchain_openai import ChatOpenAI
3from langchain_core.prompts import ChatPromptTemplate
4from langchain_core.output_parsers import StrOutputParser
5from app.services.cache_service import CacheService
6
7class AIService:
8 def __init__(self):
9 self.llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
10 self.cache = CacheService()
11
12 self.chat_prompt = ChatPromptTemplate.from_messages([
13 ("system", "You are a helpful AI assistant."),
14 ("human", "{message}")
15 ])
16
17 self.chain = self.chat_prompt | self.llm | StrOutputParser()
18
19 async def chat(self, message: str) -> str:
20 # Check cache
21 cached = await self.cache.get(message)
22 if cached:
23 return cached
24
25 # Invoke chain
26 result = await self.chain.ainvoke({"message": message})
27
28 # Cache result
29 await self.cache.set(message, result)
30
31 return result
32
33 async def stream_chat(self, message: str):
34 async for chunk in self.chain.astream({"message": message}):
35 yield chunk

Checkpoint

Bạn đã xây dựng được AI Service với caching và streaming chưa?

6

🐳 Docker Compose

TB5 min
yaml
1# docker-compose.yml
2services:
3 app:
4 build: ./app
5 ports:
6 - "8000:8000"
7 env_file: .env
8 depends_on:
9 redis:
10 condition: service_healthy
11 healthcheck:
12 test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
13 interval: 30s
14 timeout: 10s
15 retries: 3
16 restart: unless-stopped
17
18 redis:
19 image: redis:7-alpine
20 ports:
21 - "6379:6379"
22 healthcheck:
23 test: ["CMD", "redis-cli", "ping"]
24 interval: 10s
25 volumes:
26 - redis_data:/data
27
28 nginx:
29 image: nginx:alpine
30 ports:
31 - "80:80"
32 volumes:
33 - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
34 depends_on:
35 - app
36
37 prometheus:
38 image: prom/prometheus
39 ports:
40 - "9090:9090"
41 volumes:
42 - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml
43
44 grafana:
45 image: grafana/grafana
46 ports:
47 - "3001:3000"
48 environment:
49 - GF_SECURITY_ADMIN_PASSWORD=admin
50
51volumes:
52 redis_data:

Checkpoint

Bạn đã viết được Docker Compose file hoàn chỉnh chưa?

7

💻 Dockerfile

TB5 min
dockerfile
1# app/Dockerfile
2FROM python:3.11-slim
3
4WORKDIR /app
5
6COPY requirements.txt .
7RUN pip install --no-cache-dir -r requirements.txt
8
9COPY . .
10
11EXPOSE 8000
12
13CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Checkpoint

Bạn đã viết được Dockerfile cho AI application chưa?

8

🧪 Testing

TB5 min
python.py
1# tests/test_chat.py
2import pytest
3from httpx import AsyncClient, ASGITransport
4from app.main import app
5
6@pytest.mark.asyncio
7async def test_chat_endpoint():
8 transport = ASGITransport(app=app)
9 async with AsyncClient(transport=transport, base_url="http://test") as ac:
10 response = await ac.post(
11 "/api/v1/chat",
12 json={"message": "Hello"},
13 headers={"X-API-Key": "test-key"}
14 )
15 assert response.status_code == 200
16 assert "response" in response.json()
17
18@pytest.mark.asyncio
19async def test_health_check():
20 transport = ASGITransport(app=app)
21 async with AsyncClient(transport=transport, base_url="http://test") as ac:
22 response = await ac.get("/health")
23 assert response.status_code == 200
24 assert response.json()["status"] == "healthy"

Checkpoint

Bạn đã viết được tests cho các endpoints chưa?

9

📊 Evaluation Criteria

TB5 min
Grading Rubric
CriteriaPoints
FastAPI app chạy đúng với endpoints20
Docker Compose setup hoàn chỉnh20
Caching với Redis15
Monitoring (Prometheus + Grafana)15
Security (auth, rate limit, validation)15
Testing (unit + integration)10
Documentation5
Total100

Checkpoint

Bạn đã xem qua evaluation criteria và biết cần hoàn thành những gì chưa?

10

🎯 Tổng kết

TB5 min

Submission Checklist

Trước khi nộp
  1. Tất cả services start thành công với docker compose up
  2. API endpoints trả về responses đúng
  3. Caching hoạt động (check Redis)
  4. Grafana dashboard hiển thị metrics
  5. Rate limiting chặn excessive requests
  6. Tests pass: pytest tests/
  7. README với setup instructions

Câu hỏi tự kiểm tra

  1. Trong capstone project, làm thế nào để kết hợp FastAPI, Docker Compose, Redis caching và monitoring (Prometheus + Grafana) thành một hệ thống hoàn chỉnh?
  2. Security checklist cho production AI API bao gồm những gì (auth, rate limiting, input validation)? Tại sao mỗi yếu tố đều quan trọng?
  3. Giải thích quy trình testing cho AI application: unit tests, integration tests và health checks cần cover những gì?
  4. Evaluation criteria nào quan trọng nhất khi đánh giá một production-ready AI deployment và tại sao?

🎉 Chúc mừng! Bạn đã hoàn thành bài học Capstone Project và toàn bộ khóa học GenAI Deployment & MLOps!

Bạn cũng đã hoàn thành toàn bộ GenAI Coding Pathway — từ những kiến thức nền tảng về Text Generation, qua LangChain, đến Deployment & MLOps. Bạn giờ đây có đủ kỹ năng để xây dựng, triển khai và vận hành AI applications trong production. Hãy tiếp tục xây dựng các dự án thực tế để củng cố kiến thức! 🚀


Quay lại: Trang khóa học