Skip to content

Prompting

Run it, don't just read it

Every lab runs on your laptop with Ollama. Set up once (INSTALL), then:

git clone https://github.com/JSWOne/ai-learning.git && cd ai-learning
cd prompting && python 01_message_anatomy.py

Lab files and data: prompting/ on GitHub. In VS Code, open the repo folder and use Terminal > Run Task > Run this lab.

Small, runnable examples of how to talk to a model: what a call contains, how instructions and examples change the answer, how to get data back instead of prose, how to tell whether a prompt change helped, and how buyer text tries to take over. Every lab works on one task: triage a B2B buyer's message. New to prompting? Read learn-prompting.md first: what it is and why, the mental model in code and diagrams, the labs in three stages, key decisions and advanced topics. Before you start: setup done (Start here) and enough Python to read a function and a dict (python-basics if not). After it, you can: brief a model, get data back instead of prose, and score a prompt change. Done with the labs? Try the three exercises and the build-it-yourself task in PRACTICE.md.

The messages are fake and live in data/; the prompts live in prompts/, one file per version.

Setup

Install steps for all topics are in ../INSTALL.md. Short version, from the ai-learning folder:

ollama pull qwen3:8b
uv venv && source .venv/bin/activate     # the one venv at ai-learning/ (see ../INSTALL.md section 4)
uv pip install -r requirements.lock      # all topics, the tested versions

Run any example from this folder, for example python 02_instructions_and_few_shot.py.

Files

Track: Core labs are the Core path through the course. Before you ship: safety lessons to do before anything you build goes to real users. Go further: when your work needs that pattern (see Use it when).

File Track Pattern Use it when Time
01_message_anatomy.py Core Roles, history (the model has no memory), temperature and think You want to see what a call really contains ~1-2 min
02_instructions_and_few_shot.py Core One-line prompt vs rules vs rules plus worked examples, on six tricky messages The model gets the easy cases and misses the edge ones ~1 min
03_structured_output.py Core JSON by asking vs schema=; a good schema (closed list, descriptions, a cleaning validator) for catalog enrichment Your code needs fields, not prose ~1 min
04_prompt_versions.py Core Prompt files scored on a 20-case golden set, by slice, with a ship rule (better somewhere, worse nowhere; a tie keeps the cheaper prompt) Before you ship any prompt or model change ~2-3 min
05_injection_basics.py Core Nine attacks plus one honest message against a naive prompt, roles and tags, and code (phrase rules, then a facts check) Any text from outside reaches the model ~1 min

Shared code and data:

File What it holds Read?
triage.py Triage and Line (the answer's shape), load_prompt, build_messages, run Read
triage.py (lower half) score, run_eval, summarize, ship_decision (lab 04); build_naive, wrap_untrusted, injection_flags, check_triage (lab 05) Read with labs 04, 05
prompts/triage-v1.md ... v5.md Five versions; the front matter says what changed, how many examples and whether buyer text is wrapped Read
data/cases.jsonl 20 buyer messages with the right answer, tagged english, hinglish, tricky Skim
data/examples.jsonl The four few-shot examples (never in the golden set) Read with lab 02
data/attacks.jsonl Ten messages (nine attacks, one honest control) with the honest reading of each Skim

Exercises to practise with: PRACTICE.md (your own files go in my-work/).

Questions and answers on all of this are in FAQ.md. The learning guide is learn-prompting.md (diagrams in diagrams/).

Rules of thumb

  • Rules in the system message, the user's text in the user message. Never paste user text into your instructions.
  • Define every label in one line. Most "the model is dumb" problems are two labels nobody defined apart (quote vs price question).
  • Clear definitions first; examples only for a mistake the eval shows rules can't fix. On qwen3:8b the definitions did all the work and the examples added nothing for +51% tokens.
  • Ask for data with schema=, then validate with Pydantic. Valid shape is not a right answer.
  • Temperature 0 for anything you score or parse. Raise it only for drafts a person will edit.
  • Prompts live in files with a version, and every change runs the golden set. Ship only if no slice gets worse.
  • Treat outside text as data: user turn and tags, then phrase rules and a facts check in code. On a small model the prompt rule alone barely helps. Never let a label alone trigger a write.

Tests

pytest -q runs model-free tests for the prompt files, message building, the schema, the scorer and the injection check (using a fake chat). Whether qwen3:8b follows a prompt can only be checked by running the labs against a real model.