Hybrid Deep Search 🚀
Intelligent three-tier search system that automatically routes queries between Brave API and OpenAI Codex for optimal cost and quality.
Architecture
CODEBLOCK0
Quick Start
1. Install Dependencies
CODEBLOCK1
2. Configure API Keys
Brave API (Already Built-in)
No extra configuration needed. Uses OpenClaw's built-in
web_search tool.
OpenAI Codex API (Optional for Deep Search)
CODEBLOCK2
Usage
Auto Mode (Recommended)
CODEBLOCK3
Manual Mode
CODEBLOCK4
Focus Modes
CODEBLOCK5
Parameters
| Parameter | Description | Options | Default |
|---|
| query | Search query | Any text | - |
| --mode |
Search mode |
quick,
codex,
auto |
auto |
| --focus | Search focus |
web,
academic,
news,
youtube |
web |
| --max-results | Max results | 1-20 | 10 |
| --verbose | Verbose output | - | false |
Complexity Routing Rules
Auto mode routes based on query analysis:
→ Brave API (quick)
- - Simple factual queries (what/who/when/where)
- Definition lookups
- Quick fact-finding
- Single-topic searches
Examples:
- - "what is OpenClaw?"
- "who created Python?"
- "latest AI news today"
→ OpenAI Codex (codex)
- - Comparison analysis
- Deep reasoning
- Multi-source synthesis
- Complex questions
- Requires inference/summarization
Examples:
- - "compare LangChain vs LlamaIndex in detail"
- "analyze impact of AI on job market"
- "explain quantum computing applications in healthcare"
Cost Optimization
Brave API
- - ✅ Completely free
- ⚡ Fast response (<2s)
- 📊 Controllable result count
OpenAI Codex (gpt-5-codex)
- - 💰 Usage-based pricing
- 🧠 Deep reasoning capabilities
- ⏱️ Slower response (5-30s)
- 💡 Free tier available for new users
Recommendation: Use auto mode to let the system optimize costs for you.
Technical Details
Query Analyzer (router.py)
Analyzes query complexity using NLP rules:
- - Keyword detection (compare/analyze/explain...)
- Sentence length
- Complexity scoring
- Automatic routing decisions
Brave API Integration
Uses OpenClaw's built-in
web_search tool:
- - Called via Bash tool
- Handles requests automatically
- No extra authentication needed
OpenAI Codex Integration
- - Uses gpt-5-codex model
- Built-in web search tool
- OpenAI Chat Completions API format
Example Use Cases
Case 1: Quick Fact Lookup
CODEBLOCK6
Case 2: Deep Analysis
CODEBLOCK7
Case 3: Academic Research
CODEBLOCK8
Advanced Usage
Batch Search
CODEBLOCK9
Format Output
CODEBLOCK10
Troubleshooting
Brave API Not Responding
CODEBLOCK11
OpenAI Codex Authentication Failed
CODEBLOCK12
Python Dependencies
CODEBLOCK13
Complexity Scoring System
Scoring Factors (0-10 total)
- 1. Keyword Matching (+6)
- compare/analyze/explain/why/how...
- 2. Query Length (+2)
- >15 words = +2
- >8 words = +1
- 3. Question Pattern (+1)
- Complex wh-questions
- 4. Technical Terms (+1)
- API/framework/architecture...
- 5. Simple Keyword Penalty (-2)
- what is/who is/list of...
Decision Thresholds
- - 0-2 score: Brave API (quick)
- 3+ score: OpenAI Codex (codex)
Configuration
Config File
CODEBLOCK14
Example config.json:
CODEBLOCK15
Credits
- - Built by Office_bot
- Powered by OpenClaw
References
混合深度搜索 🚀
智能三层搜索系统,可在Brave API和OpenAI Codex之间自动路由查询,实现成本与质量的最优化。
架构
用户查询
↓
查询分析器 (router.py)
↓
├─→ 简单问题 → Brave API (web_search) 快速、免费
├─→ 复杂问题 → OpenAI Codex (gpt-5-codex) 深度分析、付费
└─→ 手动模式 → 用户指定
快速开始
1. 安装依赖
bash
pip install openai python-dotenv requests
2. 配置API密钥
Brave API(已内置)
无需额外配置。使用OpenClaw内置的web_search工具。
OpenAI Codex API(深度搜索可选)
bash
从以下地址获取API密钥:https://platform.openai.com/api-keys
export OPENAIAPIKEY=sk-your-openai-api-key
export OPENAIBASEURL=https://api.openai.com/v1 # 可选
使用方法
自动模式(推荐)
bash
python3 scripts/deep_search.py 你的查询
系统自动分析复杂度并选择:
- 简单问题 → Brave API
- 复杂问题 → OpenAI Codex
手动模式
bash
快速搜索(Brave API)
python3 scripts/deep_search.py 什么是OpenClaw? --mode quick
深度搜索(OpenAI Codex)
python3 scripts/deep_search.py 比较LangChain与LlamaIndex --mode codex
聚焦模式
bash
学术搜索
python3 scripts/deep_search.py AI智能体框架研究 --mode codex --focus academic
新闻搜索
python3 scripts/deep_search.py 最新AI新闻 --mode quick --focus news
通用网页搜索
python3 scripts/deep_search.py OpenClaw文档 --mode quick --focus web
参数
| 参数 | 描述 | 选项 | 默认值 |
|---|
| query | 搜索查询 | 任意文本 | - |
| --mode |
搜索模式 | quick, codex, auto | auto |
| --focus | 搜索聚焦 | web, academic, news, youtube | web |
| --max-results | 最大结果数 | 1-20 | 10 |
| --verbose | 详细输出 | - | false |
复杂度路由规则
自动模式基于查询分析进行路由:
→ Brave API (quick)
- - 简单事实查询(什么/谁/何时/何地)
- 定义查找
- 快速事实检索
- 单一主题搜索
示例:
- - 什么是OpenClaw?
- 谁创造了Python?
- 今日最新AI新闻
→ OpenAI Codex (codex)
- - 比较分析
- 深度推理
- 多源综合
- 复杂问题
- 需要推理/总结
示例:
- - 详细比较LangChain与LlamaIndex
- 分析AI对就业市场的影响
- 解释量子计算在医疗领域的应用
成本优化
Brave API
- - ✅ 完全免费
- ⚡ 快速响应(<2秒)
- 📊 结果数量可控
OpenAI Codex (gpt-5-codex)
- - 💰 按使用量计费
- 🧠 深度推理能力
- ⏱️ 响应较慢(5-30秒)
- 💡 新用户有免费额度
建议: 使用自动模式,让系统为您优化成本。
技术细节
查询分析器 (router.py)
使用NLP规则分析查询复杂度:
- - 关键词检测(比较/分析/解释...)
- 句子长度
- 复杂度评分
- 自动路由决策
Brave API集成
使用OpenClaw内置的web_search工具:
OpenAI Codex集成
- - 使用gpt-5-codex模型
- 内置网页搜索工具
- OpenAI Chat Completions API格式
使用案例
案例1:快速事实查询
bash
python3 scripts/deep_search.py OpenClaw 2026版本
→ 自动使用Brave API
→ 结果:快速返回,免费
案例2:深度分析
bash
python3 scripts/deep_search.py AI智能体架构综合分析
→ 自动使用OpenAI Codex
→ 结果:深度分析,多源综合
案例3:学术研究
bash
python3 scripts/deep_search.py 多智能体系统最新论文 --mode codex --focus academic
→ 使用OpenAI Codex
→ 结果:聚焦学术文献
高级用法
批量搜索
bash
创建queries.txt
echo 查询1 >> queries.txt
echo 查询2 >> queries.txt
批量执行
for query in $(cat queries.txt); do
python3 scripts/deep_search.py $query --mode auto
done
格式化输出
bash
JSON输出
python3 scripts/deep_search.py 查询 --format json
Markdown输出(默认)
python3 scripts/deep_search.py 查询 --format markdown
纯文本输出
python3 scripts/deep_search.py 查询 --format text
故障排除
Brave API无响应
bash
检查OpenClaw web_search工具
无需额外配置
OpenAI Codex认证失败
bash
检查环境变量
echo $OPENAI
APIKEY
重置
export OPENAI
APIKEY=sk-...
Python依赖问题
bash
pip install --upgrade openai python-dotenv requests
复杂度评分系统
评分因素(总分0-10)
- 1. 关键词匹配 (+6)
- 比较/分析/解释/为什么/如何...
- 2. 查询长度 (+2)
- >15个词 = +2
- >8个词 = +1
- 3. 问题模式 (+1)
- 复杂wh-问题
- 4. 技术术语 (+1)
- API/框架/架构...
- 5. 简单关键词惩罚 (-2)
- 什么是/谁是/列表...
决策阈值
- - 0-2分:Brave API (quick)
- 3分及以上:OpenAI Codex (codex)
配置
配置文件
bash
cp config.json.example config.json
编辑config.json设置你的参数
config.json示例:
json
{
search_settings: {
default_mode: auto,
default_focus: web,
max_results: 10,
router_threshold: 3,
verbose: false
},
openai_codex: {
enabled: true,
apikey: YOUROPENAIAPIKEY_HERE,
base_url: https://api.openai.com/v1,
model: gpt-5-codex,
max_tokens: 4096,
temperature: 0.7
}
}
致谢
- - 由 Office_bot 构建
- 由 OpenClaw 提供支持
参考