大模型实战:构建 AI Agent 与应用案例

提示:下面的架构图使用 Mermaid 绘制,您可以在支持的 Markdown 渲染器中直接查看。

graph TD
    User[用户请求] --> Prompt[构造 Prompt]
    Prompt --> Agent[AI Agent]
    Agent --> Tool1[Calculator]
    Agent --> Tool2[Web Search]
    Tool1 & Tool2 --> Agent
    Agent --> LLM[LLM 推理]
    LLM --> Response[生成答案]
    Response --> User

关键概念

  • Agent:能够根据需求动态决定使用哪些工具(如计算器、搜索、数据库)并调用它们的系统。
  • Tool Use:通过统一的接口向外部服务发送请求,获取结果后继续推理。
  • LangChain:一个开源框架,提供了 AgentExecutorTool 等抽象,帮助快速搭建具备工具调用能力的 Agent。

环境准备


_10
pip install langchain openai faiss-cpu sentence-transformers transformers torch

注意:这里使用 OpenAI 的 gpt-3.5-turbo 作为 LLM 示例,您可以替换为本地模型(如 Llama‑2)并自行实现对应的 ChatModel 接口。

示例代码:构建一个可以计算并搜索的 Agent


_42
from langchain.agents import initialize_agent, Tool
_42
from langchain.llms import OpenAI
_42
import math
_42
import requests
_42
from bs4 import BeautifulSoup
_42
_42
# 1. 定义工具函数
_42
def calculator(query: str) -> str:
_42
"""安全计算表达式,例如 '2+3*4'"""
_42
try:
_42
# 仅允许数学表达式,防止代码注入
_42
allowed_chars = set("0123456789.+-*/() ")
_42
if not set(query).issubset(allowed_chars):
_42
return "仅支持基本算术表达式"
_42
result = eval(query, {"__builtins__": {}}, {})
_42
return str(result)
_42
except Exception as e:
_42
return f"计算错误: {e}"
_42
_42
def web_search(query: str) -> str:
_42
"""使用 Bing 搜索 API(示例),返回前 3 条摘要"""
_42
# 这里使用公开的 DuckDuckGo HTML 页面作为示例(无需 API key)
_42
url = f"https://duckduckgo.com/html/?q={query}"
_42
resp = requests.get(url, timeout=5)
_42
soup = BeautifulSoup(resp.text, "html.parser")
_42
results = []
_42
for a in soup.select('.result__a')[:3]:
_42
results.append(a.get_text())
_42
return "\n".join(results) if results else "未找到结果"
_42
_42
# 2. 包装为 LangChain Tool 对象
_42
calculator_tool = Tool(name="calculator", func=calculator, description="执行基本算术运算,例如 2+3*4")
_42
search_tool = Tool(name="web_search", func=web_search, description="在网络上搜索信息,返回简短摘要")
_42
_42
# 3. 初始化 LLM(这里使用 OpenAI)
_42
llm = OpenAI(model="gpt-3.5-turbo", temperature=0)
_42
_42
# 4. 创建 Agent(使用 zero-shot-react-description 策略)
_42
agent = initialize_agent([calculator_tool, search_tool], llm, agent="zero-shot-react-description", verbose=True)
_42
_42
# 5. 运行示例交互
_42
print(agent.run("请帮我计算 12 除以 0.75,然后把结果和最近的 AI 发展新闻一起写成一段中文摘要。"))

代码说明

  • calculator 只允许基本算术,防止恶意代码执行。
  • web_search 使用 DuckDuckGo 的公开搜索页面,演示如何抓取简短摘要(实际项目可接入官方搜索 API)。
  • initialize_agent 采用 Zero‑Shot ReAct 策略,Agent 会自行决定何时调用工具并将结果反馈给 LLM 继续推理。

实战案例:构建一个 “AI 办公助理”

  1. 需求:用户可以让助理完成计算、查询天气、获取最新新闻等任务。
  2. 实现:在上述基础上再添加 weathernews 两个工具(调用公开的天气 API 与新闻 RSS),并使用 ChatOpenAI(或本地模型)进行对话。
  3. 部署:将代码包装为 FastAPI 接口,前端使用 Next.js 调用 /api/agent,实现即时交互。

_10
# FastAPI 示例(省略 import)
_10
app = FastAPI()
_10
_10
@app.post("/api/agent")
_10
async def chat(request: Request):
_10
data = await request.json()
_10
user_query = data["message"]
_10
answer = agent.run(user_query)
_10
return {"answer": answer}

小结

  • 使用 LangChain 可以在几行代码内让 LLM 具备工具调用能力,构建强大的 AI Agent。
  • 通过组合不同工具,几乎可以实现任何业务需求:计算、搜索、数据库查询、文件操作等。
  • 关键在于 安全(限制工具输入)和 可解释性(记录每一步的工具调用),便于调试和审计。

后续:探索 自定义工具(如调用内部微服务)以及 多模态 Agent(结合图像、音频),进一步提升 AI 助手的能力范围。

Discussion4

Join the conversation

Sign in to share your thoughts and connect with others.

Sign In with GitHub
Michael Chang
Michael Chang·20h ago
The section on Context Windows vs RAG was really illuminating. I've been debating which approach to take for our internal knowledge base. Do you think the 1M+ context windows in newer models will eventually make RAG obsolete?
Sarah Chen
Sarah ChenAuthor·18h ago
Great question, Michael! I don't think RAG is going away anytime soon. Even with huge context windows, RAG offers better latency, cost-efficiency, and most importantly - the ability to cite sources explicitly.
Priya Patel
Priya Patel·Dec 22, 2025
I finally understand how Positional Encodings work! The visual analogy with the clock hands was brilliant. 👏
DevOps Ninja
DevOps Ninja·Dec 22, 2025
Any chance you could cover Quantization (GGUF/GPTQ) in a future post? trying to run these locally on my MacBook and it's a bit of a jungle out there.