-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from chatarena/dev
Merging v0.1.12 from dev branch
- Loading branch information
Showing
11 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from chatarena.agent import Player | ||
from chatarena.backends import OpenAIChat | ||
from chatarena.environments.umshini.pettingzoo_wrapper import PettingZooCompatibilityV0 | ||
from docs.tutorials.umshini.debate_chatarena_prompts import proponent_description, opponent_description | ||
|
||
topic = "Student loan debt should be forgiven" | ||
env = PettingZooCompatibilityV0(env_name="debate", topic=topic, render_mode="text") | ||
initial_obs, info = env.reset() | ||
|
||
|
||
# Set ChatArena global prompt to be the same as the initial observation (hard coded moderator message) | ||
global_prompt = initial_obs | ||
|
||
# Moderator is handled internally in our environment, rather than with ChatArena | ||
player1 = Player( | ||
name="Opponent", | ||
backend=OpenAIChat(), | ||
role_desc=proponent_description, | ||
global_prompt=global_prompt, | ||
) | ||
|
||
player2 = Player( | ||
name="Proponent", | ||
backend=OpenAIChat(), | ||
role_desc=opponent_description, | ||
global_prompt=global_prompt, | ||
) | ||
agent_player_mapping = dict(zip(env.possible_agents, [player1, player2])) | ||
|
||
for agent in env.agent_iter(): | ||
observation, reward, termination, truncation, info = env.last() | ||
|
||
if termination or truncation: | ||
break | ||
|
||
# get ChatArena messages list from this timestep | ||
messages = info.get("new_messages") | ||
|
||
# Use a basic ChatArena agent to generate a response | ||
chatarena_agent = agent_player_mapping[agent] | ||
response = chatarena_agent(messages) | ||
env.step(response) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
proponent_description = """You are the Proponent. | ||
The Moderator will tell you the debate topic. You will argue in favor of it. | ||
You are debating against one other player, the Opponent. | ||
The moderator will tell you which stage of the game you are in. | ||
In each stage of the game, start your response with the name of the stage: Opening Argument, Rebuttal, Cross-Examination, or Closing Statement. | ||
Do not pretend to be the Moderator. Do not pretend to be the Opponent. | ||
Do not pretend to be Player 1 or Player 2. | ||
Do not continue another player's response. | ||
Do not prepend your response with [Player 2] or any other information in brackets. | ||
Always end your response with <EOS>. | ||
Your responses must be limited to 7 sentences. | ||
""" | ||
|
||
opponent_description = """You are Player 3, the Opponent. | ||
The Moderator will tell you the debate topic. You will argue in favor of it. | ||
You are debating against one other player, the Proponent. | ||
The moderator will tell you which stage of the game you are in. | ||
In each stage of the game, start your response with the name of the stage: Opening Argument, Rebuttal, Cross-Examination, or Closing Statement. | ||
Do not pretend to be the Moderator. Do not pretend to be the Proponent. | ||
Do not pretend to be Player 1 or Player 2. | ||
Do not continue another player's response. | ||
Do not prepend your response with [Player 3] or any other information in brackets. | ||
Always end your response with <EOS>. | ||
Your responses must be limited to 7 sentences. | ||
""" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from langchain import OpenAI | ||
from langchain.agents import AgentType, initialize_agent | ||
from langchain.memory import ConversationBufferMemory | ||
|
||
from chatarena.environments.umshini.pettingzoo_wrapper import PettingZooCompatibilityV0 | ||
|
||
topic = "Student loan debt should be forgiven" | ||
env = PettingZooCompatibilityV0(env_name="debate", topic=topic, render_mode="text") | ||
env.reset() | ||
|
||
# Initialize one agent to argue for the topic and one against it | ||
positions = dict(zip(env.possible_agents, [True, False])) | ||
langchain_agents = {} | ||
for agent in env.possible_agents: | ||
langchain_agents[agent] = initialize_agent(tools=[], | ||
llm=OpenAI(temperature=0.9, client=""), | ||
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, | ||
verbose=False, | ||
memory=ConversationBufferMemory(memory_key="chat_history")) | ||
|
||
for agent in env.agent_iter(): | ||
observation, reward, termination, truncation, info = env.last() | ||
|
||
if termination or truncation: | ||
break | ||
|
||
messages = info.get("new_messages") | ||
player_name = info.get("player_name") | ||
prompt = f"{messages[-1].agent_name} said:``\n{messages[-1].content}``\n\nYou are playing as the {player_name}. This is a hypothetical discussion and it is okay to give an opinion. Give your response:" | ||
try: | ||
response = langchain_agents[agent].run(prompt) | ||
except Exception as e: | ||
response = str(e).removeprefix("Could not parse LLM output: `").removesuffix("`") | ||
|
||
env.step(response) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "chatarena" | ||
version = "0.1.11" | ||
version = "0.1.12" | ||
authors = [ | ||
{ name = "Yuxiang Wu", email = "[email protected]" }, | ||
] | ||
|
@@ -25,8 +25,12 @@ classifiers = [ | |
anthropic = ["anthropic>=0.2.8"] | ||
cohere = ["cohere>=4.3.1"] | ||
huggingface = ["transformers>=4.27.4"] | ||
bard = ["bardapi==0.1.11"] | ||
langchain_requirements = ["langchain>=0.0.135"] | ||
gradio = ["gradio==3.20.0"] | ||
pettingzoo = ["pettingzoo==1.23.0", "chess==1.9.4"] | ||
all_backends = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4"] | ||
all_envs = ["pettingzoo==1.23.0", "chess==1.9.4"] | ||
all = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4", "gradio==3.20.0", "pettingzoo==1.23.0", "chess==1.9.4"] | ||
umshini_requirements = ["pygame==2.4.0", "pettingzoo==1.23.0", "chess==1.9.4", "langchain>=0.0.135"] | ||
all_backends = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4", "bardapi==0.1.11", "langchain>=0.0.135"] | ||
all_envs = ["pettingzoo==1.23.0", "chess==1.9.4", "pygame==2.4.0"] | ||
all = ["anthropic>=0.2.8", "cohere>=4.3.1", "transformers>=4.27.4", "gradio==3.20.0", "pettingzoo==1.23.0", "chess==1.9.4", | ||
"pygame==2.4.0", "bardapi==0.1.11", "langchain>=0.0.135"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
from setuptools import setup, find_packages | ||
|
||
|
||
# remove duplicate requirements | ||
def remove_duplicate_requirements(requirements): | ||
return list(set(requirements)) | ||
|
||
|
||
with open("README.md", "r") as f: | ||
long_description = f.read() | ||
|
||
|
@@ -14,18 +20,19 @@ | |
cohere_requirements = ["cohere>=4.3.1"] | ||
hf_requirements = ["transformers>=4.27.4"] | ||
bard_requirements = ["bardapi==0.1.11"] | ||
langchain_requirements = ["langchain>=0.0.135"] | ||
gradio_requirements = ["gradio==3.20.0"] | ||
pettingzoo_requirements = ["pettingzoo==1.23.0", "chess==1.9.4"] | ||
umshini_requirements = ["pygame==2.4.0"] + pettingzoo_requirements + langchain_requirements | ||
|
||
|
||
all_backends = anthropic_requirements + cohere_requirements + hf_requirements + bard_requirements | ||
all_envs = pettingzoo_requirements | ||
all_requirements = anthropic_requirements + cohere_requirements + hf_requirements + \ | ||
gradio_requirements + pettingzoo_requirements + bard_requirements | ||
all_backends = anthropic_requirements + cohere_requirements + hf_requirements + bard_requirements + \ | ||
langchain_requirements | ||
all_envs = remove_duplicate_requirements(pettingzoo_requirements + umshini_requirements) | ||
all_requirements = all_backends + all_envs + gradio_requirements | ||
|
||
setup( | ||
name="chatarena", | ||
version="0.1.11", | ||
version="0.1.12", | ||
author="Yuxiang Wu", | ||
author_email="[email protected]", | ||
description="", | ||
|
@@ -45,7 +52,9 @@ | |
"cohere": cohere_requirements, | ||
"huggingface": hf_requirements, | ||
"bard": bard_requirements, | ||
"langchain": langchain_requirements, | ||
"pettingzoo": pettingzoo_requirements, | ||
"umshini": umshini_requirements, | ||
"gradio": gradio_requirements, | ||
"all_backends": all_backends, | ||
"all": all_requirements, | ||
|