-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbedrock_deep_research.py
333 lines (256 loc) · 10.6 KB
/
bedrock_deep_research.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import logging
import os
import uuid
from datetime import datetime
from typing import List
import pyperclip
import pytz
import streamlit as st
from dotenv import load_dotenv
from pydantic import BaseModel, Field
from bedrock_deep_research import BedrockDeepResearch
from bedrock_deep_research.config import DEFAULT_TOPIC, SUPPORTED_MODELS, Configuration
from bedrock_deep_research.model import Section
logger = logging.getLogger(__name__)
LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper()
default_st_vals = {
"head_image_path": None,
"bedrock_deep_research": None,
"stage": "initial_form",
"article": "",
"text_error": "",
}
class Article(BaseModel):
title: str = Field(description="Title of the article")
sections: List[Section] = Field(
description="List of sections in the article")
def render_outline(self) -> str:
"""Render the article outline."""
sections_content = "\n".join(
f"{i + 1}. {section.name}" for i, section in enumerate(self.sections)
)
return f"\nTitle: **{self.title}**\n\n{sections_content}"
def render_section(self, section: Section) -> str:
"""Render a single section."""
return f"\n## {section.name}\n\n{section.content}"
def render_full_article(self) -> str:
"""Render the full article with all sections."""
sections_content = "\n".join(
self.render_section(section) for section in self.sections
)
return (
f"# {self.title}\n#### Date: {self._get_date_today()}\n\n{sections_content}"
)
@staticmethod
def _get_date_today() -> str:
"""Get today's date in UTC."""
return datetime.now(pytz.UTC).strftime("%Y-%m-%d")
def __str__(self) -> str:
"""String representation of the article."""
return self.render_outline()
def init_state():
for key, default_st_val in default_st_vals.items():
if key not in st.session_state:
st.session_state[key] = default_st_val
def render_initial_form():
"""
Renders the initial form for article generation with topic and writing_guidelines inputs.
"""
try:
with st.form("article_form"):
topic = st.text_area(
"Topic",
value=DEFAULT_TOPIC,
help="Enter the topic you want to write about",
)
writing_guidelines = st.text_area(
"Writing Guidelines",
value=Configuration.writing_guidelines,
help="Enter any specific guidelines regarding the writing length and style",
)
planner_model_name = st.selectbox(
"Select your Model for planning tasks", SUPPORTED_MODELS.keys()
)
writer_model_name = st.selectbox(
"Select your Model for writing tasks", SUPPORTED_MODELS.keys()
)
number_of_queries = st.number_input(
"Number of queries generated for each web search",
min_value=1,
max_value=5,
value=Configuration.number_of_queries,
)
max_search_depth = st.number_input(
"Maximum number of reflection and web search iterations allowed for each sections",
min_value=1,
max_value=5,
value=Configuration.max_search_depth,
)
submitted = st.form_submit_button(
"Generate Outline", type="primary")
if submitted:
logger.info(
f"generate_article on '{topic}' following '{writing_guidelines}'"
)
if not topic:
st.session_state.text_error = "Please enter a topic"
return
if not writing_guidelines:
st.session_state.text_error = (
"Please enter your writing guidelines for the article"
)
return
config = {
"configurable": {
"thread_id": str(uuid.uuid4()),
"writing_guidelines": writing_guidelines,
"max_search_depth": max_search_depth,
"number_of_queries": number_of_queries,
"planner_model": SUPPORTED_MODELS.get(planner_model_name),
"writer_model": SUPPORTED_MODELS.get(writer_model_name),
}
}
st.session_state.bedrock_deep_research = BedrockDeepResearch(
config=config, tavily_api_key=os.getenv("TAVILY_API_KEY")
)
with st.session_state.text_spinner_placeholder:
with st.spinner(
"Please wait while the article outline is being generated..."
):
response = st.session_state.bedrock_deep_research.start(
topic)
logger.debug(f"Outline response: {response}")
state = st.session_state.bedrock_deep_research.get_state()
article = Article(
title=state.values["title"],
sections=state.values["sections"],
)
st.session_state.article = article.render_outline()
st.session_state.stage = "outline_feedback"
st.rerun()
except Exception as e:
logger.error(f"An error occurred: {e}")
raise
def render_outline_feedback(article_container):
"""
Renders the article outline and gets user feedback.
"""
with article_container.container():
st.markdown("## Article Outline")
st.markdown(st.session_state.article)
st.markdown("Please provide feedback to the outline")
with st.form("feedback_form"):
feedback = st.text_area(
label="Feedback",
placeholder="Input a feedback to change the title or the sections",
)
col1, col2 = st.columns(2)
with col1:
submit_feedback_pressed = st.form_submit_button("Submit Feedback")
if submit_feedback_pressed:
on_submit_button_click(feedback)
with col2:
accept_outline_pressed = st.form_submit_button(
"Accept Outline", type="primary"
)
if accept_outline_pressed:
on_accept_outline_button_click()
def render_final_result(article_container):
"""
Renders the final article with options to copy or start over.
"""
logger.info("render_final_result")
logger.info(st.session_state)
with article_container.container():
if "head_image_path" in st.session_state and st.session_state.head_image_path:
st.image(st.session_state.head_image_path, width=1200)
st.markdown(st.session_state.article)
st.divider()
col1, col2 = st.columns(2)
with col1:
if st.button("Copy to Clipboard", type="primary"):
st.write("Article copied to clipboard!")
st.toast("Article copied to clipboard!")
pyperclip.copy(st.session_state.article)
with col2:
if st.button("Start Over"):
st.session_state.bedrock_deep_research = None
st.session_state.stage = "initial_form"
st.rerun()
# Display any error messages
if st.session_state.text_error:
st.error(st.session_state.text_error)
def on_submit_button_click(feedback):
try:
logger.info("Submit feedback pressed")
if not feedback:
st.session_state.text_error = "Please enter a feedback"
return
with st.session_state.text_spinner_placeholder:
with st.spinner("Please wait while your feedback is being processed"):
try:
response = st.session_state.bedrock_deep_research.feedback(
feedback)
logger.info(f"Feedback response: {response}")
state = st.session_state.bedrock_deep_research.get_state()
article = Article(
title=state.values["title"], sections=state.values["sections"]
)
st.session_state.article = article.render_outline()
st.session_state.text_error = ""
st.rerun()
except Exception as e:
st.error(
f"An error occurred while processing feedback: {e}")
except Exception as e:
logger.error(f"An error occurred: {e}")
st.error(f"An error occurred: {e}")
def on_accept_outline_button_click():
logger.info("Accept outline pressed")
try:
# if st.form_submit_button("Accept Outline", type="primary"):
with st.session_state.text_spinner_placeholder:
with st.spinner("Please wait while the article is being generated..."):
try:
response = st.session_state.bedrock_deep_research.feedback(
True)
logger.info(f"Accept outline response: {response}")
state = st.session_state.bedrock_deep_research.get_state()
st.session_state.head_image_path = state.values["head_image_path"]
st.session_state.article = state.values["final_report"]
st.session_state.stage = "final_result"
st.session_state.text_error = ""
st.rerun()
except Exception as e:
st.error(f"An error occurred: {e}")
except Exception as e:
logger.error(f"An error occurred: {e}")
st.error(f"An error occurred: {e}")
def main():
load_dotenv()
init_state()
logging.basicConfig(
level=LOGLEVEL,
force=True,
format="%(levelname)s:%(filename)s:L%(lineno)d - %(message)s",
)
# Header
title_container = st.container()
col1, col2 = st.columns([1, 5])
with title_container:
with col1:
st.image("static/bedrock-icon.png", width=100)
with col2:
st.title("Bedrock Deep Researcher")
st.divider()
# Main stage
st.session_state.text_spinner_placeholder = st.empty()
article_placeholder = st.empty()
if st.session_state.stage == "initial_form":
render_initial_form()
elif st.session_state.stage == "outline_feedback":
render_outline_feedback(article_placeholder)
elif st.session_state.stage == "final_result":
render_final_result(article_placeholder)
if __name__ == "__main__":
main()