nodejs-workflow-plugin-framework
v1.0.0
Published
Node.js Plugin Framework for workflow system with example plugins
Maintainers
Readme
Node.js Plugin Framework
通用的 Node.js 插件框架,用于快速开发工作流系统插件。
📖 详细文档请查看 FRAMEWORK_README.md
🎯 概述
这是一个参考 Python 版本实现的 Node.js 插件框架,提供了简单易用的基类和工具,让你可以快速开发自定义插件。
🚀 快速开始
# 1. 安装依赖
npm install
# 2. 运行示例插件
npm run start:example # 文本处理插件 (端口 50052)
npm run start:http # HTTP API 插件 (端口 50054)
npm run start:ollama # LangChain Ollama 插件 (端口 50053)
# 3. 测试插件
node test-plugin.js 50052 "Text Processor"📦 包含的插件
1. 文本处理插件 (example-plugin.js)
简单的文本处理插件,支持大写、小写、反转操作。
启动: npm run start:example 或 node example-plugin.js 50052
2. HTTP API 插件 (http-api-plugin.js)
调用外部 HTTP API 的插件。
启动: npm run start:http 或 node http-api-plugin.js 50054
3. LangChain + Ollama 插件 (server.js)
使用 LangChain.js 调用本地 Ollama 模型的插件。
启动: npm run start:ollama 或 node server.js 50053
功能特性
- ✅ 支持所有 Ollama 模型(llama2, mistral, codellama 等)
- ✅ 支持流式和非流式输出
- ✅ 可配置温度、最大 token 数等参数
- ✅ 支持自定义系统消息
- ✅ 完全本地运行,无需 API 密钥
- ✅ 使用 LangChain.js 生态系统
前置要求
1. Node.js 环境
# 需要 Node.js 18 或更高版本
node --version # 应该 >= 18.0.02. 安装 Ollama
# macOS
brew install ollama
# Linux
curl -fsSL https://ollama.com/install.sh | sh
# 或访问 https://ollama.com/download3. 下载模型
# 下载 llama2 模型
ollama pull llama2
# 下载其他模型
ollama pull mistral
ollama pull codellama
ollama pull llama2-chinese4. 启动 Ollama 服务
ollama serve
# 默认运行在 http://localhost:11434安装依赖
npm install
# 或
yarn install启动插件服务器
# 使用默认端口 50053
npm start
# 使用自定义端口
node server.js 50054
# 开发模式(自动重启)
npm run dev配置插件
在 config/plugins.yaml 中添加:
plugins:
- name: "LangChain Ollama Node.js"
kind: "langchain_ollama_nodejs"
endpoint: "localhost:50053"
enabled: true
health_check: true使用示例
基础使用
nodes:
- name: "llm_task"
type: "langchain_ollama_nodejs"
parameters:
model: "llama2"
prompt: "What is the capital of France?"
temperature: 0.7
max_tokens: 512使用模板变量
nodes:
- name: "analyze_data"
type: "langchain_ollama_nodejs"
parameters:
model: "llama2"
prompt: "Analyze the following data: {{$.parent.data}}"
system_message: "You are a data analyst."
temperature: 0.3流式输出
nodes:
- name: "story_generator"
type: "langchain_ollama_nodejs"
parameters:
model: "llama2"
prompt: "Write a short story about a robot."
stream: "true"
max_tokens: 1024代码生成
nodes:
- name: "code_generator"
type: "langchain_ollama_nodejs"
parameters:
model: "codellama"
prompt: "Write a JavaScript function to calculate fibonacci numbers"
system_message: "You are an expert JavaScript programmer."
temperature: 0.2
max_tokens: 512翻译任务
nodes:
- name: "translator"
type: "langchain_ollama_nodejs"
parameters:
model: "llama2"
prompt: "Translate to Chinese: {{$.parent.text}}"
system_message: "You are a professional translator."
temperature: 0.1参数说明
| 参数 | 类型 | 必填 | 默认值 | 说明 | |------|------|------|--------|------| | model | string | 是 | llama2 | Ollama 模型名称 | | prompt | string | 是 | - | 提示词文本 | | temperature | double | 否 | 0.7 | 采样温度 (0.0-1.0) | | max_tokens | int | 否 | 512 | 最大生成 token 数 | | ollama_base_url | string | 否 | http://localhost:11434 | Ollama API 地址 | | stream | string | 否 | false | 是否启用流式输出 | | system_message | string | 否 | - | 系统消息 |
支持的模型
通用模型
llama2- Meta 的 Llama 2 模型llama2:13b- Llama 2 13B 版本llama2:70b- Llama 2 70B 版本mistral- Mistral AI 的模型mixtral- Mixtral 8x7B 模型
代码模型
codellama- 代码生成专用codellama:python- Python 专用codellama:13b- 13B 版本
中文模型
llama2-chinese- 中文优化版本qwen- 阿里通义千问
查看所有可用模型
ollama list测试
测试连接
# 测试 Ollama 是否运行
curl http://localhost:11434/api/tags
# 测试插件健康检查
grpcurl -plaintext localhost:50053 \
node_plugin.NodePluginService/HealthCheck测试推理
# 使用 Ollama CLI 测试
ollama run llama2 "Hello, how are you?"运行测试脚本
npm test开发
项目结构
langchain-ollama-nodejs/
├── server.js # 主服务器文件
├── package.json # 依赖配置
├── README.md # 本文件
└── test.js # 测试脚本(可选)调试
# 启用详细日志
DEBUG=* node server.js
# 使用 nodemon 自动重启
npm run dev扩展功能
可以扩展插件支持更多 LangChain.js 功能:
const { ConversationChain } = require('langchain/chains');
const { BufferMemory } = require('langchain/memory');
// 添加记忆功能
const memory = new BufferMemory();
const chain = new ConversationChain({ llm, memory });性能优化
1. 使用量化模型
# 下载量化版本(更快,占用更少内存)
ollama pull llama2:7b-q4_02. 调整并发数
在 server.js 中可以配置 gRPC 服务器的并发处理能力。
3. 使用 GPU 加速
Ollama 会自动使用 GPU(如果可用)。
故障排查
Ollama 未运行
# 检查 Ollama 服务
ps aux | grep ollama
# 启动 Ollama
ollama serve模型未下载
# 下载模型
ollama pull llama2
# 查看已下载的模型
ollama list端口冲突
# 使用不同端口启动
node server.js 50054Node.js 版本过低
# 检查版本
node --version
# 升级 Node.js
nvm install 18
nvm use 18依赖安装失败
# 清理缓存
npm cache clean --force
# 重新安装
rm -rf node_modules package-lock.json
npm install与 Python 版本对比
| 特性 | Python 版本 | Node.js 版本 | |------|-------------|--------------| | 性能 | 较快 | 快 | | 内存占用 | 中等 | 较低 | | 生态系统 | LangChain | LangChain.js | | 异步支持 | asyncio | 原生 async/await | | 部署 | 需要 Python 环境 | 需要 Node.js 环境 | | 适用场景 | 数据科学、AI | Web 应用、微服务 |
高级用法
使用 Prompt Template
const { PromptTemplate } = require('@langchain/core/prompts');
const template = new PromptTemplate({
template: "Translate the following to {language}: {text}",
inputVariables: ["language", "text"],
});链式调用
const { LLMChain } = require('langchain/chains');
const chain = new LLMChain({ llm, prompt: template });
const result = await chain.call({
language: "Chinese",
text: "Hello, world!"
});添加回调
const llm = new Ollama({
model: 'llama2',
callbacks: [{
handleLLMNewToken(token) {
console.log('Token:', token);
}
}]
});相关资源
许可证
MIT
