用於模型註冊的 POST 端點
你先前建立的 GET 端點可讓使用者取得既有模型的資訊,但現在你需要一個方式,讓已授權的團隊成員可以註冊新模型,或更新現有模型的資訊。
你需要建立一個 POST 端點,讓團隊成員可以註冊新模型或更新既有模型。
此端點會在伺服器上儲存模型資訊。
本練習屬於課程
使用 FastAPI 將 AI 佈署到生產環境
練習說明
- 在
"/register-model"建立一個POST請求端點。 - 定義函式參數
model_info,並使用 Pydantic 的ModelInfo模型來接收與驗證傳入的模型資訊。 - 將
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