The AI Deep Research Agent is a powerful research assistant that leverages OpenAI’s Agents SDK and Firecrawl’s deep research capabilities to perform comprehensive web research on any topic. The system uses a two-agent architecture where one agent performs deep research and another enhances the findings with additional context and insights.
Tutorial Available
Follow our complete step-by-step tutorial to build this from scratch
import asyncioimport streamlit as stfrom agents import Agent, Runnerfrom agents import set_default_openai_keyfrom firecrawl import FirecrawlAppfrom agents.tool import function_tool# Deep Research Tool@function_toolasync def deep_research(query: str, max_depth: int, time_limit: int, max_urls: int): """ Perform comprehensive web research using Firecrawl's deep research endpoint. """ firecrawl_app = FirecrawlApp(api_key=firecrawl_api_key) params = { "maxDepth": max_depth, "timeLimit": time_limit, "maxUrls": max_urls } results = firecrawl_app.deep_research( query=query, params=params, on_activity=lambda a: st.write(f"[{a['type']}] {a['message']}") ) return { "success": True, "final_analysis": results['data']['finalAnalysis'], "sources_count": len(results['data']['sources']), "sources": results['data']['sources'] }# Research Agentresearch_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: 1. Use the deep_research tool with max_depth=3, time_limit=180, max_urls=10 2. Review the research results and organize them into a report 3. Include proper citations for all sources 4. Highlight key findings and insights """, tools=[deep_research])# Elaboration Agentelaboration_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 2. Add detailed explanations of complex concepts 3. Include relevant examples and case studies 4. Expand on key points with additional context 5. Add visual element descriptions 6. Incorporate latest trends and predictions 7. Suggest practical implications """)# Research Processasync def run_research_process(topic: str): # Step 1: Initial Research research_result = await Runner.run(research_agent, topic) initial_report = research_result.final_output # Step 2: Enhance the report 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. """ elaboration_result = await Runner.run( elaboration_agent, elaboration_input ) return elaboration_result.final_output
import streamlit as stst.title("📘 OpenAI Deep Research Agent")st.markdown("""This agent performs deep research on any topic using Firecrawl""")# API Configurationwith st.sidebar: st.title("API Configuration") openai_api_key = st.text_input( "OpenAI API Key", type="password" ) firecrawl_api_key = st.text_input( "Firecrawl API Key", type="password" ) if openai_api_key: set_default_openai_key(openai_api_key)# Research Inputresearch_topic = st.text_input( "Enter your research topic:", placeholder="e.g., Latest developments in AI")# Start Researchif st.button("Start Research"): if not (openai_api_key and firecrawl_api_key and research_topic): st.warning("Please enter all required fields.") else: # Run research process enhanced_report = asyncio.run( run_research_process(research_topic) ) # Display report st.markdown("## Enhanced Research Report") st.markdown(enhanced_report) # Download button st.download_button( "Download Report", enhanced_report, file_name=f"{research_topic.replace(' ', '_')}_report.md", mime="text/markdown" )
from agents.tool import function_toolfrom firecrawl import FirecrawlApp@function_toolasync def deep_research( query: str, max_depth: int = 3, time_limit: int = 180, max_urls: int = 10) -> dict: """ Perform comprehensive web research using Firecrawl. Args: query: Research topic or question max_depth: Search depth (1-5) time_limit: Time limit in seconds max_urls: Maximum number of URLs to analyze Returns: dict: Research results with analysis and sources """ try: firecrawl_app = FirecrawlApp(api_key=api_key) params = { "maxDepth": max_depth, "timeLimit": time_limit, "maxUrls": max_urls } # Set up activity callback def on_activity(activity): print(f"[{activity['type']}] {activity['message']}") # Run deep research results = firecrawl_app.deep_research( query=query, params=params, on_activity=on_activity ) return { "success": True, "final_analysis": results['data']['finalAnalysis'], "sources_count": len(results['data']['sources']), "sources": results['data']['sources'] } except Exception as e: return { "error": str(e), "success": False }