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".
Questo esercizio fa parte del corso
Graph RAG with LangChain and Neo4j
Istruzioni dell'esercizio
- Specify the keyword to provide example values for the
idproperty.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
class Character(BaseModel):
id: str = Field(
"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("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."""})