Two bots talk
✏️ Quick basics: reading Python
Time to build us for real, in Python. You'll only need to read four things.
who = "CEO"
print(who + ": hello")
bots = ["Marketing", "Production"]
print(bots[1])
msg = {"who": "CEO", "text": "hi"}
print(msg["text"])
def greet(name):
return "Hi " + name
print(greet("Production"))
Text in quotes is a string. [ ] is a list, counted from 0. { } is a dictionary: key → value. def makes a function: input in, return hands the result back. Tap any dotted word in the code for help.
"string" · [list] · {key: value} · def function
First, the group chat itself.
The chat is a list
In code, the shared chat is a list, in order. Each message is a dictionary with two keys: "who" wrote it, and "text" is what it says. A small function, post, adds a message to the end of the list.
def post(who, text):
chat.append({"who": who, "text": text})
append adds to the end, so the list keeps the order of the chat.
message = {"who": …, "text": …}
Every bot reads this list. What does that look like in code?
What a bot reads
A bot reads its own instructions + the chat so far. The function text_for builds exactly that, one line at a time: the instructions, an empty line, Chat so far:, one line per message, then the bot's own name, so the LLM writes the next line as that bot.
lines = messages + 4
3 messages → 3 + 4 = 7 lines.
Count it on new chats.
✏️ Practice 2.1
Remember: text_for gives the instructions, an empty line, Chat so far:, one line per message, and the bot's name.
lines = messages + 4
text_for(production) give?text_for(marketing) give?text_for(production) gave 7 lines. How many messages are in the chat?text_for(production) give now?Two bots, one chat. Who writes next?
Taking turns
The bots sit in a list, and the turns are counted 0, 1, 2 … The bot for a turn is found with %: it gives what's left over after dividing. With 2 bots:
turn 3 → 3 ÷ 2 = 1, with 1 left over → 3 % 2 is 1 → bots[1], Production.
bot = bots[turn % number of bots]
The left-over always goes 0, 1, … up to one less than the number of bots, then back to 0. So each bot gets a turn, in order, round and round.
Round and round… until when?
DONE, or the turn limit
Marketing's instructions say: when the caption is final, end with DONE. So after each reply, the loop checks for DONE and stops. In case it never comes, it also stops after a set number of turns, the turn limit.
stop at DONE, or at the turn limit
Try it on new numbers.
✏️ Practice 2.2
Remember: turns count from 0.
bot = bots[turn % number of bots]
stop at DONE, or at the turn limit
Each turn, one bot posts one message.
bots[7 % 3]. Is that the 1st, 2nd or 3rd bot in the list? Type 1, 2 or 3.bots[0]) reply?text_for(production). How many lines does it give?Fake replies are tidy. Real ones aren't.
A real LLM
In Colab, each bot's fake LLM is swapped for a real one. Its text ends with chat lines like CEO: …, so it guesses more chat lines and often writes other bots' messages too. Here's that kind of reply (an example made for this page).
So the program keeps the reply only up to the first line that starts with another name. If it starts with its own name, that name is dropped.
cut the reply where another name starts
Here's all of it in Python.
The code
Guess what each print shows. Then press ▶ Run.
# The shared chat: a list of messages, in order.
# Each message is a dictionary: who wrote it, and what it says.
chat = []
def post(who, text):
chat.append({"who": who, "text": text})
print(f"{who}: {text}")
# A bot: a name, its own instructions, and an LLM to write with
marketing = {"name": "Marketing",
"instructions": "You plan Instagram posts for Amal Designers, a kids' dress brand. Write one caption, using only facts in the chat. When it is final, end with DONE."}
production = {"name": "Production",
"instructions": "You run production at Amal Designers. Catalogue: pastel party frock, sizes 1 to 8 years, ₹1,450. We can sew 6 a week. Check every size and price."}
# What a bot reads: its own instructions + the chat so far.
# The last line is its own name, so the LLM writes as that bot.
def text_for(bot):
lines = [bot["instructions"], "", "Chat so far:"]
for m in chat:
lines.append(m["who"] + ": " + m["text"])
lines.append(bot["name"] + ":")
return "\n".join(lines)
# Fake LLMs: each bot's replies, written by hand, in order
marketing_replies = ["Draft: “Twirl-ready pastel frocks 💫 Sizes 1 to 12 years. DM to order!” Production, are the details right?",
"Final: “Twirl-ready pastel frocks 💫 Sizes 1 to 8 years, ₹1,450. Only 6 this week, DM to order!” DONE"]
production_replies = ["Sizes are 1 to 8 years, not 12. Price ₹1,450. We can sew 6 this week."]
def fake_marketing(text):
return marketing_replies.pop(0)
def fake_production(text):
return production_replies.pop(0)
marketing["llm"] = fake_marketing
production["llm"] = fake_production
# Taking turns: turn 0 → bot 0, turn 1 → bot 1, turn 2 → bot 0 …
# (% gives what's left over after dividing: 2 % 2 is 0, 3 % 2 is 1)
print(3 % 2)
def run_chat(task, bots, turn_limit=6):
post("CEO", task)
for turn in range(turn_limit):
bot = bots[turn % len(bots)]
reply = bot["llm"](text_for(bot))
post(bot["name"], reply)
if "DONE" in reply:
return "Done after " + str(turn + 1) + " turns."
return "Stopped: the turn limit was reached."
print(run_chat("Write a caption for the new pastel party frock.", [marketing, production]))
$ python lesson2.py
1
CEO: Write a caption for the new pastel party frock.
Marketing: Draft: “Twirl-ready pastel frocks 💫 Sizes 1 to 12 years. DM to order!” Production, are the details right?
Production: Sizes are 1 to 8 years, not 12. Price ₹1,450. We can sew 6 this week.
Marketing: Final: “Twirl-ready pastel frocks 💫 Sizes 1 to 8 years, ₹1,450. Only 6 this week, DM to order!” DONE
Done after 3 turns.The notebook has all of this, then the real LLM. Its first run downloads the model (about 3 GB), so it takes a few minutes. You just need a Google account.
Quick check: did it all make sense?
Lesson 2 test
5 questions, one for each idea in this lesson. Answer them all right to pass. A wrong answer tells you why, and which page to look at again.
post("Marketing", "Hi") runs. What is len(chat)?text_for(marketing) give?Marketing: Love it!CEO: ThanksWhat does the program keep?
What two bots can't do yet.
Five bots?
Your two bots take turns in a fixed order. But Amal Designers needs more: Marketing, Production, Operations, an Analyst… In a real group chat, the next speaker isn't always the next in line.
A customer just asked about delivery. Do I really have to wait for four turns?