Get startedGet started for free

Using extracted entities

The Dramatis Personæ section of the text describes the characters in the play and can be considered a comprehensive source of truth. This information also holds rich information on the personal relationships between the characters.

 Dramatis Personæ

ESCALUS, Prince of Verona.
MERCUTIO, kinsman to the Prince, and friend to Romeo.
PARIS, a young Nobleman, kinsman to the Prince.
Page to Paris.

Complete the code to extract the characters from the personae text using the character_chain, and then use the list of characters to build a list of relationships from the text using relationship_chain.

The visualize_families() function will display the resulting relationships in a graph.

This exercise is part of the course

Graph RAG with LangChain and Neo4j

View Course

Exercise instructions

  • Invoke character_chain, passing it personae to extract the character entities.
  • Invoke the relationship_chain on the character name, personae, and the extracted characters in JSON format (character_json) to extract the relationships between the three characters listed and the characters listed in personae.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Extract characters from the personae variable
output = character_chain.____({"personae": ____})

character_json = dumps([dict(character) for character in output.characters])

# Extract the relationships
for character_name in ["Romeo", "Juliet", "Friar Lawrence"]:
    relationship_output = relationship_chain.invoke({
        "character_name": character_name, 
        "personae": ____, 
        "characters": ____
    })
    
    for relationship in relationship_output.relationships:
        all_relationships.append(relationship)

visualize_families(all_relationships, output)
Edit and Run Code