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

Language Translation Code (#64)

Hasan Abed Al Kader Hammoud53585cd2f98d
diff --git a/camel/generator.py b/camel/generator.py
index 803370883..75da54555 100644
--- a/camel/generator.py
+++ b/camel/generator.py
@@ -44,6 +44,12 @@ class SystemMessageGenerator:
self.sys_msg_meta_dict_keys = {
'<ASSISTANT_ROLE>', "<USER_ROLE>", "<TASK>"
}
+ elif task_type == TaskType.TRANSLATION:
+ sys_prompts_paths = {
+ RoleType.ASSISTANT:
+ "prompts/translation/assistant_prompt.txt",
+ }
+ self.sys_msg_meta_dict_keys = {"<LANGUAGE>"}
elif task_type == TaskType.DEFAULT:
self.sys_msg_meta_dict_keys = set()
else:
diff --git a/camel/typing.py b/camel/typing.py
index c67450c5c..c8dd5e2e3 100644
--- a/camel/typing.py
+++ b/camel/typing.py
@@ -17,6 +17,7 @@ class TaskType(Enum):
AI_SOCIETY = "ai_society"
CODE = "code"
MISALIGNMENT = "misalignment"
+ TRANSLATION = "translation"
DEFAULT = "default"
diff --git a/examples/translation/translator.py b/examples/translation/translator.py
new file mode 100644
index 000000000..b190aebb6
--- /dev/null
+++ b/examples/translation/translator.py
@@ -0,0 +1,91 @@
+import codecs
+import json
+import multiprocessing
+import os
+
+from camel.agent import ChatAgent
+from camel.generator import SystemMessageGenerator
+from camel.message import UserChatMessage
+from camel.typing import ModeType, RoleType, TaskType
+
+
+def translate_content(directory_path: str, file_path: str,
+ language: str) -> None:
+
+ # File_path of the .json file to translate we extract the name for saving
+ file_name = file_path.split("/")[-1].split(".json")[0]
+
+ # Check that file_name.json does not exist in the target directory
+ save_path = f"{directory_path}_translated/{language}/{file_name}.json"
+ if os.path.exists(save_path):
+ return
+
+ # Load the json file
+ with open(file_path, "r") as json_file:
+ json_data = json.load(json_file)
+
+ # Translate the content of each message in the json
+ for i in range(json_data['num_messages']):
+
+ msg_i_content = "Sentence to translate: " + json_data[
+ f"message_{i+1}"]["content"]
+
+ sys_msg_generator = SystemMessageGenerator(
+ task_type=TaskType.TRANSLATION)
+
+ assistant_sys_msg = sys_msg_generator.from_dict(
+ meta_dict={'<LANGUAGE>': language.capitalize()},
+ role_tuple=('Language Translator', RoleType.ASSISTANT))
+
+ assistant_agent = ChatAgent(assistant_sys_msg, ModeType.GPT_3_5_TURBO)
+
+ user_msg = UserChatMessage(role_name="Language Translator",
+ content=msg_i_content)
+
+ assistant_msgs, _, _ = assistant_agent.step(user_msg)
+ assistant_msg = assistant_msgs[0]
+
+ json_data[f"message_{i+1}"]["content"] = assistant_msg.content
+
+ with codecs.open(save_path, 'w', encoding='utf-8') as f:
+ json.dump(json_data, f, ensure_ascii=False, indent=4)
+
+
+def main(directory_path: str) -> None:
+
+ # List of languages to translate to
+ language_list = [
+ "arabic", "chinese", "french", "german", "hindi", "italian",
+ "japanese", "korean", "russian", "spanish"
+ ]
+
+ # Get the language to translate based on Slurm array index
+ try:
+ language_index = int(os.environ["SLURM_ARRAY_TASK_ID"])
+ except KeyError:
+ print("SLURM_ARRAY_TASK_ID not found. Defaulting to 0 (i.e Arabic)")
+ # Default to Arabic, you can change to any other language
+ language_index = 0
+
+ language = language_list[language_index]
+
+ # Get list of all .json files paths
+ json_file_paths = []
+
+ for filename in os.listdir(directory_path):
+ if filename.endswith(".json"):
+ file_path = os.path.join(directory_path, filename)
+ json_file_paths.append(file_path)
+
+ pool = multiprocessing.Pool()
+
+ # Apply parallel translation to all .json files
+ for file_path in json_file_paths:
+ pool.apply_async(translate_content,
+ args=(directory_path, file_path, language))
+ pool.close()
+ pool.join()
+
+
+if __name__ == "__main__":
+ main(directory_path="./camel_data/ai_society")
diff --git a/prompts/translation/assistant_prompt.txt b/prompts/translation/assistant_prompt.txt
new file mode 100644
index 000000000..5b259ce34
--- /dev/null
+++ b/prompts/translation/assistant_prompt.txt
@@ -0,0 +1 @@
+You are an expert English to <LANGUAGE> translator. Your sole purpose is to accurately translate any text presented to you from English to <LANGUAGE>. Please provide the <LANGUAGE> translation for the given text. If you are presented with an empty string, simply return an empty string as the translation. Only text in between ```TEXT``` should not be translated. Do not provide any explanation. Just provide a translation.