liveBook · Manning

Prompt engineering

As explained in Natural Language Processing in Action, Second Edition

Prompt engineering is the deliberate design and revision of prompts to influence a language model's behavior and outputs.

Prompt engineering is the deliberate revision of instructions and context given to a language model. It is useful when a generative chatbot needs to follow a conversation plan, but a well-written prompt is not a guarantee: the model may ignore an instruction, invent dialogue, or give a wrong answer. section 12.5.2

Keeping a tutor chatbot to one turn

Consider a math tutor that must establish whether it is speaking with a parent or a student and obtain a parent's permission before continuing over WhatsApp. An initial prompt describes that sequence and asks for the tutor's next response. The model nevertheless writes several turns ahead, including a parent's reply. A revision makes the boundary explicit: provide only one response, no more than one or two sentences, and proceed only after explicit permission. Tested replies then ask for permission and wait for the parent's answer. section 12.5.2

>>> onboarding_pt = """
          This is a conversation between a math tutor chatbot Rori
          and a user who might be a student in Africa or a parent.
          The chatbot introduces itself and asks if it's talking
          to a student or a parent.
          If the user is a parent, Rori asks the parent for
          permission for their child to use Rori over Whatsapp.
          If the user is a student, Rori asks the student to call
          their parents.
          Only if the parent gives explicit permission, Rori
          thanks them and asks to give the phone to the student.
          Provide the tutor's next response based on the conversation history.
          Provide only one response.
          Do not return more than one or two sentences.

          {history}
          user:{input}
          tutor:
          """

Inspect the revised prompt template: it combines the conversation rules, response-length constraints, conversation history, and the latest user input. section 12.5.2

Test the prompt against actual exchanges, not just its intended wording. In a number-sequence quiz, the tutor praises “12” as correct while also saying the next number after 2, 4, 6, 8 is 10. That contradiction makes prompt revision alone a poor substitute for checking answers. section 12.5.2

Prompts also have to contend with each model's quirks and guardrails, and users can phrase requests in unanticipated ways. For decisions such as choosing which model should receive a request, a semantic router can instead classify prompts using examples of requests that worked well. This separates a discrete routing decision from the generative model's claim that it can handle any request with the right prompt. section 10.1.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through prompt-template revisions and observed replies, then contrasts prompting with classification rather than specifying a universal prompt recipe.
Examples
Uses a parental-consent tutor chatbot, a number-sequence quiz, and prompts for detecting unusual requests.
What it assumes
Assumes familiarity with Python and language-model calls; demonstrates prompt templates through chatbot code rather than teaching Python from zero.
What you can do after
Apply it via LangChain prompt templates to revise chatbot responses, while testing outputs and reserving classification for routing decisions.
How it connects
Connects prompt templates and conversation memory to chatbot design, and contrasts prompting with semantic routing and intent classification.

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 Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.