모델 등록을 위한 POST 엔드포인트
앞서 만든 GET 엔드포인트는 기존 모델 정보를 조회하는 데 사용됩니다. 이번에는 권한이 있는 팀원이 새 모델을 등록하거나 기존 모델 정보를 업데이트할 수 있는 방법이 필요합니다.
팀원이 새 모델을 등록하거나 기존 모델을 업데이트할 수 있도록 POST 엔드포인트를 만들어 보세요.
이 엔드포인트는 모델 정보를 서버에 저장합니다.
이 연습은 강의의 일부입니다
FastAPI로 AI 프로덕션 배포하기
연습 안내
"/register-model"에POST요청 엔드포인트를 생성하세요.ModelInfoPydantic 모델을 사용하여 들어오는 모델 정보를 받고 유효성을 검사할 수 있도록 함수 매개변수model_info를 정의하세요.ModelInfo객체를 딕셔너리로 변환한 후model_db에 저장하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
model_db = {}
class ModelInfo(BaseModel):
model_id: int
model_name: str
description: str
# Create the POST request endpoint
@app.____("/register-model", status_code=201)
# Pass the model info from the request as function parameter
def register_model(____: ModelInfo):
# Add new model's information dictionary to the model database
model_db[model_info.model_id] = model_info.____()
return {"message": "Model registered successfully", "model": model_info}, 201