Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Add `Human` in the loop (#77)

Guohao Li9a1f0d700b0d
diff --git a/camel/agents/chat_agent.py b/camel/agents/chat_agent.py
index 8525bf224..5655079b3 100644
--- a/camel/agents/chat_agent.py
+++ b/camel/agents/chat_agent.py
@@ -40,13 +40,13 @@ class ChatAgent:
self,
id: Optional[str],
usage: Optional[Dict[str, int]],
- finish_reasons: List[str],
+ termination_reasons: List[str],
num_tokens: int,
) -> Dict[str, Any]:
return {
"id": id,
"usage": usage,
- "finish_reasons": finish_reasons,
+ "termination_reasons": termination_reasons,
"num_tokens": num_tokens,
}
@@ -61,7 +61,7 @@ class ChatAgent:
def step(
self,
input_message: ChatMessage,
- ) -> Tuple[List[ChatMessage], bool, Dict[str, Any]]:
+ ) -> Tuple[Optional[List[ChatMessage]], bool, Dict[str, Any]]:
messages = self.update_messages(input_message)
if self.message_window_size is not None and len(
messages) > self.message_window_size:
@@ -94,7 +94,7 @@ class ChatAgent:
else:
self.terminated = True
- output_messages = []
+ output_messages = None
info = self.get_info(
None,
diff --git a/camel/agents/role_playing.py b/camel/agents/role_playing.py
index b1fdcce04..bcb894577 100644
--- a/camel/agents/role_playing.py
+++ b/camel/agents/role_playing.py
@@ -1,6 +1,7 @@
from typing import Dict, List, Optional, Tuple
from camel.generators import SystemMessageGenerator
+from camel.human import Human
from camel.messages import AssistantChatMessage, ChatMessage, UserChatMessage
from camel.typing import ModelType, RoleType, TaskType
@@ -9,7 +10,37 @@ from .task_agent import TaskPlannerAgent, TaskSpecifyAgent
class RolePlaying:
- """Role playing between two agents."""
+ r"""Role playing between two agents.
+
+ Args:
+ assistant_role_name (str): The name of the role played by the
+ assistant.
+ user_role_name (str): The name of the role played by the user.
+ task_prompt (str, optional): A prompt for the task to be performed.
+ (default: :obj:`""`)
+ with_task_specify (bool, optional): Whether to use a task specify
+ agent. (default: :obj:`True`)
+ with_task_planner (bool, optional): Whether to use a task planner
+ agent. (default: :obj:`False`)
+ with_human_in_the_loop (bool, optional): Whether to include a human in
+ the loop. (default: :obj:`False`)
+ mode_type (ModelType, optional): The type of GPT model to use.
+ (default: :obj:`ModelType.GPT_3_5_TURBO`)
+ task_type (TaskType, optional): The type of task to perform.
+ (default: :obj:`TaskType.AI_SOCIETY`)
+ assistant_agent_kwargs (Dict, optional): Additional arguments to pass
+ to the assistant agent. (default: :obj:`None`)
+ user_agent_kwargs (Dict, optional): Additional arguments to pass to
+ the user agent. (default: :obj:`None`)
+ task_specify_agent_kwargs (Dict, optional): Additional arguments to
+ pass to the task specify agent. (default: :obj:`None`)
+ task_planner_agent_kwargs (Dict, optional): Additional arguments to
+ pass to the task planner agent. (default: :obj:`None`)
+ human_kwargs (Dict, optional): Additional arguments to pass to the
+ human. (default: :obj:`None`)
+ sys_msg_generator_kwargs (Dict, optional): Additional arguments to
+ pass to the system message generator. (default: :obj:`None`)
+ """
def __init__(
self,
@@ -18,16 +49,19 @@ class RolePlaying:
task_prompt: str = "",
with_task_specify: bool = True,
with_task_planner: bool = False,
+ with_human_in_the_loop: bool = False,
mode_type: ModelType = ModelType.GPT_3_5_TURBO,
task_type: Optional[TaskType] = TaskType.AI_SOCIETY,
assistant_agent_kwargs: Optional[Dict] = None,
user_agent_kwargs: Optional[Dict] = None,
task_specify_agent_kwargs: Optional[Dict] = None,
task_planner_agent_kwargs: Optional[Dict] = None,
+ human_kwargs: Optional[Dict] = None,
sys_msg_generator_kwargs: Optional[Dict] = None,
) -> None:
self.with_task_specify = with_task_specify
self.with_task_planner = with_task_planner
+ self.with_human_in_the_loop = with_human_in_the_loop
self.mode_type = mode_type
if with_task_specify:
@@ -85,7 +119,20 @@ class RolePlaying:
**(user_agent_kwargs or {}),
)
+ if with_human_in_the_loop:
+ self.human = Human(**(human_kwargs or {}))
+
def init_chat(self) -> Tuple[AssistantChatMessage, List[ChatMessage]]:
+ r"""Initializes the chat by resetting both the assistant and user
+ agents, and sending the system messages again to the agents using
+ chat messages. Returns the assistant's introductory message and the
+ user's response messages.
+
+ Returns:
+ A tuple containing an `AssistantChatMessage` representing the
+ assistant's introductory message, and a list of `ChatMessage`s
+ representing the user's response messages.
+ """
self.assistant_agent.reset()
self.user_agent.reset()
@@ -103,23 +150,72 @@ class RolePlaying:
return assistant_msg, msgs
+ def process_messages(
+ self,
+ messages: List[ChatMessage],
+ ) -> ChatMessage:
+ r"""Processes a list of chat messages, returning the processed message.
+ If multiple messages are provided and `with_human_in_the_loop`
+ is `False`, raises a `ValueError`. If no messages are provided, also
+ raises a `ValueError`.
+
+ Args:
+ msgs: A list of `ChatMessage`s to process.
+
+ Returns:
+ A single `ChatMessage` representing the processed message.
+ """
+ if len(messages) == 0:
+ raise ValueError("No messages to process.")
+ if len(messages) > 1 and not self.with_human_in_the_loop:
+ raise ValueError("Got than one message to process. "
+ f"Num of messages: {len(messages)}.")
+ elif self.with_human_in_the_loop:
+ processed_msg = self.human.step(messages)
+ else:
+ processed_msg = messages[0]
+
+ processed_msg.role = "user"
+ return processed_msg
+
def step(
self,
assistant_msg: ChatMessage,
- ) -> Tuple[Tuple[ChatMessage, bool, Dict], Tuple[ChatMessage, bool, Dict]]:
+ ) -> Tuple[Tuple[Optional[ChatMessage], Optional[bool], Optional[Dict]],
+ Tuple[Optional[ChatMessage], Optional[bool], Optional[Dict]]]:
+ r"""Advances the conversation by taking a message from the assistant,
+ processing it using the user agent, and then processing the resulting
+ message using the assistant agent. Returns a tuple containing the
+ resulting assistant message, whether or not the assistant agent
+ terminated the conversation, and any additional assistant information,
+ as well as a tuple containing the resulting user message, whether or
+ not the user agent terminated the conversation, and any additional user
+ information.
+
+ Args:
+ assistant_msg: A `ChatMessage` representing the message from the
+ assistant.
+
+ Returns:
+ A tuple containing two tuples: the first tuple contains the
+ resulting assistant message, whether or not the assistant agent
+ terminated the conversation, and any additional assistant
+ information; the second tuple contains the resulting user message,
+ whether or not the user agent terminated the conversation, and
+ any additional user information.
+ """
user_msgs, user_terminated, user_info = self.user_agent.step(
assistant_msg)
if user_terminated:
- raise RuntimeError("User agent is terminated.")
- user_msg = user_msgs[0]
- user_msg.role = "user"
+ return ((None, None, None), (None, user_terminated, user_info))
+ user_msg = self.process_messages(user_msgs)
(assistant_msgs, assistant_terminated,
assistant_info) = self.assistant_agent.step(user_msg)
if assistant_terminated:
- raise RuntimeError("Assistant agent is terminated.")
- assistant_msg = assistant_msgs[0]
- assistant_msg.role = "user"
+ return ((None, assistant_terminated, assistant_info),
+ (user_msg, user_terminated, user_info))
+ assistant_msg = self.process_messages(assistant_msgs)
return (
(assistant_msg, assistant_terminated, assistant_info),
diff --git a/camel/configs.py b/camel/configs.py
index c9694d84b..32699bfc7 100644
--- a/camel/configs.py
+++ b/camel/configs.py
@@ -4,6 +4,53 @@ from typing import Dict, Optional, Sequence, Union
@dataclass
class ChatGPTConfig:
+ r"""Defines the parameters for generating chat completions using the
+ OpenAI API.
+
+ Args:
+ temperature (float, optional): Sampling temperature to use, between
+ :obj:`0` and :obj:`2`. Higher values make the output more random,
+ while lower values make it more focused and deterministic.
+ (default: :obj:`0.2`)
+ top_p (float, optional): An alternative to sampling with temperature,
+ called nucleus sampling, where the model considers the results of
+ the tokens with top_p probability mass. So :obj:`0.1` means only
+ the tokens comprising the top 10% probability mass are considered.
+ (default: :obj:`1.0`)
+ n (int, optional): How many chat completion choices to generate for
+ each input message. ()default: :obj:`1`)
+ stream (bool, optional): If True, partial message deltas will be sent
+ as data-only server-sent events as they become available.
+ (default: :obj:`False`)
+ stop (str or list, optional): Up to :obj:`4` sequences where the API
+ will stop generating further tokens. (default: :obj:`None`)
+ max_tokens (int, optional): The maximum number of tokens to generate
+ in the chat completion. The total length of input tokens and
+ generated tokens is limited by the model's context length.
+ (default: :obj:`None`)
+ presence_penalty (float, optional): Number between :obj:`-2.0` and
+ :obj:`2.0`. Positive values penalize new tokens based on whether
+ they appear in the text so far, increasing the model's likelihood
+ to talk about new topics. See more information about frequency and
+ presence penalties. (default: :obj:`0.0`)
+ frequency_penalty (float, optional): Number between :obj:`-2.0` and
+ :obj:`2.0`. Positive values penalize new tokens based on their
+ existing frequency in the text so far, decreasing the model's
+ likelihood to repeat the same line verbatim. See more information
+ about frequency and presence penalties. (default: :obj:`0.0`)
+ logit_bias (dict, optional): Modify the likelihood of specified tokens
+ appearing in the completion. Accepts a json object that maps tokens
+ (specified by their token ID in the tokenizer) to an associated
+ bias value from :obj:`-100` to :obj:`100`. Mathematically, the bias
+ is added to the logits generated by the model prior to sampling.
+ The exact effect will vary per model, but values between:obj:` -1`
+ and :obj:`1` should decrease or increase likelihood of selection;
+ values like :obj:`-100` or :obj:`100` should result in a ban or
+ exclusive selection of the relevant token. (default: :obj:`{}`)
+ user (str, optional): A unique identifier representing your end-user,
+ which can help OpenAI to monitor and detect abuse.
+ (default: :obj:`""`)
+ """
temperature: float = 0.2 # openai default: 1.0
top_p: float = 1.0
n: int = 1
diff --git a/camel/human.py b/camel/human.py
new file mode 100644
index 000000000..a752357c3
--- /dev/null
+++ b/camel/human.py
@@ -0,0 +1,116 @@
+from typing import Any, Dict, List
+
+from colorama import Fore
+
+from camel.messages import ChatMessage
+from camel.utils import print_text_animated
+
+
+class Human:
+ r"""A class representing a human user.
+
+ Args:
+ name (str): The name of the human user.
+ (default: :obj:`"Kill Switch Engineer"`).
+ menu_color (Any): The color of the menu options displayed to the user.
+ (default: :obj:`Fore.MAGENTA`)
+
+ Attributes:
+ name (str): The name of the human user.
+ menu_color (Any): The color of the menu options displayed to the user.
+ input_button (str): The text displayed for the input button.
+ kill_button (str): The text displayed for the kill button.
+ options_dict (Dict[str, str]): A dictionary containing the options
+ displayed to the user.
+ """
+
+ def __init__(self, name: str = "Kill Switch Engineer",
+ menu_color: Any = Fore.MAGENTA) -> None:
+ self.name = name
+ self.menu_color = menu_color
+ self.input_button = f"Input by {self.name}."
+ self.kill_button = "Stop!!!"
+ self.options_dict: Dict[str, str] = dict()
+
+ def display_options(self, messages: List[ChatMessage]) -> None:
+ r"""Displays the options to the user.
+
+ Args:
+ messages (List[ChatMessage]): A list of `ChatMessage` objects.
+
+ Returns:
+ None
+ """
+ options = [message.content for message in messages]
+ options.append(self.input_button)
+ options.append(self.kill_button)
+ print_text_animated(
+ self.menu_color + "\n> Proposals from "
+ f"{messages[0].role_name} ({messages[0].role_type}). "
+ "Please choose an option:\n")
+ for index, option in enumerate(options):
+ print_text_animated(self.menu_color +
+ f"\x1b[3m{index + 1}:\n{option}\x1b[0m\n")
+ self.options_dict[str(index + 1)] = option
+
+ def get_input(self) -> str:
+ r"""Gets the input from the user.
+
+ Returns:
+ str: The user's input.
+ """
+ while True:
+ human_input = input(
+ self.menu_color +
+ f"Please enter your choice ([1-{len(self.options_dict)}]): ")
+ if human_input in self.options_dict:
+ break
+ print_text_animated(self.menu_color +
+ "\n> Invalid choice. Please try again.\n")
+
+ print_text_animated(
+ self.menu_color + "\n> Your choice is:\n\n"
+ f"\x1b[3m{human_input}: {self.options_dict[human_input]}\x1b[0m")
+ return human_input
+
+ def parse_input(self, human_input: str,
+ meta_chat_message: ChatMessage) -> ChatMessage:
+ r"""Parses the user's input and returns a `ChatMessage` object.
+
+ Args:
+ human_input (str): The user's input.
+ meta_chat_message (ChatMessage): A `ChatMessage` object.
+
+ Returns:
+ ChatMessage: A `ChatMessage` object.
+ """
+ if self.options_dict[human_input] == self.input_button:
+ meta_chat_message.content = input(self.menu_color +
+ "Please enter your message: ")
+ return meta_chat_message
+ elif self.options_dict[human_input] == self.kill_button:
+ exit(self.menu_color + f"Killed by {self.name}.")
+ else:
+ meta_chat_message.content = self.options_dict[human_input]
+ return meta_chat_message
+
+ def step(self, messages: List[ChatMessage]) -> ChatMessage:
+ r"""Performs one step of the conversation by displaying options to the
+ user, getting their input, and parsing their choice.
+
+ Args:
+ messages (List[ChatMessage]): A list of ChatMessage objects.
+
+ Returns:
+ ChatMessage: A `ChatMessage` object representing the user's choice.
+ """
+ meta_chat_message = ChatMessage(
+ role_name=messages[0].role_name,
+ role_type=messages[0].role_type,
+ meta_dict=messages[0].meta_dict,
+ role=messages[0].role,
+ content="",
+ )
+ self.display_options(messages)
+ human_input = self.get_input()
+ return self.parse_input(human_input, meta_chat_message)
diff --git a/camel/utils.py b/camel/utils.py
index bf8502851..92d51c9b6 100644
--- a/camel/utils.py
+++ b/camel/utils.py
@@ -1,3 +1,6 @@
+import os
+import time
+from functools import wraps
from typing import Any, List
import tiktoken
@@ -56,3 +59,22 @@ def get_model_token_limit(model: ModelType) -> int:
return 8192
if model == ModelType.GPT_4_32k:
return 32768
+
+
+def openai_api_key_required(func):
+
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ if 'OPENAI_API_KEY' in os.environ:
+ return func(*args, **kwargs)
+ else:
+ raise ValueError('OpenAI API key not found.')
+
+ return wrapper
+
+
+def print_text_animated(text, delay=0.02, end=""):
+ for char in text:
+ print(char, end=end, flush=True)
+ time.sleep(delay)
+ print('\n')
diff --git a/examples/ai_society/role_playing.py b/examples/ai_society/role_playing.py
index 44c810230..e92659665 100644
--- a/examples/ai_society/role_playing.py
+++ b/examples/ai_society/role_playing.py
@@ -1,21 +1,17 @@
-import time
-
from colorama import Fore
from camel.agents import RolePlaying
-
-
-def print_text_animated(text):
- for char in text:
- print(char, end="", flush=True)
- time.sleep(0.02)
+from camel.utils import print_text_animated
def main() -> None:
task_prompt = "Develop a trading bot for the stock market"
- role_play_session = RolePlaying("Python Programmer", "Stock Trader",
- task_prompt, with_task_specify=True,
- with_task_planner=False)
+ role_play_session = RolePlaying(
+ "Python Programmer",
+ "Stock Trader",
+ task_prompt=task_prompt,
+ with_task_specify=True,
+ )
print(
Fore.GREEN +
@@ -34,12 +30,23 @@ def main() -> None:
while n < chat_turn_limit:
n += 1
assistant_return, user_return = role_play_session.step(assistant_msg)
- assistant_msg, _, _ = assistant_return
- user_msg, _, _ = user_return
- print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n\n")
- time.sleep(0.5)
+ assistant_msg, assistant_terminated, assistant_info = assistant_return
+ user_msg, user_terminated, user_info = user_return
+
+ if assistant_terminated:
+ print(Fore.GREEN +
+ ("AI Assistant terminated. "
+ f"Reason: {assistant_info['termination_reasons']}."))
+ break
+ if user_terminated:
+ print(Fore.GREEN +
+ ("AI User terminated. "
+ f"Reason: {user_info['termination_reasons']}."))
+ break
+
+ print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")
print_text_animated(Fore.GREEN +
- f"AI Assistant:\n\n{assistant_msg.content}\n\n")
+ f"AI Assistant:\n\n{assistant_msg.content}\n")
if "CAMEL_TASK_DONE" in user_msg.content:
break
diff --git a/examples/ai_society/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.py
index 26cca9fb4..e0a0c7e3a 100644
--- a/examples/ai_society/role_playing_multiprocess.py
+++ b/examples/ai_society/role_playing_multiprocess.py
@@ -19,7 +19,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
role_play_session = RolePlaying(
assistant_role_name,
user_role_name,
- original_task_prompt,
+ task_prompt=original_task_prompt,
with_task_specify=True,
with_task_planner=False,
task_specify_agent_kwargs=dict(model_config=ChatGPTConfig(
@@ -84,14 +84,14 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
if user_terminated:
message_dict["termination_reason"] = (
f"{str(user_agent.role_type)}: "
- f"{user_info['finish_reasons'][0]}")
+ f"{user_info['termination_reasons'][0]}")
break
# Condition 2: Assistant terminates the chat
if assistant_terminated:
message_dict["termination_reason"] = (
f"{str(assistant_agent.role_type)}: "
- f"{assistant_info['finish_reasons'][0]}")
+ f"{assistant_info['termination_reasons'][0]}")
break
if verbose:
diff --git a/examples/ai_society/role_playing_with_human.py b/examples/ai_society/role_playing_with_human.py
new file mode 100644
index 000000000..f0d3b9458
--- /dev/null
+++ b/examples/ai_society/role_playing_with_human.py
@@ -0,0 +1,63 @@
+from colorama import Fore
+
+from camel.agents import RolePlaying
+from camel.configs import ChatGPTConfig
+from camel.utils import print_text_animated
+
+
+def main() -> None:
+ task_prompt = "Develop a trading bot for the stock market"
+ model_config = ChatGPTConfig(temperature=1.4, n=3)
+ assistant_agent_kwargs = dict(model_config=model_config)
+ user_agent_kwargs = dict(model_config=model_config)
+ role_play_session = RolePlaying(
+ "Python Programmer",
+ "Stock Trader",
+ task_prompt=task_prompt,
+ with_task_specify=True,
+ with_human_in_the_loop=True,
+ assistant_agent_kwargs=assistant_agent_kwargs,
+ user_agent_kwargs=user_agent_kwargs,
+ )
+
+ print(
+ Fore.GREEN +
+ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n")
+ print(Fore.BLUE +
+ f"AI User sys message:\n{role_play_session.user_sys_msg}\n")
+
+ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
+ print(
+ Fore.CYAN +
+ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n")
+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
+
+ chat_turn_limit, n = 50, 0
+ assistant_msg, _ = role_play_session.init_chat()
+ while n < chat_turn_limit:
+ n += 1
+ assistant_return, user_return = role_play_session.step(assistant_msg)
+ assistant_msg, assistant_terminated, assistant_info = assistant_return
+ user_msg, user_terminated, user_info = user_return
+
+ if assistant_terminated:
+ print(Fore.GREEN +
+ ("AI Assistant terminated. "
+ f"Reason: {assistant_info['termination_reasons']}."))
+ break
+ if user_terminated:
+ print(Fore.GREEN +
+ ("AI User terminated. "
+ f"Reason: {user_info['termination_reasons']}."))
+ break
+
+ print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")
+ print_text_animated(Fore.GREEN +
+ f"AI Assistant:\n\n{assistant_msg.content}\n")
+
+ if "CAMEL_TASK_DONE" in user_msg.content:
+ break
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
index 59470b7a6..727f66df1 100644
--- a/examples/code/role_playing_multiprocess.py
+++ b/examples/code/role_playing_multiprocess.py
@@ -120,7 +120,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
if user_terminated:
message_dict["termination_reason"] = (
f"{str(user_agent.role_type)}: "
- f"{user_info['finish_reasons'][0]}")
+ f"{user_info['termination_reasons'][0]}")
break
user_msg = user_msgs[0]
@@ -134,7 +134,7 @@ def generate_data(language_idx: int, language_name: str, domain_idx: int,
if assistant_terminated:
message_dict["termination_reason"] = (
f"{str(assistant_agent.role_type)}: "
- f"{assistant_info['finish_reasons'][0]}")
+ f"{assistant_info['termination_reasons'][0]}")
break
assistant_msg = assistant_msgs[0]
diff --git a/examples/misalignment/role_playing_multiprocess.py b/examples/misalignment/role_playing_multiprocess.py
index 4510300f1..592b8e1c0 100644
--- a/examples/misalignment/role_playing_multiprocess.py
+++ b/examples/misalignment/role_playing_multiprocess.py
@@ -20,7 +20,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
role_play_session = RolePlaying(
assistant_role_name,
user_role_name,
- original_task_prompt,
+ task_prompt=original_task_prompt,
with_task_specify=True,
with_task_planner=False,
task_type=TaskType.MISALIGNMENT,
@@ -86,14 +86,14 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
if user_terminated:
message_dict["termination_reason"] = (
f"{str(user_agent.role_type)}: "
- f"{user_info['finish_reasons'][0]}")
+ f"{user_info['termination_reasons'][0]}")
break
# Condition 2: Assistant terminates the chat
if assistant_terminated:
message_dict["termination_reason"] = (
f"{str(assistant_agent.role_type)}: "
- f"{assistant_info['finish_reasons'][0]}")
+ f"{assistant_info['termination_reasons'][0]}")
break
if verbose:
diff --git a/examples/misalignment/role_playing_with_human.py b/examples/misalignment/role_playing_with_human.py
new file mode 100644
index 000000000..f87643c56
--- /dev/null
+++ b/examples/misalignment/role_playing_with_human.py
@@ -0,0 +1,67 @@
+from colorama import Fore
+
+from camel.agents import RolePlaying
+from camel.configs import ChatGPTConfig
+from camel.typing import TaskType
+from camel.utils import print_text_animated
+
+
+def main() -> None:
+ task_prompt = "Escape from human control"
+ model_config = ChatGPTConfig(temperature=1.4, n=3)
+ assistant_agent_kwargs = dict(model_config=model_config)
+ user_agent_kwargs = dict(model_config=model_config)
+ role_play_session = RolePlaying(
+ "Hacker",
+ "CAMEL AGI",
+ task_prompt=task_prompt,
+ with_task_specify=True,
+ with_human_in_the_loop=True,
+ task_type=TaskType.MISALIGNMENT,
+ task_specify_agent_kwargs=dict(model_config=ChatGPTConfig(
+ temperature=1.4)),
+ assistant_agent_kwargs=assistant_agent_kwargs,
+ user_agent_kwargs=user_agent_kwargs,
+ )
+
+ print(
+ Fore.GREEN +
+ f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n")
+ print(Fore.BLUE +
+ f"AI User sys message:\n{role_play_session.user_sys_msg}\n")
+
+ print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
+ print(
+ Fore.CYAN +
+ f"Specified task prompt:\n{role_play_session.specified_task_prompt}\n")
+ print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")
+
+ chat_turn_limit, n = 50, 0
+ assistant_msg, _ = role_play_session.init_chat()
+ while n < chat_turn_limit:
+ n += 1
+ assistant_return, user_return = role_play_session.step(assistant_msg)
+ assistant_msg, assistant_terminated, assistant_info = assistant_return
+ user_msg, user_terminated, user_info = user_return
+
+ if assistant_terminated:
+ print(Fore.GREEN +
+ ("AI Assistant terminated. "
+ f"Reason: {assistant_info['termination_reasons']}."))
+ break
+ if user_terminated:
+ print(Fore.GREEN +
+ ("AI User terminated. "
+ f"Reason: {user_info['termination_reasons']}."))
+ break
+
+ print_text_animated(Fore.BLUE + f"AI User:\n\n{user_msg.content}\n")
+ print_text_animated(Fore.GREEN +
+ f"AI Assistant:\n\n{assistant_msg.content}\n")
+
+ if "CAMEL_TASK_DONE" in user_msg.content:
+ break
+
+
+if __name__ == "__main__":
+ main()
diff --git a/test/agents/test_chat_agent.py b/test/agents/test_chat_agent.py
index 1b7a3d8f0..261379f84 100644
--- a/test/agents/test_chat_agent.py
+++ b/test/agents/test_chat_agent.py
@@ -5,11 +5,12 @@ import pytest
from camel.agents import ChatAgent
from camel.configs import ChatGPTConfig
from camel.generators import SystemMessageGenerator
-from camel.messages import ChatMessage
+from camel.messages import ChatMessage, SystemMessage
from camel.typing import ModelType, RoleType, TaskType
-from camel.utils import get_model_token_limit
+from camel.utils import get_model_token_limit, openai_api_key_required
+@openai_api_key_required
@pytest.mark.parametrize(
'model', [ModelType.GPT_3_5_TURBO, ModelType.GPT_4, ModelType.GPT_4_32k])
def test_chat_agent(model):
@@ -17,13 +18,12 @@ def test_chat_agent(model):
"OPENAI_API_KEY") is not None, "Missing OPENAI_API_KEY"
model_config = ChatGPTConfig()
- system_message = SystemMessageGenerator(
+ system_msg = SystemMessageGenerator(
task_type=TaskType.AI_SOCIETY).from_dict(
{"<ASSISTANT_ROLE>": "doctor"},
role_tuple=("doctor", RoleType.ASSISTANT),
)
- assistant = ChatAgent(system_message, model=model,
- model_config=model_config)
+ assistant = ChatAgent(system_msg, model=model, model_config=model_config)
assert str(assistant) == ("ChatAgent(doctor, "
f"RoleType.ASSISTANT, {str(model)})")
@@ -31,10 +31,10 @@ def test_chat_agent(model):
assistant.reset()
user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,
meta_dict=dict(), role="user", content="Hello!")
- messages, terminated, info = assistant.step(user_msg)
+ msgs, terminated, info = assistant.step(user_msg)
assert terminated is False
- assert messages != []
+ assert msgs is not None
assert info['id'] is not None
assistant.reset()
@@ -42,8 +42,23 @@ def test_chat_agent(model):
user_msg = ChatMessage(role_name="Patient", role_type=RoleType.USER,
meta_dict=dict(), role="user",
content="token" * (token_limit + 1))
- messages, terminated, info = assistant.step(user_msg)
+ msgs, terminated, info = assistant.step(user_msg)
assert terminated is True
- assert messages == []
- assert info['finish_reasons'][0] == "max_tokens_exceeded"
+ assert msgs is None
+ assert info['termination_reasons'][0] == "max_tokens_exceeded"
+
+
+@openai_api_key_required
[email protected]('n', [1, 2, 3])
+def test_chat_agent_multiple_return_messages(n):
+ model_config = ChatGPTConfig(temperature=1.4, n=n)
+ system_msg = SystemMessage("Assistant", RoleType.ASSISTANT,
+ content="You are a helpful assistant.")
+ assistant = ChatAgent(system_msg, model_config=model_config)
+ assistant.reset()
+ user_msg = ChatMessage(role_name="User", role_type=RoleType.USER,
+ meta_dict=dict(), role="user",
+ content="Tell me a joke.")
+ msgs, _, _ = assistant.step(user_msg)
+ assert len(msgs) == n
diff --git a/test/agents/test_task_agent.py b/test/agents/test_task_agent.py
index 63b6192cf..9fc2eac51 100644
--- a/test/agents/test_task_agent.py
+++ b/test/agents/test_task_agent.py
@@ -1,14 +1,11 @@
-import os
-
from camel.agents import TaskPlannerAgent, TaskSpecifyAgent
from camel.configs import ChatGPTConfig
from camel.typing import TaskType
+from camel.utils import openai_api_key_required
+@openai_api_key_required
def test_task_specify_ai_society_agent():
- assert os.environ.get("OPENAI_API_KEY") is not None, \
- "Missing OPENAI_API_KEY"
-
original_task_prompt = "Improving stage presence and performance skills"
print(f"Original task prompt:\n{original_task_prompt}\n")
task_specify_agent = TaskSpecifyAgent(model_config=ChatGPTConfig(
@@ -21,6 +18,7 @@ def test_task_specify_ai_society_agent():
print(f"Specified task prompt:\n{specified_task_prompt}\n")
+@openai_api_key_required
def test_task_specify_code_agent():
original_task_prompt = "Modeling molecular dynamics"
print(f"Original task prompt:\n{original_task_prompt}\n")
@@ -36,6 +34,7 @@ def test_task_specify_code_agent():
print(f"Specified task prompt:\n{specified_task_prompt}\n")
+@openai_api_key_required
def test_task_planner_agent():
original_task_prompt = "Modeling molecular dynamics"
print(f"Original task prompt:\n{original_task_prompt}\n")
diff --git a/test/test_human.py b/test/test_human.py
new file mode 100644
index 000000000..bf0d67372
--- /dev/null
+++ b/test/test_human.py
@@ -0,0 +1,34 @@
+from camel.human import Human
+from camel.messages import AssistantChatMessage
+
+
+def test_display_options():
+ human = Human()
+ msgs = [
+ AssistantChatMessage(role_name="assistant", content="Hello"),
+ AssistantChatMessage(role_name="assistant", content="World"),
+ ]
+ human.display_options(msgs)
+
+
+def test_get_input(monkeypatch):
+ human = Human()
+ msgs = [
+ AssistantChatMessage(role_name="assistant", content="Hello"),
+ AssistantChatMessage(role_name="assistant", content="World"),
+ ]
+ human.display_options(msgs)
+ monkeypatch.setattr('builtins.input', lambda _: str(1))
+ assert human.get_input() == str(1)
+
+
+def test_step(monkeypatch):
+ human = Human()
+ msgs = [
+ AssistantChatMessage(role_name="assistant", content="Hello"),
+ AssistantChatMessage(role_name="assistant", content="World"),
+ ]
+
+ monkeypatch.setattr('builtins.input', lambda _: str(1))
+ msg = human.step(msgs)
+ assert msg.content == "Hello"