Chapter 18 · Hands-on: set up your AI workbench
Save prompts and settings as files
Keeping prompts, settings and test questions in files makes your AI work repeatable and easy to improve.
Chapter 18 · Hands-on: set up your AI workbench
Keeping prompts, settings and test questions in files makes your AI work repeatable and easy to improve.
Loose notes get lost.
A prompt typed into a chat box is gone tomorrow. A prompt saved in a file can be reused, compared and improved, which is how you turn tries into progress.
You need the project folder and virtual environment from the Python lesson, and a local model from the earlier lessons. Open a terminal in my-ai-project and activate the environment. You will make four files. Use a plain-text editor (Notepad, TextEdit in plain-text mode, or a code editor) and save with the exact names, without a hidden .txt added at the end.
Create system.txt with one or two sentences that set how the model behaves, as in the system prompt lesson. Keep it short.
You are a patient tutor. Answer in two short sentences.Check: the file holds only that sentence.
Create settings.json. JSON is a strict format: use double quotes and no comma after the last item. Use the exact model name from ollama list. The temperature is low, so answers stay steady, as in the temperature lesson.
{
"model": "llama3.2:1b",
"temperature": 0.2
}Check: the model name matches ollama list exactly.
Create questions.txt with one question per line. These are your small test set: the same questions every time, so you can see whether a change helped.
What is a token?
What is a virtual environment?Check: each question is on its own line.
Create run.py. It reads the three files, asks every question, and prints each answer. If you change a file, you never need to touch the code.
import json
from pathlib import Path
import requests
folder = Path(__file__).parent
system = (folder / "system.txt").read_text(encoding="utf-8").strip()
settings = json.loads((folder / "settings.json").read_text(encoding="utf-8"))
questions = [
line.strip()
for line in (folder / "questions.txt").read_text(encoding="utf-8").splitlines()
if line.strip()
]
for question in questions:
reply = requests.post(
"http://localhost:11434/api/chat",
json={
"model": settings["model"],
"messages": [
{"role": "system", "content": system},
{"role": "user", "content": question},
],
"options": {"temperature": settings["temperature"]},
"stream": False,
},
timeout=120,
)
reply.raise_for_status()
print("Q:", question)
print("A:", reply.json()["message"]["content"], "\n")Check: all four files are in my-ai-project (list them with ls or dir).
Run the script and save the output to a file so you can compare it later. Then change one thing, for example the sentence in system.txt, run again to a second file, and compare. Changing one thing at a time tells you what caused the difference.
python run.py > run1.txt
# edit system.txt, then:
python run.py > run2.txtCheck: run1.txt and run2.txt differ in the way you expected.
Fill in your choices. Copy each block into the file with that name.
You are a patient tutor. Answer in two short sentences.{
"model": "llama3.2:1b",
"temperature": 0.2
}What is a token?
What is a virtual environment?What do you see?
Why keep prompts in files?
You want to test a new system prompt. What should you change?
JSONDecodeError points to which file?