liveBook · Manning

Template

As explained in Natural Language Processing in Action, Second Edition

A prompt template is a reusable specification that inserts variable input into a predefined language-model prompt.

A prompt template is reusable wording with placeholders for changing input. Like a Python f-string, it lets a chatbot wrap a user’s message in consistent instructions instead of rebuilding the whole prompt each time. section 12.3 section 12.5.2

In LangChain, declare the placeholder names and template, then connect the prompt to an LLM chain. Calling the chain with a value such as message supplies that input. Templates can also include a conversation-history placeholder, but the template alone does not preserve history: memory must supply it. section 12.5.2 section 12.5.2

A math-tutor prompt

A template can describe Rori as a math tutor, set expectations for the reply, and include {message} where the user’s words belong. An onboarding version can add {chat_history} and instructions for asking a parent’s permission before a child uses the chatbot. section 12.5.2 section 12.5.2

A carefully worded template can guide a response, but it does not guarantee one: a chatbot may still answer inappropriately or fail to remember earlier turns without conversation memory. section 12.5.2 section 12.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines placeholders and demonstrates LangChain setup, use in a chain, and inclusion of conversation history.
Examples
Uses a math-tutor chatbot with message and chat-history placeholders, including parent-permission onboarding instructions.
What it assumes
Assumes familiarity with Python f-strings and introduces prompt templates through a first LangChain prompt.
What you can do after
Apply prompt templates with LangChain by declaring input variables and passing values to a chain.
How it connects
Connects templates to rule-based chatbots, Python f-strings, LangChain chains, large language models, and conversation memory.

Also explained in

Compare how 21 books explain template →

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.