forked from YSocialTwin/YClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy_client.py
More file actions
165 lines (141 loc) · 4.93 KB
/
y_client.py
File metadata and controls
165 lines (141 loc) · 4.93 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
161
162
163
164
165
import os, sys, json, shutil
if __name__ == "__main__":
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument(
"-c",
"--config_file",
default="config_files/config.json",
help="JSON file describing the simulation configuration",
)
parser.add_argument(
"-f",
"--feeds",
default="config_files/feed_small.json",
help="JSON file containing rss feed categorized",
)
parser.add_argument(
"-p",
"--prompts",
default="config_files/prompts.json",
help="JSON file containing LLM prompts",
)
parser.add_argument(
"-a", "--agents", default=None, help="JSON file with pre-existing agents"
)
parser.add_argument(
"-o", "--owner", default="admin", help="Simulation owner username"
)
parser.add_argument(
"-r",
"--reset",
default=False,
help="Boolean. Whether to reset the experiment status. Default False",
)
parser.add_argument(
"-n",
"--news",
default=False,
help="Boolean. Whether to reload the rss feeds. Default False",
)
parser.add_argument(
"-x",
"--crecsys",
default="ReverseChronoFollowersPopularity",
help="Name of the content recsys to be used. Options: ...",
)
parser.add_argument(
"-y",
"--frecsys",
default="PreferentialAttachment",
help="Name of the follower recsys to be used. Options: ...",
)
parser.add_argument(
"-g",
"--graph",
default=None,
help="Name of the graph file (CSV format, number of nodes equal to the starting agents) "
"to be used for the simulation",
)
parser.add_argument(
"--news_source",
default="rss",
choices=["rss", "urls"],
help="Specify the source of news: 'rss' for RSS feeds or 'urls' for a list of URLs. Default is 'rss'.",
)
parser.add_argument(
"--urls_file",
default=None,
help="Path to a text file containing URLs (one per line) for news extraction. Required if --news_source is 'urls'.",
)
parser.add_argument(
"--max_urls",
type=int,
default=1000,
help="Maximum number of URLs to process from the URLs file (randomly sampled). Applies only to --news_source=urls. Default: 1000."
)
args = parser.parse_args()
agents_owner = args.owner
config_file = args.config_file
agents_file = args.agents
rss_feeds = args.feeds
prompts_file = args.prompts
graph_file = args.graph
# get simulation client and name
config = json.load(open(config_file, "r"))
client_name = config["simulation"]["client"]
simulation_name = config["simulation"]["name"]
# agent file output
output = f"experiments/{simulation_name}_agents.json"
# set the current config file (needed to generate the database)
shutil.copyfile(config_file, f"experiments/current_config.json")
import y_client.recsys
import y_client.clients
if not os.path.exists("./experiments"):
os.mkdir("./experiments")
# get recommender systems
content_recsys = getattr(y_client.recsys, args.crecsys)()
follow_recsys = getattr(y_client.recsys, args.frecsys)(leaning_bias=1.5)
# get and instantiate the client
experiment = getattr(y_client.clients, client_name)(
config_file,
prompts_file,
agents_filename=agents_file,
owner=agents_owner,
agents_output=output,
graph_file=graph_file,
)
if args.reset:
print("Resetting experiment...")
experiment.reset_experiment()
# Set recommendation systems
experiment.set_recsys(content_recsys, follow_recsys)
# Handle news feeds if requested
proceed_with_simulation = True
if args.news_source == "urls":
if not args.urls_file:
print("Error: --urls_file must be specified when --news_source is 'urls'.")
sys.exit(1)
print("Resetting news database...")
experiment.reset_news_db()
print("Loading news from URLs...")
proceed_with_simulation = experiment.load_news_from_urls(args.urls_file, max_urls=args.max_urls)
elif args.news:
print("Resetting news database...")
experiment.reset_news_db()
print("Loading RSS feeds...")
proceed_with_simulation = experiment.load_rrs_endpoints(rss_feeds)
if proceed_with_simulation:
print("\nInitializing agent population...")
if args.agents is None:
experiment.create_initial_population()
else:
experiment.load_existing_agents(args.agents)
experiment.save_agents()
print("\nStarting simulation...")
experiment.run_simulation()
else:
print("\nSimulation aborted by user.")
sys.exit(1)