Merge pull request #144 from Madhuvod/openai-agents-sdk
Added new Demo: Deep Research Agent using OpenAI Agents SDK
This commit is contained in:
commit
4880ace233
3 changed files with 265 additions and 0 deletions
74
ai_agent_tutorials/ai_deep_research_agent/README.md
Normal file
74
ai_agent_tutorials/ai_deep_research_agent/README.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# Deep Research Agent with OpenAI Agents SDK and Firecrawl
|
||||
|
||||
A powerful research assistant that leverages OpenAI's Agents SDK and Firecrawl's deep research capabilities to perform comprehensive web research on any topic and any question.
|
||||
|
||||
## Features
|
||||
|
||||
- **Deep Web Research**: Automatically searches the web, extracts content, and synthesizes findings
|
||||
- **Enhanced Analysis**: Uses OpenAI's Agents SDK to elaborate on research findings with additional context and insights
|
||||
- **Interactive UI**: Clean Streamlit interface for easy interaction
|
||||
- **Downloadable Reports**: Export research findings as markdown files
|
||||
|
||||
## How It Works
|
||||
|
||||
1. **Input Phase**: User provides a research topic and API credentials
|
||||
2. **Research Phase**: The tool uses Firecrawl to search the web and extract relevant information
|
||||
3. **Analysis Phase**: An initial research report is generated based on the findings
|
||||
4. **Enhancement Phase**: A second agent elaborates on the initial report, adding depth and context
|
||||
5. **Output Phase**: The enhanced report is presented to the user and available for download
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.8+
|
||||
- OpenAI API key
|
||||
- Firecrawl API key
|
||||
- Required Python packages (see `requirements.txt`)
|
||||
|
||||
## Installation
|
||||
|
||||
1. Clone this repository:
|
||||
```bash
|
||||
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
|
||||
cd ai_agent_tutorials/ai_deep_research_agent
|
||||
```
|
||||
|
||||
2. Install the required packages:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
1. Run the Streamlit app:
|
||||
```bash
|
||||
streamlit run deep_research_openai.py
|
||||
```
|
||||
|
||||
2. Enter your API keys in the sidebar:
|
||||
- OpenAI API key
|
||||
- Firecrawl API key
|
||||
|
||||
3. Enter your research topic in the main input field
|
||||
|
||||
4. Click "Start Research" and wait for the process to complete
|
||||
|
||||
5. View and download your enhanced research report
|
||||
|
||||
## Example Research Topics
|
||||
|
||||
- "Latest developments in quantum computing"
|
||||
- "Impact of climate change on marine ecosystems"
|
||||
- "Advancements in renewable energy storage"
|
||||
- "Ethical considerations in artificial intelligence"
|
||||
- "Emerging trends in remote work technologies"
|
||||
|
||||
## Technical Details
|
||||
|
||||
The application uses two specialized agents:
|
||||
|
||||
1. **Research Agent**: Utilizes Firecrawl's deep research endpoint to gather comprehensive information from multiple web sources.
|
||||
|
||||
2. **Elaboration Agent**: Enhances the initial research by adding detailed explanations, examples, case studies, and practical implications.
|
||||
|
||||
The Firecrawl deep research tool performs multiple iterations of web searches, content extraction, and analysis to provide thorough coverage of the topic.
|
||||
|
||||
|
|
@ -0,0 +1,187 @@
|
|||
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
|
||||
|
||||
# Set page configuration
|
||||
st.set_page_config(
|
||||
page_title="Enhanced Research Assistant",
|
||||
page_icon="🔍",
|
||||
layout="wide"
|
||||
)
|
||||
|
||||
# Initialize session state for API keys if not exists
|
||||
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 = ""
|
||||
|
||||
# Sidebar for API keys
|
||||
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
|
||||
|
||||
# Main content
|
||||
st.title("🔍 Enhanced Deep Research Agent")
|
||||
st.markdown("This OpenAI Agent from the OpenAI Agents SDK performs deep research on any topic using Firecrawl")
|
||||
|
||||
# Research topic input
|
||||
research_topic = st.text_input("Enter your research topic:", placeholder="e.g., Latest developments in AI")
|
||||
|
||||
# Keep the original deep_research tool
|
||||
@function_tool
|
||||
async def deep_research(query: str, max_depth: int, time_limit: int, max_urls: int) -> Dict[str, Any]:
|
||||
"""
|
||||
Perform comprehensive web research using Firecrawl's deep research endpoint.
|
||||
"""
|
||||
try:
|
||||
# Initialize FirecrawlApp with the API key from session state
|
||||
firecrawl_app = FirecrawlApp(api_key=st.session_state.firecrawl_api_key)
|
||||
|
||||
# Define research parameters
|
||||
params = {
|
||||
"maxDepth": max_depth,
|
||||
"timeLimit": time_limit,
|
||||
"maxUrls": max_urls
|
||||
}
|
||||
|
||||
# Set up a callback for real-time updates
|
||||
def on_activity(activity):
|
||||
st.write(f"[{activity['type']}] {activity['message']}")
|
||||
|
||||
# Run deep research
|
||||
with st.spinner("Performing 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:
|
||||
st.error(f"Deep research error: {str(e)}")
|
||||
return {"error": str(e), "success": False}
|
||||
|
||||
# Keep the original agents
|
||||
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
|
||||
"""
|
||||
)
|
||||
|
||||
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 explanations of complex concepts
|
||||
- Including relevant examples, case studies, and real-world applications
|
||||
- Expanding on key points with additional context and nuance
|
||||
- Adding visual elements descriptions (charts, diagrams, infographics)
|
||||
- Incorporating latest trends and future predictions
|
||||
- Suggesting practical implications for different stakeholders
|
||||
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
|
||||
"""
|
||||
)
|
||||
|
||||
# Attach the deep research tool to the research agent
|
||||
research_agent.tools.append(deep_research)
|
||||
|
||||
async def run_research_process(topic: str):
|
||||
"""Run the complete research process."""
|
||||
# Step 1: Initial Research
|
||||
with st.spinner("Conducting initial research..."):
|
||||
research_result = await Runner.run(research_agent, topic)
|
||||
initial_report = research_result.final_output
|
||||
|
||||
# Display initial report in an expander
|
||||
with st.expander("View Initial Research Report"):
|
||||
st.markdown(initial_report)
|
||||
|
||||
# Step 2: Enhance the 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_result = await Runner.run(elaboration_agent, elaboration_input)
|
||||
enhanced_report = elaboration_result.final_output
|
||||
|
||||
return enhanced_report
|
||||
|
||||
# Main research process
|
||||
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 API keys in the sidebar.")
|
||||
elif not research_topic:
|
||||
st.warning("Please enter a research topic.")
|
||||
else:
|
||||
try:
|
||||
# Create placeholder for the final report
|
||||
report_placeholder = st.empty()
|
||||
|
||||
# Run the research process
|
||||
enhanced_report = asyncio.run(run_research_process(research_topic))
|
||||
|
||||
# Display the enhanced report
|
||||
report_placeholder.markdown("## Enhanced Research Report")
|
||||
report_placeholder.markdown(enhanced_report)
|
||||
|
||||
# Add download button
|
||||
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 occurred: {str(e)}")
|
||||
|
||||
# Footer
|
||||
st.markdown("---")
|
||||
st.markdown("Powered by OpenAI Agents SDK and Firecrawl")
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
openai-agents
|
||||
firecrawl
|
||||
streamlit
|
||||
firecrawl-py
|
||||
Loading…
Reference in a new issue