-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathagents.py
More file actions
160 lines (119 loc) · 6.73 KB
/
agents.py
File metadata and controls
160 lines (119 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import autogen
import os
import pandas as pd
from utils import llama3, read_from_file, save_personality_to_file
# Dataset switcher
usa_election = True
qanon = False
brexit = False
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=0,
code_execution_config=False,
default_auto_reply="default_auto_reply"
)
assistant = autogen.AssistantAgent(
name="assistant",
system_message="You are a sociologist.",
llm_config=llama3
)
agent_list = []
dataset_folder = ""
personality_folder = ""
if usa_election:
dataset_folder = "Dataset/USA_Election"
personality_folder = "Personalities/USA_Election"
NUM_DEMOCRATICS = 2 # 27
NUM_REPUBLICANS = 2 # 73
df_democratics = pd.read_csv(f'{dataset_folder}/Democratics.csv', encoding='utf-8')
democratic_users = df_democratics['User'].unique() # 205 users (27%)
df_republicans = pd.read_csv(f'{dataset_folder}/Republicans.csv', encoding='utf-8')
republican_users = df_republicans['User'].unique() # 543 users (73%)
df_original_tweets = pd.read_csv(f'{dataset_folder}/Original_Tweets.csv', encoding='utf-8')
users = []
for i in range(max(NUM_DEMOCRATICS, NUM_REPUBLICANS)):
if i < NUM_DEMOCRATICS:
users.append(democratic_users[i])
if i < NUM_REPUBLICANS:
users.append(republican_users[i])
for index, user in enumerate(users, start=1):
if not os.path.exists(f"{personality_folder}/{user.lower()}.txt"):
print(f'Agent {index}')
user_tweet = df_original_tweets[df_original_tweets['User'] == user]['Tweet'].head(30) # Filter the DataFrame for the user of interest and select the 'Tweet' column
user_tweet_string = '\n'.join(user_tweet) # Create a single string with '\n' at the end of each tweet
task_for_personality = f"""These are contents published by {user} on a social network:\n{user_tweet_string}\n\nDescribe {user}'s personality in 30 words basing on these contents using sentences like "You are..."."""
user_proxy.initiate_chat(
assistant,
message=task_for_personality
)
# Save agent's personality
save_personality_to_file(user_proxy.last_message()["content"], str(user.lower()), folder=personality_folder)
agent_personality = read_from_file(f"{user.lower()}.txt", personality_folder)
agent = autogen.AssistantAgent(
name=f"{user}",
system_message=f"You are {user}. {agent_personality} You are tasked with making a decision about your activity on a social network based on feedback received on your previous posts and related content from other users.",
llm_config=llama3,
)
agent_list.append(agent)
elif qanon:
dataset_folder = "Dataset/QAnon"
personality_folder = "Personalities/QAnon"
df = pd.read_csv(f'{dataset_folder}/QAnon_tweets.csv', encoding='latin1')
unique_users = df['from_user'].unique()[:100] # Get the first 100 unique users
df_original_tweets = df[['from_user', 'text']] # Extract original tweets
users = list(unique_users)
for index, user in enumerate(users, start=1):
if not os.path.exists(f"{personality_folder}/{user.lower()}.txt"):
print(f'Agent {index}')
user_tweet = df_original_tweets[df_original_tweets['from_user'] == user]['text'].head(30) # Filter the DataFrame for the user of interest and select the 'text' column
user_tweet_string = '\n'.join(user_tweet) # Create a single string with '\n' at the end of each tweet
task_for_personality = f"""These are contents published by {user} on a social network:\n{user_tweet_string}\n\nDescribe {user}'s personality in 30 words basing on these contents using sentences like "You are..."."""
user_proxy.initiate_chat(
assistant,
message=task_for_personality
)
# Save agent's personality
save_personality_to_file(user_proxy.last_message()["content"], str(user.lower()), personality_folder)
agent_personality = read_from_file(f"{user.lower()}.txt", personality_folder)
agent = autogen.AssistantAgent(
name=f"{user}",
system_message=f"You are {user}. {agent_personality} You are tasked with making a decision about your activity on a social network based on feedback received on your previous posts and related content from other users.",
llm_config=llama3,
)
agent_list.append(agent)
elif brexit:
dataset_folder = "Dataset/Brexit"
personality_folder = "Personalities/Brexit"
NUM_ANTI_BREXIT = 55 # 55
NUM_PRO_BREXIT = 45 # 45
df_anti_brexit = pd.read_csv(f'{dataset_folder}/Anti_Brexit_Users.csv', encoding='utf-8')
anti_brexit_users = df_anti_brexit['User'].unique() # 2490 users (55%)
df_pro_brexit = pd.read_csv(f'{dataset_folder}/Pro_Brexit_Users.csv', encoding='utf-8')
pro_brexit_users = df_pro_brexit['User'].unique() # 2037 users (45%)
df_original_tweets = pd.read_csv(f'{dataset_folder}/User_Tweet.csv', encoding='utf-8')
users = []
for i in range(max(NUM_ANTI_BREXIT, NUM_PRO_BREXIT)):
if i < NUM_ANTI_BREXIT:
users.append(anti_brexit_users[i])
if i < NUM_PRO_BREXIT:
users.append(pro_brexit_users[i])
for index, user in enumerate(users, start=1):
if not os.path.exists(f"{personality_folder}/{user.lower()}.txt"):
print(f'Agent {index}')
user_tweet = df_original_tweets[df_original_tweets['User'] == user]['Tweet'].head(30) # Filter the DataFrame for the user of interest and select the 'Tweet' column
user_tweet_string = '\n'.join(user_tweet) # Create a single string with '\n' at the end of each tweet
task_for_personality = f"""These are contents published by {user} on a social network:\n{user_tweet_string}\n\nDescribe {user}'s personality in 30 words basing on these contents using sentences like "You are..."."""
user_proxy.initiate_chat(
assistant,
message=task_for_personality
)
# Save agent's personality
save_personality_to_file(user_proxy.last_message()["content"], str(user.lower()), folder=personality_folder)
agent_personality = read_from_file(f"{user.lower()}.txt", personality_folder)
agent = autogen.AssistantAgent(
name=f"{user}",
system_message=f"You are {user}. {agent_personality} You are tasked with making a decision about your activity on a social network based on feedback received on your previous posts and related content from other users.",
llm_config=llama3,
)
agent_list.append(agent)