创建结构化输出
结构化输出是从大型语言模型中提取一致结果的关键,这对许多应用都至关重要。
在本练习中,您将使用 pydantic 库,通过定义一个 Character 类来描述《罗密欧与朱丽叶》中的人物属性。接下来的练习中,您将用它从剧本片段中提取人物,并以结构化输出的形式返回。
本练习是课程的一部分
使用 LangChain 和 Neo4j 的 Graph RAG
练习说明
- 创建一个
Character类,使其继承自合适的pydantic基类。 - 定义类的
name属性。 - 定义一个可选的
family属性,以覆盖家族不明的人物。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from pydantic import BaseModel, Field
from typing import Optional
# Inherit the correct pydantic class
class Character(____):
# Describe the name property
name: str = ____(description="The full name of the character")
# Set family to optional
____: Optional[str] = Field(description="The name of the family they belong to")