0
0
LangChainframework~5 mins

ChatPromptTemplate for conversations in LangChain

Choose your learning style9 modes available
Introduction

A ChatPromptTemplate helps you create clear and organized messages for chatbots or AI conversations. It makes building conversations easier and more consistent.

When you want to build a chatbot that talks with users step-by-step.
When you need to organize different parts of a conversation like system instructions and user questions.
When you want to reuse conversation templates with different inputs.
When you want to keep your chat messages clear and easy to update.
When you want to combine multiple messages into one conversation flow.
Syntax
LangChain
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate

chat_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("{system_instructions}"),
    HumanMessagePromptTemplate.from_template("{user_input}")
])

Use from_messages to combine different message templates into one chat prompt.

Templates use curly braces {} to mark where input values go.

Examples
This creates a chat prompt with a system message and a fixed user message.
LangChain
chat_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("You are a helpful assistant."),
    HumanMessagePromptTemplate.from_template("Hello! Can you help me?")
])
This template lets you insert the city name dynamically when running the chat.
LangChain
chat_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("You are a friendly assistant."),
    HumanMessagePromptTemplate.from_template("What is the weather in {city}?")
])
Sample Program

This example creates a chat prompt template with a system message and a user message that asks about a topic. Then it fills in the topic and prints the conversation messages.

LangChain
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate

# Create a chat prompt template with placeholders
chat_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("You are a helpful assistant."),
    HumanMessagePromptTemplate.from_template("Tell me about {topic}.")
])

# Fill in the placeholder with actual input
filled_prompt = chat_prompt.format_prompt(topic="Python programming")

# Print the messages in the conversation
for message in filled_prompt.messages:
    print(f"{message.type}: {message.content}")
OutputSuccess
Important Notes

Always use format_prompt to fill in your template with real values before sending it to the AI.

You can add more message types like AI messages if needed for complex conversations.

Keep your templates simple and clear to avoid confusion in the conversation flow.

Summary

ChatPromptTemplate organizes chat messages into a clear conversation structure.

Use placeholders to insert dynamic user inputs easily.

This helps build reusable and consistent chatbots or AI conversations.