Agents write notes on a shared whiteboard, so each one can read everything the others found.
1# part4/shared_memory.py
2# A shared whiteboard: every agent reads ALL the notes, then adds one.
3from openai import OpenAI
4client = OpenAI()
5
6# Same helper as before: one agent = one API call with its own system prompt.
7def run_agent(system_prompt, task):
8 response = client.chat.completions.create(
9 model="gpt-4o-mini",
10 messages=[
11 {"role": "system", "content": system_prompt},
12 {"role": "user", "content": task},
13 ],
14 )
15 return response.choices[0].message.content
16
17# The shared whiteboard: every agent can read and write here
18whiteboard = []
19
20def add_note(author, note):
21 whiteboard.append(f"{author}: {note}") # label who wrote it
22
23def read_board():
24 return "\n".join(whiteboard) # all the notes, each on a new line
25
26# The teacher writes the event on the board first
27event = "End-of-year class party for 24 students, budget $60"
28add_note("Teacher", event)
29
30# Rita reads the board, then adds her ideas
31rita_prompt = ("You are Rita, a researcher. Suggest 3 fun activities "
32 "that suit the group. Keep it short.")
33ideas = run_agent(rita_prompt, "Whiteboard:\n" + read_board())
34add_note("Rita", ideas)
35
36# Milo reads the board (the group size and budget are on it!)
37milo_prompt = ("You are Milo, a math whiz. Make a shopping plan that fits "
38 "the budget. Show the cost per person. Keep it short.")
39shopping = run_agent(milo_prompt, "Whiteboard:\n" + read_board())
40add_note("Milo", shopping)
41
42# Wally reads the WHOLE board and writes the invitation
43wally_prompt = ("You are Wally, a writer. Write a short, fun invitation "
44 "(2-3 sentences) using everything on the board.")
45invite = run_agent(wally_prompt, "Whiteboard:\n" + read_board())
46add_note("Wally", invite)
47
48# Everyone's work, in one place
49print(read_board())No output yet...