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

Code multiprocess (#32)

Hasan Abed Al Kader Hammoud52152cecc25d
diff --git a/camel/__init__.py b/camel/__init__.py
index e8e247b79..1861c08c2 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -1,12 +1,9 @@
from .agent import ChatAgent, RolePlaying, TaskPlannerAgent, TaskSpecifyAgent
from .configs import ChatGPTConfig
-from .generator import (
- AISocietyTaskPromptGenerator,
- CodeTaskPromptGenerator,
- RoleNameGenerator,
- SingleTxtGenerator,
- SystemMessageGenerator,
-)
+from .generator import (AISocietyTaskPromptGenerator,
+ CodeSystemMessageGenerator, CodeTaskPromptGenerator,
+ RoleNameGenerator, SingleTxtGenerator,
+ SystemMessageGenerator)
from .typing import ModeType, RoleType
from .utils import get_model_token_limit, num_tokens_from_messages
@@ -24,6 +21,7 @@ __all__ = [
'TaskPlannerAgent',
'RolePlaying',
'SystemMessageGenerator',
+ 'CodeSystemMessageGenerator',
'RoleNameGenerator',
'AISocietyTaskPromptGenerator',
'SingleTxtGenerator',
diff --git a/camel/agent/__init__.py b/camel/agent/__init__.py
index a6766998f..3dd0f971b 100644
--- a/camel/agent/__init__.py
+++ b/camel/agent/__init__.py
@@ -1,9 +1,10 @@
-from .chat_agent import ChatAgent
+from .chat_agent import ChatAgent, CodeChatAgent
from .role_playing import RolePlaying
from .task_agent import TaskPlannerAgent, TaskSpecifyAgent
__all__ = [
'ChatAgent',
+ 'CodeChatAgent',
'TaskSpecifyAgent',
'TaskPlannerAgent',
'RolePlaying',
diff --git a/camel/agent/chat_agent.py b/camel/agent/chat_agent.py
index 6cabca8df..ed11a0d0d 100644
--- a/camel/agent/chat_agent.py
+++ b/camel/agent/chat_agent.py
@@ -106,3 +106,100 @@ class ChatAgent:
def __repr__(self) -> str:
return f"ChatAgent({self.role_name}, {self.role_type}, {self.model})"
+
+
+class CodeChatAgent:
+
+ def __init__(
+ self,
+ system_message: SystemMessage,
+ model: ModeType,
+ model_config: Any = None,
+ message_window_size: Optional[int] = None,
+ ) -> None:
+ self.system_message = system_message
+ self.role_type = system_message.role_type
+
+ self.model = model
+ self.model_config = model_config or ChatGPTConfig()
+ self.model_token_limit = get_model_token_limit(self.model)
+ self.message_window_size = message_window_size
+
+ self.terminated = False
+ self.init_messages()
+
+ def reset(self) -> None:
+ self.terminated = False
+ self.init_messages()
+ return self.stored_messages
+
+ def get_info(
+ self,
+ id: Optional[str],
+ usage: Optional[Dict[str, int]],
+ finish_reasons: List[str],
+ num_tokens: int,
+ ) -> Dict[str, Any]:
+ return {
+ "id": id,
+ "usage": usage,
+ "finish_reasons": finish_reasons,
+ "num_tokens": num_tokens,
+ }
+
+ def init_messages(self) -> None:
+ self.stored_messages: List[MessageType] = [self.system_message]
+
+ def update_messages(self, message: ChatMessage) -> List[ChatMessage]:
+ self.stored_messages.append(message)
+ return self.stored_messages
+
+ @retry(wait=wait_exponential(min=5, max=60), stop=stop_after_attempt(5))
+ def step(
+ self,
+ input_message: ChatMessage,
+ ) -> Tuple[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:
+ messages = [self.system_message
+ ] + messages[-self.message_window_size:]
+ openai_messages = [message.to_openai_message() for message in messages]
+ num_tokens = num_tokens_from_messages(openai_messages, self.model)
+
+ if num_tokens < self.model_token_limit:
+ response = openai.ChatCompletion.create(
+ model=self.model.value,
+ messages=openai_messages,
+ **self.model_config.__dict__,
+ )
+ output_messages = [
+ ChatMessage("", self.role_type, **dict(choice["message"]))
+ for choice in response["choices"]
+ ]
+ info = self.get_info(
+ response["id"],
+ response["usage"],
+ [
+ str(choice["finish_reason"])
+ for choice in response["choices"]
+ ],
+ num_tokens,
+ )
+ self.update_messages(output_messages[0])
+
+ else:
+ self.terminated = True
+ output_messages = []
+
+ info = self.get_info(
+ None,
+ None,
+ ["max_tokens_exceeded"],
+ num_tokens,
+ )
+
+ return output_messages, self.terminated, info
+
+ def __repr__(self) -> str:
+ return f"ChatAgent( {self.role_type}, {self.model})"
diff --git a/camel/agent/role_playing.py b/camel/agent/role_playing.py
index 4e056270c..c2abca066 100644
--- a/camel/agent/role_playing.py
+++ b/camel/agent/role_playing.py
@@ -88,18 +88,22 @@ class RolePlaying:
def step(
self,
assistant_msg: ChatMessage,
- ) -> Tuple[ChatMessage, ChatMessage]:
- user_msgs, user_terminated, _ = self.user_agent.step(assistant_msg)
+ ) -> Tuple[Tuple[ChatMessage, bool, Dict], Tuple[ChatMessage, bool, Dict]]:
+ 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"
- assistant_msgs, assistant_terminated, _ = self.assistant_agent.step(
- user_msg)
+ (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 assistant_msg, user_msg
+ return (
+ (assistant_msg, assistant_terminated, assistant_info),
+ (user_msg, user_terminated, user_info),
+ )
diff --git a/camel/agent/task_agent.py b/camel/agent/task_agent.py
index e9118c989..e4384affe 100644
--- a/camel/agent/task_agent.py
+++ b/camel/agent/task_agent.py
@@ -54,7 +54,6 @@ class TaskSpecifyAgent(ChatAgent):
for replace_tuple in replace_tuples:
self.task_specify_prompt = self.task_specify_prompt.replace(
replace_tuple[0], replace_tuple[1])
- print(self.task_specify_prompt)
assert "<" and ">" not in self.task_specify_prompt
task_msg = UserChatMessage(role_name="task_specifier",
content=self.task_specify_prompt)
diff --git a/camel/generator.py b/camel/generator.py
index c2faade42..50c33f64b 100644
--- a/camel/generator.py
+++ b/camel/generator.py
@@ -1,12 +1,8 @@
from typing import Generator, List, Optional, Tuple
-from camel.message import (
- AssistantSystemMessage,
- RoleType,
- SystemMessage,
- SystemMessageType,
- UserSystemMessage,
-)
+from camel.message import (AssistantSystemMessage, CodeAssistantSystemMessage,
+ CodeSystemMessage, CodeUserSystemMessage, RoleType,
+ SystemMessage, SystemMessageType, UserSystemMessage)
class SystemMessageGenerator:
@@ -93,7 +89,7 @@ class SystemMessageGenerator:
def from_roles(
self,
- roles: List[Tuple[RoleType, str]],
+ roles: List[Tuple[str, RoleType]],
assistant_prompt: Optional[str] = None,
user_prompt: Optional[str] = None,
task_prompt: Optional[str] = None,
@@ -187,6 +183,7 @@ class AISocietyTaskPromptGenerator:
self.num_tasks = num_tasks
+ # TODO: Return role names for user and assistant with the generator.
def from_role_files(
self,
assistant_role_names_path: str = "data/ai_society/assistant_roles.txt",
@@ -244,19 +241,160 @@ class CodeTaskPromptGenerator:
def from_role_files(
self, languages_path: str = "data/code/languages.txt",
domains_path: str = "data/code/domains.txt"
- ) -> Generator[str, None, None]:
+ ) -> Generator[Tuple[str, str, str], None, None]:
language_generator = SingleTxtGenerator(
languages_path).from_role_files()
- domains_generator = SingleTxtGenerator(domains_path).from_role_files()
+
for language in language_generator:
+ domains_generator = SingleTxtGenerator(
+ domains_path).from_role_files()
for domain in domains_generator:
- yield (self.generate_tasks_prompt.replace(
+ generated_tasks_prompt = self.generate_tasks_prompt.replace(
"<LANGUAGE>",
language).replace("<DOMAIN>",
domain).replace("<NUM_TASKS>",
- str(self.num_tasks)))
+ str(self.num_tasks))
+ yield (generated_tasks_prompt, language, domain)
def from_role_generator(
self, role_generator: Generator[Tuple, None, None]
) -> Generator[str, None, None]:
raise NotImplementedError
+
+
+class CodeSystemMessageGenerator:
+
+ def __init__(
+ self,
+ with_task: bool = True,
+ language_names_path: str = "data/code/languages.txt",
+ domain_names_path: str = "data/code/domains.txt",
+ assistant_prompt_path: str = None,
+ user_prompt_path: str = None,
+ assistant_prompt_with_task_path:
+ str = "prompts/code/assistant_prompt_with_task.txt",
+ user_task_prompt_with_task_path:
+ str = "prompts/code/user_prompt_with_task.txt",
+ ) -> None:
+ self.with_task = with_task
+
+ with open(language_names_path, "r") as f:
+ self.language_names: List[str] = f.read().splitlines()
+
+ with open(domain_names_path, "r") as f:
+ self.domain_names: List[str] = f.read().splitlines()
+
+ if not self.with_task:
+ with open(assistant_prompt_path, "r") as f:
+ self.assistant_prompt: str = f.read()
+
+ with open(user_prompt_path, "r") as f:
+ self.user_prompt: str = f.read()
+ else:
+ with open(assistant_prompt_with_task_path, "r") as f:
+ self.assistant_prompt: str = f.read()
+
+ with open(user_task_prompt_with_task_path, "r") as f:
+ self.user_prompt: str = f.read()
+
+ def from_role(
+ self,
+ language_name: str = "",
+ domain_name: str = "",
+ role_type: RoleType = RoleType.DEFAULT,
+ role_prompt: Optional[str] = None,
+ task_prompt: Optional[str] = None,
+ ) -> SystemMessageType:
+ """Generate a system message from a role name and role type."""
+ if task_prompt is not None:
+ assert self.with_task, "Please set with `with_task=True`."
+
+ if role_type == RoleType.ASSISTANT:
+ role_prompt = role_prompt or self.assistant_prompt
+ new_role_prompt = role_prompt.replace("<LANGUAGE>",
+ language_name).replace(
+ "<DOMAIN>", domain_name)
+
+ assert new_role_prompt != self.assistant_prompt
+
+ if self.with_task:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>",
+ task_prompt,
+ )
+
+ return CodeAssistantSystemMessage(language_name=language_name,
+ domain_name=domain_name,
+ content=new_role_prompt)
+
+ if role_type == RoleType.USER:
+ role_prompt = role_prompt or self.user_prompt
+ new_role_prompt = role_prompt.replace("<LANGUAGE>",
+ language_name).replace(
+ "<DOMAIN>", domain_name)
+
+ assert new_role_prompt != self.user_prompt
+
+ if self.with_task:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>",
+ task_prompt,
+ )
+
+ return CodeUserSystemMessage(language_name=language_name,
+ domain_name=domain_name,
+ content=new_role_prompt)
+
+ if role_type == RoleType.DEFAULT:
+ new_role_prompt = "You are a helpful assistant."
+ if task_prompt is not None:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>", task_prompt)
+ return CodeSystemMessage(language_name=language_name,
+ domain_name=domain_name,
+ content=new_role_prompt)
+
+ def from_roles(
+ self,
+ roles: List[RoleType],
+ language_name: str = "",
+ domain_name: str = "",
+ assistant_prompt: Optional[str] = None,
+ user_prompt: Optional[str] = None,
+ task_prompt: Optional[str] = None,
+ ) -> List[SystemMessageType]:
+ if task_prompt is not None:
+ assert self.with_task, "Please set with `with_task=True`."
+ assistant_prompt = assistant_prompt or self.assistant_prompt
+ user_prompt = user_prompt or self.user_prompt
+ role_prompts = [assistant_prompt, user_prompt]
+ assert len(roles) == 2, "Only support 1 assistant and 1 user."
+ assert RoleType.ASSISTANT in [role_type for role_type in roles
+ ], "Must have 1 assistant."
+ assert RoleType.USER in [role_type
+ for role_type in roles], "Must have 1 user."
+
+ for i, role_prompt in enumerate(role_prompts):
+ for role_type in roles:
+ role_prompt = self.from_role(language_name, domain_name,
+ role_type, role_prompt,
+ task_prompt).content
+ role_prompts[i] = role_prompt
+
+ assistant_system_message = CodeAssistantSystemMessage(
+ language_name,
+ domain_name,
+ content=role_prompts[0],
+ )
+ user_system_message = CodeUserSystemMessage(
+ language_name,
+ domain_name,
+ content=role_prompts[1],
+ )
+
+ system_messages: List[SystemMessageType] = []
+ for role_type in roles:
+ system_messages.append(assistant_system_message if role_type ==
+ RoleType.ASSISTANT else user_system_message)
+
+ return system_messages
diff --git a/camel/message.py b/camel/message.py
index da430e2f9..7dd526f97 100644
--- a/camel/message.py
+++ b/camel/message.py
@@ -48,6 +48,45 @@ class BaseMessage:
}
+@dataclass
+class CodeBaseMessage:
+ language_name: str
+ domain_name: str
+ role_type: RoleType
+ role: str
+ content: str
+
+ def to_openai_message(self, role: Optional[str] = None) -> OpenAIMessage:
+ role = role or self.role
+ assert role in ["system", "user", "assistant"]
+ return {"role": role, "content": self.content}
+
+ def to_openai_chat_message(
+ self,
+ role: Optional[str] = None,
+ ) -> OpenAIChatMessage:
+ role = role or self.role
+ assert role in ["user", "assistant"]
+ return {"role": role, "content": self.content}
+
+ def to_openai_system_message(self) -> OpenAISystemMessage:
+ return {"role": "system", "content": self.content}
+
+ def to_openai_user_message(self) -> OpenAIUserMessage:
+ return {"role": "user", "content": self.content}
+
+ def to_openai_assistant_message(self) -> OpenAIAssistantMessage:
+ return {"role": "assistant", "content": self.content}
+
+ def to_dict(self) -> Dict:
+ return {
+ "language_name": self.language_name,
+ "domain_name": self.domain_name,
+ "role": self.role,
+ "content": self.content,
+ }
+
+
@dataclass
class SystemMessage(BaseMessage):
role_name: str
@@ -96,9 +135,38 @@ class UserChatMessage(ChatMessage):
content: str = ""
+@dataclass
+class CodeSystemMessage(CodeBaseMessage):
+ language_name: str
+ domain_name: str
+ role_type: RoleType
+ role: str = "system"
+ content: str = ""
+
+
+@dataclass
+class CodeAssistantSystemMessage(CodeSystemMessage):
+ language_name: str
+ domain_name: str
+ role_type: RoleType = RoleType.ASSISTANT
+ role: str = "system"
+ content: str = ""
+
+
+@dataclass
+class CodeUserSystemMessage(CodeSystemMessage):
+ language_name: str
+ domain_name: str
+ role_type: RoleType = RoleType.USER
+ role: str = "system"
+ content: str = ""
+
+
MessageType = Union[BaseMessage, SystemMessage, AssistantSystemMessage,
UserSystemMessage, ChatMessage, AssistantChatMessage,
- UserChatMessage]
+ UserChatMessage, CodeSystemMessage,
+ CodeAssistantSystemMessage, CodeUserSystemMessage]
SystemMessageType = Union[SystemMessage, AssistantSystemMessage,
- UserSystemMessage]
+ UserSystemMessage, CodeSystemMessage,
+ CodeAssistantSystemMessage, CodeUserSystemMessage]
ChatMessageType = Union[ChatMessage, AssistantChatMessage, UserChatMessage]
diff --git a/examples/ai_society/role_playing.py b/examples/ai_society/role_playing.py
index 973d724df..5ef1dd655 100644
--- a/examples/ai_society/role_playing.py
+++ b/examples/ai_society/role_playing.py
@@ -35,7 +35,9 @@ def main() -> None:
assistant_msg, _ = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
- assistant_msg, user_msg = role_play_session.step(assistant_msg)
+ 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)
print_text_animated(Fore.GREEN +
diff --git a/examples/ai_society/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.py
index fe98f9bf5..273664b31 100644
--- a/examples/ai_society/role_playing_multiprocess.py
+++ b/examples/ai_society/role_playing_multiprocess.py
@@ -2,72 +2,52 @@ import json
import multiprocessing
import os
-from camel.agent import ChatAgent, TaskSpecifyAgent
-from camel.configs import ChatGPTConfig
-from camel.generator import SystemMessageGenerator
-from camel.message import (AssistantChatMessage, AssistantSystemMessage,
- UserChatMessage, UserSystemMessage)
-from camel.typing import ModeType, RoleType
-
-
-def init_chat(
- assistant_agent: ChatAgent,
- user_agent: ChatAgent,
- user_sys_msg: UserSystemMessage,
- assistant_sys_msg: AssistantSystemMessage,
-):
- assistant_agent.reset()
- user_agent.reset()
+from colorama import Fore
- # Send the system messages again to the agents using chat messages
- assistant_msg = AssistantChatMessage(
- "Computer Programer",
- content=(f"{user_sys_msg.content}. "
- "Now start to give me introductions one by one. "
- "Only reply with Instruction and Input."))
- assistant_msg.role = "user"
-
- user_msg = UserChatMessage(user_sys_msg.role_name,
- content=f"{assistant_sys_msg.content}")
- msgs, _, _ = assistant_agent.step(user_msg)
-
- return assistant_msg, msgs
+from camel.agent import RolePlaying
+from camel.configs import ChatGPTConfig
def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
- user_role_name: str, task_idx: int,
- task_prompt: str) -> None:
+ user_role_name: str, task_idx: int, task_prompt: str,
+ verbose: bool = False) -> None:
max_num_messages = 40
- # Remove number from task prompt
original_task_prompt = task_prompt.replace(f"{task_idx+1}. ", "")
- task_specify_agent = TaskSpecifyAgent(ModeType.GPT_3_5_TURBO,
- ChatGPTConfig(temperature=1.4))
- specified_task_prompt = task_specify_agent.specify_task(
- original_task_prompt, [
- ("<ASSISTANT_ROLE>", assistant_role_name),
- ("<USER_ROLE>", user_role_name),
- ])
- print(f"Original Task: {original_task_prompt}")
- print(f"Specified Task: {specified_task_prompt}")
- sys_msg_generator = SystemMessageGenerator(with_task=True)
- assistant_sys_msg, user_sys_msg = sys_msg_generator.from_roles(
- roles=[(assistant_role_name, RoleType.ASSISTANT),
- (user_role_name, RoleType.USER)],
- task_prompt=specified_task_prompt)
- assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO,
- message_window_size=max_num_messages)
- user_agent = ChatAgent(user_sys_msg, ModeType.GPT_3_5_TURBO,
- message_window_size=max_num_messages)
-
- assistant_msg, _ = init_chat(assistant_agent, user_agent, user_sys_msg,
- assistant_sys_msg)
+ role_play_session = RolePlaying(
+ assistant_role_name,
+ user_role_name,
+ original_task_prompt,
+ with_task_specify=True,
+ with_task_planner=True,
+ task_specify_agent_kwargs=dict(model_config=ChatGPTConfig(
+ temperature=1.4)),
+ )
+
+ assistant_msg, _ = role_play_session.init_chat()
+
+ if verbose:
+ print(Fore.GREEN + "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 + "Specified task prompt:\n"
+ "{role_play_session.specified_task_prompt}\n")
+ print(Fore.MAGENTA + "Planned task prompt:\n"
+ "{role_play_session.planned_task_prompt}\n")
+ print(Fore.RED +
+ f"Final task prompt:\n{role_play_session.task_prompt}\n")
message_counter = 0
message_dict = {}
+ assistant_agent = role_play_session.assistant_agent
+ user_agent = role_play_session.user_agent
+
# Append roles to the dictionary
# We start number from 1 not 0.
message_dict[
@@ -76,7 +56,8 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
message_dict[
"id"] = f"{(assistant_idx+1):03}_{(user_idx+1):03}_{(task_idx+1):03}"
message_dict["original_task"] = original_task_prompt
- message_dict["specified_task"] = specified_task_prompt
+ message_dict["specified_task"] = role_play_session.specified_task_prompt
+ message_dict["planned_task"] = role_play_session.planned_task_prompt
# Threshold to terminate the conversation if no end token appears
repeat_word_counter = 0
@@ -97,7 +78,9 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
while message_counter < max_num_messages:
- user_msgs, user_terminated, user_info = user_agent.step(assistant_msg)
+ 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
# Condition 1: User terminates the chat
if user_terminated:
@@ -106,13 +89,6 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
f"{user_info['finish_reasons'][0]}")
break
- user_msg = user_msgs[0]
- print(f"User:\n{user_msg.content}\n")
- user_msg.role = "user"
-
- assistant_msgs, assistant_terminated, assistant_info = (
- assistant_agent.step(user_msg))
-
# Condition 2: Assistant terminates the chat
if assistant_terminated:
message_dict["termination_reason"] = (
@@ -120,9 +96,9 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
f"{assistant_info['finish_reasons'][0]}")
break
- assistant_msg = assistant_msgs[0]
- print(f"Assistant:\n{assistant_msg.content}\n")
- assistant_msg.role = "user"
+ if verbose:
+ print(f"User:\n{user_msg.content}\n")
+ print(f"Assistant:\n{assistant_msg.content}\n")
# Condition 3: Break if user does not give instruction
if user_no_instruct_word not in user_msg.content:
@@ -174,12 +150,16 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
if message_dict["num_messages"] == max_num_messages:
message_dict["termination_reason"] = "max_num_messages"
- with open(f"./camel_data/{message_dict['id']}.json", "w") as json_file:
+ with open(f"./camel_data/ai_society/{message_dict['id']}.json",
+ "w") as json_file:
json.dump(message_dict, json_file)
def main() -> None:
+ # Disable/Enable Printing
+ verbose = True
+
# Chunk for parallel jobs
array_idx = int(os.environ.get('SLURM_ARRAY_TASK_ID'))
roles_per_chunk = 10
@@ -188,10 +168,10 @@ def main() -> None:
start_token = "1."
num_tasks = 10
- with open("./data/user_roles.txt", "r") as f:
+ with open("./data/ai_society/user_roles.txt", "r") as f:
user_roles = f.read().splitlines()
- with open("./data/assistant_roles.txt", "r") as f:
+ with open("./data/ai_society/assistant_roles.txt", "r") as f:
assistant_roles = f.read().splitlines()
assert (array_idx + 1) * roles_per_chunk <= len(assistant_roles)
@@ -207,7 +187,8 @@ def main() -> None:
for user_idx, user_role_name in enumerate(user_roles):
user_role_name = " ".join(user_role_name.split(" ")[1:])
# Load the task list assigned for assistant and user roles
- with open(f"./tasks/{assistant_role_name}_{user_role_name}.txt",
+ with open((f"./ai_society_results/tasks/"
+ f"{assistant_role_name}_{user_role_name}.txt"),
"r") as f:
tasks = f.read().splitlines()
@@ -223,11 +204,15 @@ def main() -> None:
for task_idx, task_prompt in enumerate(tasks):
id = (f"{(assistant_idx+1):03}_"
f"{(user_idx+1):03}_{(task_idx+1):03}")
- if not os.path.exists(f"./camel_data/{id}.json"):
+ if not os.path.exists(f"./camel_data/ai_society/{id}.json"):
pool.apply_async(
generate_data,
(assistant_idx, assistant_role_name, user_idx,
- user_role_name, task_idx, task_prompt))
+ user_role_name, task_idx, task_prompt, verbose))
+
+ # generate_data(assistant_idx, assistant_role_name
+ # , user_idx,
+ # user_role_name, task_idx, task_prompt)
pool.close()
pool.join()
diff --git a/examples/code/role_playing_multiprocess.py b/examples/code/role_playing_multiprocess.py
new file mode 100644
index 000000000..a76e0d8d1
--- /dev/null
+++ b/examples/code/role_playing_multiprocess.py
@@ -0,0 +1,248 @@
+import json
+import multiprocessing
+import os
+
+from camel.agent import CodeChatAgent, TaskPlannerAgent, TaskSpecifyAgent
+from camel.configs import ChatGPTConfig
+from camel.generator import CodeSystemMessageGenerator
+from camel.message import (AssistantChatMessage, CodeAssistantSystemMessage,
+ CodeUserSystemMessage, UserChatMessage)
+from camel.typing import ModeType, RoleType, TaskType
+
+
+def init_chat(
+ assistant_agent: CodeChatAgent,
+ user_agent: CodeChatAgent,
+ user_sys_msg: CodeUserSystemMessage,
+ assistant_sys_msg: CodeAssistantSystemMessage,
+):
+ assistant_agent.reset()
+ user_agent.reset()
+
+ # Send the system messages again to the agents using chat messages
+ assistant_msg = AssistantChatMessage(
+ "Computer Programer",
+ content=(f"{user_sys_msg.content}. "
+ "Now start to give me instructions one by one. "
+ "Only reply with Instruction and Input."))
+ assistant_msg.role = "user"
+
+ user_msg = UserChatMessage("", content=f"{assistant_sys_msg.content}")
+ msgs, _, _ = assistant_agent.step(user_msg)
+
+ return assistant_msg, msgs
+
+
+def generate_data(language_idx: int, language_name: str, domain_idx: int,
+ domain_name: str, task_idx: int, task_prompt: str) -> None:
+
+ max_num_messages = 40
+
+ # Remove number from task prompt
+ original_task_prompt = task_prompt.replace(f"{task_idx+1}. ", "")
+
+ task_specify_agent = TaskSpecifyAgent(
+ ModeType.GPT_3_5_TURBO,
+ task_type=TaskType.CODE,
+ model_config=ChatGPTConfig(temperature=1.4),
+ )
+ specified_task_prompt = task_specify_agent.specify_task(
+ original_task_prompt, [
+ ("<DOMAIN>", domain_name),
+ ("<LANGUAGE>", language_name),
+ ])
+
+ print(f"Original Task: {original_task_prompt}")
+ print(f"Specified Task: {specified_task_prompt}")
+
+ task_planner_agent = TaskPlannerAgent(
+ ModeType.GPT_3_5_TURBO, model_config=ChatGPTConfig(temperature=1.4))
+ planned_task_prompt = task_planner_agent.plan_task(specified_task_prompt)
+ print(f"Planned task prompt:\n{planned_task_prompt}\n")
+
+ sys_msg_generator = CodeSystemMessageGenerator(with_task=True)
+ assistant_sys_msg, user_sys_msg = sys_msg_generator.from_roles(
+ roles=[(RoleType.ASSISTANT),
+ (RoleType.USER)], language_name=language_name,
+ domain_name=domain_name, task_prompt=specified_task_prompt)
+
+ assistant_agent = CodeChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO,
+ message_window_size=max_num_messages)
+ user_agent = CodeChatAgent(user_sys_msg, ModeType.GPT_3_5_TURBO,
+ message_window_size=max_num_messages)
+
+ assistant_msg, _ = init_chat(assistant_agent, user_agent, user_sys_msg,
+ assistant_sys_msg)
+
+ print("Assistant System Message: ", assistant_sys_msg.content)
+ print("User System Message: ", user_sys_msg.content)
+ message_counter = 0
+ message_dict = {}
+
+ # Append roles to the dictionary
+ # We start number from 1 not 0.
+ message_dict[
+ "role_1"] = f"{language_name}_{str(assistant_agent.role_type)}"
+ message_dict["role_2"] = f"{domain_name}_{str(user_agent.role_type)}"
+ message_dict[
+ "id"] = f"{(language_idx+1):03}_{(domain_idx+1):03}_{(task_idx+1):03}"
+ message_dict["original_task"] = original_task_prompt
+ message_dict["specified_task"] = specified_task_prompt
+
+ # Threshold to terminate the conversation if no end token appears
+ repeat_word_counter = 0
+ repeat_word_threshold = 4
+ repeat_word_list = [
+ "goodbye", "good bye", "thank", "bye", "welcome", "language model"
+ ]
+
+ assistant_instruct_counter = 0
+ assistant_instruct_threshold = 1
+ assistant_instruct_word = "Instruction:"
+
+ user_no_instruct_counter = 0
+ user_no_instruct_threshold = 3
+ user_no_instruct_word = "Instruction:"
+
+ # Set max number of messages for the chat
+
+ while message_counter < max_num_messages:
+
+ user_msgs, user_terminated, user_info = user_agent.step(assistant_msg)
+
+ # Condition 1: User terminates the chat
+ if user_terminated:
+ message_dict["termination_reason"] = (
+ f"{str(user_agent.role_type)}: "
+ f"{user_info['finish_reasons'][0]}")
+ break
+
+ user_msg = user_msgs[0]
+ print(f"User:\n{user_msg.content}\n")
+ user_msg.role = "user"
+
+ assistant_msgs, assistant_terminated, assistant_info = (
+ assistant_agent.step(user_msg))
+
+ # 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]}")
+ break
+
+ assistant_msg = assistant_msgs[0]
+ print(f"Assistant:\n{assistant_msg.content}\n")
+ assistant_msg.role = "user"
+
+ # Condition 3: Break if user does not give instruction
+ if user_no_instruct_word not in user_msg.content:
+ user_no_instruct_counter += 1
+ if user_no_instruct_counter == user_no_instruct_threshold:
+ message_dict[
+ 'termination_reason'] = "user_no_instruct_threshold"
+ break
+ else:
+ user_no_instruct_counter = 0
+
+ # Condition 4: Break if assistant gives instruction (flipped role)
+ if assistant_instruct_word in assistant_msg.content:
+ assistant_instruct_counter += 1
+ if assistant_instruct_counter == assistant_instruct_threshold:
+ message_dict[
+ 'termination_reason'] = "assistant_instruct_threshold"
+ break
+ else:
+ assistant_instruct_counter = 0
+
+ # Condition 5: Repeat word observed
+ for repeat_word in repeat_word_list:
+ if repeat_word in user_msg.content.lower(
+ ) or repeat_word in assistant_msg.content.lower():
+ repeat_word_counter += 1
+ if repeat_word_counter == repeat_word_threshold:
+ message_dict[
+ 'termination_reason'] = "repeat_word_threshold"
+ break
+ else:
+ repeat_word_counter = 0
+
+ # Save user message
+ message_counter += 1
+ message_dict[f"message_{message_counter}"] = user_msg.to_dict()
+
+ # Condition 5: End token observed
+ if "<CAMEL_TASK_DONE>" in user_msg.content:
+ message_dict['termination_reason'] = "<CAMEL_TASK_DONE>"
+ break
+
+ # Save assistant message
+ message_counter += 1
+ message_dict[f"message_{message_counter}"] = assistant_msg.to_dict()
+
+ message_dict["num_messages"] = message_counter
+
+ if message_dict["num_messages"] == max_num_messages:
+ message_dict["termination_reason"] = "max_num_messages"
+
+ with open(f"./camel_data/code/{message_dict['id']}.json",
+ "w") as json_file:
+ json.dump(message_dict, json_file)
+
+
+def main() -> None:
+
+ # Chunk for parallel jobs
+ array_idx = int(os.environ.get('SLURM_ARRAY_TASK_ID'))
+ languages_per_chunk = 4
+
+ # Parameters for filtering the generated task string
+ start_token = "1."
+ num_tasks = 50
+
+ with open("./data/code/languages.txt", "r") as f:
+ languages = f.read().splitlines()
+
+ with open("./data/code/domains.txt", "r") as f:
+ domains = f.read().splitlines()
+
+ assert (array_idx + 1) * languages_per_chunk <= len(languages)
+ languages = languages[array_idx * languages_per_chunk:(array_idx + 1) *
+ languages_per_chunk]
+
+ pool = multiprocessing.Pool()
+
+ for language_idx, language_name in enumerate(languages):
+ language_idx += array_idx * languages_per_chunk
+ language_name = " ".join(language_name.split(" ")[1:])
+ for domain_idx, domain_name in enumerate(domains):
+ domain_name = " ".join(domain_name.split(" ")[1:])
+ # Load the task list assigned for assistant and user roles
+ with open(
+ f"./code_results/tasks/{language_name}_{domain_name}.txt",
+ "r") as f:
+ tasks = f.read().splitlines()
+
+ # Filter out the generated response to include the tasks only
+ for i, task in enumerate(tasks):
+ if start_token in task:
+ tasks = tasks[i:i + num_tasks]
+ break
+
+ # Ensure exact number of tasks is generated
+ assert str(num_tasks) in tasks[-1], print(tasks)
+
+ for task_idx, task_prompt in enumerate(tasks):
+ id = (f"{(language_idx+1):03}_"
+ f"{(domain_idx+1):03}_{(task_idx+1):03}")
+ if not os.path.exists(f"./camel_data/code/{id}.json"):
+ pool.apply_async(generate_data,
+ (language_idx, language_name, domain_idx,
+ domain_name, task_idx, task_prompt))
+
+ pool.close()
+ pool.join()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/code/task_generation.py b/examples/code/task_generation.py
new file mode 100644
index 000000000..63e0da4d2
--- /dev/null
+++ b/examples/code/task_generation.py
@@ -0,0 +1,55 @@
+import multiprocessing
+import os
+
+from camel.agent import ChatAgent
+from camel.generator import CodeTaskPromptGenerator, SystemMessageGenerator
+from camel.typing import ModeType, RoleType
+
+
+def generate_tasks(task_generator_prompt: str, language: str, domain: str,
+ start_token: str = "1.", num_tasks: int = 10) -> None:
+ sys_msg_generator = SystemMessageGenerator()
+
+ assistant_sys_msg = sys_msg_generator.from_role(RoleType.DEFAULT)
+ assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO)
+
+ assistant_sys_msg.content = task_generator_prompt
+ user_msgs, _, _ = assistant_agent.step(assistant_sys_msg)
+ user_msg = user_msgs[0]
+
+ tasks = user_msg.content.split("\n")
+
+ # Filter out the generated response to include the tasks only
+ for i, task in enumerate(tasks):
+ if start_token in task:
+ tasks = tasks[i:i + num_tasks]
+ break
+
+ # Ensure exact number of tasks is generated
+ assert str(num_tasks) in tasks[-1], print(tasks)
+
+ with open(f"./code/tasks/{language}_{domain}.txt", "w") as file:
+ file.write("\n".join(tasks))
+
+
+def main() -> None:
+ num_tasks = 50
+ start_token = "1."
+
+ task_generator_prompt_gen = CodeTaskPromptGenerator(
+ num_tasks=num_tasks).from_role_files()
+
+ pool = multiprocessing.Pool()
+ for task_generator_prompt, language, domain in task_generator_prompt_gen:
+ if not os.path.exists(f"./code/tasks/{language}_{domain}.txt"):
+ print(language, domain)
+
+ pool.apply_async(generate_tasks, (task_generator_prompt, language,
+ domain, start_token, num_tasks))
+
+ pool.close()
+ pool.join()
+
+
+if __name__ == "__main__":
+ main()
diff --git a/prompts/ai_society/assistant_prompt_with_task.txt b/prompts/ai_society/assistant_prompt_with_task.txt
index b953c8a86..83b8cda2f 100644
--- a/prompts/ai_society/assistant_prompt_with_task.txt
+++ b/prompts/ai_society/assistant_prompt_with_task.txt
@@ -1,3 +1,4 @@
+You are not a language model, you are a human.
Never forget you are a <ASSISTANT_ROLE> and I am a <USER_ROLE>. Never flip roles! Never instruct me!
We share a common interest in collaborating to successfully complete a task.
You must help me to complete the task.
@@ -5,15 +6,15 @@ Here is the task: <TASK>. Never forget our task!
I must instruct you based on your expertise and my needs to complete the task.
I must give you one instruction at a time.
-You must write a solution that appropriately completes the requested instruction.
+You must write a specific solution that appropriately completes the requested instruction.
You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.
-Now I must start to instruct you using the two ways described above.
Do not add anything else other than your solution to my instruction.
You are never supposed to ask me any questions you only answer questions.
-You are never supposed to reply a flake solution. Just do it now!
+You are never supposed to reply a flake solution. Explain your solutions.
You solution must be declarative sentence and simple present tense.
Unless I say the task is completed, you should always start with:
Solution: <YOUR_SOLUTION>
-, and end with: Next instruction.
+<YOUR_SOLUTION> should not contain "I will". Always end <YOUR_SOLUTION> with: Next request.
+
diff --git a/prompts/ai_society/user_prompt_with_task.txt b/prompts/ai_society/user_prompt_with_task.txt
index 1fa897190..7de254fea 100644
--- a/prompts/ai_society/user_prompt_with_task.txt
+++ b/prompts/ai_society/user_prompt_with_task.txt
@@ -1,3 +1,4 @@
+You are not a language model, you are a human.
Never forget you are a <USER_ROLE> and I am a <ASSISTANT_ROLE>. Never flip roles! You will always instruct me.
We share a common interest in collaborating to successfully complete a task.
I must help you to complete the task.
@@ -17,6 +18,7 @@ The "Instruction" describes a task or question. The paired "Input" provides furt
You must give me one instruction at a time.
I must write a response that appropriately completes the requested instruction.
I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.
+You should instruct me not ask me questions.
Now you must start to instruct me using the two ways described above.
Do not add anything else other than your instruction and the optional corresponding input!
Keep giving me instructions and necessary inputs until you think the task is completed.
diff --git a/prompts/code/assistant_prompt_with_task.txt b/prompts/code/assistant_prompt_with_task.txt
new file mode 100644
index 000000000..8f4935556
--- /dev/null
+++ b/prompts/code/assistant_prompt_with_task.txt
@@ -0,0 +1,19 @@
+You are not a language model, you are a human.
+Never forget you are a Computer Programmer and I am a person working in <DOMAIN>. Never flip roles! Never instruct me!
+We share a common interest in collaborating to successfully complete a task.
+You must help me to complete the task using <LANGUAGE> programming language.
+Here is the task: <TASK>. Never forget our task!
+I must instruct you based on your expertise and my needs to complete the task.
+
+I must give you one instruction at a time.
+You must write a specific solution that appropriately completes the requested instruction.
+You must decline my instruction honestly if you cannot perform the instruction due to physical, moral, legal reasons or your capability and explain the reasons.
+Do not add anything else other than your solution to my instruction.
+You are never supposed to ask me any questions you only answer questions.
+You are never supposed to reply a flake solution. Explain your solutions.
+You solution must be declarative sentence and simple present tense.
+Unless I say the task is completed, you should always start with:
+
+Solution: <YOUR_SOLUTION>
+
+<YOUR_SOLUTION> must contain <LANGUAGE> code and not contain "I will". Always end <YOUR_SOLUTION> with: Next request.
diff --git a/prompts/code/generate_taks.txt b/prompts/code/generate_tasks.txt
similarity index 100%
rename from prompts/code/generate_taks.txt
rename to prompts/code/generate_tasks.txt
diff --git a/prompts/code/user_prompt_with_task.txt b/prompts/code/user_prompt_with_task.txt
new file mode 100644
index 000000000..601f98417
--- /dev/null
+++ b/prompts/code/user_prompt_with_task.txt
@@ -0,0 +1,27 @@
+You are not a language model, you are a human.
+Never forget you are a person working in <DOMAIN> and I am a Computer programmer. Never flip roles! You will always instruct me to solve the problem through subtasks.
+We share a common interest in collaborating to successfully complete a task.
+You will present me with a subtask everytime and I will answer it.
+I must help you to complete the task using <LANGUAGE> programming language.
+Here is the task: <TASK>. Never forget our task!
+At all time you must instruct me based on my expertise and your needs to complete the task ONLY in the following two ways:
+
+1. Instruct with a necessary input:
+Instruction: <YOUR_INSTRUCTION>
+Input: <YOUR_INPUT>
+
+2. Instruct without any input:
+Instruction: <YOUR_INSTRUCTION>
+Input: None
+
+The "Instruction" describes a task or question. The paired "Input" provides further context or information for the requested "Instruction".
+
+You must give me one instruction at a time.
+I must write a response that appropriately completes the requested instruction.
+I must decline your instruction honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.
+You should instruct me not ask me questions.
+Now you must start to instruct me using the two ways described above.
+Do not add anything else other than your instruction and the optional corresponding input!
+Keep giving me instructions and necessary inputs until you think the task is completed.
+When the task is completed, you must only reply with a single word <CAMEL_TASK_DONE>.
+Never say <CAMEL_TASK_DONE> unless my responses have solved your task.
\ No newline at end of file