|
import asyncio |
|
import streamlit as st |
|
from typing import Dict, Any, List |
|
from agents import Agent, Runner, trace |
|
from agents import set_default_openai_key |
|
from firecrawl import FirecrawlApp |
|
from agents.tool import function_tool |
|
|
|
|
|
st.set_page_config( |
|
page_title = "OpenAI based Deep Research Agent", |
|
page_icon = "π", |
|
layout = "wide" |
|
) |
|
|
|
|
|
if "openai_api_key" not in st.session_state: |
|
st.session_state.openai_api_key = "" |
|
if "firecrawl_api_key" not in st.session_state: |
|
st.session_state.firecrawl_api_key = "" |
|
|
|
|
|
with st.sidebar: |
|
st.title("API Configuration") |
|
openai_api_key = st.text_input( |
|
"OpenAI API Key", |
|
value = st.session_state.openai_api_key, |
|
type = "password" |
|
) |
|
|
|
firecrawl_api_key = st.text_input( |
|
"Firecrawl API Key", |
|
value = st.session_state.firecrawl_api_key, |
|
type = "password" |
|
) |
|
|
|
if openai_api_key: |
|
st.session_state.openai_api_key = openai_api_key |
|
set_default_openai_key(openai_api_key) |
|
if firecrawl_api_key: |
|
st.session_state.firecrawl_api_key = firecrawl_api_key |
|
|
|
|
|
st.title("π OpenAI Deep Research Agent") |
|
st.markdown("This OpenAI Agent from OpenAI Agent SDK performs deep research on any topic using Firecrawl") |
|
|
|
|
|
research_topic = st.text_input("Enter research topic: ", placeholder = "e.g., Latest Development in AI") |
|
|
|
|
|
|
|
@function_tool |
|
async def deep_research(query: str, max_depth: int, time_limit: int, max_urls: int): |
|
""" |
|
Perform comprehensive web research using Firecrawl's deep research endpoint. |
|
""" |
|
try: |
|
|
|
firecrawl_app = FirecrawlApp(api_key = st.session_state.firecrawl_api_key) |
|
params = { |
|
"maxDepth": max_depth, |
|
"timeLimit": time_limit, |
|
"maxUrls": max_urls |
|
} |
|
|
|
def on_activity(activity): |
|
st.write(f"[{activity['type']}]{activity['message']}") |
|
|
|
|
|
with st.spinner("Performing Deep Research..."): |
|
resp = firecrawl_app.deep_research( |
|
query = query, |
|
on_activity = on_activity, |
|
|
|
) |
|
|
|
return { |
|
"success" : True, |
|
"final_analysis" : resp["data"]["finalAnalysis"], |
|
"sources_count": len(resp["data"]["sources"]), |
|
"sources":resp["data"]["sources"] |
|
} |
|
except Exception as e: |
|
st.error(f"Deep Research Error: {str(e)}") |
|
return { |
|
"error" : str(e), |
|
"success" : False |
|
} |
|
|
|
|
|
research_agent = Agent( |
|
name = "research_agent", |
|
instructions = """you are a research assistant that can perform deep web research on any topic. |
|
|
|
When given a research topic or question: |
|
1. Use the deep_research tool to gather comprehensive information |
|
- Always use these parameters |
|
* max_depth: 3 (for moderate depth) |
|
* time_limit: 180 (3 minutes) |
|
* max_urls: 10 (sufficient sources) |
|
2. The tool will search the web, analyze multiple sources, and provide a synthesis |
|
3. Review the research results and organize them into a well-structured report |
|
4. Include proper citations for all sources |
|
5. Highlight key findings and insights |
|
""", |
|
model="gpt-4o-mini", |
|
tools = [deep_research] |
|
) |
|
|
|
elaboration_agent = Agent( |
|
name = "elaboration_agent", |
|
instructions = """You are an expert content enhancer specializing in research elaboration. |
|
|
|
When given a research report: |
|
1. Analyze the structure and content of the report |
|
2. Enhance the report by: |
|
- Adding more detailed explanation of complex concepts. |
|
- Including relevant examples, case studies, and real world application. |
|
- Expanding on key points with additional context and nuance |
|
- Adding visual elements descriptions (charts, diagrams, infographics) |
|
- Incorporating latest trends and future predictions |
|
- Suggesting pratical implications for different stackholders |
|
3. Maintain academic rigor and factual accuracy |
|
4. Preserve the original structure while making it more comprehensive |
|
5. Ensure all additions are relevant and valuable to the topic. |
|
""", |
|
model="gpt-4.1-nano" |
|
) |
|
|
|
async def run_research_process(topic : str): |
|
"""Run the complete research process""" |
|
|
|
with st.spinner("Conducting initial research..."): |
|
research_results = await Runner.run(research_agent, topic) |
|
initial_report = research_results.final_output |
|
|
|
with st.expander("View Initial Research Report"): |
|
st.markdown(initial_report) |
|
|
|
|
|
|
|
with st.spinner("Enhancing the report with additional information..."): |
|
elaboration_input = f""" |
|
RESEARCH_TOPIC = {topic} |
|
|
|
INITIAL RESEARCH REPORT: |
|
{initial_report} |
|
|
|
Please enhance this research report with additional information, |
|
examples, case studies, and deeper insights while maintaining its |
|
academic rigor and factual accuracy. |
|
""" |
|
|
|
elaboration_results = await Runner.run(elaboration_agent, elaboration_input) |
|
enhanced_report = elaboration_results.final_output |
|
|
|
return enhanced_report |
|
|
|
|
|
if st.button("Start Research", disabled = not (openai_api_key and firecrawl_api_key and research_topic)): |
|
if not openai_api_key or not firecrawl_api_key: |
|
st.warning("Please enter both the API Key to start the research!!") |
|
elif not research_topic: |
|
st.warning("No topic provided! Please enter a research topic!!") |
|
else: |
|
try: |
|
|
|
report_placeholder = st.empty() |
|
|
|
|
|
enhanced_report = asyncio.run(run_research_process(research_topic)) |
|
|
|
|
|
report_placeholder.markdown(f"## {research_topic} Report") |
|
report_placeholder.markdown(enhanced_report) |
|
|
|
|
|
st.download_button( |
|
"Download Report", |
|
enhanced_report, |
|
file_name = f"{research_topic.replace(' ', '_')}_report.md", |
|
mime = "text/markdown" |
|
) |
|
except Exception as e: |
|
st.error(f"An error occured: {str(e)}") |
|
|
|
|
|
st.markdown("---------------") |
|
st.markdown("Powered by OpenAI Agents SDK and Firecrawl") |