What this combines
Nothing new here, this is where the beginner tier's pieces meet:
chat()with a system message (Lesson 4): a persona set once, applied to every question.- Streaming (Lesson 5): each answer prints as it's generated, instead of appearing all at once.
options={"temperature": 0.3}(Lesson 6): a lower, fixed temperature, appropriate for a factual Q&A assistant rather than a creative one.
And, underneath all of it, the theme of Lessons 1-3: no .env, no API key, no internet required at any point. If you want proof, turn off your network connection and run this again, it'll work identically.
The design
ask() wraps a single question in a fresh messages list every time, the same SYSTEM_PROMPT repeated on each call rather than accumulated conversation history. That's intentional here: real multi-turn memory, where the assistant remembers earlier answers in the same conversation, is Lesson 12's job, not this checkpoint's. Each question in this lesson is answered independently.
def ask(question: str) -> str: stream = ollama.chat( model="llama3.2", messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": question}, ], stream=True, options={"temperature": 0.3}, )main() just loops over three fixed questions and calls ask() on each, printing "You:" and "Assistant:" labels to make the transcript read like an actual conversation, even though each turn is technically a fresh, independent call.
Checkpoint
If this lesson ran cleanly and made sense without looking anything up, you're ready for the intermediate tier. You should be able to explain, in your own words:
- Why
ollama.chat()is preferred overollama.generate()once a system prompt is involved (Lesson 4). - What
stream=Truechanges about the return value, and why it matters for a real user (Lesson 5). - What
temperatureandseedeach control, and why they're different knobs (Lesson 6). - The difference between
ollama.list()(on disk) andollama.ps()(in memory) (Lesson 7). - Why an embedding model like
nomic-embed-textcan't answer questions the wayllama3.2does (Lesson 8).
If any of those feel shaky, it's worth a quick re-read of that lesson before continuing, everything from here builds on this tier directly.