🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
Add task specifier agent (#8)
Guohao Licf4b928ec781
diff --git a/camel/chat_agent.py b/camel/chat_agent.pyindex 1bd4367c5..c96abe16e 100644--- a/camel/chat_agent.py+++ b/camel/chat_agent.py@@ -2,13 +2,14 @@ from typing import Any, Dict, List, Optional, Tupleimport openaifrom camel_typing import (ChatMessage, MessageType, ModeType, RoleType,- SystemMessage)+ SystemMessage, UserChatMessage)from camel_utils import get_model_token_limit, num_tokens_from_messagesfrom configs import ChatGPTConfig-from tenacity import retry, stop_after_attempt+from tenacity import retry, stop_after_attempt, wait_fixedclass ChatAgent:+def __init__(self,system_message: SystemMessage,@@ -52,7 +53,7 @@ class ChatAgent:self.stored_messages.append(message)return self.stored_messages- @retry(stop=stop_after_attempt(5))+ @retry(wait=wait_fixed(60), stop=stop_after_attempt(5))def step(self, input_message: ChatMessage) -> Tuple[List[ChatMessage], bool, Dict[str, Any]]:@@ -99,10 +100,50 @@ class ChatAgent:return f"ChatAgent({self.role_name}, {self.role_type}, {self.model})"+class TaskSpecifyAgent(ChatAgent):++ def __init__(+ self,+ model: ModeType,+ model_config: Any = None,+ task_specify_prompt: Optional[str] = None,+ task_specify_prompt_path: str = "prompts/task_specify_prompt.txt",+ word_limit: int = 50,+ ) -> None:+ if task_specify_prompt is None:+ with open(task_specify_prompt_path, "r") as f:+ self.task_specify_prompt = f.read().replace(+ "<WORD_LIMIT>", str(word_limit))+ else:+ self.task_specify_prompt = task_specify_prompt++ system_message = SystemMessage(+ role_name="task_specifier",+ role_type=RoleType.ASSISTANT,+ content="You can specify a task for the assistant to perform.",+ )+ super().__init__(system_message, model, model_config)++ def specify_task(self, original_task_prompt: str) -> str:+ self.reset()+ self.task_specify_prompt = self.task_specify_prompt.replace(+ "<TASK>", original_task_prompt)+ task_msg = UserChatMessage(role_name="task_specifier",+ content=self.task_specify_prompt)+ specified_task_msgs, terminated, _ = self.step(task_msg)+ specified_task_msg = specified_task_msgs[0]++ if terminated:+ raise RuntimeError("Task specification failed.")+ else:+ return specified_task_msg.content++if __name__ == "__main__":+ # test_chat_agent.py::test_chat_agentfrom configs import SystemMessageGeneratorchat_gpt_args = ChatGPTConfig()- system_message = SystemMessageGenerator().from_role(+ system_message = SystemMessageGenerator(with_task=False).from_role("doctor", RoleType.ASSISTANT)assistant = ChatAgent(system_message,@@ -128,3 +169,12 @@ if __name__ == "__main__":assert terminated is Trueassert messages == []print(info)++ # test_chat_agent.py::test_task_specify_agent+ original_task_prompt = "Developing custom game mods or plugins"+ print(f"Original task prompt:\n{original_task_prompt}\n")+ task_specify_agent = TaskSpecifyAgent(+ ModeType.GPT_3_5_TURBO, model_config=ChatGPTConfig(temperature=1.4))+ specified_task_prompt = task_specify_agent.specify_task(+ original_task_prompt)+ print(f"Specified task prompt:\n{specified_task_prompt}\n")diff --git a/camel/role_playing.py b/camel/role_playing.pyindex 975b288bf..42495aeb1 100644--- a/camel/role_playing.py+++ b/camel/role_playing.py@@ -1,8 +1,10 @@+from typing import List, Tuple+from camel_typing import (AssistantChatMessage, AssistantSystemMessage,- ModeType, RoleType, UserChatMessage,+ ChatMessage, ModeType, RoleType, UserChatMessage,UserSystemMessage)-from chat_agent import ChatAgent-from configs import SystemMessageGenerator+from chat_agent import ChatAgent, TaskSpecifyAgent+from configs import ChatGPTConfig, SystemMessageGeneratordef init_chat(@@ -10,7 +12,7 @@ def init_chat(user_agent: ChatAgent,user_sys_msg: UserSystemMessage,assistant_sys_msg: AssistantSystemMessage,-):+) -> Tuple[AssistantChatMessage, List[ChatMessage]]:assistant_agent.reset()user_agent.reset()@@ -30,11 +32,18 @@ def init_chat(def main() -> None:- task_prompt = "Developing custom game mods or plugins for Minecraft"+ original_task_prompt = "Developing custom game mods or plugins"+ print(f"Original task prompt:\n{original_task_prompt}\n")+ task_specify_agent = TaskSpecifyAgent(ModeType.GPT_3_5_TURBO,+ ChatGPTConfig(temperature=1.4))+ specified_task_prompt = task_specify_agent.specify_task(+ original_task_prompt)+ print(f"Specified task prompt:\n{specified_task_prompt}\n")+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)], task_prompt=task_prompt)+ ("Gamer", RoleType.USER)], task_prompt=specified_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/task_specify_prompt.txt b/prompts/task_specify_prompt.txtnew file mode 100644index 000000000..2e0eacb4c--- /dev/null+++ b/prompts/task_specify_prompt.txt@@ -0,0 +1,2 @@+Here is a task: <TASK>. Please make it more specific? Be creative and imaginative.+Please reply with the specified task in <WORD_LIMIT> words or less. Do not add anything else.\ No newline at end of file