开始使用免费开始使用

提供少样本示例

虽然结构化输出能描述您希望数据如何返回,但对于究竟该返回什么,仍然留有相当多的解释空间。

Field 类允许您提供示例值,帮助 LLM 理解良好的抽取结果应当是什么样子。这是一种用于结构化输出的少样本提示方式。

您将扩展 Character 类,加入用于唯一标识角色的 id 字段。您希望为模型提供两个可参考的示例:"romeo-montague""lady-capulet"

本练习是课程的一部分

使用 LangChain 和 Neo4j 的 Graph RAG

查看课程

练习说明

  • 指定用于为 id 属性提供示例值的关键字。

交互式实操练习

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

class Character(BaseModel):
	id: str = Field(
		description="A unique identifer consisting of the Character's given name and their family name in slug case.", 
		# Provide example values of an ID
		____=["romeo-montague", "lady-capulet"]
	)
	name: str = Field(description="The name of the character")

chain = prompt | llm.with_structured_output(Character)

chain.invoke({"text": """JULIET.
O Romeo, Romeo, wherefore art thou Romeo? Deny thy father and refuse thy name. Or if thou wilt not, be but sworn my love, And I’ll no longer be a Capulet."""})
编辑并运行代码