From messages takes a list of role and text pairs. The system message sets the tone, with a blank for the shop name. The human message has a blank for the question. Run it. Invoking the template gives two real message objects, ready for the model.
chat_template.pypython
from langchain_core.prompts import ChatPromptTemplateprompt = ChatPromptTemplate.from_messages([ ("system", "You are a friendly assistant for {shop}. Answer in one sentence."), ("human", "{question}"),])value = prompt.invoke({"shop": "Riverside Books", "question": "Do you sell maps?"})for m in value.messages: print(type(m).__name__, "->", m.content)
Output
SystemMessage -> You are a friendly assistant for Riverside Books. Answer in one
sentence.
HumanMessage -> Do you sell maps?
Now we send the filled template to a model. Run it. The model answers as the Riverside Books assistant, in a single friendly sentence, because the system message says so. Next lesson, we'll make room in the template for a whole conversation.
template_model.pypython
from langchain.chat_models import init_chat_modelfrom langchain_core.prompts import ChatPromptTemplatemodel = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)prompt = ChatPromptTemplate.from_messages([ ("system", "You are a friendly assistant for {shop}. Answer in one sentence."), ("human", "{question}"),])messages = prompt.invoke({"shop": "Riverside Books", "question": "Do you sell maps?"})print(model.invoke(messages).text)
Output
Yes, we carry a lovely selection of local and regional maps at Riverside Books!
Chat prompt templates build role tagged messages from your data. Fill them with invoke, and pass them to the model. Next, a placeholder for chat history.