liveBook · Manning

Prompt

As explained in Natural Language Processing in Action, Second Edition

A prompt is an input sequence of instructions or tokens supplied to a language model to guide its generated output.

A prompt is the input that guides a language model’s next response. In a chatbot, it can be a human directive; in an LLM chain, it is the tokens supplied to start generation. Unlike a fixed menu of replies, a prompt lets a generative chatbot respond to unexpected messages—but it does not make those responses predictable or reliable. section 12.4 section 12.5.2

A prompt template keeps reusable instructions separate from changing input. For a math tutor, a template can describe the conversation, insert a user’s message at `{message}`, and end where the chatbot should respond. `PromptTemplate` declares that variable; an `LLMChain` combines the template with an LLM so the application can call `chain.predict(message=...)` without rebuilding the whole prompt. section 12.5.2

>>> from langchain.prompts import PromptTemplate
>>> from langchain.chains import LLMChain
>>> template = """
...     This is a conversation between a math tutor
...     chatbot Rori and a user who might be a student
...     in Africa or a parent.
...
...     Human says: {message}
...     Chatbot responds:
...     """
>>> prompt = PromptTemplate(
...     input_variables = ["message"],     #1
...     template=template)
>>> chain = LLMChain(
...     llm=llm, verbose=True, prompt=prompt    #2
...     )

The Python listing shows a reusable tutor prompt with a `{message}` slot, passed to `PromptTemplate` and `LLMChain`. section 12.5.2

Wording matters, but it is not a guarantee. A tutor prompt produced a plausible greeting and then generated lines for the student too. A later call asking for the student’s name failed to recall it: ordinary LLM calls do not retain earlier conversation turns. To supply that context, record conversation history and include it as another template variable. 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:
          """

This revised template includes a `{history}` slot, onboarding instructions, and an explicit request for only one short response. section 12.5.2

A math answer that needs checking

A number-sequence prompt asked the tutor to compare a student’s answer with the next number. When the student answered “12” after 2, 4, 6, 8, the bot praised the answer as correct while saying the next number was 10. A detailed prompt did not make its evaluation dependable. section 12.5.2

Treat a prompt as one component of a chatbot, not its safety system or memory. Grounding, fine-tuning, guardrails, and checks such as intent recognition can help manage unhelpful or harmful replies; conversation history must be supplied when a reply depends on earlier turns. section 12.4 section 12.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines prompts as generation input and works through templates, variables, calls, and history; does not show prompts reliably controlling output.
Examples
Uses Python tutor templates, chatbot exchanges, parent onboarding, and a mistaken number-sequence evaluation.
What it assumes
Assumes basic Python and an initialized LLM; introduces prompt templates and chains through code.
What you can do after
Apply it via LangChain to template chatbot input and supply conversation history, while checking generated replies.
How it connects
Connects prompts to LLM chains, prompt templates, conversation memory, intent recognition, and guardrails.

Also explained in

Compare how 50 books explain prompt →

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.