Working through this module?0 of 17 done
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.

5.1

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.
5.2

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.
5.3

Types of Prompts in LangChain

TypeDescriptionUse Case
PromptTemplateSimple text prompt with variablesBasic Q&A, text generation
ChatPromptTemplateChat-based prompt with system, human, AI messagesChat models (ChatOpenAI, ChatAnthropic, etc.)
MessagesPlaceholderPlaceholder for dynamic message historyChat with conversation history
FewShotPromptTemplateUse examples in the promptFew-shot learning
PipelinePromptTemplateCombine multiple templatesComplex multi-step prompts
5.4

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.
5.5

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.
5.6

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"]
)
5.7

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.
5.8

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)