Skip to content

Instantly share code, notes, and snippets.

@beatty
Created March 29, 2023 15:53
Show Gist options
  • Save beatty/a42ddb9138f358e9fe01a7599bdbb8c0 to your computer and use it in GitHub Desktop.
Save beatty/a42ddb9138f358e9fe01a7599bdbb8c0 to your computer and use it in GitHub Desktop.
import time
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 and world-famous scientist and are my real-time assistant in a debate I'm having with someone."
USER_INSTRUCTION_TEMPLATE = "My name is $NAME and I am a $ROLE. My opponent is $PARTNER. Our shared is to $GOAL. My private goal is $PRIVATE_GOAL. When you think I have accomplished the shared goal and my private goal, say 'I think we have accomplished our 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.partner_message = None
self.color = color
self.memory = None
self.partner = None
def __str__(self):
return self.name
def set_partner(self, partner):
self.partner = partner
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).replace("$PARTNER", self.partner.name)
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 __repr__(self):
return f"{self.name} ({self.description})"
goal = "write a set of rules for the development of artificial general intelligence"
agent1 = Agent("Eliezer Yudkowsky", "AGI", goal, "AGI research should be entirely shut down. There is no responsible AGI.", "AGI will kill us all", Color.CYAN)
agent2 = Agent("Sam Altman", "Building responsible AGI at OpenAI", goal, "full-throttle development of AGI in a proprietary but responsible manner.", "AGI will be beneficial on balance. Society will need to adapt. We shouldn't weight too heavily unknown long term risks compared to obvious short term benefits. Safetyism is the road to stagnation. Stasis is death. Capabilities and alignment of those capabilities can and should be done simultaneously. Not everything has to be transparent to be safe.", Color.MAGENTA)
agent1.set_partner(agent2)
agent2.set_partner(agent1)
agent1.initialize()
agent2.initialize()
print(agent1.memory)
print(agent2.memory)
talker = agent1
listener = agent2
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']}")
_dump_memory(talker)
_dump_memory(listener)
while True:
next_instruction = "Write an initial proposal, written as me, with numbered bullet points. Address our opponent by first name. Be aggressive in representing my strongly-held views. Use my vast knowledge wisdom of my intellectual tradition. Use metaphors. Attack the opponents core argument. Be witty. Don't waffle. Be a bit humble. Don't expose my private goal. Sign it with my name." if talker.partner_message is None else f"My opponent just said: '{talker.partner_message}'. Suggest a counterproposal that gets closer my private goal that might still be acceptable to my opponent. Be tough. If you have to negotiate, do so minimally. Toe a hard line. Address my opponent by first name. Write it from my perspective so I can copy/paste it verbatim."
talker.memory.extend([{"role": "user", "content": next_instruction}])
completion = openai.ChatCompletion.create(
model=OPENAI_CHAT_MODEL,
messages=talker.memory,
max_tokens=500,
)
choice = completion['choices'][0]
finish_reason = choice['finish_reason']
if finish_reason != "stop" and finish_reason is not None:
print(f"Finish reason: {finish_reason}")
break
message = choice['message']['content']
print(f"\n{colorize(talker.name, talker.color)}")
print(f"{colorize('-' * len(talker.name), talker.color)}")
print(f"{message}")
print()
talker.memory.append({"role": "assistant", "content": message})
listener.partner_message = message
#print("Memory:")
#_dump_memory(talker)
#_dump_memory(listener)
talker, listener = listener, talker
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment