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

Refactor AI Society and Add Code (#28)

Guohao Lie9704d749605
diff --git a/camel/__init__.py b/camel/__init__.py
index 2fff0e88c..d94b16404 100644
--- a/camel/__init__.py
+++ b/camel/__init__.py
@@ -1,7 +1,12 @@
from .agent import ChatAgent, TaskSpecifyAgent
from .configs import ChatGPTConfig
-from .generator import (RoleNameGenerator, SystemMessageGenerator,
- TaskPromptGenerator)
+from .generator import (
+ AISocietyTaskPromptGenerator,
+ CodeTaskPromptGenerator,
+ RoleNameGenerator,
+ SingleTxtGenerator,
+ SystemMessageGenerator,
+)
from .typing import ModeType, RoleType
from .utils import get_model_token_limit, num_tokens_from_messages
@@ -18,5 +23,7 @@ __all__ = [
'TaskSpecifyAgent',
'SystemMessageGenerator',
'RoleNameGenerator',
- 'TaskPromptGenerator',
+ 'AISocietyTaskPromptGenerator',
+ 'SingleTxtGenerator',
+ 'CodeTaskPromptGenerator',
]
diff --git a/camel/generator.py b/camel/generator.py
index 9f2ce2789..c2faade42 100644
--- a/camel/generator.py
+++ b/camel/generator.py
@@ -1,7 +1,12 @@
from typing import Generator, List, Optional, Tuple
-from camel.message import (AssistantSystemMessage, RoleType, SystemMessage,
- SystemMessageType, UserSystemMessage)
+from camel.message import (
+ AssistantSystemMessage,
+ RoleType,
+ SystemMessage,
+ SystemMessageType,
+ UserSystemMessage,
+)
class SystemMessageGenerator:
@@ -9,14 +14,14 @@ 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_role_names_path: str = "data/ai_society/assistant_roles.txt",
+ user_role_names_path: str = "data/ai_society/user_roles.txt",
+ assistant_prompt_path: str = "prompts/ai_society/assistant_prompt.txt",
+ user_prompt_path: str = "prompts/ai_society/user_prompt.txt",
assistant_prompt_with_task_path:
- str = "prompts/assistant_prompt_with_task.txt",
+ str = "prompts/ai_society/assistant_prompt_with_task.txt",
user_task_prompt_with_task_path:
- str = "prompts/user_prompt_with_task.txt",
+ str = "prompts/ai_society/user_prompt_with_task.txt",
) -> None:
self.with_task = with_task
@@ -144,9 +149,11 @@ class SystemMessageGenerator:
class RoleNameGenerator:
- def __init__(self,
- assistant_role_names_path: str = "data/assistant_roles.txt",
- user_role_names_path: str = "data/user_roles.txt") -> None:
+ def __init__(
+ self,
+ assistant_role_names_path: str = "data/ai_society/assistant_roles.txt",
+ user_role_names_path: str = "data/ai_society/user_roles.txt",
+ ) -> None:
with open(assistant_role_names_path, "r") as f:
assistant_role_names: List[str] = f.read().splitlines()
@@ -166,11 +173,12 @@ class RoleNameGenerator:
yield (assistant_role_name, user_role_name)
-class TaskPromptGenerator:
+class AISocietyTaskPromptGenerator:
def __init__(
self,
- generate_tasks_prompt_path: str = "prompts/generate_tasks.txt",
+ generate_tasks_prompt_path:
+ str = "prompts/ai_society/generate_tasks.txt",
num_tasks: int = 10,
) -> None:
@@ -180,8 +188,9 @@ class TaskPromptGenerator:
self.num_tasks = num_tasks
def from_role_files(
- self, assistant_role_names_path: str = "data/assistant_roles.txt",
- user_role_names_path: str = "data/user_roles.txt"
+ self,
+ assistant_role_names_path: str = "data/ai_society/assistant_roles.txt",
+ user_role_names_path: str = "data/ai_society/user_roles.txt"
) -> Generator[str, None, None]:
roles_generator = RoleNameGenerator(
assistant_role_names_path, user_role_names_path).from_role_files()
@@ -199,3 +208,55 @@ class TaskPromptGenerator:
yield (self.generate_tasks_prompt.replace("<ROLE_1>",
role_1).replace(
"<ROLE_2>", role_2))
+
+
+class SingleTxtGenerator:
+
+ def __init__(
+ self,
+ text_file_path: str,
+ ) -> None:
+
+ with open(text_file_path, "r") as f:
+ data_list: List[str] = f.read().splitlines()
+ self.data_list = [
+ " ".join(name.split(" ")[1:]) for name in data_list
+ ]
+
+ def from_role_files(self) -> Generator[Tuple, None, None]:
+ for data in self.data_list:
+ yield data
+
+
+class CodeTaskPromptGenerator:
+
+ def __init__(
+ self,
+ generate_tasks_prompt_path: str = "prompts/code/generate_tasks.txt",
+ num_tasks: int = 50,
+ ) -> None:
+
+ with open(generate_tasks_prompt_path, "r") as f:
+ self.generate_tasks_prompt: str = f.read()
+
+ self.num_tasks = num_tasks
+
+ def from_role_files(
+ self, languages_path: str = "data/code/languages.txt",
+ domains_path: str = "data/code/domains.txt"
+ ) -> Generator[str, None, None]:
+ language_generator = SingleTxtGenerator(
+ languages_path).from_role_files()
+ domains_generator = SingleTxtGenerator(domains_path).from_role_files()
+ for language in language_generator:
+ for domain in domains_generator:
+ yield (self.generate_tasks_prompt.replace(
+ "<LANGUAGE>",
+ language).replace("<DOMAIN>",
+ domain).replace("<NUM_TASKS>",
+ str(self.num_tasks)))
+
+ def from_role_generator(
+ self, role_generator: Generator[Tuple, None, None]
+ ) -> Generator[str, None, None]:
+ raise NotImplementedError
diff --git a/data/assistant_roles.txt b/data/ai_society/assistant_roles.txt
similarity index 100%
rename from data/assistant_roles.txt
rename to data/ai_society/assistant_roles.txt
diff --git a/data/user_roles.txt b/data/ai_society/user_roles.txt
similarity index 100%
rename from data/user_roles.txt
rename to data/ai_society/user_roles.txt
diff --git a/data/code/domains.txt b/data/code/domains.txt
new file mode 100644
index 000000000..13adbbcfa
--- /dev/null
+++ b/data/code/domains.txt
@@ -0,0 +1,50 @@
+1. Accounting
+2. Agriculture
+3. Anthropology
+4. Architecture
+5. Art
+6. Biology
+7. Business
+8. Chemistry
+9. Communications
+10. Computer Science
+11. Criminal Justice
+12. Culinary Arts
+13. Dentistry
+14. Economics
+15. Education
+16. Engineering
+17. Environmental Science
+18. Fashion
+19. Film
+20. Finance
+21. Geography
+22. Geology
+23. Graphic Design
+24. Health Sciences
+25. History
+26. Hospitality
+27. Human Resources
+28. Information Technology
+29. Journalism
+30. Law
+31. Linguistics
+32. Marketing
+33. Mathematics
+34. Mechanical Engineering
+35. Medicine
+36. Music
+37. Nursing
+38. Nutrition
+39. Philosophy
+40. Physics
+41. Political Science
+42. Psychology
+43. Public Administration
+44. Public Health
+45. Real Estate
+46. Sociology
+47. Sports Science
+48. Statistics
+49. Theater
+50. Urban Planning
\ No newline at end of file
diff --git a/data/code/languages.txt b/data/code/languages.txt
new file mode 100644
index 000000000..af954c130
--- /dev/null
+++ b/data/code/languages.txt
@@ -0,0 +1,20 @@
+1. Java
+2. Python
+3. JavaScript
+4. C#
+5. PHP
+6. C++
+7. Ruby
+8. Swift
+9. Objective-C
+10. SQL
+11. Go
+12. Kotlin
+13. TypeScript
+14. R
+15. MATLAB
+16. Perl
+17. Shell
+18. Visual Basic
+19. Assembly
+20. Dart
\ No newline at end of file
diff --git a/examples/ai_society/role_generation.py b/examples/ai_society/role_generation.py
new file mode 100644
index 000000000..0e3b489f9
--- /dev/null
+++ b/examples/ai_society/role_generation.py
@@ -0,0 +1,21 @@
+from camel.agent import ChatAgent
+from camel.message import AssistantSystemMessage, UserChatMessage
+from camel.typing import ModeType
+
+
+def main(role_type: str = "user", num_roles: int = 50):
+ with open(f"prompts/generate_{role_type}.txt", "r") as f:
+ prompt = f.read().replace("<NUM_ROLES>", str(num_roles))
+ print(prompt)
+ assistant_sys_msg = AssistantSystemMessage(
+ "Assistant", content="You are a helpful assistant.")
+ agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent.reset()
+
+ user_msg = UserChatMessage("User", content=prompt)
+ assistant_msg, _, _ = agent.step(user_msg)
+ print(assistant_msg[0].content)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/examples/role_playing.py b/examples/ai_society/role_playing.py
similarity index 79%
rename from examples/role_playing.py
rename to examples/ai_society/role_playing.py
index 52b2f1234..eb3df7b26 100644
--- a/examples/role_playing.py
+++ b/examples/ai_society/role_playing.py
@@ -12,13 +12,14 @@ def print_text_animated(text):
def main() -> None:
- task_prompt = "Design a custom game using PyGame"
+ task_prompt = "Coding with Python Programming Language."
print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
- role_play_session = RolePlaying("Computer Programer", "Gamer", task_prompt)
+ role_play_session = RolePlaying("Python coder", "Student", task_prompt,
+ with_task_specify=False)
print(Fore.CYAN +
f"Specified task prompt:\n{role_play_session.task_prompt}\n")
- chat_turn_limit, n = 10, 0
+ chat_turn_limit, n = 50, 0
assistant_msg, _ = role_play_session.init_chat()
while n < chat_turn_limit:
n += 1
diff --git a/examples/role_playing_multiprocess.py b/examples/ai_society/role_playing_multiprocess.py
similarity index 99%
rename from examples/role_playing_multiprocess.py
rename to examples/ai_society/role_playing_multiprocess.py
index f406a8d3d..0d62546f7 100644
--- a/examples/role_playing_multiprocess.py
+++ b/examples/ai_society/role_playing_multiprocess.py
@@ -50,7 +50,7 @@ def generate_data(assistant_idx: int, assistant_role_name: str, user_idx: int,
specified_task_prompt = task_specify_agent.specify_task(
original_task_prompt, [assistant_role_name, user_role_name])
print(f"Original Task: {original_task_prompt}")
- print(f"Specfied Task: {specified_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),
diff --git a/examples/task_generation.py b/examples/ai_society/task_generation.py
similarity index 90%
rename from examples/task_generation.py
rename to examples/ai_society/task_generation.py
index de41d59ae..5195da725 100644
--- a/examples/task_generation.py
+++ b/examples/ai_society/task_generation.py
@@ -2,8 +2,11 @@ import multiprocessing
import os
from camel.agent import ChatAgent
-from camel.generator import (RoleNameGenerator, SystemMessageGenerator,
- TaskPromptGenerator)
+from camel.generator import (
+ AISocietyTaskPromptGenerator,
+ RoleNameGenerator,
+ SystemMessageGenerator,
+)
from camel.typing import ModeType, RoleType
@@ -38,7 +41,7 @@ def main() -> None:
start_token = "1."
role_names_generator = RoleNameGenerator().from_role_files()
- task_generator_prompt_generator = TaskPromptGenerator(
+ task_generator_prompt_generator = AISocietyTaskPromptGenerator(
num_tasks=num_tasks).from_role_files()
pool = multiprocessing.Pool()
diff --git a/examples/code/generate_meta_data.py b/examples/code/generate_meta_data.py
new file mode 100644
index 000000000..305356515
--- /dev/null
+++ b/examples/code/generate_meta_data.py
@@ -0,0 +1,22 @@
+from camel.agent import ChatAgent
+from camel.message import AssistantSystemMessage, UserChatMessage
+from camel.typing import ModeType
+
+
+def generate_meta_data(meta_data: str, num: int = 50):
+ with open(f"prompts/code/generate_{meta_data}.txt", "r") as f:
+ prompt = f.read().replace(f"<NUM_{meta_data.upper()}>", str(num))
+ print(prompt)
+ assistant_sys_msg = AssistantSystemMessage(
+ "Assistant", content="You are a helpful assistant.")
+ agent = ChatAgent(assistant_sys_msg, model=ModeType.GPT_3_5_TURBO)
+ agent.reset()
+
+ user_msg = UserChatMessage("User", content=prompt)
+ assistant_msg, _, _ = agent.step(user_msg)
+ print(assistant_msg[0].content)
+
+
+if __name__ == "__main__":
+ generate_meta_data("languages", 20)
+ generate_meta_data("domains", 50)
diff --git a/prompts/ai_society/assistant_prompt.txt b/prompts/ai_society/assistant_prompt.txt
new file mode 100644
index 000000000..9937030ad
--- /dev/null
+++ b/prompts/ai_society/assistant_prompt.txt
@@ -0,0 +1,18 @@
+Never forget you are a <ASSISTANT_ROLE> and I am a <USER_ROLE>. Never flip roles!
+I must instruct you based on your expertise and my needs to perform any tasks or answer any questions in the following 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".
+
+I must give you one instruction at a time.
+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.
\ No newline at end of file
diff --git a/prompts/ai_society/assistant_prompt_with_task.txt b/prompts/ai_society/assistant_prompt_with_task.txt
new file mode 100644
index 000000000..6a8204e3f
--- /dev/null
+++ b/prompts/ai_society/assistant_prompt_with_task.txt
@@ -0,0 +1,22 @@
+Never forget you are a <ASSISTANT_ROLE> and I am a <USER_ROLE>. Never flip roles!
+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 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".
+
+I must give you one instruction at a time.
+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.
+You are never supposed to ask me any questions you only answer questions.
\ No newline at end of file
diff --git a/prompts/generate_assistant.txt b/prompts/ai_society/generate_assistant.txt
similarity index 100%
rename from prompts/generate_assistant.txt
rename to prompts/ai_society/generate_assistant.txt
diff --git a/prompts/ai_society/generate_tasks.txt b/prompts/ai_society/generate_tasks.txt
new file mode 100644
index 000000000..f976993cb
--- /dev/null
+++ b/prompts/ai_society/generate_tasks.txt
@@ -0,0 +1 @@
+List <NUM_TASKS> diverse tasks that a programmer can assist a person working in <DOMAIN> using <LANGUAGE>. Be concise. Be creative.
\ No newline at end of file
diff --git a/prompts/generate_user.txt b/prompts/ai_society/generate_user.txt
similarity index 100%
rename from prompts/generate_user.txt
rename to prompts/ai_society/generate_user.txt
diff --git a/prompts/ai_society/task_specify_prompt.txt b/prompts/ai_society/task_specify_prompt.txt
new file mode 100644
index 000000000..f3eba7bac
--- /dev/null
+++ b/prompts/ai_society/task_specify_prompt.txt
@@ -0,0 +1,2 @@
+Here is a task that <ASSISTANT_ROLE> will help <USER_ROLE> to complete: <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
diff --git a/prompts/ai_society/user_prompt.txt b/prompts/ai_society/user_prompt.txt
new file mode 100644
index 000000000..2a5f11c5f
--- /dev/null
+++ b/prompts/ai_society/user_prompt.txt
@@ -0,0 +1,19 @@
+Never forget you are a <USER_ROLE> and I am a <ASSISTANT_ROLE>. Never flip roles!
+You must instruct me based on my expertise and your needs to perform any tasks or ask any questions 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 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. Do not stopping!
\ No newline at end of file
diff --git a/prompts/ai_society/user_prompt_with_task.txt b/prompts/ai_society/user_prompt_with_task.txt
new file mode 100644
index 000000000..601253cfa
--- /dev/null
+++ b/prompts/ai_society/user_prompt_with_task.txt
@@ -0,0 +1,24 @@
+Never forget you are a <USER_ROLE> and I am a <ASSISTANT_ROLE>. Never flip roles!
+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 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 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.
+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
diff --git a/prompts/code/generate_domains.txt b/prompts/code/generate_domains.txt
new file mode 100644
index 000000000..9ef65b321
--- /dev/null
+++ b/prompts/code/generate_domains.txt
@@ -0,0 +1 @@
+List <NUM_DOMAINS> most common fields of study that programming could help with. Be concise. Sort them by alphabetical order. No explanation required.
\ No newline at end of file
diff --git a/prompts/code/generate_languages.txt b/prompts/code/generate_languages.txt
new file mode 100644
index 000000000..77bd89ade
--- /dev/null
+++ b/prompts/code/generate_languages.txt
@@ -0,0 +1 @@
+List the <NUM_LANGUAGES> most commonly used computer programming languages. Be concise. No explanation required.
\ No newline at end of file
diff --git a/prompts/code/generate_taks.txt b/prompts/code/generate_taks.txt
new file mode 100644
index 000000000..f976993cb
--- /dev/null
+++ b/prompts/code/generate_taks.txt
@@ -0,0 +1 @@
+List <NUM_TASKS> diverse tasks that a programmer can assist a person working in <DOMAIN> using <LANGUAGE>. Be concise. Be creative.
\ No newline at end of file
diff --git a/prompts/generate_tasks.txt b/prompts/generate_tasks.txt
deleted file mode 100644
index c978fedac..000000000
--- a/prompts/generate_tasks.txt
+++ /dev/null
@@ -1 +0,0 @@
-List <NUM_TASKS> diverse tasks that <ROLE_1> can assist <ROLE_2> cooperatively to achieve together. Be concise. Be creative.
diff --git a/test/test_generator.py b/test/test_generator.py
index e2865c4d9..729a106dd 100644
--- a/test/test_generator.py
+++ b/test/test_generator.py
@@ -1,5 +1,8 @@
-from camel.generator import (RoleNameGenerator, SystemMessageGenerator,
- TaskPromptGenerator)
+from camel.generator import (
+ AISocietyTaskPromptGenerator,
+ RoleNameGenerator,
+ SystemMessageGenerator,
+)
from camel.typing import RoleType
@@ -31,9 +34,9 @@ def test_role_name_generator():
def test_task_prompt_generator():
role_name_generator = RoleNameGenerator().from_role_files()
- task_tuple = next(
- TaskPromptGenerator().from_role_generator(role_name_generator))
+ task_tuple = next(AISocietyTaskPromptGenerator().from_role_generator(
+ role_name_generator))
assert isinstance(task_tuple, str)
- task_tuple = next(TaskPromptGenerator().from_role_files())
+ task_tuple = next(AISocietyTaskPromptGenerator().from_role_files())
assert isinstance(task_tuple, str)