Step 1: Variables and strings: write the assistant's instructions
Every assistant starts with a paragraph of instructions, the system prompt, telling the model who it is and how to answer. Brightline will want to change the tone and length without rewriting the sentence, so you write it once as a function that fills in the parts that change.
Three Python ideas do all the work:
- A variable is a name for a value.
SHOP = "Brightline Books"near the top ofchatbot.pyis one. Text in quotes is a string. - A function is a named recipe.
def build_system_prompt(shop, tone, max_sentences):takes three values (its parameters) andreturnhands one value back. - An f-string puts values inside text:
f"Hello {name}"becomesHello Samwhennameis"Sam". Thefbefore the quote switches it on.
1. Write build_system_prompt(). Return this sentence, with the three
parameters in the braces:
return (
f"You are the assistant of {shop}, an independent bookshop. "
f"Answer in a {tone} tone, in at most {max_sentences} sentences."
)
Two strings side by side inside brackets are joined into one, which lets a long sentence span two lines. Mind the space before the closing quote of the first line.
2. Run. It builds the prompt with a cheerful tone and a two-sentence
limit, sends it with a question, and prints the answer. Change "cheerful"
to "formal" in try_it.py and run again: one word changes the whole
voice.
chatbot.py, the file you edit64 lines
"""Brightline Books' chat assistant, one Python idea per step.
Run it yourself in the terminal: python3 chatbot.py
(type a message and press Enter; type quit to stop)
"""
from openai import OpenAI
CLIENT = OpenAI()
MODEL = "meta/llama-3.1-8b-instruct"
SHOP = "Brightline Books"
MAX_CHARS = 500
# ---------- Step 1: variables, strings and a function ----------
def build_system_prompt(shop, tone, max_sentences):
"""Return the assistant's standing instructions as one string."""
# TODO (Step 1): return ONE string built with an f-string, exactly:
# You are the assistant of <shop>, an independent bookshop. Answer in a <tone> tone, in at most <max_sentences> sentences.
# where <shop>, <tone> and <max_sentences> are the three parameters.
raise NotImplementedError("Step 1: write build_system_prompt()")
# ---------- Step 2: lists and dictionaries ----------
def new_history(system_prompt):
"""A fresh conversation: a list holding only the system message."""
raise NotImplementedError("new_history() arrives in Step 2")
def add_message(history, role, content):
"""Append one message to the conversation (changes the list in place)."""
raise NotImplementedError("add_message() arrives in Step 2")
def reply(history, client=None):
"""Send the whole conversation, store the model's answer in it, return the answer."""
client = client or CLIENT
response = client.chat.completions.create(model=MODEL, messages=history, temperature=0.3)
answer = response.choices[0].message.content
add_message(history, "assistant", answer)
return answer
# ---------- Step 4: decisions (if / elif / else) ----------
def handle(line, history, client=None):
"""Decide what to do with one line the user typed. Return the text to show."""
raise NotImplementedError("handle() arrives in Step 4")
# ---------- Step 5: slicing ----------
def trim(history, max_messages):
"""Keep the system message plus only the newest max_messages messages."""
raise NotImplementedError("trim() arrives in Step 5")
# ---------- Step 3: a loop that reads what the user types ----------
def chat(read=input, show=print, client=None, max_messages=None):
"""Run the conversation until the user types quit. Return the history."""
history = new_history(build_system_prompt(SHOP, "warm", 3))
raise NotImplementedError("chat() arrives in Step 3")
if __name__ == "__main__":
chat(max_messages=20)try_it.py