用于模型注册的 POST 端点
之前您创建的 GET 端点可用于检索已有模型的信息,但现在还需要一种方式,让已授权的团队成员可以注册新模型,或更新已有模型的信息。
请创建一个 POST 端点,允许团队成员注册新模型或更新现有模型。
该端点会在服务器上存储模型信息。
本练习是课程的一部分
使用 FastAPI 在生产环境中部署 AI
练习说明
- 在
"/register-model"创建一个POST请求端点。 - 定义函数参数
model_info,使用ModelInfoPydantic 模型来接收并校验传入的模型信息。 - 将
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