开始使用免费开始使用

每次都得到一致的输出!

您正在为一家流媒体平台开发个性化的电影推荐系统。为确保推荐内容能够正确显示在应用的 UI 中,您需要结合使用 pydanticOpenAI 客户端生成结构化输出。您将定义一个电影推荐的架构,并提取结构化结果。

本练习是课程的一部分

使用 OpenAI Responses API

查看课程

练习说明

  • 定义一个名为 MovieRecommendationpydantic 类,包含 titlegenrevibewhy 字段。
  • 使用提供的提示和 MovieRecommendation 类生成结构化的推荐结果。
  • 从响应中提取已解析的推荐,然后访问其中的 titlewhy 信息。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Define the book recommendation schema
class ____(BaseModel):
    ____: str = Field(description="The book title")
    ____: str = Field(description="Primary genre")
    ____: str = Field(description="One-word vibe: cozy, thrilling, emotional, or fun")
    ____: str = Field(description="One sentence explaining why this matches")

# Generate structured recommendation
response = client.responses.____(
    model="gpt-5.4-mini",
    instructions="You are a knowledgeable movie recommender.",
    input="Recommend a movie for someone who loved Inception and wants something mind-bending",
    text_format=____,
)

# Extract the parsed output and results
recommendation = response.____
print(f"Title: {recommendation.____}")
print(f"Reason: {recommendation.____}")
编辑并运行代码