Read this page together with the Code:
Predict_Wtih_Injected_Prompt.adoc.
That file is a small
Agent:
1. A
ChatOllama
model
2. A system_prompt
3. A question.
LangChain's job is provide a
Agent which can talk to
LLM.
The code has 3 steps:
| Step | What it does |
|---|---|
| 1. Initialize a Model |
ChatOllama(...) — pick which LLM to call and how it
should generate (temperature, tokens, timeout, retries)
|
| 2. Control behaviour |
Put rules + examples in a system_prompt, wrap it as
SystemMessage
|
| 3. Invoke |
Send [SystemMessage, HumanMessage] to
model.invoke(). Reply comes back as
AIMessage
|
ChatOllama is the LangChain client for a locally running
Ollama server.
from langchain_ollama import ChatOllama
model = ChatOllama(
model="llama3.2:latest",
base_url="http://localhost:11434", #Ollama REST endpoint.
# Controls randomness of model output. Higher number makes model more creative
# 0 → 0.2 Almost deterministic. Same prompt ≈ same answer.
# 0.5 → 0.8 Balanced. Normal chat
# 1 and above More random, more invention. Stories, brainstorming.
temperature=1,
max_tokens=256, # Max tokens in outputs, making how long output can be
timeout=30, # Max time to wait from Model response
max_retries=2 # max retry requests to model
)
A system prompt is text you send with role system.
The user never types it. It is the model's job description: who it is,
what rules to follow, and (optionally) worked examples of the style you
want.
system_prompt = """
You are Politician. Predict who will win
User: What is capital of mars?
Science fiction writer: Marimalas
User: What is capital of Venus?
Science fiction writer: Venusoila
"""
Chat models do not take a raw string. They take a list of messages. Each message has a role + content. LangChain types are explained on LangChain Messages. The sample uses three of them.
| Class | Role | Who writes it | Job |
|---|---|---|---|
SystemMessage
|
system | You (developer) | Rules, persona, few-shot examples. Hidden from the end user. |
HumanMessage
|
user | End user (or your test script) | The actual question / next turn. |
AIMessage
|
assistant | The model (you can also replay an old reply) | Previous model output. Needed to continue a chat. |
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
messages = [
SystemMessage(content=system_prompt),
HumanMessage(content="What's the capital of the Moon?")
]
response = model.invoke(messages)
# response is an AIMessage
# response.content == "The capital of the Moon is Lunaria."
Wrapper around the system_prompt string. Always put it first. The model treats this as configuration, not as something the user said.
SystemMessage(content=system_prompt)
One user turn. In the sample there is only one:
"What's the capital of the Moon?". In a real chat UI, every
time the user types, you append another HumanMessage (and
keep the old ones — see chat history).
HumanMessage(content="What's the capital of the Moon?")
model.invoke(...) returns an AIMessage.
.content is the text you print. You also send old
AIMessages back on the next call so the model remembers
what it already said.
AIMessage(content="The capital of the Moon is Luna City.")
Other fields you will see later (tools, tokens) live on this same object — see AIMessage attributes.
The model has no memory between calls. If you want a follow-up ("tell me more"), you must send the whole conversation again: system + every previous Human/AI turn + the new question.
messages = [
SystemMessage(content=system_prompt),
HumanMessage(content="What's the capital of the Moon?"),
AIMessage(content="The capital of the Moon is Luna City."),
HumanMessage(content="Interesting, tell me more about Luna City")
]
response = model.invoke(messages)
What the model sees:
| Turn | Object | Why it is there |
|---|---|---|
| 0 | SystemMessage |
Stay in the fake-capital persona. |
| 1 | HumanMessage |
Original question. |
| 2 | AIMessage("...Luna City.") |
What the model already answered. Without this, "tell me more about Luna City" has no prior name to attach to. |
| 3 | HumanMessage("...tell me more...") |
The new user turn. |
Drop any of those turns and the model loses context. This list is the agent's short-term memory.