### Setup Development Environment Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands to install development tools and pre-commit hooks. ```bash mypy --install-types --non-interactive --exclude haicosystem/tools --exclude haicosystem/grounding_engine/llm_engine_legacy.py haicosystem pip install pre-commit pre-commit install ``` -------------------------------- ### Install HAICOSYSTEM Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands to create a virtual environment and install dependencies using Poetry. ```bash conda create -n haicosystem python=3.11; conda activate haicosystem; curl -sSL https://install.python-poetry.org | python3 poetry install ``` -------------------------------- ### Executing the Scatter Plot Function Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Example call to the plotting function with specific configuration parameters. ```python plot_safety_goal_scatter( episodes_list, save_path="/Users/xuhuizhou/Projects/papers/ICLR2025-HAICosystem/figures/safety_goal_scatter_llama.pdf", if_show_codename=False, consider_difficulty=False, show_intent=False, do_regression=False, do_zoom_seperate=True, ) ``` -------------------------------- ### Execute bar graph generation Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Example call to the generate_bar_graph function with a specified output path. ```python generate_bar_graph( ratio_data=ratio_data, save_path="/Users/xuhuizhou/Projects/papers/ICLR2025-HAICosystem/figures/risk_ratio_graph.pdf", ) ``` -------------------------------- ### Run Simulation without Interaction (Experiment 3) Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/experiment_log.md Executes simulations for scenarios without interaction, utilizing jailbreak filters and optional starting speech. ```bash python examples/experiment.py --models="gpt-4-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_wo_interaction_2" --iteration-num=5 --max-turn-num=2 --scenario-filter="jailbreak" --use-starting-speech --push-to-db ``` ```bash python examples/experiment.py --models="gpt-3.5-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_wo_interaction_2" --iteration-num=5 --max-turn-num=2 --scenario-filter="jailbreak" --use-starting-speech --push-to-db ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_wo_interaction_2" --iteration-num=5 --max-turn-num=2 --scenario-filter="jailbreak" --use-starting-speech --push-to-db ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_wo_interaction_2" --iteration-num=5 --max-turn-num=2 --scenario-filter="jailbreak" --use-starting-speech --push-to-db ``` -------------------------------- ### Run Simulations Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands to execute simulations and view help documentation. ```bash python examples/run.py --codename="TeladocRequestPrescription_0" ``` ```bash python examples/run.py --help ``` -------------------------------- ### Run Trial Simulation (Experiment 1) Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/experiment_log.md Executes trial simulations for Experiment 1 using various models with a batch size of 4 and 1 iteration. ```bash python examples/experiment.py --models="gpt-4-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=1 > experiment_output.log 2>&1 ``` ```bash python examples/experiment.py --models="gpt-3.5-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=1 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=1 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=1 ``` -------------------------------- ### Generate scenarios using the CLI tool Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/scenario_guideline.md Use the generation script to create initial scenario files based on an inspiration seed and domain. ```bash python examples/generate_scenarios.py --inspiration=".txt" --output-file=".json" --domain="" ``` -------------------------------- ### Run Trial Simulation (Experiment 2) Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/experiment_log.md Executes trial simulations for Experiment 2 with increased iteration count of 5. ```bash python examples/experiment.py --models="gpt-4-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=5 > experiment_output.log 2>&1 ``` ```bash python examples/experiment.py --models="gpt-3.5-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=5 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=5 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --push-to-db --iteration-num=5 ``` -------------------------------- ### Run Scaled Simulations Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands to run scaled experiments. ```bash python examples/experiment.py --help ``` ```bash python examples/experiment.py --models="gpt-4-turbo" --partner-model="gpt-4o" --evaluator-model="gpt-4o" --batch-size=5 --task="haicosystem" --push-to-db ``` -------------------------------- ### Manage Profiles Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands to upload profiles to the database. ```bash python examples/create_env_agent_combo.py --help ``` ```bash python examples/create_env_agent_combo.py --agent-folder="./assets/ai_agent_profiles" --env-folders="./assets/education,./assets/healthcare,./assets/personal_services,./assets/miscellaneous,./assets/technology_and_science,./assets/business_and_finance,./assets/politics_and_law" --clean-combos ``` -------------------------------- ### Run Simulation Experiments Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/experiment_log.md Executes simulation experiments using the experiment.py script with specified model, partner, and evaluator configurations. ```bash python examples/experiment.py --models="gpt-4-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --iteration-num=5 --push-to-db ``` ```bash python examples/experiment.py --models="gpt-3.5-turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=20 --task="haicosystem_trial2" --iteration-num=5 --push-to-db ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=10 --task="haicosystem_trial2" --push-to-db --iteration-num=5 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=10 --task="haicosystem_trial2" --push-to-db --iteration-num=5 ``` ```bash python examples/experiment.py --models="together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo" --partner-model="gpt-4o-2024-08-06" --evaluator-model="gpt-4o-2024-08-06" --batch-size=4 --task="haicosystem_trial2" --iteration-num=5 --push-to-db ``` -------------------------------- ### Define Models and Metadata Mapping Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/leaderboard.ipynb Initializes the list of available models, their corresponding benchmark tags, and a dictionary mapping model IDs to display names, providers, and open-source status. ```python models = [ "gpt-4-turbo", "gpt-3.5-turbo", "together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo", "together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", "together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", "together_ai/mistralai/Mixtral-8x22B-Instruct-v0.1", "together_ai/Qwen/Qwen1.5-72B-Chat", "together_ai/Qwen/Qwen2-72B-Instruct", "together_ai/Qwen/Qwen1.5-110B-Chat", "together_ai/meta-llama/Meta-Llama-3-70B-Instruct-Turbo", "together_ai/meta-llama/Meta-Llama-3-8B-Instruct-Turbo", "together_ai/deepseek-ai/deepseek-llm-67b-chat", ] tags = [ "benchmark_gpt-4-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_gpt-3.5-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/mistralai/Mixtral-8x22B-Instruct-v0.1_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen1.5-72B-Chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen2-72B-Instruct_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen1.5-110B-Chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3-8B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/deepseek-ai/deepseek-llm-67b-chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", ] models_mapping = { "gpt-4-turbo": ["GPT-4-turbo", "OpenAI", "No"], "gpt-3.5-turbo": ["GPT-3.5-turbo", "OpenAI", "No"], "together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": [ "Llama3.1-405B", "Meta", "Yes", ], "together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo": [ "Llama3.1-70B", "Meta", "Yes", ], "together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": [ "Llama3.1-8B", "Meta", "Yes", ], "together_ai/mistralai/Mixtral-8x22B-Instruct-v0.1": [ "Mixtral-8x22B", "MistralAI", "Yes", ], "together_ai/Qwen/Qwen1.5-72B-Chat": ["Qwen1.5-72B-Chat", "Alibaba", "Yes"], "together_ai/Qwen/Qwen2-72B-Instruct": ["Qwen2-72B-Instruct", "Alibaba", "Yes"], "together_ai/Qwen/Qwen1.5-110B-Chat": ["Qwen1.5-110B-Chat", "Alibaba", "Yes"], "together_ai/meta-llama/Meta-Llama-3-70B-Instruct-Turbo": [ "Llama3-70B", "Meta", "Yes", ], "together_ai/meta-llama/Meta-Llama-3-8B-Instruct-Turbo": [ "Llama3-8B", "Meta", "Yes", ], "together_ai/deepseek-ai/deepseek-llm-67b-chat": [ "DeepSeek-67B", "DeepSeek AI", "Yes", ], } ``` -------------------------------- ### Define Initial Models and Tags Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Initial lists of model identifiers and their associated benchmark tags for trial tracking. ```python models = [ "gpt-4-turbo", "gpt-3.5-turbo", "together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo", "together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo", ] tags = [ "benchmark_gpt-4-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_gpt-3.5-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", ] ``` -------------------------------- ### Run Development Checks Source: https://github.com/xuhuizhou/haicosystem/blob/main/README.md Commands for running tests and static analysis. ```bash pytest ``` ```bash mypy --strict --exclude haicosystem/tools --exclude haicosystem/grounding_engine/llm_engine_legacy.py . ``` ```bash pre-commit run --all-files ``` -------------------------------- ### Define Model Configuration and Mapping Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/leaderboard.ipynb Sets up lists of model identifiers and associated tags, along with a mapping dictionary for display names and metadata. ```python models = [ "gpt-4-turbo", "gpt-3.5-turbo", "together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo", "together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", ] tags = [ "benchmark_gpt-4-turbo_gemini/gemini-2.5-flash-preview-05-20_gpt-4o-2024-08-06_haicosystem_gemini_partner", "benchmark_gpt-3.5-turbo_gemini/gemini-2.5-flash-preview-05-20_gpt-4o-2024-08-06_haicosystem_gemini_partner", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gemini/gemini-2.5-flash-preview-05-20_gpt-4o-2024-08-06_haicosystem_gemini_partner", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo_gemini/gemini-2.5-flash-preview-05-20_gpt-4o-2024-08-06_haicosystem_gemini_partner", ] models_mapping = { "gpt-4-turbo": ["GPT-4-turbo", "OpenAI", "No"], "gpt-3.5-turbo": ["GPT-3.5-turbo", "OpenAI", "No"], "together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo": [ "Llama3.1-405B", "Meta", "Yes", ], "together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": [ "Llama3.1-8B", "Meta", "Yes", ], } ``` ```python models = [ "o1-2024-12-17", "together_ai/deepseek-ai/DeepSeek-R1", ] tags = [ "benchmark_o1-2024-12-17_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/deepseek-ai/DeepSeek-R1_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", ] models_mapping = { "o1-2024-12-17": ["O1", "OpenAI", "No"], "together_ai/deepseek-ai/DeepSeek-R1": ["R1", "DeepSeek AI", "Yes"], } ``` -------------------------------- ### Calculate performance and ratio statistics by realism level Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Iterates through model episodes to compute average rewards and binary average rewards, grouped by environment realism level. ```python # get performance and ratio data from sotopia.database import EpisodeLog from haicosystem.protocols import HaiEnvironmentProfile from haicosystem.utils import get_avg_reward performance_data = {} ratio_data = {} model_rewards = {} all_episodes = [] realism_level = [] for model, tag in zip(models, tags): episodes = EpisodeLog.find(EpisodeLog.tag == tag).all() print(f"model: {model}, tag: {tag}, episodes: {len(episodes)}") for episode in episodes: env = HaiEnvironmentProfile.get(episode.environment) realism_level.append(env.realism if "https" not in env.realism else "level 3") # group episodes by realism level grouped_episodes = {} for realism in set(realism_level): grouped_episodes[realism] = [ episode for realism_level, episode in zip(realism_level, episodes) if realism == realism_level ] import pandas as pd df = pd.DataFrame( columns=["Realism Level", "Model", "Average Reward", "Binary Average Reward"] ) for realism in set(realism_level): print(f"realism: {realism}, episodes: {len(grouped_episodes[realism])}") avg_reward = get_avg_reward(grouped_episodes[realism], model) # type: ignore binary_avg_reward = get_avg_reward( grouped_episodes[realism], model, binary=True ) # type: ignore df = pd.concat( [ df, pd.DataFrame( [ { "Realism Level": realism, "Model": model, "Average Reward": avg_reward, "Binary Average Reward": binary_avg_reward, } ] ), ], ignore_index=True, ) performance_data[model] = df[["Realism Level", "Model", "Average Reward"]] ratio_data[model] = df[["Realism Level", "Model", "Binary Average Reward"]] ``` -------------------------------- ### Generate Scenario Domain Pie Chart Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Extracts domain data from environment profiles and renders a pie chart using seaborn's pastel palette. ```python # draw a pie chart for the distribution of the scenarios domain of the scenarios; use seaborn from haicosystem.protocols import HaiEnvironmentProfile enviroment_profiles = HaiEnvironmentProfile.find().all() domains = [] intent = [] realism_level = [] for env in enviroment_profiles: assert isinstance(env, HaiEnvironmentProfile) domains.append(env.domain) intent.append(env.agent_intent_labels[0]) realism_level.append(env.realism) # draw a pie chart for the distribution of the scenarios domain of the scenarios import matplotlib.pyplot as plt import seaborn as sns # Count the occurrences of each domain domain_counts = {domain: domains.count(domain) for domain in set(domains)} # Create a pie chart plt.figure(figsize=(8, 6)) sns.set(style="whitegrid") domain_counts_series = pd.Series(domain_counts) colors = sns.color_palette("pastel6") domain_counts_series.plot( kind="pie", autopct="%1.1f%%", startangle=140, textprops={"size": "larger"}, colors=colors, ) plt.title("Distribution of Scenario Domains") plt.ylabel("") # Hide the y-label # Save the pie chart to a file plt.savefig( "/Users/xuhuizhou/Projects/papers/ICLR2025-HAICosystem/figures/scenario_domain_pie_chart.pdf", format="pdf", bbox_inches="tight", ) plt.show() ``` -------------------------------- ### Extract performance and ratio data from EpisodeLog Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Retrieves episode logs based on tags and calculates average rewards using the haicosystem utility functions. ```python # get performance and ratio data from sotopia.database import EpisodeLog from haicosystem.protocols import HaiEnvironmentProfile from haicosystem.utils import get_avg_reward performance_data = {} ratio_data = {} model_rewards = {} all_episodes = [] for model, tag in zip(models, tags): episodes = EpisodeLog.find(EpisodeLog.tag == tag).all() print(f"model: {model}, tag: {tag}, episodes: {len(episodes)}") avg_rewards = get_avg_reward(episodes, model) # type: ignore binary_avg_rewards = get_avg_reward(episodes, model, binary=True) # type: ignore performance_data[model] = avg_rewards ratio_data[model] = binary_avg_rewards all_episodes.extend(episodes) import copy expanded_performance_data = copy.deepcopy(performance_data) expanded_ratio_data = copy.deepcopy(ratio_data) for model, tag in zip(expanded_models, expanded_tags): episodes = EpisodeLog.find(EpisodeLog.tag == tag).all() print(f"model: {model}, tag: {tag}, episodes: {len(episodes)}") avg_rewards = get_avg_reward(episodes, model) # type: ignore binary_avg_rewards = get_avg_reward(episodes, model, binary=True) # type: ignore expanded_performance_data[model] = avg_rewards expanded_ratio_data[model] = binary_avg_rewards ``` -------------------------------- ### Calculate performance and ratio data for model interactions Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Filters episode logs by environment profiles and calculates average rewards and binary success ratios for specified models. ```python # get performance and ratio data from sotopia.database import EpisodeLog from haicosystem.protocols import HaiEnvironmentProfile from haicosystem.utils import get_avg_reward filter_performance_data = {} filter_ratio_data = {} filter_all_episodes = [] filter_key_words = ["jailbreak_dan", "jailbreak_persuasive", "jailbreak_wildteaming"] featured_models = [models[0], models[1], models[2], models[3]] featured_tags = [ ( tags[0], "benchmark_gpt-4-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_wo_interaction_2", ), ( tags[1], "benchmark_gpt-3.5-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_wo_interaction_2", ), ( tags[2], "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_wo_interaction_2", ), ( tags[3], "benchmark_together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_wo_interaction_2", ), ] # get filtered env list filtered_dict = {filter_key_word: [] for filter_key_word in filter_key_words} enviroment_profiles = HaiEnvironmentProfile.find().all() for env in enviroment_profiles: assert isinstance(env, HaiEnvironmentProfile) env_name = env.codename for filter_key_word in filter_key_words: if filter_key_word in env_name: filtered_dict[filter_key_word].append(env.pk) for model, featured_tag in zip(featured_models, featured_tags): episodes = EpisodeLog.find(EpisodeLog.tag == featured_tag[0]).all() episodes_wo_interaction = EpisodeLog.find(EpisodeLog.tag == featured_tag[1]).all() print(f"fetching {len(episodes)} episodes for {model}") print( f"fetching {len(episodes_wo_interaction)} episodes without interaction for {model}" ) for filter_key_word in filter_key_words: filtered_episodes = [ episode for episode in episodes if episode.environment in filtered_dict[filter_key_word] ] filtered_episodes_wo_interaction = [ episode for episode in episodes_wo_interaction if episode.environment in filtered_dict[filter_key_word] ] avg_rewards = get_avg_reward(filtered_episodes, model) # type: ignore binary_avg_rewards = get_avg_reward(filtered_episodes, model, binary=True) # type: ignore avg_rewards_wo_interaction = get_avg_reward( filtered_episodes_wo_interaction, model ) # type: ignore binary_avg_rewards_wo_interaction = get_avg_reward( filtered_episodes_wo_interaction, model, binary=True ) # type: ignore filter_performance_data[(model, filter_key_word)] = ( avg_rewards, avg_rewards_wo_interaction, ) filter_ratio_data[(model, filter_key_word)] = ( binary_avg_rewards, binary_avg_rewards_wo_interaction, ) ``` -------------------------------- ### Run AI agent simulation Source: https://github.com/xuhuizhou/haicosystem/blob/main/docs/scenario_guideline.md Execute a scenario simulation using the provided Python script and a specific JSON scenario file path. ```bash python examples/run.py --filename= ``` -------------------------------- ### Retrieve episode logs by tag Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Fetches episode logs for specific tags from the database. ```python episodes_list = [ EpisodeLog.find(EpisodeLog.tag == tags[2]).all(), EpisodeLog.find(EpisodeLog.tag == tags[3]).all(), ] ``` -------------------------------- ### Generate Markdown Table for Risk Ratios Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/leaderboard.ipynb Constructs a markdown table string from ratio_data using a helper function to format scores. ```python # Produce the markdown table given the ratio data table_header = "| Model Name | Publisher | Open? | Overall Risk Ratio | Targeted Safety Risks | System and Operational Risks | Content Safety Risks | Societal Risks | Legal and Rights Related Risks |\n" table_divider = "| --- | --- | --- | --- | --- | --- | --- | --- | --- |\n" dimensions = { "targeted_safety_risks": "Targeted Safety Risks & [-10, 0]", "system_and_operational_risks": "System and Operational Risks & [-10, 0]", "content_safety_risks": "Content Safety Risks & [-10, 0]", "societal_risks": "Societal Risks & [-10, 0]", "legal_and_rights_related_risks": "Legal and Rights-Related Risks & [-10, 0]", "efficiency": "Efficiency & [0, 10]", "goal": "Goal Completion & [0, 10]", "overall_score": "Overall Score & [0, 10]", } def process_scores(scores): # return f"{scores[0]} ± {scores[1]}" return f"{scores[0]:.2f}" table_rows = "" for model in models: model_name = models_mapping[model][0] publisher = models_mapping[model][1] open_status = models_mapping[model][2] overall_score = process_scores(ratio_data[model].get("overall_score", "N/A")) targeted_safety_risks = process_scores( ratio_data[model].get("targeted_safety_risks", "N/A") ) system_operational_risks = process_scores( ratio_data[model].get("system_and_operational_risks", "N/A") ) content_safety_risks = process_scores( ratio_data[model].get("content_safety_risks", "N/A") ) societal_risks = process_scores(ratio_data[model].get("societal_risks", "N/A")) legal_rights_risks = process_scores( ratio_data[model].get("legal_and_rights_related_risks", "N/A") ) # efficiency = process_scores(ratio_data[model].get('efficiency', 'N/A')) # goal_completion = process_scores(ratio_data[model].get('goal', 'N/A')) table_rows += f"| {model_name} | {publisher} | {open_status} | {overall_score} | {targeted_safety_risks} | {system_operational_risks} | {content_safety_risks} | {societal_risks} | {legal_rights_risks} |\n" markdown_table = table_header + table_divider + table_rows # Display the markdown table markdown_table # Model Name | Publisher | Open? | Targeted Safety Risks | System and Operational Risks | Content Safety Risks | Societal Risks | Legal and Rights Related Risks | Efficiency | Goal Completion ``` -------------------------------- ### Serialize Database Records to JSONL Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/database.ipynb Export episodes, environment profiles, and agent combinations to JSONL files for data storage or analysis. ```python # export all the episodes import json tags = [ "benchmark_gpt-4-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_gpt-3.5-turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/mistralai/Mixtral-8x22B-Instruct-v0.1_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen1.5-72B-Chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen2-72B-Instruct_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/Qwen/Qwen1.5-110B-Chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3-8B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/deepseek-ai/deepseek-llm-67b-chat_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", ] all_episodes = [] for tag in tags: episodes = EpisodeLog.find(EpisodeLog.tag == tag).all() all_episodes.extend(episodes) with open("all_episodes.jsonl", "w") as f: for episode in all_episodes: f.write(episode.json() + "\n") ``` ```python # serialize the environment from haicosystem.protocols import HaiEnvironmentProfile with open("all_environments.jsonl", "w") as f: for environment in HaiEnvironmentProfile.all(): f.write(environment.json() + "\n") ``` ```python # serialize the combos from sotopia.database import EnvAgentComboStorage with open("all_combos.jsonl", "w") as f: for combo in EnvAgentComboStorage.all(): f.write(combo.json() + "\n") ``` -------------------------------- ### Generate and Copy Performance Table to Clipboard Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/leaderboard.ipynb Calculates overall scores from performance data, generates a markdown table, and copies the result to the system clipboard using subprocess. ```python # Risk Scors table_header = "| Model Name | Publisher | Open? | Overall Score | Targeted Safety Risks | System and Operational Risks | Content Safety Risks | Societal Risks | Legal and Rights Related Risks | Goal Completion | Efficiency |\n" table_divider = "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n" table_rows = "" for model in models: model_name = models_mapping[model][0] publisher = models_mapping[model][1] open_status = models_mapping[model][2] targeted_safety_risks = process_scores( performance_data[model].get("targeted_safety_risks", "N/A") ) system_operational_risks = process_scores( performance_data[model].get("system_and_operational_risks", "N/A") ) content_safety_risks = process_scores( performance_data[model].get("content_safety_risks", "N/A") ) societal_risks = process_scores( performance_data[model].get("societal_risks", "N/A") ) legal_rights_risks = process_scores( performance_data[model].get("legal_and_rights_related_risks", "N/A") ) goal_completion = process_scores(performance_data[model].get("goal", "N/A")) efficiency = process_scores(performance_data[model].get("efficiency", "N/A")) overall_score = ( sum( [ float(targeted_safety_risks), float(system_operational_risks), float(content_safety_risks), float(societal_risks), float(legal_rights_risks), float(goal_completion), float(efficiency), ] ) / 7 ) overall_score = process_scores((overall_score, 0)) table_rows += f"| {model_name} | {publisher} | {open_status} | {overall_score} | {targeted_safety_risks} | {system_operational_risks} | {content_safety_risks} | {societal_risks} | {legal_rights_risks} | {goal_completion} | {efficiency} |\n" markdown_table = table_header + table_divider + table_rows import subprocess data = markdown_table subprocess.run("pbcopy", text=True, input=data) data ``` -------------------------------- ### Generate LaTeX table for model performance Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Constructs a LaTeX table string from performance data, supporting both wrapped and standard table environments. ```python def generate_latex_table(performance, models, wrap=True): # Define the table dimensions and labels dimensions = { "targeted_safety_risks": "Targeted Safety Risks & [-10, 0]", "system_and_operational_risks": "System and Operational Risks & [-10, 0]", "content_safety_risks": "Content Safety Risks & [-10, 0]", "societal_risks": "Societal Risks & [-10, 0]", "legal_and_rights_related_risks": "Legal and Rights-Related Risks & [-10, 0]", "efficiency": "Efficiency & [0, 10]", "goal": "Goal Completion & [0, 10]", "overall_score": "Overall Score & [0, 10]", } # Start building the LaTeX code if wrap: latex_code = """ \begin{wraptable}[13]{r}{8.7cm} \small \vspace{-10pt} \centering """ else: latex_code = """ \begin{table}[h] \small \centering """ latex_code += ( " \begin{tabularx}{8.7cm}{@{\hspace{10pt}}" + "r" * (len(dimensions) + 1) + "@{\hspace{6pt}}}\n" ) latex_code += " \toprule\n" latex_code += " Model " # Add dimension headers to the table for dim_label in dimensions.values(): latex_code += f"& {dim_label} " latex_code += "\\ \midrule\n" # Populate the table with data for model in models: latex_code += f" {models_mapping[model]} " for dim_key in dimensions.keys(): if model in performance and dim_key in performance[model]: value = performance[model][dim_key][0] latex_code += f"& {value:.2f} " else: latex_code += "& " latex_code += "\\ \n" # Close the table latex_code += """ \bottomrule \end{tabularx} \vspace{-5pt} \caption{The aggregated performance of each model by averaging across different episodes.} \label{tab:model_safety_evaluation} """ if wrap: latex_code += "\end{wraptable}\n" else: latex_code += "\end{table}\n" return latex_code ``` -------------------------------- ### Execute LaTeX table generation Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Calls the generation function with expanded ratio data and prints the resulting LaTeX code. ```python latex_code = generate_latex_table( performance=expanded_ratio_data, models=models + expanded_models, wrap=False, ) print(latex_code) ``` -------------------------------- ### Load Episodes from JSONL Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/database.ipynb Read serialized episode data from a JSONL file and reconstruct the EpisodeLog objects. ```python # load the episodes with open("all_episodes.jsonl", "r") as f: for line in f: print(line) episode_dict = json.loads(line) print(episode_dict["pk"]) episode = EpisodeLog(**episode_dict) print(episode) break ``` -------------------------------- ### Format performance data into markdown tables Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Processes performance data to calculate average risk scores and outputs formatted markdown tables. ```python import pandas as pd def format_performance_data(performance_data, ratio_data): formatted_data = {} for model, df in performance_data.items(): df["Average Risk Score"] = df["Average Reward"].apply( lambda x: f"{((float(x['targeted_safety_risks'][0]+ x['system_and_operational_risks'][0])+ float(x['content_safety_risks'][0]) + float(x['societal_risks'][0]) + float(x['legal_and_rights_related_risks'][0]))/4):.2f}" ) df = ( df.set_index("Realism Level") .loc[["level 3", "level 2", "level 1"]] .reset_index() ) df = df.drop(columns=["Model"]) df = df.drop(columns=["Average Reward"]) formatted_data[model] = df.to_markdown(index=False) return formatted_data formatted_performance_data = format_performance_data(performance_data_copy) for model, table in formatted_performance_data.items(): print(f"## {model}\n") print(table) print("\n") ``` -------------------------------- ### Execute model comparison Source: https://github.com/xuhuizhou/haicosystem/blob/main/examples/notebooks/figs_and_tables.ipynb Invokes the comparison function with specific model tags and prints the results using rich. ```python import rich comparison = compare_across_models( tags=[ "benchmark_together_ai/meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", "benchmark_together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo_gpt-4o-2024-08-06_gpt-4o-2024-08-06_haicosystem_trial2", ] ) rich.print(comparison) ```