Writing better prompts

A prompt is the text you give a language model. Each fact you add rules out some of the answers it might give.

Typing "Write a function that enlists a student in a course" into an AI chatbot returns code in seconds. It is written for a different Python toolkit than your project uses. You add one sentence naming your toolkit and ask again, and the new answer is different.

Your requestPrompt
The AI chatbotModel
The code it returnsAnswer
Think first. The model did not ask which toolkit you meant. For this request, which kind of answer will it give most often, and why?
Let students commit to a guess before they open the widget. Most guess the careful-looking answer that checks the slot count first. Ask them what happens when two students click at the same moment.

You ask a model for Django code that enlists a student in a course. Each bar is one kind of answer, and its length is how likely the model is to give it. Blue bars keep working when two students click at once. Turn on the three facts one at a time and watch the bars.

Add to the prompt
More detail

The three numbers

Answers still possible
—
Most likely answer
—
Chance the answer is correct
—

The eight kinds of answer

    The code for the most likely answer

    The code uses Django's ORM, the part of Django that reads and writes database rows as Python objects, so nobody types SQL. Each bar is code that has been used in real systems.

    The model does not ask what you meant

    The model does not ask which toolkit you meant. It picks one answer from the range that is left, as described in Predicting the next word.

    A fact helps when it rules out answers. "Be careful" rules out none, so it changes little. The facts that matter are the ones the model could not know: your framework, that two requests can overlap, and your tables. Most examples it learned from check first and write second, which fails as a race condition.

    You can now write a prompt that adds the facts a model could not guess.

    More detail

    System prompts and context

    A system prompt is text placed ahead of every conversation. It holds rules that are true every time, such as the framework you use, your house style and what the model must never do without asking. Context is what applies to one task: the table definitions, the file, an example. Both narrow the range of answers.

    Why the most likely answer is wrong

    With nothing specified, the most likely answer reads the slot count, checks it and then creates the row. Both of two simultaneous requests can read the same count before either one writes. The model gives this answer because it is the most common pattern in the text it learned from, and nothing in the prompt ruled it out.

    See Transactions and Reviewing AI-written code.

    Lock or conditional update

    Two answers survive every fact. One locks the course row, which makes other requests wait. The other runs one conditional update and checks how many rows changed. The second holds no lock, which helps on enlistment morning.