A prompt template is reusable text with placeholders for parts that change. Replace the placeholders with actual values to produce a prompt for a language model. This is useful when the task stays the same but each input differs—for example, classifying many customer reviews. section 1.4.1 section 4.2.2
A practical template separates three jobs:
- Give the model relevant context, such as what product is being reviewed or how a database is structured. section 1.4.1 section 1.4.2
- State the task and supply placeholders for changing inputs, such as [Review] or [Question]. A template can have more than one placeholder. section 4.2.2 section 4.3.2
- Specify the expected output. Requiring the same classification labels for every review makes the results easier to aggregate. section 4.2.2
Listing 1.2 Prompt template for classifying laptop reviews
We are considering product reviews for laptops. #1 For each review, output "satisfied" or "dissatisfied", depending on whether the customer is satisfied with the product or not. #2 Examples: This is a great laptop! I recommend everyone to buy it! satisfied #3 This laptop did not work. I had to return it. dissatisfied #4 [ReviewText] #5
In this laptop-review template, the instructions and two labeled examples stay fixed. Substitute each new review for [ReviewText] to request a “satisfied” or “dissatisfied” result. section 1.4.1
Build prompts in Python
For movie-review classification, a create_prompt(text) function can combine the review, the question “Is the sentiment positive or negative?”, and the required answers “Positive” or “Negative”. Calling it with different reviews produces prompts with the same task and output format. For email extraction, a similar function accepts both source text and a list of attributes, then specifies a table format and a value to use when an attribute is unavailable. section 4.2.2 section 4.3.2
Templates can also ask a model to write a query rather than analyze data directly. Keep the database description and translation instruction fixed, substitute a user question, then send the resulting prompt to the model. A SQL template can include table definitions; a Cypher template can include node labels, relationship types, and properties. Change that hardcoded description when changing databases. section 5.2.4 section 5.4.5
Treat the template as something to test, not a guarantee of a good answer: different wording can produce different results. When building a chat-model application with LangChain, ChatPromptTemplate provides a dedicated way to instantiate templates; its output can then pass to a model and an output parser. section 1.5.3 section 10.2.1 section 10.2.2




















