Working through this module?
Module 05 · Page 6 of 20 · 8 sections
Prompts and Prompt Templates
What prompts are, why templates help, the main template types, worked examples including few-shot, and prompt engineering tips.
What are Prompts?
- A prompt is the input instruction given to the LLM.
- It guides the model to generate the desired output.
- Good prompts lead to better, accurate and consistent responses.
- LangChain provides prompt templates to create dynamic and reusable prompts.
Why Use Prompt Templates?
- Reusable and maintainable prompts.
- Insert dynamic variables (e.g., user input).
- Separate prompt logic from code.
- Easy to manage complex prompts.
- Support different prompt types (chat, system, human, etc.).
- Helps in building structured and consistent LLM applications.
Types of Prompts in LangChain
| Type | Description | Use Case |
|---|---|---|
| PromptTemplate | Simple text prompt with variables | Basic Q&A, text generation |
| ChatPromptTemplate | Chat-based prompt with system, human, AI messages | Chat models (ChatOpenAI, ChatAnthropic, etc.) |
| MessagesPlaceholder | Placeholder for dynamic message history | Chat with conversation history |
| FewShotPromptTemplate | Use examples in the prompt | Few-shot learning |
| PipelinePromptTemplate | Combine multiple templates | Complex multi-step prompts |
PromptTemplate
python
from langchain_core.prompts import PromptTemplate template = PromptTemplate( input_variables=["topic"], template="Explain {topic} in simple words." ) prompt = template.format(topic="LangChain") print(prompt) # Output: Explain LangChain in simple words.
ChatPromptTemplate
python
from langchain_core.prompts import ChatPromptTemplate template = ChatPromptTemplate.from_messages([ ("system", "You are a helpful AI assistant."), ("human", "Explain {topic} in simple words.") ]) prompt = template.format_messages(topic="LangChain") # This creates formatted chat messages # to be used with ChatOpenAI or other chat models.
FewShotPromptTemplate
python
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate examples = [ {"input": "Python", "output": "A programming language."}, {"input": "LangChain", "output": "A framework to build LLM applications."} ] example_prompt = PromptTemplate( input_variables=["input", "output"], template="Input: {input}\nOutput: {output}" ) few_shot = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix="Answer the question:", suffix="Input: {question}\nOutput:", input_variables=["question"] )
Prompt Engineering Tips
- Be clear and specific in instructions.
- Provide examples (few-shot) when needed.
- Use a structured format (JSON, lists, etc.).
- Set a role using system prompts.
- Break complex tasks into smaller steps.
- Use constraints (e.g., length, tone, format).
- Experiment with different phrasings.
Example: Using a Prompt with an LLM
python
from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7) template = ChatPromptTemplate.from_messages([ ("human", "Explain {topic} in simple words.") ]) chain = template | llm response = chain.invoke({"topic": "LangChain"}) print(response.content)