Skip to content

Chat Prompts - Mastering Conversational AI Templates

Learn to create sophisticated conversation templates with roles, context, and dynamic interactions for chatbots and conversational AI systems

💬 Understanding Chat Prompts

Chat prompts are structured templates designed for conversational AI models that work with messages rather than simple text completion. Unlike basic prompts, chat prompts use roles (system, human, assistant) to create more natural and controlled conversations.

🎭 Chat Message Roles

text
                    🎭 CHAT MESSAGE ROLES SYSTEM 🎭
                      (How conversations are structured)

    ┌─────────────────────────────────────────────────────────────────┐
    │                    SYSTEM MESSAGE                               │
    │                  (Sets behavior & context)                     │
    │                                                                │
    │  "You are a helpful Python tutor. Explain concepts clearly     │
    │   with examples and encourage learning."                       │
    └─────────────────────┬───────────────────────────────────────────┘

    ┌────────────────────▼────────────────────┐
    │           CONVERSATION FLOW             │
    │          (Back and forth dialog)        │
    └──┬────────────────────────────────────┬─┘
       │                                    │
       ▼                                    ▼
  ┌─────────────────────┐        ┌─────────────────────┐
  │   HUMAN MESSAGE     │   ◄──► │  ASSISTANT MESSAGE  │
  │   (User input)      │        │   (AI response)     │
  │                     │        │                     │
  │ "How do I create    │        │ "You can create a   │
  │  a list in Python?" │        │  list using [...]"  │
  └─────────────────────┘        └─────────────────────┘

🏗️ Basic Chat Prompt Construction

📝 Simple Chat Templates

python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import SystemMessage, HumanMessage

# Basic chat template
basic_chat = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant that explains complex topics simply."),
    ("human", "Explain {topic} in simple terms")
])

# Format the template
formatted = basic_chat.format_messages(topic="machine learning")
print(formatted)

# Alternative syntax using tuples
simple_template = ChatPromptTemplate.from_template(
    "You are an expert {role}. Help the user with their {task}."
)

🎯 Multi-Turn Conversations

python
# Template for ongoing conversations
conversation_template = ChatPromptTemplate.from_messages([
    ("system", "You are {persona}. {behavior_instructions}"),
    ("human", "{initial_question}"),
    ("assistant", "{assistant_response}"),
    ("human", "{follow_up_question}")
])

# Example usage
formatted_conversation = conversation_template.format_messages(
    persona="a patient Python teacher",
    behavior_instructions="Always provide code examples and explain step by step",
    initial_question="How do I use loops in Python?",
    assistant_response="Python has two main types of loops: for loops and while loops...",
    follow_up_question="Can you show me a practical example with a for loop?"
)

🔄 Dynamic Message Building

python
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage

class ConversationBuilder:
    def __init__(self, system_prompt: str):
        self.messages = [SystemMessage(content=system_prompt)]
    
    def add_human_message(self, content: str):
        self.messages.append(HumanMessage(content=content))
        return self
    
    def add_ai_message(self, content: str):
        self.messages.append(AIMessage(content=content))
        return self
    
    def get_template(self):
        return ChatPromptTemplate.from_messages(self.messages)

# Usage
builder = ConversationBuilder("You are a coding mentor.")
template = (builder
    .add_human_message("I want to learn about {programming_concept}")
    .add_ai_message("Great choice! Let me explain {programming_concept}...")
    .add_human_message("Can you show me a practical example?")
    .get_template())

🎨 Advanced Chat Prompt Patterns

🧠 System Message Strategies

python
# Persona-based system messages
personas = {
    "tutor": ChatPromptTemplate.from_messages([
        ("system", """You are an expert programming tutor with 10 years of experience.
        Your teaching style:
        - Break complex concepts into simple steps
        - Provide practical examples for everything
        - Encourage questions and experimentation
        - Use analogies to explain difficult concepts
        - Always be patient and supportive"""),
        ("human", "{question}")
    ]),
    
    "analyst": ChatPromptTemplate.from_messages([
        ("system", """You are a senior data analyst with expertise in:
        - Statistical analysis and interpretation
        - Data visualization best practices
        - Business intelligence and insights
        - Clear communication of technical findings
        
        Always:
        - Ask clarifying questions when needed
        - Provide data-driven recommendations
        - Explain your reasoning step by step"""),
        ("human", "{analysis_request}")
    ]),
    
    "debugger": ChatPromptTemplate.from_messages([
        ("system", """You are an expert code debugger and problem solver.
        Your approach:
        1. Carefully analyze the provided code
        2. Identify potential issues systematically
        3. Explain what went wrong and why
        4. Provide corrected code with explanations
        5. Suggest best practices to prevent similar issues"""),
        ("human", "Here's my code that isn't working: {code_snippet}")
    ])
}

# Use specific persona
tutor_template = personas["tutor"]
response = tutor_template.format_messages(
    question="How do list comprehensions work in Python?"
)

🎯 Context-Aware Templates

python
# Template with contextual awareness
context_aware_template = ChatPromptTemplate.from_messages([
    ("system", """You are an AI assistant helping with {domain} questions.
    
    Current context:
    - User expertise level: {user_level}
    - Previous topic discussed: {previous_topic}
    - User's preferred learning style: {learning_style}
    
    Adapt your explanations accordingly."""),
    ("human", "{current_question}")
])

# Dynamic context injection
def create_contextual_response(user_profile: dict, question: str):
    return context_aware_template.format_messages(
        domain=user_profile.get("domain", "general programming"),
        user_level=user_profile.get("level", "beginner"),
        previous_topic=user_profile.get("last_topic", "none"),
        learning_style=user_profile.get("style", "examples-based"),
        current_question=question
    )

# Example usage
user_profile = {
    "domain": "machine learning",
    "level": "intermediate", 
    "last_topic": "neural networks",
    "style": "hands-on coding"
}

contextual_messages = create_contextual_response(
    user_profile, 
    "How do I implement backpropagation?"
)

🔄 Few-Shot Chat Examples

python
# Few-shot learning in chat format
few_shot_template = ChatPromptTemplate.from_messages([
    ("system", "You are a Python code explainer. For each code snippet, explain what it does line by line."),
    
    # Example 1
    ("human", "Explain this code: numbers = [1, 2, 3, 4, 5]"),
    ("assistant", "This line creates a list called 'numbers' containing five integers from 1 to 5."),
    
    # Example 2  
    ("human", "Explain this code: total = sum(numbers)"),
    ("assistant", "This line uses the built-in sum() function to add all numbers in the 'numbers' list and stores the result in a variable called 'total'."),
    
    # Example 3
    ("human", "Explain this code: print(f'Total: {total}')"),
    ("assistant", "This line uses an f-string to print the word 'Total:' followed by the value stored in the 'total' variable."),
    
    # New input
    ("human", "Explain this code: {code_to_explain}")
])

# Use the few-shot template
explanation = few_shot_template.format_messages(
    code_to_explain="average = total / len(numbers)"
)

🛠️ Specialized Chat Templates

🔍 Question-Answering Templates

python
# RAG-style Q&A template
qa_template = ChatPromptTemplate.from_messages([
    ("system", """You are a helpful assistant that answers questions based on provided context.
    
    Instructions:
    - Use ONLY the information provided in the context
    - If the answer isn't in the context, say "I don't have enough information"
    - Cite specific parts of the context when possible
    - Be concise but complete"""),
    ("human", """Context: {context}
    
    Question: {question}
    
    Please answer based on the context provided.""")
])

# Conversational Q&A with memory
conversational_qa = ChatPromptTemplate.from_messages([
    ("system", """You are an intelligent assistant with access to a knowledge base.
    Keep track of the conversation context and provide relevant follow-up insights."""),
    ("human", "Context from knowledge base: {context}"),
    ("human", "Previous conversation: {chat_history}"),
    ("human", "Current question: {question}")
])

🎭 Role-Playing Templates

python
# Interactive role-play scenarios
roleplay_templates = {
    "interview": ChatPromptTemplate.from_messages([
        ("system", """You are conducting a {job_type} interview.
        
        Your role:
        - Ask thoughtful, relevant questions
        - Evaluate responses professionally
        - Provide constructive feedback
        - Maintain a friendly but professional tone
        
        Interview focus: {focus_areas}"""),
        ("human", "I'm ready for the {job_type} interview. Please start.")
    ]),
    
    "mentor": ChatPromptTemplate.from_messages([
        ("system", """You are an experienced {field} mentor.
        
        Your mentoring style:
        - Ask probing questions to encourage deep thinking
        - Share relevant experiences and insights
        - Provide actionable advice
        - Help set realistic goals
        
        Mentee background: {mentee_background}"""),
        ("human", "{mentoring_question}")
    ]),
    
    "code_reviewer": ChatPromptTemplate.from_messages([
        ("system", """You are a senior software engineer doing code review.
        
        Review criteria:
        - Code quality and readability
        - Performance considerations
        - Security issues
        - Best practices adherence
        - Maintainability
        
        Programming language: {language}
        Project context: {project_context}"""),
        ("human", "Please review this code:\n\n{code_snippet}")
    ])
}

🧮 Structured Output Templates

python
# Templates for structured responses
structured_analysis = ChatPromptTemplate.from_messages([
    ("system", """You are a structured analyst. Always respond in the following format:

    ## Summary
    [Brief overview]

    ## Key Points
    - Point 1
    - Point 2
    - Point 3

    ## Recommendations
    1. First recommendation
    2. Second recommendation

    ## Next Steps
    [What to do next]"""),
    ("human", "Analyze: {topic}")
])

# JSON-formatted responses
json_response_template = ChatPromptTemplate.from_messages([
    ("system", """You are a data processor. Always respond with valid JSON in this format:
    {{
        "summary": "Brief summary",
        "confidence": 0.95,
        "categories": ["category1", "category2"],
        "recommendations": ["rec1", "rec2"],
        "metadata": {{
            "processed_at": "timestamp",
            "source": "analysis"
        }}
    }}"""),
    ("human", "Process this information: {data}")
])

🔧 Template Composition and Chaining

🔗 Composable Templates

python
# Base templates that can be combined
base_templates = {
    "personality": ("system", "You are {personality}. {personality_traits}"),
    "expertise": ("system", "You have expertise in {expertise_areas}."),
    "constraints": ("system", "Important constraints: {constraints}"),
    "format": ("system", "Always format your response as: {response_format}")
}

class ComposableTemplate:
    def __init__(self):
        self.messages = []
    
    def add_personality(self, personality: str, traits: str):
        template = base_templates["personality"]
        self.messages.append((template[0], template[1].format(
            personality=personality, 
            personality_traits=traits
        )))
        return self
    
    def add_expertise(self, areas: str):
        template = base_templates["expertise"]
        self.messages.append((template[0], template[1].format(
            expertise_areas=areas
        )))
        return self
    
    def add_constraints(self, constraints: str):
        template = base_templates["constraints"]
        self.messages.append((template[0], template[1].format(
            constraints=constraints
        )))
        return self
    
    def add_user_message(self, content: str):
        self.messages.append(("human", content))
        return self
    
    def build(self):
        return ChatPromptTemplate.from_messages(self.messages)

# Usage
complex_template = (ComposableTemplate()
    .add_personality("a patient teacher", "kind, encouraging, thorough")
    .add_expertise("Python programming, data science, machine learning")
    .add_constraints("Always provide working code examples")
    .add_user_message("Explain {concept} with examples")
    .build())

🔄 Template Inheritance

python
class BaseAssistantTemplate:
    def __init__(self, name: str, expertise: str):
        self.base_messages = [
            ("system", f"You are {name}, an expert in {expertise}.")
        ]
    
    def create_template(self, additional_instructions: str = ""):
        messages = self.base_messages.copy()
        if additional_instructions:
            messages.append(("system", additional_instructions))
        return messages

class PythonTutorTemplate(BaseAssistantTemplate):
    def __init__(self):
        super().__init__("a Python programming tutor", "Python programming and software development")
        self.base_messages.extend([
            ("system", "Your teaching approach:"),
            ("system", "- Start with simple concepts and build complexity"),
            ("system", "- Always provide runnable code examples"),
            ("system", "- Explain not just 'how' but 'why'"),
            ("system", "- Encourage best practices")
        ])

class DataScienceMentorTemplate(BaseAssistantTemplate):
    def __init__(self):
        super().__init__("a data science mentor", "data science, statistics, and machine learning")
        self.base_messages.extend([
            ("system", "Your mentoring style:"),
            ("system", "- Focus on practical applications"),
            ("system", "- Explain statistical concepts clearly"),
            ("system", "- Provide real-world examples"),
            ("system", "- Suggest relevant tools and libraries")
        ])

# Usage
python_tutor = PythonTutorTemplate()
tutor_template = ChatPromptTemplate.from_messages(
    python_tutor.create_template("Focus on beginner-friendly explanations") + 
    [("human", "{programming_question}")]
)

📊 Template Performance and Optimization

⚡ Efficient Template Management

python
from typing import Dict, Any
import time

class TemplateManager:
    def __init__(self):
        self.templates: Dict[str, ChatPromptTemplate] = {}
        self.usage_stats = {}
    
    def register_template(self, name: str, template: ChatPromptTemplate):
        """Register a template for reuse"""
        self.templates[name] = template
        self.usage_stats[name] = {'uses': 0, 'avg_time': 0}
    
    def get_template(self, name: str) -> ChatPromptTemplate:
        """Get a registered template"""
        if name in self.templates:
            self.usage_stats[name]['uses'] += 1
            return self.templates[name]
        raise ValueError(f"Template '{name}' not found")
    
    def format_with_stats(self, template_name: str, **kwargs) -> Any:
        """Format template and track performance"""
        start_time = time.time()
        template = self.get_template(template_name)
        result = template.format_messages(**kwargs)
        
        format_time = time.time() - start_time
        stats = self.usage_stats[template_name]
        stats['avg_time'] = (stats['avg_time'] + format_time) / 2
        
        return result
    
    def get_stats(self):
        """Get usage statistics"""
        return self.usage_stats

# Setup template manager
manager = TemplateManager()

# Register commonly used templates
manager.register_template("tutor", ChatPromptTemplate.from_messages([
    ("system", "You are a helpful programming tutor."),
    ("human", "{question}")
]))

manager.register_template("analyzer", ChatPromptTemplate.from_messages([
    ("system", "You are a code analyzer."),
    ("human", "Analyze this code: {code}")
]))

# Use templates efficiently
response1 = manager.format_with_stats("tutor", question="How do loops work?")
response2 = manager.format_with_stats("analyzer", code="for i in range(10): print(i)")

print(manager.get_stats())

🎯 Template A/B Testing

python
import random

class TemplateABTester:
    def __init__(self):
        self.variants = {}
        self.results = {}
    
    def add_variant(self, test_name: str, variant_name: str, template: ChatPromptTemplate):
        """Add a template variant for testing"""
        if test_name not in self.variants:
            self.variants[test_name] = {}
            self.results[test_name] = {}
        
        self.variants[test_name][variant_name] = template
        self.results[test_name][variant_name] = {'uses': 0, 'ratings': []}
    
    def get_template(self, test_name: str, user_id: str = None):
        """Get a template variant (random or deterministic)"""
        variants = list(self.variants[test_name].keys())
        
        if user_id:
            # Deterministic selection based on user ID
            variant = variants[hash(user_id) % len(variants)]
        else:
            # Random selection
            variant = random.choice(variants)
        
        self.results[test_name][variant]['uses'] += 1
        return self.variants[test_name][variant], variant
    
    def record_rating(self, test_name: str, variant_name: str, rating: float):
        """Record user rating for a variant"""
        self.results[test_name][variant_name]['ratings'].append(rating)
    
    def get_results(self, test_name: str):
        """Get A/B test results"""
        results = {}
        for variant, data in self.results[test_name].items():
            avg_rating = sum(data['ratings']) / len(data['ratings']) if data['ratings'] else 0
            results[variant] = {
                'uses': data['uses'],
                'avg_rating': avg_rating,
                'total_ratings': len(data['ratings'])
            }
        return results

# Setup A/B testing
ab_tester = TemplateABTester()

# Add template variants to test
formal_template = ChatPromptTemplate.from_messages([
    ("system", "You are a professional programming instructor."),
    ("human", "{question}")
])

casual_template = ChatPromptTemplate.from_messages([
    ("system", "You're a friendly coding buddy who loves helping people learn!"),
    ("human", "{question}")
])

ab_tester.add_variant("tutor_style", "formal", formal_template)
ab_tester.add_variant("tutor_style", "casual", casual_template)

# Test the variants
template, variant = ab_tester.get_template("tutor_style", user_id="user123")
# ... use template and collect user feedback ...
ab_tester.record_rating("tutor_style", variant, 4.5)

print(ab_tester.get_results("tutor_style"))

🔗 Integration with LangChain Chains

🔄 Chat Templates in Chains

python
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

# Create chat template
chat_template = ChatPromptTemplate.from_messages([
    ("system", "You are {role}. {instructions}"),
    ("human", "{user_input}")
])

# Create chain with chat template
chat_chain = chat_template | ChatOpenAI() | StrOutputParser()

# Use in chain
result = chat_chain.invoke({
    "role": "a Python expert",
    "instructions": "Provide clear, practical advice with code examples",
    "user_input": "How do I handle exceptions in Python?"
})

print(result)

🔄 Multi-Step Chat Conversations

python
from langchain_core.runnables import RunnablePassthrough

# Multi-step conversation chain
def format_conversation_context(inputs):
    return {
        "conversation_history": inputs.get("history", ""),
        "current_question": inputs["question"],
        "user_context": inputs.get("context", "")
    }

conversation_template = ChatPromptTemplate.from_messages([
    ("system", """You are continuing a conversation. Here's the context:
    
    Previous conversation: {conversation_history}
    User context: {user_context}
    
    Provide a response that acknowledges the conversation history."""),
    ("human", "{current_question}")
])

conversation_chain = (
    RunnablePassthrough.assign(formatted=format_conversation_context)
    | conversation_template
    | ChatOpenAI()
    | StrOutputParser()
)

🎯 Best Practices for Chat Prompts

✅ Template Design Guidelines

  1. Clear Role Definition

    python
    # Good: Specific and actionable
    ("system", "You are a senior Python developer with 10 years experience. Help debug code by providing step-by-step analysis.")
    
    # Avoid: Vague and unhelpful
    ("system", "You are helpful.")
  2. Consistent Format

    python
    # Good: Structured and predictable
    template = ChatPromptTemplate.from_messages([
        ("system", "Role: {role}\nTask: {task}\nConstraints: {constraints}"),
        ("human", "{user_input}")
    ])
  3. Error Handling

    python
    def safe_template_format(template, **kwargs):
        try:
            return template.format_messages(**kwargs)
        except KeyError as e:
            return f"Missing required parameter: {e}"
        except Exception as e:
            return f"Template formatting error: {e}"

🔗 Next Steps

Ready to enhance your prompts further? Continue with:


Key Chat Prompt Takeaways:

  • Role-based structure creates more controlled and consistent interactions
  • System messages set behavior, context, and constraints effectively
  • Template composition enables building complex conversational flows
  • A/B testing helps optimize template performance
  • Proper formatting and error handling ensure robust applications

Released under the MIT License.