1. Learn
  2. /
  3. Courses
  4. /
  5. Graph RAG with LangChain and Neo4j

Connected

Exercise

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.

Instructions

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