Optimize Prompt
工作流概述
这是一个包含10个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "heyKyETy1uK0xoX4",
"meta": {
"instanceId": "d00caf92aa0876c596905aea78b35fa33a722cc8e479133822c17064d15c2c1d",
"templateCredsSetupCompleted": true
},
"name": "Optimize Prompt",
"tags": [],
"nodes": [
{
"id": "a58be0f5-d11d-4bec-bd8c-0c3a7325b22b",
"name": "When Executed by Another Workflow",
"type": "n8n-nodes-base.executeWorkflowTrigger",
"position": [
-1880,
820
],
"parameters": {
"inputSource": "passthrough"
},
"typeVersion": 1.1
},
{
"id": "67fe408f-e889-4eeb-9e48-f60a579c69f0",
"name": "AI Agent",
"type": "@n8n/n8n-nodes-langchain.agent",
"position": [
-1600,
720
],
"parameters": {
"text": "={{ $json.query }}",
"options": {
"systemMessage": "Given the user's initial prompt below, please enhance it. Start with a clear, precise instruction at the beginning. Include specific details about the desired context, outcome, length, format, and style. Provide examples of the desired output format, if applicable. Use appropriate leading words or phrases to guide the desired output, especially for code generation. Avoid any vague or imprecise language. Rather than only stating what not to do, provide guidance on what should be done instead. Ensure the revised prompt remains true to the user's original intent. Do not provide examples of desired prompt format, only describe it. Format your response in markdown."
},
"promptType": "define",
"hasOutputParser": true
},
"typeVersion": 1.7
},
{
"id": "8a041b31-1873-4559-96d0-35d313bffbbd",
"name": "Telegram3",
"type": "n8n-nodes-base.telegram",
"onError": "continueErrorOutput",
"position": [
-1000,
820
],
"webhookId": "4f57022f-14cf-4c3e-b810-ae9395bf3d04",
"parameters": {
"text": "={{ $json.text }}",
"chatId": "={{ $('When Executed by Another Workflow').item.json.chat_id }}",
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"id": "Vh36aBswWhClYxBM",
"name": "Telegram account 2"
}
},
"typeVersion": 1.1
},
{
"id": "5161b177-0663-41c5-b778-ac14756f699c",
"name": "OpenAI Chat Model",
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"position": [
-1680,
860
],
"parameters": {
"model": {
"__rl": true,
"mode": "list",
"value": "gpt-4o-mini"
},
"options": {}
},
"credentials": {
"openAiApi": {
"id": "vIXW5likFrTSZUgz",
"name": "Litellm-account"
}
},
"typeVersion": 1.2
},
{
"id": "d5f36955-74a0-4a9a-b49d-0230d6ee35bf",
"name": "Split into chunks1",
"type": "n8n-nodes-base.code",
"position": [
-1180,
820
],
"parameters": {
"jsCode": "// Get the entire output of the previous node
let text = $input.all() || '';
// Convert the output to a string if it's not already
if (typeof text !== 'string') {
text = JSON.stringify(text, null, 2); // Pretty-print JSON objects
}
// Replace multiple newlines (\n\n+) with a single newline (\n)
text = text.replace(/\n{2,}/g, '\n');
const maxLength = 3072; // Telegram message character limit
const messages = [];
// Add an optional header for the first chunk
const header = `# Optimized prompt\n\n`;
let currentText = header + text;
// Split the output into chunks of maxLength without splitting words
while (currentText.length > 0) {
let chunk = currentText.slice(0, maxLength);
// Ensure we don't split in the middle of a word
if (chunk.length === maxLength && currentText[maxLength] !== ' ') {
const lastSpaceIndex = chunk.lastIndexOf(' ');
if (lastSpaceIndex > -1) {
chunk = chunk.slice(0, lastSpaceIndex);
}
}
messages.push(chunk.trim()); // Trim extra whitespace for cleaner output
currentText = currentText.slice(chunk.length).trim(); // Remove the chunk from the remaining text
}
// Return the split messages in Markdown format
return messages.map((chunk) => ({ json: { text: `\`\`\`markdown\n${chunk}\n\`\`\`` } }));
"
},
"typeVersion": 2
},
{
"id": "b22f3481-caeb-4506-8fe0-c7e2597772b9",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"disabled": true,
"position": [
-2120,
600
],
"parameters": {
"color": 5,
"width": 389,
"height": 381,
"content": "## Trigger
- Trigger can be anything. For this example the trigger is a call from another workflow and a received Telegram message.
- Note that this workflow can be integrated in the middle of another larger workflow."
},
"typeVersion": 1
},
{
"id": "2bf7ebcc-2d34-4c56-b9de-c930ccb4f30f",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"disabled": true,
"position": [
-1720,
600
],
"parameters": {
"color": 6,
"width": 489,
"height": 381,
"content": "# Inference / Optimization
- Incoming trigger is processed by a LLM with a specific system prompt set aimed at improving the input prompt."
},
"typeVersion": 1
},
{
"id": "ccc5f97e-6215-41fc-9633-f57857743282",
"name": "Simple Memory",
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"position": [
-1340,
860
],
"parameters": {},
"typeVersion": 1.3
},
{
"id": "3bfb31b6-add3-4d5b-989e-df88d69e07e8",
"name": "Sticky Note3",
"type": "n8n-nodes-base.stickyNote",
"disabled": true,
"position": [
-1220,
600
],
"parameters": {
"width": 349,
"height": 381,
"content": "# Improved prompt:
- Send as a response
- Use as input for next nodes"
},
"typeVersion": 1
},
{
"id": "a36fdc9d-d000-4120-99e8-53d49edec74a",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"disabled": true,
"position": [
-2120,
1000
],
"parameters": {
"color": 7,
"width": 1249,
"height": 541,
"content": "# Workflow Documentation
## Description:
This workflow is designed to optimize prompts by enhancing user inputs for clarity and specificity using AI. The workflow takes a user-provided prompt as input and uses a Natural Language Processing (NLP) model to refine and improve the prompt. The optimized prompt is then sent back to the user, ready for use in further workflows or processes.
## Setup:
1. This workflow is suitable for users who want to improve their prompts for better communication and understanding in their workflows.
2. The workflow utilizes an AI Agent powered by an OpenAI Chat Model to enhance user prompts.
3. A Telegram node is used to deliver the optimized prompt back to the user.
4. Ensure you have the necessary credentials set up for Telegram and OpenAI accounts.
5. Customize the workflow's settings, such as the AI model used for prompt optimization, to suit your requirements.
6. Activate the workflow once all configurations are set to start optimizing prompts efficiently.
## Expected Outcomes:
- Users can provide vague or imprecise prompts as input to the workflow.
- The AI Agent will refine and optimize the prompt, adding clarity and specific details.
- The optimized prompt will be delivered back to the user via Telegram for further use in workflows or processes.
For more detailed instructions and guidelines on using this workflow, refer to the detailed setup guide above."
},
"typeVersion": 1
}
],
"active": false,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "05beb500-d266-45e7-8f5a-ad3a8c9a72e1",
"connections": {
"AI Agent": {
"main": [
[
{
"node": "Split into chunks1",
"type": "main",
"index": 0
}
]
]
},
"Simple Memory": {
"ai_memory": [
[
{
"node": "AI Agent",
"type": "ai_memory",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Split into chunks1": {
"main": [
[
{
"node": "Telegram3",
"type": "main",
"index": 0
}
]
]
},
"When Executed by Another Workflow": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Executeworkflowtrigger
- @N8N/N8N Nodes Langchain.Agent
- Telegram
- @N8N/N8N Nodes Langchain.Lmchatopenai
- Code
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作