forked from awslabs/amazon-bedrock-agent-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (199 loc) · 10.3 KB
/
Copy pathmain.py
File metadata and controls
221 lines (199 loc) · 10.3 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# Copyright 2024 Amazon.com and its affiliates; all rights reserved.
# This file is AWS Content and may not be duplicated or distributed without permission
import sys
from pathlib import Path
import datetime
import traceback
import yaml
import uuid
from textwrap import dedent
import os
import argparse
sys.path.append(str(Path(__file__).parent.parent.parent.parent))
from src.utils.bedrock_agent import Agent, SupervisorAgent, Task, region, account_id
current_dir = os.path.dirname(os.path.abspath(__file__))
task_yaml_path = os.path.join(current_dir, "tasks.yaml")
agent_yaml_path = os.path.join(current_dir, "agents.yaml")
def main(args):
if args.recreate_agents == "false":
Agent.set_force_recreate_default(False)
else:
Agent.set_force_recreate_default(True)
Agent.delete_by_name("startup_advisor", verbose=True)
if args.clean_up == "true":
Agent.delete_by_name("startup_advisor", verbose=True)
Agent.delete_by_name("lead_market_analyst", verbose=True)
Agent.delete_by_name("chief_strategist", verbose=True)
Agent.delete_by_name("creative_director", verbose=True)
Agent.delete_by_name("content_writer", verbose=True)
Agent.delete_by_name("formatted_report_writer", verbose=True)
else:
inputs = {
'web_domain': args.web_domain,
'project_description': args.project,
'feedback_iteration_count': args.iterations,
}
with open(task_yaml_path, 'r') as file:
task_yaml_content = yaml.safe_load(file)
research_task = Task('research_task', task_yaml_content, inputs)
marketing_strategy_task = Task('marketing_strategy_task', task_yaml_content, inputs)
campaign_idea_task = Task('campaign_idea_task', task_yaml_content, inputs)
copy_creation_task = Task('copy_creation_task', task_yaml_content, inputs)
detailed_campaign_task = Task('detailed_campaign_task', task_yaml_content, inputs)
iterative_revisions_task = Task('iterative_revisions_task', task_yaml_content, inputs)
final_report_output_task = Task('final_report_output_task', task_yaml_content, inputs)
web_search_tool = {
"code":f"arn:aws:lambda:{region}:{account_id}:function:web_search",
"definition":{
"name": "web_search",
"description": "Searches the web for information",
"parameters": {
"search_query": {
"description": "The query to search the web with",
"type": "string",
"required": True,
},
"target_website": {
"description": "The specific website to search including its domain name. If not provided, the most relevant website will be used",
"type": "string",
"required": False,
},
"topic": {
"description": "The topic being searched. 'news' or 'general'. Helps narrow the search when news is the focus.",
"type": "string",
"required": False,
},
"days": {
"description": "The number of days of history to search. Helps when looking for recent events or news.",
"type": "string",
"required": False,
},
},
},
}
set_value_for_key = {
"code":f"arn:aws:lambda:{region}:{account_id}:function:working_memory",
"definition":{
"name": "set_value_for_key",
"description": " Stores a key-value pair in a DynamoDB table. Creates the table if it doesn't exist.",
"parameters": {
"key": {
"description": "The name of the key to store the value under.",
"type": "string",
"required": True,
},
"value": {
"description": "The value to store for that key name.",
"type": "string",
"required": True,
},
"table_name": {
"description": "The name of the DynamoDB table to use for storage.",
"type": "string",
"required": True,
}
},
},
}
get_key_value = {
"code":f"arn:aws:lambda:{region}:{account_id}:function:working_memory",
"definition":{
"name": "get_key_value",
"description": "Retrieves a value for a given key name from a DynamoDB table.",
"parameters": {
"key": {
"description": "The name of the key to store the value under.",
"type": "string",
"required": True,
},
"table_name": {
"description": "The name of the DynamoDB table to use for storage.",
"type": "string",
"required": True,
}
},
},
}
with open(agent_yaml_path, 'r') as file:
agent_yaml_content = yaml.safe_load(file)
lead_market_analyst = Agent('lead_market_analyst', agent_yaml_content,
tools=[web_search_tool, set_value_for_key, get_key_value])
chief_strategist = Agent('chief_strategist', agent_yaml_content,
tools=[web_search_tool, set_value_for_key, get_key_value])
creative_director = Agent('creative_director', agent_yaml_content,
tools=[web_search_tool, set_value_for_key, get_key_value])
content_creator = Agent('content_writer', agent_yaml_content,
tools=[web_search_tool, set_value_for_key, get_key_value])
formatted_report_writer = Agent('formatted_report_writer', agent_yaml_content,
tools=[web_search_tool, set_value_for_key, get_key_value])
print("\n\nCreating marketing_strategy_agent as a supervisor agent...\n\n")
startup_advisor = SupervisorAgent("startup_advisor", agent_yaml_content,
[lead_market_analyst, chief_strategist,
content_creator, creative_director,
formatted_report_writer],
verbose=False)
if args.recreate_agents == "false":
print("\n\nInvoking supervisor agent...\n\n")
time_before_call = datetime.datetime.now()
print(f"time before call: {time_before_call}\n")
try:
folder_name = "startup-advisor-" + str(uuid.uuid4())
result = startup_advisor.invoke_with_tasks([
research_task, marketing_strategy_task,
campaign_idea_task, copy_creation_task,
detailed_campaign_task, iterative_revisions_task,
final_report_output_task
],
additional_instructions=dedent(f"""
Use a single Working Memory table for this entire set of tasks, with
table name: {folder_name}. Tell your collaborators this table name as part of
every request, so that they are not confused and they share state effectively.
The keys they use in that table will allow them to keep track of any number
of state items they require. When you have completed all tasks, summarize
your work, and share the table name so that all the results can be used and
analyzed."""),
processing_type="sequential",
enable_trace=True, trace_level=args.trace_level,
verbose=True)
print(result)
except Exception as e:
print(e)
traceback.print_exc()
pass
duration = datetime.datetime.now() - time_before_call
print(f"\nTime taken: {duration.total_seconds():,.1f} seconds")
else:
print("Recreated agents.")
if __name__ == '__main__':
default_inputs = {
'customer_domain': 'flyingCars.com',
'project_description': dedent("""
FlyingCars wants to be the leading supplier of flying cars.
The project is to build an innovative marketing strategy to showcase FlyingCars' advanced
offerings, emphasizing ease of use, cost effectiveness, productivity, and safety.
Target high net worth individuals, highlighting success stories and transformative
potential. Be sure to include a draft for a video ad.
"""),
'iterations': "1"
}
parser = argparse.ArgumentParser()
parser.add_argument("--recreate_agents", required=False, default='true', help="False if reusing existing agents.")
parser.add_argument("--web_domain", required=False,
default=default_inputs['customer_domain'],
help="The web domain name for the project (e.g., AnyCompany.ai).")
parser.add_argument("--iterations", required=False,
default=default_inputs['iterations'],
help="The number of rounds of feedback to use when producing the campaign report")
parser.add_argument("--project", required=False,
default=default_inputs['project_description'],
help="The project that needs a marketing strategy.")
parser.add_argument("--trace_level", required=False, default="core", help="The level of trace, 'core', 'outline', 'all'.")
parser.add_argument(
"--clean_up",
required=False,
default="false",
help="Cleanup all infrastructure.",
)
args = parser.parse_args()
main(args)