AI Agent 实战教程(一): 概述与环境搭建
学习视频:什么是 AI Agent

1.1 从 Chat 到 Agent
核心区别:Agent 不只是"回答问题",而是"解决问题"。它能够:
•
感知:理解用户的真实意图
•
规划:将复杂任务拆解为可执行的步骤
•
行动:调用外部工具(搜索、数据库、API 等)
•
记忆:记住上下文和历史交互
•
反思:根据执行结果调整策略
1.2 Agent 的应用场景
#arObMEweEpAS text { white-space: pre; }#arObMEweEpAS .fb0 { stroke: var(--N900-FG, 0.15); }#arObMEweEpAS .fb0 {stroke: rgba(31,35,41, 0.15)}#arObMEweEpAS .fx0 { stroke-miterlimit: 10; }#arObMEweEpAS .fx0 {stroke-miterlimit: 10}#arObMEweEpAS .fa1 { fill: rgb(187, 191, 196); }#arObMEweEpAS .fa1 {fill: rgb(187, 191, 196)}#arObMEweEpAS .fx1 { font: 400 12px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#arObMEweEpAS .fx1 {font-style: normal}#arObMEweEpAS .fx2 { text-anchor: middle; }#arObMEweEpAS .fx2 {text-anchor: middle}#arObMEweEpAS .fx3 { dominant-baseline: central; }#arObMEweEpAS .fx3 {dominant-baseline: central}#arObMEweEpAS .fx4 { stroke: none; }#arObMEweEpAS .fx4 {stroke: none}#arObMEweEpAS .fc0 { fill: var(--text-caption); }#arObMEweEpAS .fc0 {fill: #646a73}#arObMEweEpAS .fa2 { fill: var(--text-link-disabled); }#arObMEweEpAS .fa2 {fill: rgb(187, 191, 196)}#arObMEweEpAS .fa3 { fill: rgb(239, 243, 245); }#arObMEweEpAS .fa3 {fill: rgb(239, 243, 245)}#arObMEweEpAS .fx5 { font: 700 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#arObMEweEpAS .fx5 {font-style: normal}#arObMEweEpAS .fx6 { text-anchor: start; }#arObMEweEpAS .fx6 {text-anchor: start}#arObMEweEpAS .fx7 { dominant-baseline: alphabetic; }#arObMEweEpAS .fx7 {dominant-baseline: alphabetic}#arObMEweEpAS .fc1 { fill: rgb(79, 79, 79); }#arObMEweEpAS .fc1 {fill: rgb(79, 79, 79)}#arObMEweEpAS .fx8 { font: 400 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#arObMEweEpAS .fx8 {font-style: normal}#arObMEweEpAS .fb1 { fill: var(--N900-FG, 0.15); }#arObMEweEpAS .fb1 {fill: rgba(31,35,41, 0.15)}#arObMEweEpAS .fb2 { fill: rgb(221, 221, 221); }#arObMEweEpAS .fb2 {fill: rgb(221, 221, 221)}#arObMEweEpAS .fa4 { fill: var(--bg-body); }#arObMEweEpAS .fa4 {fill: #fff}ABC123456场景智能客服数据分析代码开发内容创作知识问答说明典型案例自动处理用户咨询,查询订单、退款等自然语言查询数据库,生成报表理解需求、编写代码、运行测试搜索资料、整理大纲、生成文章基于企业知识库回答专业问题电商售后机器人数据分析助手AI 编程助手自动写稿系统企业知识库助手
2. Agent 的核心架构
2.1 四大核心组件
组件详解:
#qqwqRBhzehRu text { white-space: pre; }#qqwqRBhzehRu .fb0 { stroke: var(--N900-FG, 0.15); }#qqwqRBhzehRu .fb0 {stroke: rgba(31,35,41, 0.15)}#qqwqRBhzehRu .fx0 { stroke-miterlimit: 10; }#qqwqRBhzehRu .fx0 {stroke-miterlimit: 10}#qqwqRBhzehRu .fa1 { fill: rgb(187, 191, 196); }#qqwqRBhzehRu .fa1 {fill: rgb(187, 191, 196)}#qqwqRBhzehRu .fx1 { font: 400 12px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#qqwqRBhzehRu .fx1 {font-style: normal}#qqwqRBhzehRu .fx2 { text-anchor: middle; }#qqwqRBhzehRu .fx2 {text-anchor: middle}#qqwqRBhzehRu .fx3 { dominant-baseline: central; }#qqwqRBhzehRu .fx3 {dominant-baseline: central}#qqwqRBhzehRu .fx4 { stroke: none; }#qqwqRBhzehRu .fx4 {stroke: none}#qqwqRBhzehRu .fc0 { fill: var(--text-caption); }#qqwqRBhzehRu .fc0 {fill: #646a73}#qqwqRBhzehRu .fa2 { fill: var(--text-link-disabled); }#qqwqRBhzehRu .fa2 {fill: rgb(187, 191, 196)}#qqwqRBhzehRu .fa3 { fill: rgb(239, 243, 245); }#qqwqRBhzehRu .fa3 {fill: rgb(239, 243, 245)}#qqwqRBhzehRu .fx5 { font: 700 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#qqwqRBhzehRu .fx5 {font-style: normal}#qqwqRBhzehRu .fx6 { text-anchor: start; }#qqwqRBhzehRu .fx6 {text-anchor: start}#qqwqRBhzehRu .fx7 { dominant-baseline: alphabetic; }#qqwqRBhzehRu .fx7 {dominant-baseline: alphabetic}#qqwqRBhzehRu .fc1 { fill: rgb(79, 79, 79); }#qqwqRBhzehRu .fc1 {fill: rgb(79, 79, 79)}#qqwqRBhzehRu .fx8 { font: 400 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#qqwqRBhzehRu .fx8 {font-style: normal}#qqwqRBhzehRu .fc2 { fill: rgb(0, 0, 255); }#qqwqRBhzehRu .fc2 {fill: rgb(0, 0, 255)}#qqwqRBhzehRu .fc3 { fill: rgb(0, 0, 0); }#qqwqRBhzehRu .fc3 {fill: rgb(0, 0, 0)}#qqwqRBhzehRu .fc4 { fill: rgb(0, 0, 128); }#qqwqRBhzehRu .fc4 {fill: rgb(0, 0, 128)}#qqwqRBhzehRu .fb1 { fill: var(--N900-FG, 0.15); }#qqwqRBhzehRu .fb1 {fill: rgba(31,35,41, 0.15)}#qqwqRBhzehRu .fb2 { fill: rgb(221, 221, 221); }#qqwqRBhzehRu .fb2 {fill: rgb(221, 221, 221)}#qqwqRBhzehRu .fa4 { fill: var(--bg-body); }#qqwqRBhzehRu .fa4 {fill: #fff}ABC12345组件LLM(大模型)规划引擎(Planner)记忆系统(Memory)工具集(Tools)作用LangChain 对应理解指令、推理决策、生成内容拆解任务、制定执行步骤存储对话历史、知识、经验与外部世界交互的能力, , langchain-openailangchain-communityLangChain ChainAgentlangchain.langchain.toolsmemory
2.2 Agent 的工作循环:ReAct 模式
目前最主流的 Agent 工作模式是 ReAct(Reasoning + Acting):
2.3 记忆的类型
#BNfhykpTxFtb text { white-space: pre; }#BNfhykpTxFtb .fb0 { stroke: var(--N900-FG, 0.15); }#BNfhykpTxFtb .fb0 {stroke: rgba(31,35,41, 0.15)}#BNfhykpTxFtb .fx0 { stroke-miterlimit: 10; }#BNfhykpTxFtb .fx0 {stroke-miterlimit: 10}#BNfhykpTxFtb .fa1 { fill: rgb(187, 191, 196); }#BNfhykpTxFtb .fa1 {fill: rgb(187, 191, 196)}#BNfhykpTxFtb .fx1 { font: 400 12px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#BNfhykpTxFtb .fx1 {font-style: normal}#BNfhykpTxFtb .fx2 { text-anchor: middle; }#BNfhykpTxFtb .fx2 {text-anchor: middle}#BNfhykpTxFtb .fx3 { dominant-baseline: central; }#BNfhykpTxFtb .fx3 {dominant-baseline: central}#BNfhykpTxFtb .fx4 { stroke: none; }#BNfhykpTxFtb .fx4 {stroke: none}#BNfhykpTxFtb .fc0 { fill: var(--text-caption); }#BNfhykpTxFtb .fc0 {fill: #646a73}#BNfhykpTxFtb .fa2 { fill: var(--text-link-disabled); }#BNfhykpTxFtb .fa2 {fill: rgb(187, 191, 196)}#BNfhykpTxFtb .fa3 { fill: rgb(239, 243, 245); }#BNfhykpTxFtb .fa3 {fill: rgb(239, 243, 245)}#BNfhykpTxFtb .fx5 { font: 700 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#BNfhykpTxFtb .fx5 {font-style: normal}#BNfhykpTxFtb .fx6 { text-anchor: start; }#BNfhykpTxFtb .fx6 {text-anchor: start}#BNfhykpTxFtb .fx7 { dominant-baseline: alphabetic; }#BNfhykpTxFtb .fx7 {dominant-baseline: alphabetic}#BNfhykpTxFtb .fc1 { fill: rgb(79, 79, 79); }#BNfhykpTxFtb .fc1 {fill: rgb(79, 79, 79)}#BNfhykpTxFtb .fx8 { font: 400 14px MonospacedNumber, LarkHackSafariFont, LarkEmojiFont, LarkChineseQuote, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Tahoma, "PingFang SC", "Microsoft Yahei", Arial, "Hiragino Sans GB", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; }#BNfhykpTxFtb .fx8 {font-style: normal}#BNfhykpTxFtb .fc2 { fill: rgb(0, 0, 255); }#BNfhykpTxFtb .fc2 {fill: rgb(0, 0, 255)}#BNfhykpTxFtb .fb1 { fill: var(--N900-FG, 0.15); }#BNfhykpTxFtb .fb1 {fill: rgba(31,35,41, 0.15)}#BNfhykpTxFtb .fb2 { fill: rgb(221, 221, 221); }#BNfhykpTxFtb .fb2 {fill: rgb(221, 221, 221)}#BNfhykpTxFtb .fa4 { fill: var(--bg-body); }#BNfhykpTxFtb .fa4 {fill: #fff}ABCD1234类型短期记忆长期记忆工作记忆说明LangChain 组件持久性当前对话的上下文跨会话的用户偏好、历史摘要任务执行过程中的中间状态Chain 状态变量会话结束即消失永久存储任务结束即消失ConversationBufferMemoryVectorStoreRetrieverMemory
3. LangChain 生态系统概览
3.1 LangChain 架构图
3.2 核心包说明
3.3 LangChain vs 原生开发对比
4. 开发环境搭建
4.1 基础环境
代码块
Plain Text
1. 创建项目目录mkdir langchain-agent-tutorial && cd langchain-agent-tutorial
# 2. 创建虚拟环境(推荐 Python 3.10+)
python -m venv venv
4.3 项目结构
代码块
Bash
langchain-agent-tutorial/
├── .env # API 密钥(不要提交到 Git!)
├── .gitignore # Git 忽略文件
├── requirements.txt # 依赖清单
├── config.py # 配置管理
├── chains/ # Chain 实现
│ ├──
init
.py
│ └── basic_chain.py # 基础 Chain
├── agents/ # Agent 实现
│ ├──
init
.py
│ └── react_agent.py # ReAct Agent
├── tools/ # 工具函数
│ └──
init
.py
4.4 配置 API 密钥
创建 .env 文件:
代码块
Plain Text
===== 国产模型 =====# DeepSeek(推荐入门,性价比最高)DEEPSEEK_API_KEY=sk-your-deepseek-key
# Kimi(月之暗面)MOONSHOT_API_KEY=sk-your-kimi-key
# 豆包(字节跳动/火山引擎)ARK_API_KEY=your-ark-api-key
# 智谱 GLMZHIPUAI_API_KEY=your-glm-api-key
5. 多模型适配:LangChain Chat Models
5.1 LangChain 统一模型接口
LangChain 提供了统一的 BaseChatModel 接口,所有模型都遵循相同的调用方式:
5.2 各模型配置一览
5.3 统一模型工厂
代码块
Python
"""
models.py - 统一模型工厂
使用 LangChain ChatOpenAI 兼容接口适配所有模型
"""import os
from typing import Optionalfrom langchain_openai import ChatOpenAI
from langchain_core.language_models import BaseChatModel
from dotenv import load_dotenv
load_dotenv()
5.4 流式输出
代码块
Python
"""
stream_example.py - LangChain 流式输出示例
"""from models import get_chat_model
llm = get_chat_model("deepseek", temperature=0.7)
# 方式一:使用 stream 方法print("=== 流式输出 ===")
for chunk in llm.stream("讲一个简短的笑话"):print(chunk.content, end="", flush=True)
print("\n")
# 方式二:使用 astream 异步流式
import asyncio
async def async_stream():print("=== 异步流式输出 ===")
async for chunk in llm.astream("用50字介绍Python"):print(chunk.content, end="", flush=True)print("\n")
asyncio.run(async_stream())
6. 第一个 Chain:从零到一
6.1 什么是 Chain
Chain 是 LangChain 的核心概念,它将多个组件串联起来形成一个处理管道:
6.2 使用 LCEL(LangChain Expression Language)
代码块
Python
LangChain 使用 LCEL 声明式语法来构建 Chain:
"""
chains/basic_chain.py - 基础 Chain 示例
使用 LCEL(LangChain Expression Language)构建处理管道
"""from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from models import get_chat_model
6.3 LCEL 核心概念
LCEL 关键特性:
6.4 Chain 的调用方式
代码块
Python
7. 小结与下一步
7.1 本篇回顾
7.2 关键收获
Agent = LLM + 工具 + 记忆 + 规划,LangChain 提供了完整的组件支持
LCEL 语法让构建 Chain 变得简洁优雅,使用 | 操作符串联组件
ChatOpenAI 兼容接口可以适配所有国产模型和海外模型
7.3 下一篇预告
第二篇:Prompt 工程与工具调用 将深入讲解:
•
LangChain PromptTemplate 高级用法
•
LangChain Tools 定义和工具调用
•
LangChain Agent 构建(ReAct Agent)
•
使用 LangChain 构建完整的工具调用流程