Requesting a structured output
When creating lexical graphs, you extracted the character name and the lines from the text using a text splitter, but this approach often results in lost context around the split boundaries. For example, the stage directions contained in the text can be used to determine who the character is speaking to at the time.
You'll use Character
class inside a new Line
class to describe the line being spoken, along with the speaker and a list of characters the line is being spoken to. You'll also create a wrapper LineOutput
class to extract multiple of these lines at once.
The prompt
variable containing extraction instructions has been defined for you, along with an instance of ChatOpenAI
as llm
.
This exercise is part of the course
Graph RAG with LangChain and Neo4j
Exercise instructions
- Update the
LineOutput
class to request a list ofLine
objects. - Define an LLM that generates structured outputs using the
LineOutput
class.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class Line(BaseModel):
text: str = Field(..., description="The text of the line")
spoken_by: Character = Field(..., description="The character who speaks the line")
spoken_to: Optional[list[Character]] = Field(None, description="The character who the line is spoken to")
class LineOutput(BaseModel):
# Request more than one line from the text
lines: ____[Line] = Field(..., description="The lines from the text")
# Return an instance of LineOutput
structured_llm = llm.____(____)
for line in structured_llm.invoke(prompt.format_messages(text=text)):
print(line)