# node-langchain **Repository Path**: runoob_xie/node-langchain ## Basic Information - **Project Name**: node-langchain - **Description**: No description available - **Primary Language**: JavaScript - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-28 - **Last Updated**: 2026-04-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # node-langchain ## 简介 在node.js中使用langchain大语言模型的能力 本项目包含了丰富的示例代码,演示了 LangChain 的各种核心功能,包括对话模型、链式调用、智能体(Agent)、RAG 检索增强生成等。 ## 特性 - 简洁易用的 API 设计 - 支持多种 LLM 提供商(OpenAI、DeepSeek 等) - 链式调用支持(RunnableSequence) - 智能体(Agent)功能封装 - RAG 检索增强生成支持 - 完整的事件历史管理 - 完整的 TypeScript 类型定义 - 模块化架构,易于扩展 ## 项目结构 ``` node-langchain/ ├── index.js # 基础示例 ├── index-chat.js # 对话示例 ├── index-prompt.js # 提示词模板示例 ├── index-rag.js # RAG 检索增强生成 ├── index-tools.js # 工具调用示例 ├── index-obj.js # 对象处理示例 ├── index-vision.js # 视觉模型示例 ├── index-graph-demo.js # 状态图示例 ├── index-langgraph.js # LangGraph 工作流 ├── index-multi-agent.js # 多智能体协作 ├── index-parallel-agent.js # 并行智能体 ├── index-supervisor-agent.js # 监管智能体 ├── index-daily-report-summary.js # 日报生成 ├── server.js # Express 服务器 └── public/ └── index.html # 聊天界面 ``` ## 安装 ```bash npm install ``` 或使用 yarn: ```bash yarn install ``` 或使用 pnpm: ```bash pnpm install ``` ## 快速开始 ### 环境配置 ```bash # 设置 OpenAI API 密钥 export OPENAI_API_KEY=your-api-key # 或设置 DeepSeek API 密钥 export DEEPSEEK_API_KEY=your-api-key ``` ### 运行示例 ```bash # 运行基础示例 node index.js # 运行对话示例 node index-chat.js # 运行 RAG 示例 node index-rag.js ``` ## 使用示例 ### 基础对话 ```javascript const { ChatDeepSeek } = require('langchain/langchain'); const { PromptTemplate } = require('langchain/langchain'); const { RunnableLambda } = require('langchain/langchain'); const prompt = PromptTemplate.fromTemplate('请介绍一下{topic}'); const model = new ChatDeepSeek({ model: 'deepseek-chat', temperature: 0.7, }); async function callModel() { const chain = prompt.pipe(model); const result = await chain.invoke({ topic: '人工智能' }); console.log(result); } callModel(); ``` ### 链式调用 ```javascript const { RunnableSequence } = require('langchain/langchain'); const { StringOutputParser } = require('langchain/langchain'); const chain = RunnableSequence.from([ prompt, model, new StringOutputParser(), ]); ``` ### RAG 检索增强生成 ```javascript const { MemoryVectorStore } = require('langchain/langchain'); const { AlibabaTongyiEmbeddings } = require('langchain/langchain'); const { RunnableSequence, RunnablePassthrough } = require('langchain/langchain'); // 创建向量存储 const vectorStore = new MemoryVectorStore(embeddings); // 构建 RAG 链 const ragChain = RunnableSequence.from([ { context: vectorStore.asRetriever(), question: new RunnablePassthrough(), }, prompt, model, new StringOutputParser(), ]); ``` ### 工具调用 Agent ```javascript const { tool } = require('langchain/langchain'); const { initializeAgentExecutorWithOptions } = require('langchain/langchain'); // 定义工具 const getWeather = tool(async (input) => { // 获取天气逻辑 return '天气晴朗,25°C'; }, 'getWeather', '获取指定城市的天气信息'); async function run() { const executor = await initializeAgentExecutorWithOptions([getWeather], model); const result = await executor.run('北京天气怎么样?'); console.log(result); } ``` ### 多智能体协作 ```javascript const { ChatDeepSeek } = require('langchain/langchain'); const { PromptTemplate } = require('langchain/langchain'); const { StringOutputParser } = require('langchain/langchain'); const { RunnableSequence } = require('langchain/langchain'); // 报道员智能体 const reporterModel = new ChatDeepSeek({ model: 'deepseek-chat' }); const reporterPrompt = PromptTemplate.fromTemplate('写一篇关于{topic}的报道'); const reporterChain = reporterPrompt.pipe(reporterModel).pipe(new StringOutputParser()); // 编辑智能体 const editorPrompt = PromptTemplate.fromTemplate('编辑以下文章: {article}'); const editorChain = editorPrompt.pipe(reporterModel).pipe(new StringOutputParser()); // 新闻室协作链 const newsroomChain = RunnableSequence.from([ reporterChain, editorChain, ]); ``` ## 配置说明 | 参数 | 类型 | 必填 | 说明 | |------|------|------|------| | apiKey | string | 是 | API 密钥 | | model | string | 否 | 使用的模型 | | temperature | number | 否 | 生成温度,范围 0-2 | | maxTokens | number | 否 | 最大令牌数 | | baseUrl | string | 否 | API 基础 URL | ## 功能演示 | 示例文件 | 说明 | |----------|------| | index.js | 基础模型调用 | | index-chat.js | 聊天对话 | | index-prompt.js | 提示词模板 | | index-rag.js | RAG 检索增强 | | index-tools.js | 工具调用 | | index-vision.js | 视觉模型 | | index-graph-demo.js | 状态图 | | index-langgraph.js | LangGraph 工作流 | | index-multi-agent.js | 多智能体 | | index-parallel-agent.js | 并行智能体 | | index-supervisor-agent.js | 监管智能体 | ## 贡献指南 欢迎提交 Pull Request 或 Issue! 1. Fork 本仓库 2. 创建特性分支 (`git checkout -b feature/xxx`) 3. 提交更改 (`git commit -m 'Add xxx'`) 4. 推送分支 (`git push origin feature/xxx`) 5. 创建 Pull Request ## 许可证 MIT License ## 问题反馈 如果您在使用过程中遇到任何问题,请在 [Gitee Issues](https://gitee.com/runoob_xie/node-langchain/issues) 中提交反馈。