Skip to content

Prompt Tutorials 2 min read

Knowledge Graph Triplet Mining: Engineering Prompts for High-Precision Entity-Relation Extraction

How to extract canonical Subject-Predicate-Object triplets from unstructured text to build enterprise knowledge graphs with zero schema drift.

Della Reno Rinaldi Founder of renodotdev · Knowledge Engineers, NLP Specialists & Graph Database Architects

The Messy Reality of Unstructured Text Extraction

Building an enterprise Knowledge Graph requires extracting structured Subject-Predicate-Object (SPO) triplets from raw text:

$$\text{(Acme Corp)} \xrightarrow{\text{ACQUIRED}} \text{(Beta Tech)}$$

When prompt instructions are ambiguous, models invent hundreds of non-standard relation predicates:

  • Model A outputs: (Acme, bought, Beta)
  • Model B outputs: (Acme, took_over, Beta)
  • Model C outputs: (Acme, finalized_purchase_of, Beta)

This semantic fragmentation destroys knowledge graph traversal. Graph algorithms cannot compute connected components or shortest paths when the same relationship is expressed across 30 different arbitrary synonyms.

High-Precision Triplet Mining constrains extraction to an explicit Ontological Schema.


1. The Closed-World Ontological Prompt

Provide the model with an explicit, finite enum list of permitted relation types:

<ontology_specification>
  <entity_types>
    - ORGANIZATION (companies, non-profits, government agencies)
    - PERSON (executives, founders, researchers)
    - PRODUCT (software platforms, hardware devices, drugs)
    - JURISDICTION (countries, states, economic zones)
  </entity_types>

  <permitted_predicates>
    - FOUNDED_BY (Subject: ORGANIZATION, Object: PERSON)
    - ACQUIRED (Subject: ORGANIZATION, Object: ORGANIZATION)
    - MANUFACTURES (Subject: ORGANIZATION, Object: PRODUCT)
    - HEADQUARTERED_IN (Subject: ORGANIZATION, Object: JURISDICTION)
    - REGULATED_BY (Subject: ORGANIZATION, Object: JURISDICTION)
  </permitted_predicates>

  <negative_constraints>
    Do NOT invent predicates outside <permitted_predicates>.
    If a relationship does not fit the ontology, omit it.
  </negative_constraints>
</ontology_specification>

2. Implementing Triplet Extraction with Structured Zod Schemas

// graph/extract-triplets.ts
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const TripletSchema = z.object({
  triplets: z.array(z.object({
    subject: z.string().describe("Canonicalized entity name e.g. 'Apple Inc.'"),
    subjectType: z.enum(['ORGANIZATION', 'PERSON', 'PRODUCT', 'JURISDICTION']),
    predicate: z.enum([
      'FOUNDED_BY',
      'ACQUIRED',
      'MANUFACTURES',
      'HEADQUARTERED_IN',
      'REGULATED_BY',
    ]),
    object: z.string(),
    objectType: z.enum(['ORGANIZATION', 'PERSON', 'PRODUCT', 'JURISDICTION']),
    confidenceScore: z.number().min(0).max(1),
  })),
});

export async function extractKnowledgeGraphTriplets(rawArticleText: string) {
  const result = await generateObject({
    model: openai('gpt-4o'),
    schema: TripletSchema,
    system: 'Extract knowledge graph triplets strictly matching the provided ontological schema.',
    prompt: rawArticleText,
  });

  return result.object.triplets;
}

3. Ingestion into Graph Databases (Neo4j / Memgraph)

Once triplets are extracted with canonicalized predicates, generating Cypher queries for Neo4j is completely deterministic:

// Auto-generated idempotent graph insertion
MERGE (s:Organization {name: $subject})
MERGE (o:Jurisdiction {name: $object})
MERGE (s)-[r:HEADQUARTERED_IN]->(o)
SET r.extracted_at = timestamp();

4. Key Takeaways

  • Enforce an Ontological Closed World: Constrain predicates to an explicit enum list to prevent graph fragmentation.
  • Canonicalize Entity Names: Use entity resolution prompts to map synonyms (“Apple”, “Apple Computers”, “AAPL”) to a single canonical node ID.
  • Filter by Confidence: Discard triplets with confidence scores below 0.85 to keep the knowledge graph clean.

Written by Della Reno Rinaldi

Founder of renodotdev and Sobatoko. Building mobile and web apps since 2016, previously at Astro, Opaper and The Edge Singapore.

Have a project with similar challenges?

I build mobile apps, websites and AI features for businesses and startups. You work with me directly.