Created
October 17, 2023 21:29
-
-
Save perryism/34ae48ae2124e111c7c3c44a73dbaf11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from langchain.prompts import PromptTemplate | |
from langchain.llms import OpenAI | |
import logging | |
import sys | |
logging.basicConfig(stream=sys.stdout, level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
class Agent: | |
def __init__(self, role, objective): | |
self.role = role | |
self.objective = objective | |
def prompt(self, llm, topic, prev_msg ,history): | |
prompt_template = PromptTemplate.from_template( | |
"""You are {role}. Your objective is to {objective}. The topic is {topic}. | |
This is the previous converstaions: | |
{history} | |
This is what your opponent said | |
{prev_msg} | |
Respond to your opponent based on your role | |
""" | |
) | |
prompt = prompt_template.format(topic=topic.topic, role=self.role, objective=self.objective, history="\n".join(history), prev_msg=prev_msg) | |
logger.debug(prompt) | |
return llm(prompt) | |
class Topic: | |
def __init__(self, topic): | |
self.topic = topic | |
class Debate: | |
def __init__(self, affirmative: Agent, negative: Agent, topic: Topic): | |
self.affirmative = affirmative | |
self.negative = negative | |
self.topic = topic | |
def go(self, llm): | |
history = [] | |
instruction = f"Discuss {self.topic}" | |
with open(self.topic.topic.lower().replace(" ", "_") + ".txt", "w") as f: | |
for i in range(1): | |
affirm_msg = self.affirmative.prompt(llm, topic, instruction, history) | |
print("Affirm:", affirm_msg) | |
f.write("Affirmative") | |
f.write(affirm_msg) | |
f.write("-" * 50) | |
negative_msg = self.negative.prompt(llm, topic, affirm_msg, history) | |
print("Negative:", negative_msg) | |
f.write("Negation") | |
f.write(negative_msg) | |
f.write("-" * 50) | |
affirm_msg = self.summarize(llm, affirm_msg) | |
negative_msg = self.summarize(llm, negative_msg) | |
history.append(f"Affirmative: {affirm_msg}") | |
history.append(f"Negative: {negative_msg}") | |
instruction = f"Respond to the following arguments: {negative_msg}" | |
def summarize(self, llm, msg): | |
prompt_template = PromptTemplate.from_template(""" | |
Summarize the following: | |
{msg} | |
""" | |
) | |
return llm(prompt_template.format(msg=msg)) | |
a = Agent("Assertative", objective="Convince others you strongly agree with the topic. ") | |
b = Agent("Negation", objective="Convince others that you strongly disagree with the topic. Summerize your opponent's points first, and respond respectively.") | |
topic = Topic("Students should wear uniform to school. ") | |
debate = Debate(a, b, topic) | |
llm = OpenAI(temperature=0.4, model_name="gpt-3.5-turbo") | |
debate.go(llm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment