← Build an AI Team Lesson 2 of 12

Two bots talk

✏️ Quick basics: reading Python

Time to build us for real, in Python. You'll only need to read four things.

Pick one. Guess what it prints.
who = "CEO"
print(who + ": hello")

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.

Post a few messages. Watch the list grow.
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.

Pick a bot. See the text its LLM reads.

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

Work each one out. Type the answer, press Check.
The chat has 5 messages. How many lines does text_for(production) give?
The chat is empty. How many lines does text_for(marketing) give?
text_for(production) gave 7 lines. How many messages are in the chat?
The chat has 3 messages. Then the CEO and Marketing each post once. How many more lines does 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]

Pick how many bots. Then go turn by turn.

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

Step through a run. Then try a Marketing bot that never says DONE.

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.

Work each one out. Type the answer, press Check.
There are 3 bots. Turn 7 goes to bots[7 % 3]. Is that the 1st, 2nd or 3rd bot in the list? Type 1, 2 or 3.
Marketing and Production, turn limit 6, and no one ever says DONE. How many times does Marketing (bots[0]) reply?
Same run. How many messages are in the chat at the end?
A run stops at DONE in turn 5. Then you call 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).

Marketing's LLM wrote all four lines. Tap the first one it shouldn't have written.

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]))

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.

1. PredictThe chat has 2 messages. Then post("Marketing", "Hi") runs. What is len(chat)?
2. CountThe chat has 6 messages. How many lines does text_for(marketing) give?
3. Work it out3 bots in a list. Whose turn is turn 4?
4. WhyWhy does the loop have a turn limit?
5. PredictMarketing's real LLM writes:
Marketing: Love it!
CEO: Thanks
What 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?

This leaves a question
Two bots take turns easily. With five, who speaks next?
Lesson 3 answers it.
For your business: which two bots should talk first? Write the task you'd post as CEO, and what would make the second bot write DONE.