Extract a Knowledge Graph From a Document

Pulls the central entities and their relations out of a document, with a one-sentence description for each entity that later helps tell apart similar names.

  • 🤖 Claude
  • 🗂️ Data

Last verified: write-up checked

Prompt by Anthropic (anthropics/claude-cookbooks) · Original source (opens in a new tab)

Extract a Knowledge Graph From a Document — prompt preview

The prompt

prompt
Extract a knowledge graph from the document below.

<document>
{text}
</document>

Guidelines:
- Extract only entities that are central to what this document is about — skip incidental mentions.
- For each entity, write a one-sentence description grounded in this document. These descriptions are used later to disambiguate entities with similar names.
- Predicates should be short verb phrases ("commanded", "launched from", "part of").
- Every relation must connect two entities you extracted.

What it is

Pulls the central entities and their relations out of a document, with a one-sentence description for each entity that later helps tell apart similar names. It replaces separate entity tagging and relation classification with one model call per document. The prompt text comes from the Anthropic claude-cookbooks repository (MIT licence, Copyright (c) 2023 Anthropic). Only the code indentation around it was removed. Original author: Anthropic (anthropics/claude-cookbooks).

Who it's for

  • Data and RAG engineers

Requirements

Requirements

  • The source sends this with a structured-output schema (entities and relations) through client.messages.parse().

يستخرج الكيانات الأساسية والعلاقات بينها من مستند، مع وصف من جملة واحدة لكل كيان يساعد لاحقًا على التمييز بين الأسماء المتشابهة.

Examples

Documents used in the notebook

python
python
"Apollo program",
"Apollo 11",
"Neil Armstrong",
"Saturn V",
"Buzz Aldrin",
"Kennedy Space Center",

What it does: The six Wikipedia summaries the notebook extracts from.

Result per document (excerpt)

text
text
Apollo program              7 entities    6 relations
Apollo 11                   7 entities    6 relations
Neil Armstrong              3 entities    3 relations

What it does: Counts printed by the source notebook.

Tips

  • The source runs it once per document with structured outputs, then merges different forms of the same entity in a later step.