Skip to content

Klacb/ai-agent-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Agent Tutorial - 从零开始构建 AI Agent

License: MIT Python 3.10+ Code style: black

📚 配套教程:[从零开始构建 AI Agent:完整实战指南 (2026)](https://dev.to/your-username/从零开始构建 ai-agent-完整实战指南 -2026)

这是一个完整的 AI Agent 实现示例,使用 LangGraph 构建状态工作流,支持工具调用和记忆系统。


🚀 快速开始

环境要求

  • Python 3.10+
  • OpenAI API Key 或 Anthropic API Key

安装

# 1. 克隆仓库
git clone https://github.com/your-username/ai-agent-tutorial.git
cd ai-agent-tutorial

# 2. 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate

# 3. 安装依赖
pip install -r requirements.txt

# 4. 配置 API 密钥
cp .env.example .env
# 编辑 .env 文件,填入你的 API 密钥

运行示例

# 运行基础示例
python main.py

# 运行数据分析示例
python examples/data_agent.py

# 运行测试
pytest tests/

📁 项目结构

ai-agent-tutorial/
├── agent/
│   ├── __init__.py      # 包初始化
│   ├── core.py          # Agent 核心逻辑(状态图、节点)
│   ├── memory.py        # 记忆系统(短期 + 长期记忆)
│   └── tools.py         # 工具注册表
├── tests/
│   └── test_agent.py    # 单元测试
├── examples/
│   ├── data_agent.py    # 数据分析 Agent 示例
│   └── research_agent.py # 研究 Agent 示例
├── .env.example         # 环境变量模板
├── .gitignore
├── requirements.txt     # Python 依赖
├── main.py             # 入口文件
├── README.md
└── LICENSE

🧠 核心概念

AI Agent 架构

┌──────────────────────────────────────────────┐
│              AI Agent 架构                    │
├──────────────────────────────────────────────┤
│  感知层 → 决策层 → 执行层 → 观察层            │
│       ↑                                      │
│       └──────────────┬───────────────────────┘
│                      │
│              ┌───────────────┐
│              │   记忆层      │
│              └───────────────┘
└──────────────────────────────────────────────┘

状态图 (StateGraph)

使用 LangGraph 的状态图管理 Agent 的执行流程:

from langgraph.graph import StateGraph, END

workflow = StateGraph(AgentState)
workflow.add_node("think", think_function)
workflow.add_node("act", act_function)
workflow.add_edge("think", "act")
workflow.add_edge("act", END)
graph = workflow.compile()

🛠️ 功能特性

✅ 已实现

  • 基于 LangGraph 的状态工作流
  • 可配置的工具注册系统
  • 双层记忆系统(短期 + 长期)
  • 支持 OpenAI 和 Anthropic API
  • Docker 部署配置
  • 完整的单元测试

🚧 计划中

  • 向量数据库长期记忆
  • 多 Agent 协作
  • Web UI 界面
  • API 服务部署

📖 使用示例

基础使用

from agent.core import AIAgent
from agent.memory import Memory

# 创建 Agent
agent = AIAgent(model="gpt-4o")
memory = Memory()

# 运行任务
result = agent.run("分析这个数据文件并生成报告")
print(result)

添加工具

from agent.tools import registry

# 注册自定义工具
@registry.register(name="custom_tool", description="我的自定义工具")
def my_tool(param1: str, param2: int):
    return f"结果:{param1}, {param2}"

使用记忆

from agent.memory import Memory

memory = Memory()

# 添加消息
memory.add_message("user", "我喜欢 Python 编程")
memory.add_long_term("preference", "Python", category="user_preferences")

# 获取上下文
context = memory.get_context()
print(context)

# 保存和加载
memory.save()
memory.load()

🧪 测试

# 运行所有测试
pytest tests/ -v

# 运行特定测试
pytest tests/test_agent.py::test_agent_creation -v

# 生成覆盖率报告
pytest --cov=agent tests/

📦 部署

Docker

# 构建镜像
docker-compose build

# 运行
docker-compose up -d

# 查看日志
docker-compose logs -f

Vercel

# 安装 Vercel CLI
npm install -g vercel

# 部署
vercel --prod

AWS Lambda

参考 lambda_function.py 示例。


🔧 配置

环境变量

变量名 说明 默认值
OPENAI_API_KEY OpenAI API 密钥 -
ANTHROPIC_API_KEY Anthropic API 密钥 -
DEFAULT_MODEL 默认模型 gpt-4o
DEBUG 调试模式 false

🤝 贡献

欢迎贡献!请遵循以下步骤:

  1. Fork 本项目
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。


🙏 致谢


📬 联系方式

  • 📧 Email: your@email.com
  • 🐦 Twitter: [@your-handle]
  • 💼 LinkedIn: [your-profile]

如果这个项目对你有帮助,请给一个 ⭐️ Star!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors