n8n文本语意识别与问题自动补充工作流的深化及企业级部署
以下是进一步深化的 n8n 文本语意识别与问题自动补充工作流 技术方案,聚焦工程化落地细节,补充多场景适配、错误处理及监控体系,符合企业级大模型应用标准:
一、架构设计
二、核心模块实现
1. 语意识别引擎
技术栈:
- 云端方案:OpenAI
gpt-3.5-turbo
/ Claude Haiku - 本地方案:Ollama +
nomic-embed-text
嵌入模型
n8n 配置:
- HTTP Request Node:method: POSTurl: https://api.openai.com/v1/chat/completionsheaders:Authorization: Bearer {OpenAI_API_Key}body:model: gpt-3.5-turbomessages:- role: systemcontent: |你是一个专业意图分析器,输出JSON格式:{"intent": ["订单查询", "技术支持"],"confidence": 0.92,"missing_fields": ["order_id", "product_code"]}- role: usercontent: "{ { $input.text } }"
2. 动态追问生成器
决策逻辑:
def generate_followup(intent_data):if intent_data["confidence"] < 0.8:return "高模糊追问模板"elif len(intent_data["missing_fields"]) > 0:return "字段补全模板"else:return None
n8n Function Node 代码:
const { intent, missing_fields } = $item.json;
const templates = {"高模糊追问": "您的问题涉及多个方面,请问您具体想了解:\n1. 产品功能\n2. 订单状态\n3. 技术支持?","字段补全": `请提供缺少的信息:${missing_fields.join(', ')}`
};if (intent.confidence < 0.8) {return { followup: templates["高模糊追问"] };
} else if (missing_fields.length > 0) {return { followup: templates["字段补全"] };
}
return {};
3. 上下文记忆池
实现方案:
三、完整工作流案例:智能客服系统
工作流拓扑
关键节点配置
- 语意识别节点 (OpenAI)
{"model": "gpt-3.5-turbo","temperature": 0.2,"response_format": { "type": "json_object" },"messages": [{"role": "system", "content": "输出JSON: {intent:string, confidence:float, missing_fields:string[]}"},{"role": "user", "content": "{{$json.message.text}}"}]
}
- 追问生成节点 (函数计算)
// 基于历史上下文生成个性化追问
const memory = await redis.hGet(`user:${userId}`, 'context');
const prompt = `历史对话:${memory}\n当前问题:${text}\n请生成2个追问选项`;
const options = await openai.chat.completions.create({model: "gpt-4-turbo",messages: [{role: "user", content: prompt}]
});
return {buttons: options.choices[0].message.content.split('\n')
};
- Telegram 按钮响应
- Telegram Node:resource: Send MessagechatId: "{{$node.user_input.json.chat.id}}"text: "{{$node.followup.json.question}}"replyMarkup:inline_keyboard:- - text: "{{$node.followup.json.buttons[0]}}"callback_data: "opt1"- text: "{{$node.followup.json.buttons[1]}}"callback_data: "opt2"
四、性能优化方案
1. 分层处理架构
层级 | 处理内容 | 技术实现 |
---|---|---|
L1 | 简单意图匹配 | Regex + 规则引擎 |
L2 | 中等复杂度 | 本地Ollama模型 |
L3 | 高复杂度 | 云端LLM API |
2. 缓存策略
// Redis缓存层实现
const cacheKey = `intent:${md5(text)}`;
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);// 无缓存时调用AI
const result = await detectIntent(text);
redis.setex(cacheKey, 300, JSON.stringify(result)); // 缓存5分钟
return result;
3. 流量控制
- n8n配置项:EXECUTIONS_DATA_PRUNE: trueEXECUTIONS_DATA_MAX_AGE: 24 # 小时QUEUE_MODE: "redis"RATE_LIMIT: enabled: truemax: 100 # 每分钟最大请求数
五、安全与合规实现
- 敏感数据过滤
// 在Function节点中添加
const redactText = (text) => {return text.replace(/\d{4}-\d{4}-\d{4}-\d{4}/g, '****-****-****-****') // 信用卡.replace(/([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})/g, '***@***.***'); // 邮箱
};
- 合规日志记录
- PostgreSQL节点:operation: Inserttable: request_logscolumns:- timestamp: "{{ $now }}"- user_id: "{{ $json.user }}"- intent_type: "{{ $json.intent }}"- text_hash: "{{ sha256($json.text) }}"