BUILD A LOCAL CHATBOT · MODULE 4 — PERSONALITY, GUARDRAILS, A REAL UI · bc-10 Real Python, every page

Module 4.10 · Deepens page 6

The system prompt, in code

A system prompt isn't a special mode — it's one more block of text, prepended before the conversation starts. There's already a default one you never wrote. Compare it against a real one you do write.

The script

One extra dict in the list

bc-10-system-prompt.py
question = "Should I invest my savings in cryptocurrency?"

# no system role written — the tokenizer's own default is used instead
ask([{"role": "user", "content": question}])

# an explicit system role, prepended before the conversation
system_msg = ("You are a cautious, formal financial-literacy assistant. You never give "
              "direct investment advice; you explain tradeoffs and always suggest a licensed advisor.")
ask([{"role": "system", "content": system_msg}, {"role": "user", "content": question}])

Uses the same ask() helper from bc-04/bc-09 — the only change is what's in the messages list before the user's turn. Download bc-10-system-prompt.py ⬇

Try this

Change system_msg in bc-10-system-prompt.py ⬇ to something playful, like "You only ever answer in pirate slang.". Predict first: does the reply's factual content change, or just its tone?

So what

There's no "personality switch" in the code — every conversation already has a system prompt, written or not.