Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 24 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
## 特性

- **无框架设计**: 从零实现,不依赖LangChain等重型框架
- **多LLM支持**: 支持DeepSeek、OpenAI等主流大语言模型
- **多LLM支持**: 支持各种模型服务商提供的LLM,如DeepSeek、OpenAI、MiniMax等
- **智能搜索**: 集成Tavily搜索引擎,提供高质量网络搜索
- **反思机制**: 多轮反思优化,确保研究深度和完整性
- **状态管理**: 完整的研究过程状态跟踪和恢复
Expand Down Expand Up @@ -66,40 +66,35 @@ python --version

```bash
git clone <your-repo-url>
cd Demo\ DeepSearch\ Agent
cd DeepSearchAgent-Demo
```

### 3. 安装依赖

```bash
# 激活虚拟环境(推荐)
conda activate pytorch_python11 # 或者使用其他虚拟环境
# 使用conda创建并激活虚拟环境
conda create -n deepresearch python=3.11 -y
conda activate deepresearch

# 安装依赖
pip install -r requirements.txt
```

### 4. 配置API密钥

项目根目录下已有`config.py`配置文件,请直接编辑此文件设置您的API密钥
请复制项目根目录下的`config.py.example`配置文件改名为`config.py`,请直接编辑此文件设置您使用服务商的base url,API Key,模型名称以及Tavily搜索API Key

```python
# Deep Search Agent 配置文件
# 请在这里填入您的API密钥

# DeepSeek API Key
DEEPSEEK_API_KEY = "your_deepseek_api_key_here"
# 请在这里填入服务商的Base URL、API Key和模型名称
BASE_URL = ""
API_KEY = ""
MODEL_NAME = ""

# OpenAI API Key (可选)
OPENAI_API_KEY = "your_openai_api_key_here"
# 请在这里填入Tavily搜索API Key,前往https://app.tavily.com/home获取
TAVILY_API_KEY = ""

# Tavily搜索API Key
TAVILY_API_KEY = "your_tavily_api_key_here"

# 配置参数
DEFAULT_LLM_PROVIDER = "deepseek"
DEEPSEEK_MODEL = "deepseek-chat"
OPENAI_MODEL = "gpt-4o-mini"

MAX_REFLECTIONS = 2
SEARCH_RESULTS_PER_QUERY = 3
Expand Down Expand Up @@ -167,15 +162,15 @@ from src import DeepSearchAgent, Config

# 自定义配置
config = Config(
default_llm_provider="deepseek",
deepseek_model="deepseek-chat",
max_reflections=3, # 增加反思次数
max_search_results=5, # 增加搜索结果数
output_dir="my_reports" # 自定义输出目录
)

# 设置API密钥
config.deepseek_api_key = "your_api_key"
config.base_url = "base_url"
config.api_key = "your_api_key"
config.model = "model_name"
config.tavily_api_key = "your_tavily_key"

agent = DeepSearchAgent(config)
Expand All @@ -188,8 +183,7 @@ Demo DeepSearch Agent/
├── src/ # 核心代码
│ ├── llms/ # LLM调用模块
│ │ ├── base.py # LLM基类
│ │ ├── deepseek.py # DeepSeek实现
│ │ └── openai_llm.py # OpenAI实现
│ │ ├── llm.py # 通用LLM实现
│ ├── nodes/ # 处理节点
│ │ ├── base_node.py # 节点基类
│ │ ├── report_structure_node.py # 结构生成
Expand Down Expand Up @@ -341,15 +335,12 @@ class DeepSearchAgent:

```python
class Config:
# API密钥
deepseek_api_key: Optional[str]
openai_api_key: Optional[str]
tavily_api_key: Optional[str]
"""配置类"""
base_url: Optional[str] = None
api_key: Optional[str] = None
tavily_api_key: Optional[str] = None

# 模型配置
default_llm_provider: str = "deepseek"
deepseek_model: str = "deepseek-chat"
openai_model: str = "gpt-4o-mini"
model: str = ""

# 搜索配置
max_search_results: int = 3
Expand Down Expand Up @@ -412,16 +403,6 @@ print(f"研究进度: {progress['progress_percentage']}%")

## 高级功能

### 多模型支持

```python
# 使用DeepSeek
config = Config(default_llm_provider="deepseek")

# 使用OpenAI
config = Config(default_llm_provider="openai", openai_model="gpt-4o")
```

### 自定义输出

```python
Expand All @@ -435,17 +416,15 @@ config = Config(

### Q: 支持哪些LLM?

A: 目前支持:
- **DeepSeek**: 推荐使用,性价比高
- **OpenAI**: GPT-4o、GPT-4o-mini等
- 可以通过继承`BaseLLM`类轻松添加其他模型
A: 支持各种模型服务商提供的LLM,如DeepSeek、OpenAI、MiniMax等。只需在配置文件中设置相应的Base URL、API Key和模型名称即可。

### Q: 如何获取API密钥?

A:
- **DeepSeek**: 访问 [DeepSeek平台](https://platform.deepseek.com/) 注册获取
- **Tavily**: 访问 [Tavily](https://tavily.com/) 注册获取(每月1000次免费)
- **OpenAI**: 访问 [OpenAI平台](https://platform.openai.com/) 获取
- **MiniMax**: 访问 [MiniMax AI](https://platform.minimaxi.com/user-center/basic-information/interface-key) 获取

获取密钥后,直接编辑项目根目录的`config.py`文件填入即可。

Expand Down Expand Up @@ -481,9 +460,8 @@ A: 当前主要支持Tavily,但可以通过修改`src/tools/search.py`添加

## 致谢

- 感谢 [DeepSeek](https://www.deepseek.com/) 提供优秀的LLM服务
- 感谢 [DeepSeek](https://www.deepseek.com/) [MiniMax](https://minimaxi.com/) 提供优秀的LLM服务
- 感谢 [Tavily](https://tavily.com/) 提供高质量的搜索API

---

如果这个项目对您有帮助,请给个Star!
16 changes: 16 additions & 0 deletions config.py.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Deep Search Agent 配置文件

# 请在这里填入服务商的Base URL、API Key和模型名称
BASE_URL = ""
API_KEY = ""
MODEL_NAME = ""

# 请在这里填入Tavily搜索API Key,前往https://app.tavily.com/home获取
TAVILY_API_KEY = ""


MAX_REFLECTIONS = 2
SEARCH_RESULTS_PER_QUERY = 3
SEARCH_CONTENT_MAX_LENGTH = 20000
OUTPUT_DIR = "reports"
SAVE_INTERMEDIATE_STATES = True
52 changes: 30 additions & 22 deletions examples/advanced_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from src import DeepSearchAgent, Config
from src import DeepSearchAgent, Config, load_config
from src.utils.config import print_config


Expand All @@ -22,22 +22,30 @@ def advanced_example():
try:
# 自定义配置
print("正在创建自定义配置...")
config = Config(
# 使用OpenAI而不是DeepSeek
default_llm_provider="openai",
openai_model="gpt-4o-mini",
# 自定义搜索参数
max_search_results=5, # 更多搜索结果
max_reflections=3, # 更多反思次数
max_content_length=15000,
# 自定义输出
output_dir="custom_reports",
save_intermediate_states=True
)

config = load_config()
config.max_search_results = 5
config.max_reflections = 3
config.max_content_length = 15000
config.output_dir = "custom_reports"
config.save_intermediate_states = True
# config = Config(
# # 填入您使用的模型服务商的 base url, api key和模型名称
# # base_url="...",
# # api_key="", # 您的API密钥
# # model="...",
# # 自定义搜索参数
# max_search_results=5, # 更多搜索结果
# max_reflections=3, # 更多反思次数
# max_content_length=15000,
# # 自定义输出
# output_dir="custom_reports",
# save_intermediate_states=True
# )
# 从环境变量设置API密钥
config.openai_api_key = os.getenv("OPENAI_API_KEY")
config.tavily_api_key = os.getenv("TAVILY_API_KEY")
if not config.api_key:
config.api_key = os.getenv("API_KEY")
if not config.tavily_api_key:
config.tavily_api_key = os.getenv("TAVILY_API_KEY")

if not config.validate():
print("配置验证失败,请检查API密钥设置")
Expand All @@ -51,9 +59,9 @@ def advanced_example():

# 执行多个研究任务
queries = [
"深度学习在医疗领域的应用",
"区块链技术的最新发展",
"可持续能源技术趋势"
"深度学习在三维重建领域的应用",
# "中国基座大模型的最新发展",

]

for i, query in enumerate(queries, 1):
Expand Down Expand Up @@ -96,22 +104,22 @@ def state_management_example():

try:
# 创建配置
config = Config.from_env()
config = load_config()
if not config.validate():
print("配置验证失败")
return

# 创建Agent
agent = DeepSearchAgent(config)

query = "量子计算的发展现状"
query = "SAM模型的技术细节与应用"
print(f"开始研究: {query}")

# 执行研究
final_report = agent.research(query)

# 保存状态
state_file = "custom_reports/quantum_computing_state.json"
state_file = "custom_reports/sam_model_state.json"
agent.save_state(state_file)
print(f"状态已保存到: {state_file}")

Expand Down
15 changes: 2 additions & 13 deletions src/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime
from typing import Optional, Dict, Any, List

from .llms import DeepSeekLLM, OpenAILLM, BaseLLM
from .llms import LLM, BaseLLM
from .nodes import (
ReportStructureNode,
FirstSearchNode,
Expand Down Expand Up @@ -52,18 +52,7 @@ def __init__(self, config: Optional[Config] = None):

def _initialize_llm(self) -> BaseLLM:
"""初始化LLM客户端"""
if self.config.default_llm_provider == "deepseek":
return DeepSeekLLM(
api_key=self.config.deepseek_api_key,
model_name=self.config.deepseek_model
)
elif self.config.default_llm_provider == "openai":
return OpenAILLM(
api_key=self.config.openai_api_key,
model_name=self.config.openai_model
)
else:
raise ValueError(f"不支持的LLM提供商: {self.config.default_llm_provider}")
return LLM(self.config.api_key, self.config.base_url, self.config.model)

def _initialize_nodes(self):
"""初始化处理节点"""
Expand Down
5 changes: 2 additions & 3 deletions src/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""

from .base import BaseLLM
from .deepseek import DeepSeekLLM
from .openai_llm import OpenAILLM
from .llm import LLM

__all__ = ["BaseLLM", "DeepSeekLLM", "OpenAILLM"]
__all__ = ["BaseLLM", "LLM"]
13 changes: 2 additions & 11 deletions src/llms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class BaseLLM(ABC):
"""LLM基础抽象类"""

def __init__(self, api_key: str, model_name: Optional[str] = None):
def __init__(self, api_key: str, base_url: Optional[str] = None, model_name: Optional[str] = None):
"""
初始化LLM客户端

Expand All @@ -19,6 +19,7 @@ def __init__(self, api_key: str, model_name: Optional[str] = None):
model_name: 模型名称,如果不指定则使用默认模型
"""
self.api_key = api_key
self.base_url = base_url
self.model_name = model_name

@abstractmethod
Expand All @@ -36,16 +37,6 @@ def invoke(self, system_prompt: str, user_prompt: str, **kwargs) -> str:
"""
pass

@abstractmethod
def get_default_model(self) -> str:
"""
获取默认模型名称

Returns:
默认模型名称
"""
pass

def validate_response(self, response: str) -> str:
"""
验证和清理响应内容
Expand Down
Loading