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.- 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 usingChatPromptTemplate.from_messages. Create message template objects from the raw templates, then pass them to from_messages:
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 forsubject and concept. format_messages returns a list of message objects that you can send directly to a chat model:
prompt_messages contains two message objects:
- System message: “You are a Chemistry teacher”
- Human message: “Tell me about the Periodic Table”
5. Invoke the chat model and read the response
Create aChatOpenAI instance and call it with the formatted messages. The generate_messages API returns a ChatResult containing the generated assistant messages.
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.
- 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
HumanMessagePromptTemplateandAIMessagePromptTemplate(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
temperatureandmax_tokenssettings onChatOpenAIto 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.
Links and references
- LangChain course on KodeKloud
- Introduction to OpenAI on KodeKloud
- Official LangChain docs: https://langchain.readthedocs.io
- OpenAI API docs: https://platform.openai.com/docs
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.