项目背景与目的
来自著名 LLM/RAG/Agents 教育和开源项目作者 Shubham Saboo 的一篇教程「Build a Deep Research Agent with OpenAI Agents SDK and Firecrawl」介绍了一个名为 "Deep Research Agent" 的开源项目,它利用 OpenAI Agents SDK 和 Firecrawl 来创建一个强大的研究助手系统。
该项目旨在提供一个替代昂贵研究工具的解决方案,如 ChatGPT Pro(20/月)。作者提出了一个完全开源的方案,让用户可以构建自己的深度研究 Agent 系统,无需支付高额订阅费用。
技术架构
项目基于两个主要技术组件:
1. OpenAI Agents SDK :OpenAI 最新推出的轻量级 Agent 框架,用于构建具有专业 Agent 能力的 AI 应用,这些 Agent 可以协同工作。它提供了代理、交接和护栏等原语,使多个 AI 助手之间的任务协调变得容易。
OpenAI 官方文档:
https://platform.openai.com/docs/guides/agents-sdk
- Firecrawl:提供深度研究端点,使 Agent 能够自主探索网络、收集相关信息并将发现综合为全面的见解。
系统功能
这个多 Agent 研究系统包含两个专门的代理:
1. Research Agent(Agent 1) :利用 Firecrawl 的深度研究端点从多个网络源收集全面信息,作为数据收集者和初步综合者。
- Elaboration Agent(Agent 2):分析初始报告并将其转化为更全面的文档,添加额外的上下文、示例、案例研究和含义,充当内容增强者和专家编辑。
项目特点
· 多 Agent 架构:两个专业 Agent 协同工作
· 深度网络研究:自动搜索网络、提取内容和综合发现
· 增强分析:第二个 Agent 通过额外的上下文和见解来扩展初始研究
· 交互式UI:清晰的 Streamlit 界面,便于交互
· 可下载报告:将研究结果导出为 markdown 文件
实现步骤
教程提供了详细的实现步骤,包括:
-
设置开发环境(克隆GitHub仓库、安装依赖)
-
创建Streamlit应用程序
-
实现深度研究功能工具
-
创建两个代理(研究代理和深化代理)
-
定义多代理研究流程函数
-
实现主要研究按钮和处理逻辑
代码结构清晰,包含了完整的应用程序实现,从API配置到UI设计,再到多代理协作流程。
使用方法
用户需提供 OpenAI 和 Firecrawl 的 API Key,输入研究主题,点击"开始多 Agent 研究"按钮,系统将自动执行研究过程,并提供可下载的综合研究报告。项目为那些需要深度研究能力但不想支付高额订阅费用的用户提供了一个实用的开源解决方案,同时也展示了如何利用 OpenAI Agents SDK 和 Firecrawl 构建复杂的 AI 应用。
代码实现
设置环境
- 项目仓库克隆
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
- 找到项目目录
cd ai\_agent\_tutorials/ai\_deep\_research\_agent
- 安装依赖
pip install -r requirements.txt
构建主要逻辑和 Streamlit App
- import 主要依赖库
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
- 设置 Streamlit 和配置页面
# 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 = ""
- 为 API Key 输入设置一个 sidebar
# 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 multi-agent system performs comprehensive research using OpenAI Agents SDK and Firecrawl")
# Research topic input
research_topic = st.text_input("Enter your research topic:", placeholder="e.g., Latest developments in AI")
- 定义 Deep Research 方法工具
@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}
- 创建首个 Agent - Research Agent
# Create first agent in our multi-agent system - focused on gathering research
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
""",
tools=[deep_research]
)
- 创建第二个 Agent - Elaboration Agent
# Create second agent in our multi-agent system - focused on enhancing content
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
"""
)
- 定义多 Agent 流程
async def run_research_process(topic: str):
"""Run the complete multi-agent research process."""
# Step 1: Initial Research with first agent
with st.spinner("Agent 1: 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 (Agent 1 Output)"):
st.markdown(initial_report)
# Step 2: Enhance the report with second agent
with st.spinner("Agent 2: 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 Multi-Agent 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 multi-agent research process
enhanced_report = asyncio.run(run_research_process(research_topic))
# Display the enhanced report
report_placeholder.markdown("## Enhanced Research Report (Multi-Agent Output)")
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 - Your own Deep Research solution without the subscription fees")
运行 App
streamlit run deep\_research\_openai.py
在线教程地址
https://www.theunwindai.com/p/build-a-deep-research-agent-with-openai-agents-sdk-and-firecrawl
欢迎朋友们关注❤️和星标⭐️
