liveBook · Manning

Prompt engineering

As explained in Knowledge Graphs and LLMs in Action

Prompt engineering is the deliberate design and iterative refinement of instructions and examples given to a language model to obtain desired outputs.

Prompt engineering deliberately designs and iteratively refines natural-language instructions and examples so a language model produces useful, task-specific outputs. In knowledge-graph construction, it formulates entity and relation extraction as text completion, often avoiding a separately engineered NLP pipeline. section 5.3.1 section 5.3.4

A reliable prompting workflow combines domain analysis, explicit output constraints, representative examples, and controlled evaluation:

  • Explore the data and anticipate domain-specific difficulties, such as names later appearing as initials, surnames, or pronouns. section 5.3.1
  • Describe the task precisely, and enumerate the entity and relation classes that matter. section 5.3.3
  • Add complex, representative input-output examples containing difficult linguistic forms and all important relation types. section 5.3.3
  • Iterate on wording, class names, model choice, and parameters; lower temperature favors deterministic output, while higher temperature increases variation and hallucination risk. section 5.3.1 section 5.3.3
  • Inspect outputs for inconsistent schemas, overly granular relations, hallucinations, and unresolved entities before generating or querying a knowledge graph. section 5.3 section 5.3.2

Normalizing entity and relation extraction

An initial prompt asks for every entity and relation, but produces variants such as “specializes in,” “is measuring,” and “demonstrates.” A revised prompt names target classes and prioritizes normalized relations such as “works on,” while specifying how initials and abbreviated titles should be resolved. The resulting output expands “Prof. Chem.” to “Professor of Chemistry” and links abbreviated references to full names, though it still requires validation.

Listing 5.1 Prompt version 1; identifying entities and relations
prompt_segments = dict()

prompt_segments['task'] =   #1
"""You are an expert on constructing Knowledge 
Graphs from texts using named entity recognition and relation extraction.
Given a prompt, identify as many entities and relations among them as 
possible and output a list of relations in the format [ENTITY 1, ENTITY 1 
TYPE, RELATION, ENTITY 2, ENTITY 2 TYPE]. 
The relations are directed, so the order matters."""

prompt_segments['example'] = "J.R.Smith (Prof. Phys.) is employed by MIT 
and mentioned another scientist, Mary Hodge, who works on cyclotron 
research."   #2

prompt_segments['example_output'] =   #3
"""["J. R. Smith", "person", "has 
title", "Professor of Physics", "title"]
["J. R. Smith", "person", "works for", "MIT", "organization"]
["J. R. Smith", "person", "talked about", "Mary", "person"] 
["Mary", "person", "works on", "cyclotron", "occupation"]"""

Use the listing to inspect a concrete prompt assembled from a task description, an example, and an expected output format. section 5.3.2

Prompt engineering is not a guarantee of correctness. If prompting remains insufficient for a complex task, expected outputs can seed a human-annotated training set for fine-tuning. Later systems may also use chain-of-thought or scratchpad prompting to encourage intermediate reasoning before a final answer. section 5.3.1 section 14.7

How Knowledge Graphs and LLMs in Action covers it

How deep it goes
Explains iterative design, class naming, examples, configuration, and failure modes, but defers model internals and detailed fine-tuning.
Examples
Demonstrates diary-entry extraction, abbreviated names and titles, normalized relations, OpenAI API calls, and chain-of-thought prompting.
What it assumes
Assumes data exploration and basic task knowledge, while enabling practitioners with minimal technical skills to begin without specialized model training.
What you can do after
Build it from scratch by iterating prompts, selecting examples and parameters, and evaluating structured LLM outputs for a domain task.
How it connects
Connects large language models with knowledge graphs, named entity recognition, relation extraction, coreference resolution, and fine-tuning.

Also explained in

Compare how 38 books explain prompt engineering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Knowledge Graphs and LLMs in Action. The section links point to the parts of the book it draws on.