Start learning free

LESSON 17 OF 129 · AI ENGINEERING: PROMPTS THAT HOLD UP

Show Examples in Python

Practice

Every shop has its own voice. You could describe that voice to a model in words, but a tone is hard to describe and easy to show. Showing means examples: a question and the answer you would give, placed in front of the real question.

Examples go into the messages list as turns that already happened: a "user" message with the question, then an "assistant" message with the answer. The model never wrote that answer: you did. It reads the conversation so far and continues in the same voice. Here a swimming pool shows a very dry style:

messages = [
    {"role": "user", "content": "Is the pool open today?"},
    {"role": "assistant", "content": "Yes. 9:00 to 18:00."},
    {"role": "user", "content": "Is there parking?"},
]

One example is only a hint. Two make a pattern: the model sees what the answers have in common and copies that style, not their content. This is called few-shot prompting. Every example travels with every call, so two or three good ones are enough.

In this lesson a customer asks the Corner Shop "Do you gift wrap?". Sent alone, the question gets a stiff answer. With two examples in the shop's short, friendly voice in front of it, the answer sounds like the shop. The check also prints the roles of the messages in your last call:

Without examples: Thank you for your question. Yes, the Corner Shop offers a gift wrapping service. … please do not hesitate to ask.
With examples: Yes, and it's free! Just ask.
Roles sent in the last call: user, assistant, user, assistant, user

Your task: SYSTEM (the shop's facts), question and the list messages (holding only question) are ready. Send the question alone and print "Without examples:" with the answer. Then add two example pairs above question in messages, send the whole list and print "With examples:" with the answer.


Assignment
Follow the Coding Tutorial to teach the model the shop's voice with two example question-and-answer pairs.


Hint
An example is two dictionaries: the "user" one first, then the "assistant" one. Both pairs go above question, so the real question stays the last message. Both calls pass system=SYSTEM.


STEP 1 OF 8
    Ctrl + Enter to check

    Ai Few Shot in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free