From 00d56a267d59133e309f8fefa37e1dbe3ed4cc92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=85=9C?= Date: Wed, 1 Oct 2025 17:06:51 +0800 Subject: [PATCH] Refactor project structure for better organization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move notebooks to dedicated notebooks/ directory - Create src/ directory for reusable code (utils/, chains/) - Add tests/ directory for unit tests - Add comprehensive .gitignore file - Update CLAUDE.md and README.md with new structure - Remove old app/ directory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .claude/settings.local.json | 5 +- .gitignore | 60 ++++++++++++++++ CLAUDE.md | 25 +++++-- README.md | 98 +++++++++++++++++++++++++- {app/demo => notebooks}/demo1.ipynb | 0 sample.ipynb => notebooks/sample.ipynb | 0 src/__init__.py | 0 src/chains/__init__.py | 0 src/utils/__init__.py | 0 tests/__init__.py | 0 10 files changed, 180 insertions(+), 8 deletions(-) create mode 100644 .gitignore rename {app/demo => notebooks}/demo1.ipynb (100%) rename sample.ipynb => notebooks/sample.ipynb (100%) create mode 100644 src/__init__.py create mode 100644 src/chains/__init__.py create mode 100644 src/utils/__init__.py create mode 100644 tests/__init__.py diff --git a/.claude/settings.local.json b/.claude/settings.local.json index c8b38e1..7de7983 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -3,7 +3,10 @@ "allow": [ "Bash(git init:*)", "Bash(git remote add:*)", - "Bash(git add:*)" + "Bash(git add:*)", + "Bash(git commit:*)", + "Bash(git push:*)", + "Bash(tree:*)" ], "deny": [], "ask": [] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04c91cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# Virtual Environment +venv/ +env/ +ENV/ + +# Jupyter Notebook +.ipynb_checkpoints +*.ipynb_checkpoints + +# Environment variables +.env +.env.local + +# IDE +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS +.DS_Store +Thumbs.db + +# Data +data/* +!data/.gitkeep + +# Models +models/* +!models/.gitkeep + +# Logs +*.log + +# FAISS indexes +*.faiss +*.index diff --git a/CLAUDE.md b/CLAUDE.md index 77af06c..afd79b2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -26,11 +26,23 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## 代码结构 ### 目录组织 -- `/app/demo/`: 包含演示代码的 Jupyter notebook - - `demo1.ipynb`: 主要演示文件,包含 7 个核心示例 -- `/models/`: 模型相关文件(目前为空) -- `/data/`: 数据文件(目前为空) -- `sample.ipynb`: 根目录的示例 notebook,展示环境配置和基础模型调用 +``` +lang-demo1/ +├── notebooks/ # 学习笔记和示例 +│ ├── demo1.ipynb # 主要演示: LLM调用、提示词、解析器、向量、RAG、Agent +│ └── sample.ipynb # 环境配置和基础调用示例 +├── src/ # 可复用的源代码 +│ ├── utils/ # 工具函数 +│ └── chains/ # 自定义链 +├── tests/ # 单元测试 +├── data/ # 数据文件 +├── models/ # 模型文件 +├── .env # 环境变量 (不提交到 Git) +├── .gitignore # Git 忽略配置 +├── requirements.txt # Python 依赖 +├── README.md # 项目说明 +└── CLAUDE.md # 本文件 +``` ### 核心功能模块 @@ -83,7 +95,8 @@ chain = prompt | llm ### 运行 Jupyter Notebooks 主要开发工作在 Jupyter notebook 中进行: - 启动 Jupyter: `jupyter notebook` 或 `jupyter lab` -- 主要演示代码位于 `app/demo/demo1.ipynb` +- 主要演示代码位于 `notebooks/demo1.ipynb` +- 基础示例位于 `notebooks/sample.ipynb` ### 环境初始化模式 代码使用严格的环境变量检查: diff --git a/README.md b/README.md index 428a94c..3adc8bb 100644 --- a/README.md +++ b/README.md @@ -1 +1,97 @@ -**1** 开始 \ No newline at end of file +# LangChain 学习项目 + +这是一个 LangChain 学习演示项目,涵盖了从基础调用到高级应用(RAG、Agent)的完整示例。 + +## 项目结构 + +``` +lang-demo1/ +├── notebooks/ # Jupyter 学习笔记 +│ ├── demo1.ipynb # 核心示例(7个主题) +│ └── sample.ipynb # 基础示例 +├── src/ # 可复用代码 +│ ├── utils/ # 工具函数 +│ └── chains/ # 自定义链 +├── tests/ # 单元测试 +├── data/ # 数据文件 +└── models/ # 模型文件 +``` + +## 快速开始 + +### 1. 安装依赖 + +```bash +pip install -r requirements.txt +``` + +主要依赖: +- `langchain` - LangChain 核心库 +- `langchain-openai` - OpenAI 集成 +- `langchain-community` - 社区工具 +- `faiss-cpu` - 向量数据库 +- `python-dotenv` - 环境变量管理 + +### 2. 配置环境变量 + +在项目根目录创建 `.env` 文件: + +```env +# OpenAI API 配置 +OPENAI_BASE_URL=你的API地址 +OPENAI_API_KEY1=你的API密钥 + +# LangSmith 追踪配置(可选) +LANGSMITH_TRACING=true +LANGSMITH_API_KEY=你的LangSmith密钥 +LANGSMITH_PROJECT=你的项目名 +``` + +### 3. 运行示例 + +```bash +jupyter notebook +# 或 +jupyter lab +``` + +然后打开 `notebooks/demo1.ipynb` 开始学习。 + +## 学习内容 + +### 1. 基础 LLM 调用 +使用 `ChatOpenAI` 进行基本的模型调用 + +### 2. 提示词模板 +使用 `ChatPromptTemplate` 创建结构化提示词 + +### 3. 输出解析器 +使用 `JsonOutputParser` 将输出解析为 JSON 格式 + +### 4. 向量存储 +使用 FAISS 向量数据库存储和检索文档 + +### 5. RAG (检索增强生成) +结合向量检索和 LLM 实现知识问答 + +### 6. Agent 系统 +使用 LangChain Agent 实现工具调用和自主决策 + +## 技术栈 + +- **LLM**: GPT-4.1-nano (通过 OpenAI 兼容接口) +- **Embeddings**: text-embedding-3-small +- **向量数据库**: FAISS +- **Web 抓取**: BeautifulSoup4 +- **开发环境**: Jupyter Notebook/Lab + +## 注意事项 + +- 代码中使用 `OPENAI_API_KEY1` 而不是标准的 `OPENAI_API_KEY` +- 向量数据存储在内存中,未做持久化 +- `.env` 文件包含敏感信息,已在 `.gitignore` 中排除 + +## 学习资源 + +- [LangChain 官方文档](https://python.langchain.com/) +- [LangChain 中文文档](https://www.langchain.com.cn/) diff --git a/app/demo/demo1.ipynb b/notebooks/demo1.ipynb similarity index 100% rename from app/demo/demo1.ipynb rename to notebooks/demo1.ipynb diff --git a/sample.ipynb b/notebooks/sample.ipynb similarity index 100% rename from sample.ipynb rename to notebooks/sample.ipynb diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chains/__init__.py b/src/chains/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29