Files
ai-agent-dev/01_开发环境搭建.ipynb
2026-07-08 10:09:42 +08:00

214 lines
6.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 01 开发环境搭建\n",
"\n",
"## 学习目标\n",
"1. 了解课程整体安排与学习目标\n",
"2. 明确 LangChain 和 LangGraph 在 AI 智能体开发中的定位\n",
"3. 熟悉本课程使用的技术栈和工具链VS Code + Python 3.12 + 虚拟环境)\n",
"4. 学会使用 venv 创建虚拟环境并使用清华镜像源加速 pip 安装"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. 课程概述与技术栈\n",
"\n",
"本课程面向具备 Python 基础语法的大学生,系统讲解 **LangChain** 和 **LangGraph** 两大框架,帮助你从零构建 AI 智能体应用。\n",
"\n",
"### 核心技术栈\n",
"- **开发工具**VS CodeVisual Studio Code\n",
"- **编程语言**Python 3.12.11\n",
"- **虚拟环境**venvPython 内置)\n",
"- **主要框架**LangChain、LangGraph\n",
"- **镜像源**:清华大学 TUNA 镜像(加速 pip 安装)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. VS Code 安装与配置\n",
"\n",
"### 步骤 1下载并安装 VS Code\n",
"1. 访问官网https://code.visualstudio.com/\n",
"2. 下载 Windows 版本安装包\n",
"3. 运行安装程序,建议勾选:\n",
" - **添加到 PATH**(添加到系统环境变量)\n",
" - **右键菜单集成**(在资源管理器中打开)\n",
"\n",
"### 步骤 2安装 Python 扩展\n",
"打开 VS Code按 `Ctrl+Shift+X` 进入扩展市场,搜索并安装:\n",
"- **Python**Microsoft 官方扩展)\n",
"- **Jupyter**(用于运行 .ipynb 文件)\n",
"\n",
"### 步骤 3选择 Python 解释器\n",
"按 `Ctrl+Shift+P` 打开命令面板,输入 `Python: Select Interpreter`,选择后续步骤中创建的虚拟环境。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Python 3.12.11 环境准备\n",
"\n",
"本课程统一使用 **Python 3.12.11**,确保版本一致可避免兼容性问题。\n",
"\n",
"### 检查当前 Python 版本\n",
"在 VS Code 终端(`Ctrl+ ``)或 PowerShell 中执行:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"print(f\"Python 版本:{sys.version}\")\n",
"print(f\"Python 路径:{sys.executable}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果你的系统中没有 Python 3.12.11,请前往 [Python 官网](https://www.python.org/downloads/release/python-31211/) 下载安装,或使用 `pyenv-win` 管理多版本。\n",
"\n",
"> ⚠️ **注意**:安装时请务必勾选 **Add Python to PATH**。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. 创建虚拟环境venv\n",
"\n",
"使用虚拟环境可以隔离项目依赖避免不同项目之间的包冲突。Python 3.12 内置了 `venv` 模块,无需额外安装。\n",
"\n",
"### 在项目目录下创建虚拟环境\n",
"\n",
"在 VS Code 终端(确保终端为 PowerShell 或 CMD中执行\n",
"\n",
"```powershell\n",
"# 创建名为 .venv 的虚拟环境\n",
"python -m venv .venv\n",
"```\n",
"\n",
"执行后,你会在当前目录下看到 `.venv` 文件夹。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. 激活虚拟环境\n",
"\n",
"### Windows 系统PowerShell\n",
"```powershell\n",
".venv\\Scripts\\Activate.ps1\n",
"```\n",
"\n",
"### Windows 系统CMD\n",
"```cmd\n",
".venv\\Scripts\\activate.bat\n",
"```\n",
"\n",
"> ⚠️ **PowerShell 执行策略提示**:如果提示无法执行脚本,请以管理员身份运行 PowerShell 并执行:\n",
"> ```powershell\n",
"> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser\n",
"> ```\n",
"\n",
"激活成功后,终端提示符前会显示 `(.venv)`。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. 使用清华镜像源安装依赖\n",
"\n",
"为了加快 pip 下载速度,我们使用清华大学 TUNA 镜像源。\n",
"\n",
"### 临时使用清华源(推荐)\n",
"在 VS Code 终端中执行以下命令(确保虚拟环境已激活):\n",
"\n",
"```powershell\n",
"pip install langchain langgraph langchain-openai -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
"```\n",
"\n",
"### 配置全局默认镜像源(可选)\n",
"如果你想让所有 pip 安装默认使用清华源:\n",
"\n",
"```powershell\n",
"pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. 验证环境安装\n",
"\n",
"运行以下代码,确认核心库已正确安装:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from importlib.metadata import version\n",
"\n",
"print(f\"Python 版本:{sys.version}\")\n",
"print(f\"LangChain 版本:{version('langchain')}\")\n",
"print(f\"LangGraph 版本:{version('langgraph')}\")\n",
"print(\"✅ 环境安装成功!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. VS Code 关联虚拟环境\n",
"\n",
"1. 按 `Ctrl+Shift+P` 打开命令面板\n",
"2. 输入 `Python: Select Interpreter`\n",
"3. 选择 `./.venv/Scripts/python.exe`\n",
"4. 重新启动 Jupyter 内核(点击右上角内核选择器,选择 `.venv` 环境)\n",
"\n",
"现在你可以在 VS Code 中直接运行本课程的所有 `.ipynb` 文件了!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}