Relationship extraction identifies entities in source text and describes semantic connections between related entity pairs. It turns unstructured passages into structured records suitable for graph construction. section 7.2.2
A practical extraction prompt specifies:
- Entity names, types, and comprehensive descriptions. section 7.2.2
- Source and target entities for every clearly related pair. section 7.2.2
- A relationship description and numeric strength score. section 7.2.2
- Delimited output that can be parsed and imported into a graph database. section 7.2.2
Listing 7.5 Entity and relationship extraction
ENTITY_TYPES = ["PERSON", "ORGANIZATION", "LOCATION",
"GOD", "EVENT", "CREATURE", "WEAPON_OR_TOOL"]
def extract_entities(text: str) -> List[Dict]: #1
messages = [
{"role": "user",
"content": ch07_tools.create_extraction_prompt(ENTITY_TYPES, text)}, #2
]
output = chat(messages, model = "gpt-4o") #3
return ch07_tools.parse_extraction_output(output) #4
The implementation calls an extraction prompt with configured entity types, sends the text to GPT-4o, and parses the structured response. section 7.2.2


