🔍 智能AI搜索Agent
一个基于大语言模型的智能搜索代理,能够理解用户查询意图、自动规划搜索策略、从多个来源获取信息,并生成高质量、有来源引用的答案。
✨ 功能特点
| 能力 | 描述 |
|---|---|
| 🧠 查询理解 | 分析用户意图,提取关键实体,生成扩展查询 |
| 📋 搜索规划 | 智能分解问题,制定搜索策略 |
| 🔎 多源搜索 | 支持Web搜索和新闻搜索 |
| 📄 内容提取 | 智能提取网页核心内容 |
| 🎯 结果排序 | 基于相关性重排搜索结果 |
| ✍️ 答案生成 | 综合信息生成结构化回答 |
| 🔄 自我反思 | 评估答案质量,决定是否迭代 |
🛠️ 技术栈
| 组件 | 选型 | 说明 |
|---|---|---|
| LLM | xchat52 (GPT-5.2) | 主推理引擎 |
| Web搜索 | Serper API | Google搜索代理 |
| 内容提取 | Jina Reader | 网页转Markdown |
| 重排序 | Jina Reranker | 结果相关性排序 |
| 框架 | Python原生 + asyncio | 异步高效执行 |
📁 项目结构
search_agent/
├── main.py # 程序入口
├── config.py # 配置管理
├── requirements.txt # Python依赖
├── .env # 环境变量配置
│
├── agent/
│ ├── __init__.py
│ ├── search_agent.py # 主Agent类
│ └── prompts.py # Prompt模板
│
├── modules/
│ ├── __init__.py
│ ├── query_analyzer.py # 查询理解模块
│ ├── search_planner.py # 搜索规划模块
│ ├── search_executor.py # 搜索执行模块
│ ├── content_extractor.py # 内容提取模块
│ ├── result_processor.py # 结果处理模块
│ ├── answer_generator.py # 答案生成模块
│ └── reflector.py # 反思迭代模块
│
├── tools/
│ ├── __init__.py
│ ├── serper.py # Serper API封装
│ ├── jina_reader.py # Jina Reader封装
│ └── jina_reranker.py # Jina Reranker封装
│
├── models/
│ ├── __init__.py
│ └── schemas.py # 数据模型定义
│
└── utils/
├── __init__.py
├── llm_client.py # LLM客户端
└── helpers.py # 工具函数
🚀 快速开始
1. 安装依赖
cd search_agent
pip install -r requirements.txt
2. 配置环境变量
创建 .env 文件:
# LLM配置 (xchat52)
LLM_BASE_URL=https://apis.openroutex.com/openai/deployments/xchat52
LLM_API_KEY=你的API密钥
LLM_MODEL=xchat52
# Serper配置 (Google搜索)
SERPER_API_KEY=你的Serper_API_KEY
# Jina配置 (内容提取和重排序)
JINA_API_KEY=你的Jina_API_KEY
# Agent配置
MAX_ITERATIONS=3 # 最大迭代次数
MAX_RESULTS_PER_QUERY=10 # 每次搜索返回结果数
CONTENT_MAX_LENGTH=5000 # 提取内容最大长度
# 日志配置
LOG_LEVEL=INFO
TIMEOUT=30
3. 运行程序
交互模式(推荐):
python main.py
单次查询:
python main.py "你的问题"
📖 使用示例
🔍 智能AI搜索Agent
======================================================================
输入您的问题进行搜索,输入 'quit' 或 'exit' 退出
======================================================================
🔎 请输入问题: 什么是大语言模型?
======================================================================
📝 答案:
======================================================================
## 大语言模型(LLM)是什么?
**大语言模型(Large Language Model, LLM)**是一类用**海量文本数据**进行
**预训练**的**超大规模深度学习模型**...
----------------------------------------------------------------------
📚 来源:
----------------------------------------------------------------------
[1] 大语言模型 (LLM)
🔗 https://www.ibm.com/cn-zh/think/topics/large-language-models
[2] 什么是 LLM(大型语言模型)?
🔗 https://aws.amazon.com/cn/what-is/large-language-model/
...
----------------------------------------------------------------------
📊 统计:
----------------------------------------------------------------------
• 置信度: high
• 迭代次数: 1
• 参考来源数: 10
• 搜索查询数: 3
======================================================================
🔄 工作流程
用户查询
│
▼
┌───────────────────┐
│ 查询理解 │ ──▶ 分析意图、提取实体、生成扩展查询
└───────────────────┘
│
▼
┌───────────────────┐
│ 搜索规划 │ ──▶ 制定搜索策略(Web/新闻、并行/串行)
└───────────────────┘
│
▼
┌───────────────────┐
│ 搜索执行 │ ──▶ 调用Serper API执行搜索
└───────────────────┘
│
▼
┌───────────────────┐
│ 内容提取 │ ──▶ 使用Jina Reader提取网页内容
└───────────────────┘
│
▼
┌───────────────────┐
│ 结果处理 │ ──▶ 去重 + Jina Reranker重排序
└───────────────────┘
│
▼
┌───────────────────┐
│ 答案生成 │ ──▶ LLM综合生成结构化答案
└───────────────────┘
│
▼
┌───────────────────┐
│ 反思评估 │ ──▶ 评估完整性,决定是否继续迭代
└───────────────────┘
│
├──(完整)──▶ 返回最终答案
│
└──(不完整)──▶ 补充搜索(回到搜索规划)
⚙️ 配置说明
| 配置项 | 默认值 | 说明 |
|---|---|---|
MAX_ITERATIONS |
3 | 最大迭代次数,防止无限循环 |
MAX_RESULTS_PER_QUERY |
10 | 每次搜索返回的结果数量 |
CONTENT_MAX_LENGTH |
5000 | 提取内容的最大字符数 |
LOG_LEVEL |
INFO | 日志级别 (DEBUG/INFO/WARNING/ERROR) |
TIMEOUT |
30 | API请求超时时间(秒) |
🔧 API说明
Serper API
- Web搜索:
POST https://google.serper.dev/search - 新闻搜索:
POST https://google.serper.dev/news - 获取API Key
Jina API
- 内容提取:
GET https://r.jina.ai/{URL} - 重排序:
POST https://api.jina.ai/v1/rerank - 获取API Key
LLM API (Azure OpenAI风格)
- Chat:
POST {BASE_URL}/chat/completions?api-version=2024-10-21
📝 编程接口
import asyncio
from config import Config
from agent.search_agent import SearchAgent
async def main():
# 加载配置
config = Config.from_env()
# 创建Agent
agent = SearchAgent(config)
# 执行搜索
response = await agent.search("你的问题")
# 获取答案
print(response.answer.content)
print(response.answer.sources)
print(response.answer.confidence)
asyncio.run(main())
📄 License
MIT License
🤝 贡献
欢迎提交Issue和Pull Request!