Providing few-shot examples
While structured outputs describe how you would like the data to be returned, it still leaves a lot open to interpretation when it comes to what should be returned.
The Field class allows you to provide example values that give the LLM information on what a good extraction looks like. This is an example of few-shot prompting for structured outputs.
You'll extend the Character class to contain an id field for uniquely identifying characters. You want to provide the model with two examples for it to draw from: "romeo-montague" and "lady-capulet".
Latihan ini merupakan bagian dari kursus
Graph RAG with LangChain and Neo4j
Instruksi latihan
- Specify the keyword to provide example values for the
idproperty.
Latihan interaktif langsung praktik
Cobalah latihan ini dengan melengkapi kode contoh ini.
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."""})