# LangChain Learning [![](https://img.shields.io/badge/version-0.0.5-blue.svg)](https://github.com/your-repo/langchain-learning) [![](https://img.shields.io/badge/python-3.11+-green.svg)](https://www.python.org/) [![](https://img.shields.io/badge/LangChain-v1.2-orange.svg)](https://www.langchain.com/) > LangChain 框架学习项目,集成 SiliconFlow API ## 功能特性 - **多 LLM 集成**:支持 OpenAI API、Silicon Flow 及 LangChain 抽象层 - **流式响应**:实时流式输出,带来更好的使用体验 - **Prompt 工程**:多种 Prompt 模板构建方式 - **输出解析**:支持 JSON 等格式解析 - **Token 用量追踪**:轻松监控 API 调用消耗 - **内存管理**:实现对话历史持久化(ConversationBufferMemory, SummaryMemory) - **实战示例**:从基础到进阶的使用模式 ## 快速开始 ### 1. 安装依赖 ```bash pip install langchain>=1.2.15 langchain-community>=0.4.1 langchain-siliconflow>=1.0.0 requests>=2.33.1 ``` ***注意:*** *如果需要完整的记忆功能和更高级的模型,你可能需要安装额外的库。* ### 2. 配置环境变量 在项目根目录创建 `.env` 文件: ```env SILICONFLOW_API_KEY=your_api_key_here SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1 ``` ### 3. 运行示例 **Hello World 示例** | 示例 | 命令 | 说明 | |------|------|------| | 直接调用 API | `python helloworld/helloworld.py` | 使用 requests 直接调用 SiliconFlow API | | LangChain + ChatOpenAI | `python helloworld/helloworld_langchain_openai.py` | 通过 OpenAI 接口调用 LLM | | LangChain + ChatSiliconFlow | `python helloworld/helloworld_siliconflow.py` | 使用 LangChain SiliconFlow 集成 | | OpenAI 客户端 + SiliconFlow | `python helloworld/openai_siliconflow.py` | OpenAI 客户端兼容 SiliconFlow | **Prompt 示例** | 示例 | 命令 | 说明 | |------|------|------| | PromptTemplate | `python prompt/prompt_demo.py` | 演示 PromptTemplate 模板构建 | | Few-shot Learning | `python prompt/fewshot_demo.py` | 带示例的少样本提示学习 | | 从文件加载 Prompt | `python prompt/promt_from_file.py` | 从 YAML 文件加载提示词模板 | **输出解析示例** | 示例 | 命令 | 说明 | |------|------|------| | JSON 解析器 | `python parser/json_parser_demo.py` | 使用 JsonOutputParser 解析 LLM 输出 | **Token 用量示例** | 示例 | 命令 | 说明 | |------|------|------| | Token 追踪 | `python token/token_demo.py` | 使用 get_openai_callback 追踪 token 消耗 | **内存管理示例** | 示例 | 命令 | 说明 | |------|------|------| | 基础记忆 | `python memory/memory_desc.py` | 演示不同类型的 Memory 对象。| | 带内存聊天 | `python memory/with_memory_demo.py` | 在对话链中管理和利用聊天历史记录。 | ## 项目结构 ``` langchain-learning/ ├── helloworld/ │ ├── helloworld.py # 直接调用 SiliconFlow API │ ├── helloworld_langchain_openai.py # LangChain + ChatOpenAI │ ├── helloworld_siliconflow.py # LangChain + ChatSiliconFlow │ └── openai_siliconflow.py # OpenAI 客户端 + SiliconFlow ├── prompt/ │ ├── prompt_demo.py # PromptTemplate 模板示例 │ ├── fewshot_demo.py # Few-shot Learning 示例 │ ├── promt_from_file.py # 从文件加载 Prompt │ └── prompt_from_file.yaml # Prompt 模板文件 ├── parser/ │ └── json_parser_demo.py # JSON 输出解析示例 ├── token/ │ └── token_demo.py # Token 用量追踪示例 ├── memory/ # 记忆管理模块 │ ├── memory_desc.py # 演示 Memory 对象类型 │ └── with_memory_do.py # 演示使用带内存的聊天循环 ├── main.py # 入口文件 ├── pyproject.toml # 项目配置 └── README.md ``` ## 可用模型 - `deepseek-ai/DeepSeek-R1-0528-Qwen3-8B` - `Qwen/Qwen3.5-4B` ## 技术栈 | 类别 | 技术 | |------|------| | 框架 | LangChain | | LLM 提供商 | SiliconFlow | | 语言 | Python 3.11+ | ## 许可证 MIT License