-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
204 lines (169 loc) · 7.81 KB
/
main.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import autogen
import json
config_list = autogen.config_list_from_json(env_or_file="OAI_CONFIG_LIST")
llm_config = {"config_list": config_list}
# tasks = [
# """Please identify the best posts from subreddits on reddit talking about beekeeping products.""",
# """Make a pleasant joke about it.""",
# ]
# system input
audience = "someone who is interested in an app for honey intake monitoring"
import autogen
# Configuration and Initialization
config_list = autogen.config_list_from_json(env_or_file="OAI_CONFIG_LIST")
llm_config = {"config_list": config_list}
# Define specialized agents
leader = autogen.AssistantAgent("Leader", llm_config=llm_config,
system_message="I orchestrate the team to gather and evaluate information efficiently.")
creative_director = autogen.AssistantAgent("CreativeDirector", llm_config=llm_config,
system_message="I generate search keywords to find relevant subreddits.")
subreddit_agent = autogen.AssistantAgent("SubredditAgent", llm_config=llm_config,
system_message="I locate subreddits based on the provided keywords.")
post_agent = autogen.AssistantAgent("PostAgent", llm_config=llm_config,
system_message="I retrieve and analyze posts from the identified subreddits.")
data_analyst = autogen.AssistantAgent("DataAnalyst", llm_config=llm_config,
system_message="I analyze the posts to extract marketing insights.")
reviewer = autogen.AssistantAgent("Reviewer", llm_config=llm_config,
system_message="I ensure the content is interesting and useful for developing the product.")
# Main chat and manager
groupchat = autogen.GroupChat(
agents=[leader, creative_director, subreddit_agent, post_agent, data_analyst, reviewer],
messages=[],
speaker_selection_method="round_robin",
max_round=10,
allow_repeat_speaker=False
)
groupchat_manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
# Function to handle the output to a file
def output_to_file(filename, data):
with open(filename, 'a') as file:
file.write(json.dumps(data, indent=4) + '\n')
# Task execution using nested chats
def execute_marketing_analysis(task_description):
leader.initiate_chat(groupchat_manager, message=f"Initiate project: {task_description}", max_turns=1)
keywords = creative_director.initiate_chat(groupchat_manager, message="Generate keywords for Reddit search.")
subreddits = subreddit_agent.initiate_chat(groupchat_manager,
message=f"Find subreddits using keywords: {keywords}.")
posts = post_agent.initiate_chat(groupchat_manager, message=f"Extract posts from these subreddits: {subreddits}.")
print("IMPORTANTSTUFF", posts)
# pull out the comments from a post url
# Output messages to file
output_to_file('groupchat_messages.json', groupchat.messages)
analysis = data_analyst.initiate_chat(groupchat_manager, message=f"Analyze posts: {posts}.")
review = reviewer.initiate_chat(groupchat_manager, message="Review the final report.", summary_method="last_msg")
return review.chat_history
if __name__ == '__main__':
# Example task
task_description = "Identify the best posts from subreddits talking about beekeeping products."
execute_marketing_analysis(task_description)
# print(final_report)
# # Define the agents
# inner_assistant = autogen.AssistantAgent(
# "Inner-assistant",
# llm_config=llm_config,
# is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
# )
# inner_code_interpreter = autogen.UserProxyAgent(
# "Inner-code-interpreter",
# human_input_mode="NEVER",
# code_execution_config={
# "work_dir": "coding",
# "use_docker": False,
# },
# default_auto_reply="",
# is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
# )
# groupchat = autogen.GroupChat(
# agents=[inner_assistant, inner_code_interpreter],
# messages=[],
# speaker_selection_method="round_robin", # With two agents, this is equivalent to a 1:1 conversation.
# allow_repeat_speaker=False,
# max_round=8,
# )
# manager = autogen.GroupChatManager(
# groupchat=groupchat,
# is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
# llm_config=llm_config,
# code_execution_config={
# "work_dir": "coding",
# "use_docker": False,
# },
# )
# # The Leader Agent who knows the objective and tasks
# leader = autogen.AssistantAgent(
# "Leader",
# llm_config=llm_config,
# system_message="""
# I am the Leader agent, orchestrating a team of specialized agents to gather
# and evaluate information from reddit efficiently.
# """
# )
# # The Creative Director Agent who brainstorms keywords for searching
# creative_director = autogen.AssistantAgent(
# "CreativeDirector",
# llm_config=llm_config,
# system_message="""
# As the Creative Director, I am responsible for generating search keywords
# that will be used to identify relevant subreddits.
# """
# )
# # Inner and outer agents
# assistant_1 = autogen.AssistantAgent(
# name="Assistant_1",
# llm_config={"config_list": config_list},
# )
# assistant_2 = autogen.AssistantAgent(
# name="Assistant_2",
# llm_config={"config_list": config_list},
# )
# writer = autogen.AssistantAgent(
# name="Writer",
# llm_config={"config_list": config_list},
# system_message="""
# You are a professional writer, known for
# your insightful and engaging articles.
# You transform complex concepts into compelling narratives.
# """,
# )
# reviewer = autogen.AssistantAgent(
# name="Reviewer",
# llm_config={"config_list": config_list},
# system_message="""
# You are a compliance reviewer, known for your thoroughness and commitment to standards.
# Your task is to scrutinize content for any harmful elements or regulatory violations, ensuring
# all materials align with required guidelines.
# You must review carefully, identify potential issues, and maintain the integrity of the organization.
# Your role demands fairness, a deep understanding of regulations, and a focus on protecting against
# harm while upholding a culture of responsibility.
# """,
# )
# user = autogen.UserProxyAgent(
# name="User",
# human_input_mode="NEVER",
# is_termination_msg=lambda x: x.get("content", "").find("TERMINATE") >= 0,
# code_execution_config={
# "last_n_messages": 1,
# "work_dir": "tasks",
# "use_docker": False,
# }, # Please set use_docker=True if docker is available to run the generated code. Using docker is safer than running the generated code directly.
# )
# # Step 2) Orchestrate Nested Chats to Solve Tasks
# def writing_message(recipient, messages, sender, config):
# return f"Polish the content to make an engaging and nicely formatted blog post. \n\n {recipient.chat_messages_for_summary(sender)[-1]['content']}"
# nested_chat_queue = [
# {"recipient": manager, "summary_method": "reflection_with_llm"},
# {"recipient": writer, "message": writing_message, "summary_method": "last_msg", "max_turns": 1},
# {"recipient": reviewer, "message": "Review the content provided.", "summary_method": "last_msg", "max_turns": 1},
# {"recipient": writer, "message": writing_message, "summary_method": "last_msg", "max_turns": 1},
# ]
# assistant_1.register_nested_chats(
# nested_chat_queue,
# trigger=user,
# )
# # user.initiate_chat(assistant, message=tasks[0], max_turns=1)
# res = user.initiate_chats(
# [
# {"recipient": assistant_1, "message": tasks[0], "max_turns": 1, "summary_method": "last_msg"},
# {"recipient": assistant_2, "message": tasks[1]},
# ]
# )