Update src/streamlit_app.py
Browse files- src/streamlit_app.py +75 -1
src/streamlit_app.py
CHANGED
@@ -11,4 +11,78 @@ st.set_page_config(
|
|
11 |
page_title = "OpenAI based Deep Research Agent",
|
12 |
page_icon = "π",
|
13 |
layout = "wide"
|
14 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
page_title = "OpenAI based Deep Research Agent",
|
12 |
page_icon = "π",
|
13 |
layout = "wide"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Initialize session state for API Key if don't exist
|
17 |
+
if "openai_api_key" not in st.session_state:
|
18 |
+
st.session_state.openai_api_key = ""
|
19 |
+
if "firecrawl_api_key" not in st.session_state:
|
20 |
+
st.session_state.firecrawl_api_key = ""
|
21 |
+
|
22 |
+
# Sidebar for API Key
|
23 |
+
with st.sidebar:
|
24 |
+
st.title("API Configuration")
|
25 |
+
openai_api_key = st.text_input(
|
26 |
+
"OpenAI API Key",
|
27 |
+
value = st.session_state.openai_api_key,
|
28 |
+
type = "password"
|
29 |
+
)
|
30 |
+
|
31 |
+
firecrawl_api_key = st.text_input(
|
32 |
+
"Firecrawl API Key",
|
33 |
+
value = st.session_state.firecrawl_api_key,
|
34 |
+
type = "password"
|
35 |
+
)
|
36 |
+
|
37 |
+
if openai_api_key:
|
38 |
+
st.session_state.openai_api_key = openai_api_key
|
39 |
+
set_default_openai_key(openai_api_key)
|
40 |
+
if firecrawl_api_key:
|
41 |
+
st.session_state.firecrawl_api_key = firecrawl_api_key
|
42 |
+
|
43 |
+
# Main Application and Input Field
|
44 |
+
st.title("π OpenAI Deep Research Agent")
|
45 |
+
st.markdown("This OpenAI Agent from OpenAI Agent SDK performs deep research on any topic using Firecrawl")
|
46 |
+
|
47 |
+
# This takes the input from the user for the specific research related concern
|
48 |
+
research_topic = st.text_input("Enter research topic: ", placeholder = "e.g., Latest Development in AI")
|
49 |
+
|
50 |
+
# This function tools help us to register this function
|
51 |
+
# as a tool to the agent
|
52 |
+
@function_tool
|
53 |
+
async def deep_research(query: str, max_depth: int, time_limit: int, max_urls: int):
|
54 |
+
"""
|
55 |
+
Perform comprehensive web research using Firecrawl's deep research endpoint.
|
56 |
+
"""
|
57 |
+
try:
|
58 |
+
# Initialize the firecrawl using the saved API key
|
59 |
+
firecrawl_app = FirecrawlApp(api_key = st.session_state.firecrawl_api_key)
|
60 |
+
params = {
|
61 |
+
"maxDepth": max_depth,
|
62 |
+
"timeLimit": time_limit,
|
63 |
+
"maxUrls": max_urls
|
64 |
+
}
|
65 |
+
# Callback Setup for real-time update
|
66 |
+
def on_activity(activity):
|
67 |
+
st.write(f"[{activity['type']}]{activity['message']}")
|
68 |
+
|
69 |
+
# Run the deep research using firecrawl
|
70 |
+
with st.spinner("Performing Deep Research..."):
|
71 |
+
resp = firecrawl_app.deep_research(
|
72 |
+
query = query,
|
73 |
+
params = params,
|
74 |
+
on_activity = on_activity
|
75 |
+
)
|
76 |
+
|
77 |
+
return {
|
78 |
+
"success" : True,
|
79 |
+
"final_analysis" : resp["data"]["finalAnalysis"]
|
80 |
+
"sources_count": len(resp["data"]["sources"]),
|
81 |
+
"sources":resp["data"]["sources"]
|
82 |
+
}
|
83 |
+
except Exception as e:
|
84 |
+
st.error(f"Deep Research Error: {str(e)}")
|
85 |
+
return {
|
86 |
+
"error" : str(e),
|
87 |
+
"success" : False
|
88 |
+
}
|