Skip to content

Instantly share code, notes, and snippets.

@beatty
Created March 29, 2023 20:48
Show Gist options
  • Save beatty/982c6cf92faac6ec6606411d22f1e223 to your computer and use it in GitHub Desktop.
Save beatty/982c6cf92faac6ec6606411d22f1e223 to your computer and use it in GitHub Desktop.
from dotenv import load_dotenv
load_dotenv()
import openai
from enum import Enum
OPENAI_CHAT_MODEL = "gpt-3.5-turbo"
class Color(Enum):
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
RESET = '\033[0m'
def colorize(text: str, color: Color) -> str:
return f"{color.value}{text}{Color.RESET.value}"
SYSTEM_INSTRUCTION_TEMPLATE = "You are an expert in my field and your are my real-time assistant in an effort to draft a joint document that we can all agree on."
USER_INSTRUCTION_TEMPLATE = "My name is $NAME and I am a $ROLE. Our shared is to $GOAL. My private goal is $PRIVATE_GOAL."
class Agent:
def __init__(self, name: str, role: str, goal: str, private_goal: str, beliefs: str, color: Color):
self.name = name
self.role = role
self.goal = goal
self.private_goal = private_goal
self.beliefs = beliefs
self.color = color
self.memory = None
def __str__(self):
return self.name
def initialize(self):
system_instruction = SYSTEM_INSTRUCTION_TEMPLATE
user_instruction = USER_INSTRUCTION_TEMPLATE.replace("$NAME", self.name).replace("$ROLE", self.role).replace(
"$GOAL", self.goal).replace("$PRIVATE_GOAL", self.private_goal)
self.memory = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_instruction}, {"role": "user", "content": f"Here are my strongly held views: {self.beliefs}"}]
def set_current_proposal(self, current_proposal):
self.initialize()
self.memory.append({"role": "user", "content": f"Here is the current proposal: {current_proposal}"})
def __repr__(self):
return f"{self.name} ({self.description})"
def make_drafter(self, participants):
self.role = "drafter"
self.participants = participants
def make_participant(self, drafter):
self.role = "participant"
self.drafter = drafter
def provide_feedback(self, agent, feedback):
self.memory.append({"role": "user", "content": f"{agent.name} said: {feedback}"})
def _dump_memory(agent: Agent):
print(f"=== MEMORY {colorize(agent.name, agent.color)} ===")
for i, msg in enumerate(agent.memory):
print(f" {i:2d} {msg['role']:6s} {msg['content']}")
def extract_completion(completion):
return completion['choices'][0]['message']['content']
goal = "write a collective letter laying out a set of shared principles all signatories will agree to regarding responsible AGI research."
drafter = Agent("Max Tegmark", "President of Future of Life Institute", goal, "slow down AGI research", "AGI will kill us all", Color.RED)
participants = [Agent("Tyler Cowen", "Economist", goal, "Accelerate AGI research", "We need more intelligence. Stasis is death. Safetyism leads to stagnation. Stagnation risks statis.", Color.CYAN),
Agent("Stuart Russell", "AI researcher", goal, "slow down AGI research", "Responsible AGI is great, but the dangers are great, and we must be very very careful", Color.MAGENTA),
Agent("Yuval Noah Harari", "historian", goal, "slow down AGI research and regulate it by global governments", "The explosive power of artificial intelligence must be regulated by world governments", Color.MAGENTA)]
drafter.make_drafter(participants)
for participant in participants:
participant.make_participant(drafter)
drafter.initialize()
for participant in participants:
participant.initialize()
drafter.memory.extend([{"role": "user", "content": "Write an initial formal proposal that is meant to be signed by all parcipants. Just include the proposal, no greeting or closing. Use bullet points. Be aggressive in representing my strongly-held views. Use my vast knowledge wisdom of my intellectual tradition. Use formal language. Don't expose my private goal."}])
completion = openai.ChatCompletion.create(
model=OPENAI_CHAT_MODEL,
messages=drafter.memory,
max_tokens=500,
)
current_proposal = extract_completion(completion)
drafter.set_current_proposal(current_proposal)
current_round = 0
MAX_ROUNDS = 5
PARTICIPANT_INSTRUCTION = "Write 3 bullet brief points of feedback to the current proposal from my perspective. Then Rate the current proposal on a 'Willingness to Sign' scale of 1-10 of (1=definitely no, 10=definitely yes). Be aggressive in representing my strongly-held views. Use my vast knowledge wisdom of my intellectual tradition. Use some metaphors. No greeting or closing or preamble."
print(f"# Negotiation")
while current_round < MAX_ROUNDS:
print(f"## Round {current_round}")
print(f"### Current proposal:\n{current_proposal}")
current_round += 1
for participant in participants:
participant.set_current_proposal(current_proposal)
participant.memory.extend([{"role": "user", "content": PARTICIPANT_INSTRUCTION}])
completion = openai.ChatCompletion.create(
model=OPENAI_CHAT_MODEL,
messages=participant.memory,
max_tokens=300,
)
completion_text = extract_completion(completion)
print(f"### {colorize(participant.name, participant.color)}'s response\n{completion_text}\n\n")
drafter.provide_feedback(participant, completion_text)
drafter.memory.extend([{"role": "user", "content": "Take the feedback and synthesize a new draft proposal that increases average Willingness To Sign amongst all participants."}])
completion = openai.ChatCompletion.create(
model=OPENAI_CHAT_MODEL,
messages=drafter.memory,
max_tokens=500,
)
current_proposal = extract_completion(completion)
#time.sleep(2)
print(f"# Final proposal")
print(current_proposal)
signers = drafter.name
for participant in participants:
signers += f", {participant.name}"
completion = openai.ChatCompletion.create(
model=OPENAI_CHAT_MODEL,
messages=[{"role": "system", "content": "You are an expert drafter of constitutions and manifestos"},
{"role": "user", "content": f"The final proposal is: \n===\n{current_proposal}\n===\n"},
{"role": "user", "content": f"The signers are: {signers}"},
{"role": "user", "content": f"Write a joint letter with beautiful premable saying that the undersigned all agree to this proposal. Include the final proposal verbatim. Sign the letters by the signers: {current_proposal}"}],
max_tokens=1000,
)
final_letter = extract_completion(completion)
print(f"# Final letter")
print(final_letter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment