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

Data Explorer based on Gradio (#22)

Dmitrii Khizbullin0317d7bea666
diff --git a/data_explorer/.gitignore b/data_explorer/.gitignore
new file mode 100644
index 000000000..f62cb4dfb
--- /dev/null
+++ b/data_explorer/.gitignore
@@ -0,0 +1,2 @@
+DATA/
+camel_data/
diff --git a/data_explorer/README.md b/data_explorer/README.md
new file mode 100644
index 000000000..8b14c05ef
--- /dev/null
+++ b/data_explorer/README.md
@@ -0,0 +1,12 @@
+# Data Explorer tool to browse Camel dataset
+
+## How to run Gradio web UI
+1. Put jsons into `data_explorer/camel_data/`.
+2. Add `pip install gradio`.
+3. In `data_explorer` run `gradio data_explorer.py`. Alternatively run `python data_explorer.py`.
+4. Open the web UI at `localhost:8080`.
+5. Have fun!
+
+Validated for python 3.8 and 3.10.
+
+Run `python data_explorer.py --help` for command line options.
\ No newline at end of file
diff --git a/data_explorer/data_explorer.py b/data_explorer/data_explorer.py
new file mode 100644
index 000000000..7fd16c326
--- /dev/null
+++ b/data_explorer/data_explorer.py
@@ -0,0 +1,183 @@
+"""
+Gradio-based web UI to explore the Camel dataset.
+"""
+
+import argparse
+import random
+from typing import Any, Dict, List, Tuple
+
+import gradio as gr
+
+from loader import load_data
+
+
+def parse_arguments():
+ """ Get command line arguments. """
+
+ parser = argparse.ArgumentParser("Camel data explorer")
+ parser.add_argument('--data-path', type=str, default="camel_data/",
+ help='Path to the folder with chat JSONs')
+ parser.add_argument('--share', type=bool, default=False,
+ help='Expose the web UI to Gradio')
+ parser.add_argument('--server-port', type=int, default=8080,
+ help='Port ot run the web page on')
+ parser.add_argument('--inbrowser', type=bool, default=False,
+ help='Open the web UI in the default browser on lunch')
+ parser.add_argument(
+ '--concurrency-count', type=int, default=10,
+ help='Number if concurrent threads at Gradio websocket queue. ' +
+ 'Increase to serve more requests but keep an eye on RAM usage.')
+ args, unknown = parser.parse_known_args()
+ if len(unknown) > 0:
+ print("Unknown args: ", unknown)
+ return args
+
+
+def construct_demo(data: Dict[str, Any]):
+ """ Build Gradio UI and populate with chat data from JSONs.
+
+ Args:
+ data (Dict[str, Any]): Parsed multi-JSON dataset with chats.
+
+ Returns:
+ None
+ """
+ assistant_roles = data['assistant_roles']
+ user_roles = data['user_roles']
+ assistant_role = random.choice(assistant_roles) \
+ if len(assistant_roles) > 0 else ""
+ user_role = random.choice(user_roles) if len(user_roles) > 0 else ""
+ with gr.Row().style():
+ with gr.Column(scale=2.5):
+ assistant_dd = gr.Dropdown(assistant_roles, label="ASSISTANT",
+ value=assistant_role, interactive=True)
+ with gr.Column(scale=2.5):
+ user_dd = gr.Dropdown(user_roles, label="USER", value=user_role,
+ interactive=True)
+ with gr.Column(scale=3):
+ gr.Markdown("## CAMEL: Communicative Agents for Mind Extraction"
+ " from Large Scale Language Model Society\n"
+ "Github repo: [https://github.com/lightaime/camel]"
+ "(https://github.com/lightaime/camel)")
+ task_dd = gr.Dropdown([], label="Original task", value="",
+ interactive=True)
+ specified_task_ta = gr.TextArea(label="Specified task", lines=2)
+ chatbot = gr.Chatbot()
+
+ def roles_dd_change(assistant_role: str, user_role: str) -> Dict:
+ """ Update the displayed chat upon inputs change.
+
+ Args:
+ assistant_role (str): Assistant dropdown value.
+ user_role (str): User dropdown value.
+
+ Returns:
+ Dict: New original roles state dictionary.
+ """
+ matrix = data['matrix']
+ if (assistant_role, user_role) in matrix:
+ record: Dict[str, Dict] = matrix[(assistant_role, user_role)]
+ original_task_options = list(record.keys())
+ original_task = original_task_options[0]
+ else:
+ original_task = "N/A"
+ original_task_options = []
+
+ choices = gr.Dropdown.update(choices=original_task_options,
+ value=original_task, interactive=True)
+ return choices
+
+ def build_chat_history(messages: Dict[int, Dict]) -> List[Tuple]:
+ """ Structures chatbot contents from the loaded data.
+
+ Args:
+ messages (Dict[int, Dict]): Messages loaded from JSON.
+
+ Returns:
+ List[Tuple]: Chat history in chatbot UI element format.
+ """
+ history = []
+ curr_qa = (None, None)
+ for k in sorted(messages.keys()):
+ msg = messages[k]
+ content = msg['content']
+ if msg['role_type'] == "USER":
+ if curr_qa[0] is not None:
+ history.append(curr_qa)
+ curr_qa = (content, None)
+ else:
+ curr_qa = (content, None)
+ elif msg['role_type'] == "ASSISTANT":
+ curr_qa = (curr_qa[0], content)
+ history.append(curr_qa)
+ curr_qa = (None, None)
+ else:
+ pass
+ return history
+
+ def task_dd_change(assistant_role: str, user_role: str,
+ original_task: str) -> Tuple[str, List]:
+ """ Load task details and chatbot history into UI elements.
+
+ Args:
+ assistant_role (str): An assistan role.
+ user_role (str): An user role.
+ original_task (str): The original task.
+
+ Returns:
+ Tuple[str, List]: New contents of the specified task
+ and chatbot history UI elements.
+ """
+
+ matrix = data['matrix']
+ if (assistant_role, user_role) in matrix:
+ task_dict: Dict[str, Dict] = matrix[(assistant_role, user_role)]
+ if original_task in task_dict:
+ chat = task_dict[original_task]
+ specified_task = chat['specified_task']
+ history = build_chat_history(chat['messages'])
+ else:
+ specified_task = "N/A"
+ history = []
+ else:
+ specified_task = "N/A"
+ history = []
+ return specified_task, history
+
+ func_args = (roles_dd_change, [assistant_dd, user_dd], task_dd)
+ assistant_dd.change(*func_args)
+ user_dd.change(*func_args)
+
+ task_dd.change(task_dd_change, [assistant_dd, user_dd, task_dd],
+ [specified_task_ta, chatbot])
+
+ task_dd_update_dict = roles_dd_change(assistant_dd.value, user_dd.value)
+ task_dd.choices = task_dd_update_dict['choices']
+ task_dd.value = task_dd_update_dict['value']
+
+ specified_task, chatbot_history = task_dd_change(assistant_dd.value,
+ user_dd.value,
+ task_dd.value)
+ specified_task_ta.value = specified_task
+ chatbot.value = chatbot_history
+
+
+if __name__ == "__main__":
+ """ Entry point. """
+
+ args = parse_arguments()
+
+ print("Loading the dataset...")
+ data = load_data(args.data_path)
+ print("Dataset is loaded")
+
+ print("Getting Data Explorer web server online...")
+
+ with gr.Blocks() as demo:
+ construct_demo(data)
+
+ demo.queue(args.concurrency_count)
+ demo.launch(share=args.share, inbrowser=args.inbrowser,
+ server_name="0.0.0.0", server_port=args.server_port)
+
+ print("Exiting.")
diff --git a/data_explorer/loader.py b/data_explorer/loader.py
new file mode 100644
index 000000000..e540dd0c0
--- /dev/null
+++ b/data_explorer/loader.py
@@ -0,0 +1,132 @@
+"""
+Everything related to parsing the data JSONs into UI-compatible format.
+"""
+
+import glob
+import json
+import os
+import re
+from typing import Any, Union, Dict, List, Tuple
+
+from tqdm import tqdm
+
+ChatHistory = Dict[str, Any]
+ParsedChatHistory = Dict[str, Any]
+AllChats = Dict[str, Any]
+
+
+def parse(raw_chat: ChatHistory) -> Union[ParsedChatHistory, None]:
+ """ Gets the JSON raw chat data, validates it and transforms
+ into an easy to work with form.
+
+ Args:
+ raw_chat (ChatHistory): In-memory loaded JSON data file.
+
+ Returns:
+ Union[ParsedChatHistory, None]: Parsed chat data or None
+ if there were parsing errors.
+ """
+
+ if "role_1" not in raw_chat:
+ return None
+
+ role_1 = raw_chat["role_1"]
+ if "_RoleType.ASSISTANT" not in role_1:
+ return None
+ assistant_role = role_1.split("_RoleType.ASSISTANT")
+ if len(assistant_role) < 1:
+ return None
+ if len(assistant_role[0]) <= 0:
+ return None
+ assistant_role = assistant_role[0]
+
+ role_2 = raw_chat["role_2"]
+ if "_RoleType.USER" not in role_2:
+ return None
+ user_role = role_2.split("_RoleType.USER")
+ if len(user_role) < 1:
+ return None
+ if len(user_role[0]) <= 0:
+ return None
+ user_role = user_role[0]
+
+ original_task = raw_chat["original_task"]
+ if len(original_task) <= 0:
+ return None
+
+ specified_task = raw_chat["specified_task"]
+ if len(specified_task) <= 0:
+ return None
+
+ messages = dict()
+ for key in raw_chat:
+ match = re.search("message_(?P<number>[0-9]+)", key)
+ if match:
+ number = int(match.group("number"))
+ messages[number] = raw_chat[key]
+
+ return dict(
+ assistant_role=assistant_role,
+ user_role=user_role,
+ original_task=original_task,
+ specified_task=specified_task,
+ messages=messages,
+ )
+
+
+def load_data(path: str) -> AllChats:
+ """ Load all JSONs from a folder and parse them.
+
+ Args:
+ path (str): path to the folder with JSONs.
+
+ Returns:
+ AllChats: A dictionary with all possible assistant and
+ user roles and the matrix of chats.
+ """
+
+ filt = os.path.join(path, "*.json")
+ files = glob.glob(filt)
+ parsed_list = []
+ for file_name in tqdm(files):
+ with open(file_name, "rb") as f:
+ try:
+ raw_chat = json.load(f)
+ except Exception as ex:
+ print(str(ex))
+ continue
+ parsed = parse(raw_chat)
+ if parsed is None:
+ continue
+ parsed_list.append(parsed)
+
+ assistant_roles = set()
+ user_roles = set()
+ for parsed in parsed_list:
+ assistant_roles.add(parsed['assistant_role'])
+ user_roles.add(parsed['user_role'])
+ assistant_roles = list(sorted(assistant_roles))
+ user_roles = list(sorted(user_roles))
+ matrix: Dict[Tuple[str, str], List[Dict]] = dict()
+ for parsed in parsed_list:
+ key = (parsed['assistant_role'], parsed['user_role'])
+ original_task = parsed['original_task']
+ new_item = {
+ k: v
+ for k, v in parsed.items()
+ if k not in {'assistant_role', 'user_role', 'original_task'}
+ }
+ if key in matrix:
+ matrix[key][original_task] = new_item
+ else:
+ matrix[key] = {original_task: new_item}
+
+ return dict(
+ assistant_roles=assistant_roles,
+ user_roles=user_roles,
+ matrix=matrix,
+ )
+
+
+if __name__ == "__main__":
+ data = load_data("DATA/")