Skip to content

Repository files navigation

大模型联网模块 (LLM Web Search Module)

零成本 AI 联网搜索:智谱免费 LLM + 360 搜索直接抓取,无需任何付费 API Key。

架构

User Query
    │
    ▼
┌──────────────────────┐
│ ① need_search        │  规则引擎 (<1ms) + LLM 兜底
│   需要搜索吗?         │  18条正则覆盖 85% 场景
└────────┬─────────────┘
         │ yes
         ▼
┌──────────────────────┐
│ ② rewrite_query      │  LLM 改写 + 硬规则后处理
│   优化搜索关键词      │  年份自动后移、去冗余问词
└────────┬─────────────┘
         │
         ▼
┌──────────────────────┐
│ ③ search_and_fetch   │  360 / Bing 直接抓取
│   搜索 & 抓取正文     │  冷门实体名自动回退
└────────┬─────────────┘
         │
         ▼
┌──────────────────────┐
│ ④ summarize          │  LLM 结构化总结
│   生成答案+来源       │  置信度自评 + 引用标注
└────────┬─────────────┘
         │
         ▼
    Final Answer

实测性能

指标 数值
平均耗时 ~8s
搜索费用 ¥0
LLM 费用 ¥0(智谱免费模型)
冷门实体 自动回退,不掉链子
中文分词 360 搜索原生支持

文件结构

llm_web_module/
├── config.py           # 统一配置 (LLM / Search / 抓取)
├── need_search.py      # ① 联网判断 (规则 + LLM)
├── query_rewriter.py   # ② Query 改写 (LLM + 硬规则后处理)
├── crawler.py          # ③ 搜索 + 爬虫 (360 / Bing 免费)
├── summarizer.py       # ④ LLM 总结 (结构化输出)
├── pipeline.py          # 完整流水线
├── hybrid_pipeline.py   # 混合流水线 (规则优先)
├── unified_judge.py     # 统一判断 (一步到位)
├── demo_judge.py        # 规则引擎演示
├── test_run.py          # 端到端测试
├── main.py              # 入口 & 示例
└── README.md

快速开始

1. 安装依赖

pip install -r requirements.txt

2. 设置 API Key

复制模板,填入你的智谱 API Key(免费获取):

cp .env.example .env
# 编辑 .env: ZHIPU_API_KEY=你的key

3. 运行

# 规则引擎演示 (无需 API key)
python demo_judge.py

# 端到端测试
python test_run.py

代码调用

import asyncio
from config import ModuleConfig, LLMConfig, SearchConfig
from pipeline import run_pipeline

async def main():
    config = ModuleConfig(
        llm=LLMConfig(
            api_key="你的智谱key",
            model="glm-4-flashx",   # 免费,最快
        ),
        search=SearchConfig(
            provider="360",          # 360 搜索,中文分词最好
            max_results=10,          # 搜索结果数
            fetch_content=2,         # 抓取前2条详情页,0=不抓
        ),
        verbose=True,
    )

    result = await run_pipeline("今天比特币价格?", config)
    print(result.display())

asyncio.run(main())

搜索后端

Provider 需要 Key 中文分词 说明
360 ⭐⭐⭐⭐⭐ 默认,中文最好
bing_free ⭐⭐ 冷门实体需回退
bing 🔑 ⭐⭐⭐ Azure API,1000次/月免费
serpapi 🔑 ⭐⭐⭐⭐ Google 结果,付费

冷门实体名自动回退:当 cn.bing.com 把"连淮伟"拆成"连"+"淮伟"时,自动回退到只搜实体名,再不行用双词组合。360 搜索不触发此逻辑(分词本身就好)。

LLM 后端

Provider 模型 费用 速度
智谱 glm-4-flashx 免费
智谱 glm-4-flash 免费 中等
OpenAI gpt-4o-mini 付费
任意兼容接口

配置中 api_base 支持任意 OpenAI 兼容接口。

设计思想

  1. 零成本优先:免 API Key 搜索 + 免费 LLM,全流程不花钱
  2. 渐进式增强:规则 → LLM,成本逐级递增
  3. 可插拔:每个模块独立,换搜索/换模型只需改配置
  4. 透明可观测:verbose 模式打印每步决策和耗时
  5. 容错:搜索失败自动回退,LLM 输出异常 fallback 兜底

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages