Skip to main content
This lesson demonstrates how to convert message patterns into reusable prompt templates and then create concrete prompts to send to a chat model using LangChain. The example below walks through the essential steps in order: imports, defining message templates, building a chat prompt template, populating it at runtime, invoking a chat model, and reading the model response.
Before running the examples, ensure your OpenAI API key is set in the environment, for example: export OPENAI_API_KEY="sk-...". See Introduction to OpenAI for details and safe handling of secrets.

Overview

  • Build modular message templates (system/human).
  • Combine them into a ChatPromptTemplate.
  • Populate templates at runtime using format_messages.
  • Send the formatted messages to a chat model (e.g., ChatOpenAI) and extract the assistant response.
  • Optionally extend with few-shot examples, output parsing, or post-processing steps.

Quick reference: core classes

1. Imports

Import the LangChain chat model and prompt template helpers:

2. Define message templates

Define your system and human message templates with placeholders for runtime variables. These templates describe the pattern of messages but do not include concrete values yet.
These define two reusable patterns:
  • System template: instructs the assistant’s role and behavior.
  • Human template: expresses the user query with a placeholder.

3. Create a ChatPromptTemplate from message templates

Combine the message templates into a structured prompt template using ChatPromptTemplate.from_messages. Create message template objects from the raw templates, then pass them to from_messages:
If you inspect prompt_template, it describes its input variables and the underlying message templates. Example representation (console output):

4. Populate the template to create a concrete prompt

Format the template with actual values for subject and concept. format_messages returns a list of message objects that you can send directly to a chat model:
After formatting, prompt_messages contains two message objects:
  • System message: “You are a Chemistry teacher”
  • Human message: “Tell me about the Periodic Table”
These message objects are ready to pass to the model.

5. Invoke the chat model and read the response

Create a ChatOpenAI instance and call it with the formatted messages. The generate_messages API returns a ChatResult containing the generated assistant messages.
Example output you might receive:

6. How this fits into a chain

Conceptually, this is a simple chain consisting of:
  • A prompt: built from templates and populated at runtime.
  • A model: the chat model that consumes the prompt and returns a response.
You can extend this chain with:
  • Output parsers (to structure model output).
  • Post-processing steps (validation, formatting).
  • Storage layers (logs, databases).

7. Few-shot prompting (brief)

Few-shot prompting supplies examples to demonstrate desired output style or format. In LangChain, include example message turns in your prompt template so the model sees them along with the instruction and the current query. This helps steer tone, structure, and level of detail. Example approaches:
  • Add one or more example conversation turns using HumanMessagePromptTemplate and AIMessagePromptTemplate (if available).
  • Provide formatted output examples illustrating how the model should structure its response.

Troubleshooting & tips

  • If the model response seems off-topic, enrich the system message with clearer constraints or add few-shot examples demonstrating the desired format.
  • Use temperature and max_tokens settings on ChatOpenAI to control randomness and length.
  • Avoid committing API keys to source control; use environment variables or secrets management.
Never commit your OpenAI API key to version control. Use environment variables or secret managers to keep keys safe. Monitor usage to avoid unexpected costs.

This concise walkthrough covers building prompt templates, populating them at runtime, and invoking a chat model with LangChain. You can expand this pattern into pipelines for parsing, validation, or integrating with downstream applications.

Watch Video

Practice Lab