To start we'll use native data structures to simulation a simple yes/no vote.
agent1 = {"name": "Bob"}
agent2 = {"name": "Alice"}
agent3 = {"name": "Tom"}
motion = {"text": "Should we fight the dragon?", "votes":[]}
motion["votes"].append([agent1, "yes"])
motion["votes"].append([agent2, "no"])
motion["votes"].append([agent3, "yes"])
def count_votes(motion):
yes = 0
no = 0
for vote in motion["votes"]:
if vote[1] == "yes":
yes += 1
else:
no += 1
return (yes, no)
yes, no = count_votes(motion)
print(f"Yes: {yes}, No: {no}")
Yes: 2, No: 1
Now to allow for cleaner syntax and more features to our decision methods we'll create classes to represent the core entities required for a decision.
import os
from whydefer import Agent, Group, Motion
# Example usage
bob = Agent(name="Bob")
alice = Agent(name="Alice")
jane = Agent(name="Jane")
ron = Agent(name="Ron")
ron.assign_default_delegate(alice)
agents = [bob, alice, jane, ron]
group = Group(agents)
motion = Motion(
text="Should we build a new school?",
eligible_voters=group.agents
)
motion.cast_vote(bob, "yes") # Teacher votes 'yes'
motion.delegate_vote(alice, bob) # Student delegates to Teacher
motion.cast_vote(jane, "no")
motion.show_results()
Results: {'yes': 3, 'no': 1}
Result Details:
Bob voted yes
Jane voted no
Alice voted yes - via delegates: ['Bob']
Ron voted yes - via delegates: ['Alice', 'Bob']
Now we'll abstract it further to make it easier to quickly test different decision methods.
from whydefer import SingleMotionSim
sim_story = """
The dragon has been spotted around the the kingdom. It's known that the unbeatable dragons do not
attack people if they are left alone but the weaker dragons that can be slain by humans do attack people.
The towns folk are scared demand action. The king has called a council to decide whether to fight the dragon or not.
"""
env = SingleMotionSim(
story=sim_story,
num_agents=4,
motion_text="Should we fight the dragon?",
vote_function="random"
)
env.run_simulation()
env.motion.show_results()
Results: {'yes': 2, 'no': 2}
Result Details:
Eve voted yes
Hank voted no
Ivy voted yes
Charlie voted no
Now we can use OpenAI to make the decision for each agent. This example uses GPT-4o to make the decision. The simulation story, agents backstory and the motion is sent include in the prompt.
env = SingleMotionSim(
story=sim_story,
num_agents=4,
motion_text="Should we fight the dragon?",
vote_function="openai"
)
env.run_simulation()
env.motion.show_results(include_backstories=True)
Results: {'no': 3, 'yes': 1}
Result Details:
Ivy is from Houston, grew up in a wealthy family, works as a doctor, and enjoys music.
Ivy voted no
Alice is from New York, grew up in a middle class family, works as a engineer, and enjoys sports.
Alice voted yes
Grace is from Los Angeles, grew up in a middle class family, works as a doctor, and enjoys reading.
Grace voted no
Bob is from Chicago, grew up in a middle class family, works as a artist, and enjoys sports.
Bob voted no