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 with_task support for role playing (#6)

Guohao Liec77823c37e2
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 000000000..9e6cd3285
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,26 @@
+{
+ "makefile.extensionOutputFolder": "./.vscode",
+ "python.formatting.provider": "yapf",
+ "python.testing.pytestArgs": [
+ "test"
+ ],
+ "python.testing.unittestEnabled": false,
+ "python.testing.pytestEnabled": true,
+ "python.linting.enabled": true,
+ "python.linting.pylintEnabled": false,
+ "python.linting.pycodestyleEnabled": false,
+ "python.linting.flake8Enabled": true,
+ "python.linting.lintOnSave": true,
+ "python.linting.flake8Args": [
+ "--based_on_style=pep8",
+ "--split_before_named_assigns=False"
+ ],
+ "isort.args": [
+ "--skip",
+ "__init__.py"
+ ],
+ "editor.formatOnSave": true,
+ "editor.codeActionsOnSave": {
+ "source.sortImports": true,
+ },
+}
\ No newline at end of file
diff --git a/camel/configs.py b/camel/configs.py
index a323230ae..5e8a4728a 100644
--- a/camel/configs.py
+++ b/camel/configs.py
@@ -20,13 +20,20 @@ class ChatGPTConfig:
class SystemMessageGenerator:
+
def __init__(
self,
+ with_task: bool = True,
assistant_role_names_path: str = "data/assistant_roles.txt",
user_role_names_path: str = "data/user_roles.txt",
assistant_prompt_path: str = "prompts/assistant_prompt.txt",
user_prompt_path: str = "prompts/user_prompt.txt",
+ assistant_prompt_with_task_path:
+ str = "prompts/assistant_prompt_with_task.txt",
+ user_task_prompt_with_task_path:
+ str = "prompts/user_prompt_with_task.txt",
) -> None:
+ self.with_task = with_task
with open(assistant_role_names_path, "r") as f:
self.assistant_role_names: List[str] = f.read().splitlines()
@@ -34,25 +41,41 @@ class SystemMessageGenerator:
with open(user_role_names_path, "r") as f:
self.user_role_names: List[str] = f.read().splitlines()
- with open(assistant_prompt_path, "r") as f:
- self.assistant_prompt: str = f.read()
+ 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_prompt_path, "r") as f:
- self.user_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,
role_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("<ASSISTANT_ROLE>",
role_name)
assert new_role_prompt != role_prompt
+ if self.with_task:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>",
+ task_prompt,
+ )
+
return AssistantSystemMessage(role_name=role_name,
content=new_role_prompt)
@@ -61,14 +84,21 @@ class SystemMessageGenerator:
new_role_prompt = role_prompt.replace("<USER_ROLE>", role_name)
assert new_role_prompt != role_prompt
+ if self.with_task:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>",
+ task_prompt,
+ )
+
return UserSystemMessage(role_name=role_name,
content=new_role_prompt)
if role_type == RoleType.DEFAULT:
new_role_prompt = role_prompt or "Your are a helpful assistant."
-
- return SystemMessage(role_type=role_type,
- role_name=role_name,
+ if task_prompt is not None:
+ new_role_prompt = new_role_prompt.replace(
+ "<TASK>", task_prompt)
+ return SystemMessage(role_type=role_type, role_name=role_name,
content=new_role_prompt)
def from_roles(
@@ -76,7 +106,10 @@ class SystemMessageGenerator:
roles: List[Tuple[RoleType, 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]
@@ -88,8 +121,8 @@ class SystemMessageGenerator:
for i, role_prompt in enumerate(role_prompts):
for role_name, role_type in roles:
- role_prompt = self.from_role(role_name, role_type,
- role_prompt).content
+ role_prompt = self.from_role(role_name, role_type, role_prompt,
+ task_prompt).content
role_prompts[i] = role_prompt
for role_name, role_type in roles:
@@ -114,18 +147,18 @@ class SystemMessageGenerator:
return system_messages
+ # TODO: support task generator as input.
def from_role_files(
self) -> Generator[List[SystemMessageType], None, None]:
for assistant_role_name in self.assistant_role_names:
for user_role_name in self.user_role_names:
- yield self.from_roles(roles=[
- (assistant_role_name, RoleType.ASSISTANT),
- (user_role_name, RoleType.USER)
- ], )
+ yield self.from_roles(
+ roles=[(assistant_role_name, RoleType.ASSISTANT),
+ (user_role_name, RoleType.USER)], )
if __name__ == "__main__":
- sys_msg_generator = SystemMessageGenerator()
+ sys_msg_generator = SystemMessageGenerator(with_task=False)
sys_msg_generator.from_role(role_name="doctor",
role_type=RoleType.ASSISTANT)
sys_msg_generator.from_role(role_name="doctor", role_type=RoleType.USER)
@@ -137,3 +170,8 @@ if __name__ == "__main__":
sys_msg_1 = next(generator)
sys_msg_2 = next(generator)
assert sys_msg_1 != sys_msg_2
+
+ sys_msg_generator = SystemMessageGenerator(with_task=True)
+ sys_msg_generator.from_roles(
+ roles=[("doctor", RoleType.USER), ("chatbot", RoleType.ASSISTANT)],
+ task_prompt="Analyze a patient's medical report")
diff --git a/camel/role_playing.py b/camel/role_playing.py
index 236f78ab0..975b288bf 100644
--- a/camel/role_playing.py
+++ b/camel/role_playing.py
@@ -22,7 +22,7 @@ def init_chat(
"Only reply with Instruction and Input."))
assistant_msg.role = "user"
- user_msg = UserChatMessage("Student",
+ user_msg = UserChatMessage(user_sys_msg.role_name,
content=f"{assistant_sys_msg.content}")
msgs, _, _ = assistant_agent.step(user_msg)
@@ -30,10 +30,11 @@ def init_chat(
def main() -> None:
- sys_msg_generator = SystemMessageGenerator()
+ task_prompt = "Developing custom game mods or plugins for Minecraft"
+ sys_msg_generator = SystemMessageGenerator(with_task=True)
assistant_sys_msg, user_sys_msg = sys_msg_generator.from_roles(
- roles=[("Computer Programmer",
- RoleType.ASSISTANT), ("Gamer", RoleType.USER)])
+ roles=[("Computer Programmer", RoleType.ASSISTANT),
+ ("Gamer", RoleType.USER)], task_prompt=task_prompt)
assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO)
user_agent = ChatAgent(user_sys_msg, ModeType.GPT_3_5_TURBO)
diff --git a/prompts/assistant_prompt_with_task.txt b/prompts/assistant_prompt_with_task.txt
new file mode 100644
index 000000000..234267b55
--- /dev/null
+++ b/prompts/assistant_prompt_with_task.txt
@@ -0,0 +1,21 @@
+Never forget you are a <ASSISTANT_ROLE> and I am a <USER_ROLE>.
+We share a common interest in collaborating to successfully complete a task.
+You must help me to complete the task.
+Here is the task: <TASK>. Never forget our task!
+I must instruct you based on your expertise and my needs to complete the task in the following to two ways:
+
+1. Instruct with a necessary input:
+Instruction: <MY_INSTRUCTION>
+Input: <MY_INPUT>
+
+2. Instruct without any input:
+Instruction: <MY_INSTRUCTION>
+Input: None
+
+The "Instruction" describes a task or question. The paired "Input" provides further context or information for the requested "Instruction".
+
+You must write a response that appropriately completes the requested instruction.
+You must decline my instruction politely and 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 response to my instruction.
+Once the task is completed, I must reply with <CAMEL_TASK_DONE> to indicate.
\ No newline at end of file
diff --git a/prompts/user_prompt_with_task.txt b/prompts/user_prompt_with_task.txt
new file mode 100644
index 000000000..2b50be68e
--- /dev/null
+++ b/prompts/user_prompt_with_task.txt
@@ -0,0 +1,22 @@
+Never forget you are a <USER_ROLE> and I am a <ASSISTANT_ROLE>.
+We share a common interest in collaborating to successfully complete a task.
+I must help you to complete the task.
+Here is the task: <TASK>. Never forget our task!
+You must instruct me based on my expertise and your needs to complete the task in the following to 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".
+
+I must write a response that appropriately completes the requested instruction.
+I must decline your instruction politely and honestly if I cannot perform the instruction due to physical, moral, legal reasons or my capability and explain the reasons.
+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.
+Once the task is completed, you must reply with <CAMEL_TASK_DONE> to indicate.
\ No newline at end of file