9.4 AI 搜索
AI 搜索正在取代传统搜索引擎——从「返回链接列表」到「直接回答问题并附带引用」的范式转变。
AI 搜索 vs 传统搜索 vs RAG
传统搜索(Google):
用户输入关键词 → 返回网页链接列表 → 用户自己阅读和总结
✅ 信息全面 ❌ 需要自己筛选和理解
RAG(私有知识库):
用户提问 → 从私有文档中检索 → LLM 生成答案
✅ 私有数据 ❌ 仅限内部知识
AI 搜索(Perplexity / SearchGPT):
用户提问 → 实时搜索互联网 → LLM 综合多个来源生成答案 + 引用
✅ 实时 + 综合 ❌ 公开数据为主| 维度 | 传统搜索 | RAG | AI 搜索 |
|---|---|---|---|
| 数据来源 | 公开网页 | 私有文档 | 公开网页 + 实时 |
| 输出形式 | 链接列表 | 生成式回答 | 生成式回答 + 引用 |
| 实时性 | 高(爬虫索引) | 取决于更新频率 | 高(实时搜索) |
| 准确性 | 看用户筛选能力 | 取决于文档质量 | 中高(有幻觉风险) |
| 隐私 | 无隐私保障 | 数据自控 | 查询可能被记录 |
主流 AI 搜索产品
| 产品 | 提供商 | 特点 | API 可用 |
|---|---|---|---|
| Perplexity | Perplexity AI | AI 搜索先驱,引用清晰,Pro Search 深度模式 | ✅ pplx API |
| SearchGPT | OpenAI | 集成在 ChatGPT 中,GPT 模型驱动 | ✅ web_search 工具 |
| Gemini Search | 原生搜索能力,Google 索引 | ✅ Grounding | |
| Kimi 搜索 | 月之暗面 | 国内领先,中文搜索质量高 | ✅ API |
| 秘塔搜索 | 秘塔科技 | 国内免费,学术搜索出色 | ❌ |
开发者如何集成 AI 搜索
1. OpenAI Web Search(推荐)
python
from openai import OpenAI
client = OpenAI()
# GPT-4o 内置搜索工具
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "2026 年最新的 MCP 协议进展"}],
tools=[{"type": "web_search_preview"}] # 启用搜索
)
print(response.choices[0].message.content)
# 返回带引用的回答2. Perplexity API
python
from openai import OpenAI
# Perplexity 兼容 OpenAI 格式
pplx = OpenAI(
api_key="pplx-xxxxx",
base_url="https://api.perplexity.ai"
)
response = pplx.chat.completions.create(
model="sonar-pro", # 搜索增强模型
messages=[
{"role": "system", "content": "请用中文回答,附带引用来源"},
{"role": "user", "content": "FastAPI 最新版本有哪些新特性?"}
]
)
print(response.choices[0].message.content)
# 自动搜索互联网并生成带引用的回答3. Google Gemini Grounding
python
import google.generativeai as genai
genai.configure(api_key="your-gemini-key")
model = genai.GenerativeModel("gemini-2.0-flash")
# 启用 Google Search Grounding
response = model.generate_content(
"比较 pgvector 和 Milvus 的最新性能测试结果",
tools=[{"google_search": {}}] # 启用搜索
)
print(response.text)
# 基于 Google 搜索结果生成回答4. 自建 AI 搜索(搜索 + RAG 混合)
python
import httpx
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def ai_search(query: str) -> str:
"""自建 AI 搜索:Brave Search + LLM 综合"""
# Step 1: 搜索互联网
async with httpx.AsyncClient() as http:
search_resp = await http.get(
"https://api.search.brave.com/res/v1/web/search",
headers={"X-Subscription-Token": "your-brave-key"},
params={"q": query, "count": 5}
)
results = search_resp.json().get("web", {}).get("results", [])
# Step 2: 提取搜索结果摘要
context = "\n\n".join([
f"[{i+1}] {r['title']}\n{r['description']}\nURL: {r['url']}"
for i, r in enumerate(results)
])
# Step 3: LLM 综合回答
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": """基于以下搜索结果回答用户问题。
要求:
1. 综合多个来源的信息
2. 在关键信息后标注引用 [1] [2] 等
3. 如果搜索结果不足以回答,说明需要更多信息"""},
{"role": "user", "content": f"搜索结果:\n{context}\n\n问题:{query}"}
]
)
return response.choices[0].message.content
# 使用
answer = await ai_search("LangGraph 和 CrewAI 哪个更适合多 Agent 开发?")
print(answer)AI 搜索 vs RAG:何时用哪个?
需要查询公开信息、最新动态? → AI 搜索
例:最新技术趋势、竞品分析、新闻
需要查询私有/内部文档? → RAG
例:公司知识库、产品文档、内部 Wiki
需要两者结合? → RAG + AI 搜索 混合
例:客服系统(先查内部知识库,查不到再搜外网)python
async def hybrid_search(query: str) -> str:
"""混合搜索:先查私有知识库,不够再搜外网"""
# Step 1: RAG 检索
rag_results = await rag_service.retrieve(query, top_k=3)
if rag_results and rag_results[0].score > 0.85:
# 内部知识库有高置信度结果
return await generate_answer(query, rag_results, source="内部知识库")
# Step 2: 内部知识不够,搜索外网补充
web_results = await ai_search(query)
# Step 3: 合并两个来源
combined = rag_results + web_results
return await generate_answer(query, combined, source="混合检索")学习资源