HOW-TO · RAG
How to Build Custom LLM Chain with Prompt Templates
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
LangChain installed, Python 3.10+
What this does
Custom LLM chains let you define reusable prompt templates with dynamic variables, few-shot examples, and structured outputs — giving full control over how the LLM is instructed.
Steps
- Define a prompt template with multiple variables. Use
ChatPromptTemplatefor structured messages.
from langchain.prompts import ChatPromptTemplate
from langchain_ollama import ChatOllama
from langchain.schema.output_parser import StrOutputParser
llm = ChatOllama(model="llama3.2", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a {role} expert. Answer concisely."),
("human", "{question}")
])
- Create and invoke the chain.
chain = prompt | llm | StrOutputParser()
result = chain.invoke({
"role": "Python",
"question": "What is a decorator?"
})
print(result)
- Add few-shot examples. Include example pairs in the prompt.
from langchain.prompts import FewShotChatMessagePromptTemplate
examples = [
{"input": "What is a list?", "output": "An ordered collection."},
{"input": "What is a dict?", "output": "A key-value mapping."},
]
example_prompt = ChatPromptTemplate.from_messages([
("human", "{input}"),
("ai", "{output}"),
])
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
final_prompt = ChatPromptTemplate.from_messages([
("system", "You are a Python tutor."),
few_shot_prompt,
("human", "{question}"),
])
chain = final_prompt | llm | StrOutputParser()
print(chain.invoke({"question": "What is a tuple?"}))
- Use partial variables. Pre-bind variables that don't change per call.
prompt = ChatPromptTemplate.from_template("""
Context: {context}
Question: {question}
Answer:""").partial(context="Default knowledge base content.")
chain = prompt | llm | StrOutputParser()
print(chain.invoke({"question": "What is RAG?"}))
Verification
python -c "
from langchain.prompts import ChatPromptTemplate
pt = ChatPromptTemplate.from_template('Hello {name}')
r = pt.format(name='World')
print(r)
# Expected: Hello World
"
Common failures
- Message role order matters. LangChain validates message order (system, then alternating human/ai). Wrong order raises
ValidationError. - Few-shot example key mismatch. Example dict keys must match
example_prompttemplate variables exactly. - Partial overwrite ignored. Calling
invokewith a key that was alreadypartial-bound silently uses the partial value. Verify by inspectingprompt.input_variables. - Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
- Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.
Related guides
- How to Create Basic LangChain Chains with LCEL
- How to Compose Multiple Chains with Pipe Operator