RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to Build Custom LLM Chain with Prompt Templates
HOW-TO · RAG

How to Build Custom LLM Chain with Prompt Templates

intermediate·15 min·By Fredoline Eruo
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 ChatPromptTemplate for 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_prompt template variables exactly.
  • Partial overwrite ignored. Calling invoke with a key that was already partial-bound silently uses the partial value. Verify by inspecting prompt.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
← All how-to guidesCourses →