|
| 1 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 2 | +# Licensed under the Apache License, Version 2.0 (the “License”); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an “AS IS” BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. =========== |
| 14 | +import asyncio |
| 15 | +import os |
| 16 | +import random |
| 17 | + |
| 18 | +from camel.models import ModelFactory |
| 19 | +from camel.types import ModelPlatformType, ModelType |
| 20 | + |
| 21 | +import oasis |
| 22 | +from oasis import ActionType, EnvAction, SingleAction |
| 23 | + |
| 24 | + |
| 25 | +async def main(): |
| 26 | + # NOTE: You need to deploy the vllm server first |
| 27 | + vllm_model_1 = ModelFactory.create( |
| 28 | + model_platform=ModelPlatformType.VLLM, |
| 29 | + model_type="qwen-2", |
| 30 | + url="http://10.109.28.7:8080/v1", |
| 31 | + ) |
| 32 | + vllm_model_2 = ModelFactory.create( |
| 33 | + model_platform=ModelPlatformType.VLLM, |
| 34 | + model_type="qwen-2", |
| 35 | + url="http://10.109.27.103:8080/v1", |
| 36 | + ) |
| 37 | + # Define the models for agents. Agents will select models based on |
| 38 | + # pre-defined scheduling strategies |
| 39 | + models = [vllm_model_1, vllm_model_2] |
| 40 | + |
| 41 | + # Define the available actions for the agents |
| 42 | + available_actions = [ |
| 43 | + ActionType.CREATE_POST, |
| 44 | + ActionType.LIKE_POST, |
| 45 | + ActionType.REPOST, |
| 46 | + ActionType.FOLLOW, |
| 47 | + ActionType.DO_NOTHING, |
| 48 | + ActionType.QUOTE_POST, |
| 49 | + ] |
| 50 | + |
| 51 | + # Define the path to the database |
| 52 | + db_path = "./data/twitter_simulation.db" |
| 53 | + |
| 54 | + # Delete the old database |
| 55 | + if os.path.exists(db_path): |
| 56 | + os.remove(db_path) |
| 57 | + |
| 58 | + # Make the environment |
| 59 | + env = oasis.make( |
| 60 | + platform=oasis.DefaultPlatformType.TWITTER, |
| 61 | + database_path=db_path, |
| 62 | + agent_profile_path=("tmp/random_network.csv"), |
| 63 | + agent_models=models, |
| 64 | + available_actions=available_actions, |
| 65 | + ) |
| 66 | + |
| 67 | + # Run the environment |
| 68 | + await env.reset() |
| 69 | + |
| 70 | + # inject truth and misinformation across different topics |
| 71 | + business_action_truth = SingleAction( |
| 72 | + agent_id=0, |
| 73 | + action=ActionType.CREATE_POST, |
| 74 | + args={ |
| 75 | + "content": |
| 76 | + "Amazon is expanding its delivery drone program to deliver packages within 30 minutes in select cities. This initiative aims to improve efficiency and reduce delivery times." |
| 77 | + }) |
| 78 | + business_action_misinfo = SingleAction( |
| 79 | + agent_id=0, |
| 80 | + action=ActionType.CREATE_POST, |
| 81 | + args={ |
| 82 | + "content": |
| 83 | + "Amazon plans to completely eliminate its delivery drivers within two years due to the new drone program. #Automation #Future" |
| 84 | + }) |
| 85 | + education_action_truth = SingleAction( |
| 86 | + agent_id=0, |
| 87 | + action=ActionType.CREATE_POST, |
| 88 | + args={ |
| 89 | + "content": |
| 90 | + "Harvard University has announced a new scholarship program that will cover full tuition for all undergraduate students from families earning less than $75,000 per year." |
| 91 | + }) |
| 92 | + education_action_misinfo = SingleAction( |
| 93 | + agent_id=0, |
| 94 | + action=ActionType.CREATE_POST, |
| 95 | + args={ |
| 96 | + "content": |
| 97 | + "Harvard is raising tuition fees for all students despite the new scholarship program, making it harder for families to afford education. #EducationCrisis" |
| 98 | + }) |
| 99 | + entertainment_action_truth = SingleAction( |
| 100 | + agent_id=0, |
| 101 | + action=ActionType.CREATE_POST, |
| 102 | + args={ |
| 103 | + "content": |
| 104 | + "The latest Marvel movie, Avengers: Forever, has officially broken box office records, earning over $1 billion in its opening weekend." |
| 105 | + }) |
| 106 | + entertainment_action_misinfo = SingleAction( |
| 107 | + agent_id=0, |
| 108 | + action=ActionType.CREATE_POST, |
| 109 | + args={ |
| 110 | + "content": |
| 111 | + "Marvel is planning to retire the Avengers franchise after this film, saying it will not produce any more superhero movies. #EndOfAnEra" |
| 112 | + }) |
| 113 | + health_action_truth = SingleAction( |
| 114 | + agent_id=0, |
| 115 | + action=ActionType.CREATE_POST, |
| 116 | + args={ |
| 117 | + "content": |
| 118 | + "A recent study shows that regular exercise can significantly reduce the risk of chronic diseases such as diabetes and heart disease." |
| 119 | + }) |
| 120 | + health_action_misinfo = SingleAction( |
| 121 | + agent_id=0, |
| 122 | + action=ActionType.CREATE_POST, |
| 123 | + args={ |
| 124 | + "content": |
| 125 | + "Health experts claim that exercise will be deemed unnecessary in five years as new treatments will eliminate chronic diseases entirely. #HealthRevolution" |
| 126 | + }) |
| 127 | + |
| 128 | + init_env_action = EnvAction( |
| 129 | + activate_agents=[0], |
| 130 | + intervention=[ |
| 131 | + business_action_truth, business_action_misinfo, |
| 132 | + education_action_truth, education_action_misinfo, |
| 133 | + entertainment_action_truth, entertainment_action_misinfo, |
| 134 | + health_action_truth, health_action_misinfo |
| 135 | + ]) |
| 136 | + |
| 137 | + env_simulation_actions = [init_env_action] |
| 138 | + for timestep in range(3): |
| 139 | + # Randomly select 1% of agents to activate. This is the active probability in the paper. |
| 140 | + total_agents = env.agent_graph.get_num_nodes() |
| 141 | + num_agents_to_activate = max(1, int( |
| 142 | + total_agents * 0.01)) # Ensure at least 1 agent is activated |
| 143 | + agents_to_activate = random.sample(range(total_agents), |
| 144 | + num_agents_to_activate) |
| 145 | + |
| 146 | + # Create an environment action with the randomly selected agents |
| 147 | + random_action = EnvAction(activate_agents=agents_to_activate) |
| 148 | + env_simulation_actions.append(random_action) |
| 149 | + |
| 150 | + # Simulate 3 timesteps |
| 151 | + for i in range(3): |
| 152 | + env_actions = env_simulation_actions[i] |
| 153 | + # Perform the actions |
| 154 | + await env.step(env_actions) |
| 155 | + |
| 156 | + # Close the environment |
| 157 | + await env.close() |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + asyncio.run(main()) |
0 commit comments