ComeçarComece de graça

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.

Este exercício faz parte do curso

Graph RAG with LangChain and Neo4j

Ver curso

Instruções do exercício

  • Update the LineOutput class to request a list of Line objects.
  • Define an LLM that generates structured outputs using the LineOutput class.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

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)
Editar e executar o código