refactor(workflow): relocate template directory into workflow

This commit is contained in:
Eternity
2026-02-03 15:24:16 +08:00
parent ec41d45234
commit e196f86e30
5 changed files with 18 additions and 15 deletions

View File

@@ -4,16 +4,19 @@
从文件系统加载预定义的工作流模板
"""
import os
from pathlib import Path
from typing import Optional
import yaml
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
class TemplateLoader:
"""工作流模板加载器"""
def __init__(self, templates_dir: str = "app/templates/workflows"):
def __init__(self, templates_dir: str = TEMPLATE_DIR):
"""初始化模板加载器
Args:
@@ -22,7 +25,7 @@ class TemplateLoader:
self.templates_dir = Path(templates_dir)
if not self.templates_dir.exists():
raise ValueError(f"模板目录不存在: {templates_dir}")
def list_templates(self) -> list[dict]:
"""列出所有可用的模板
@@ -30,22 +33,22 @@ class TemplateLoader:
模板列表,每个模板包含 id, name, description 等信息
"""
templates = []
# 遍历模板目录
for template_dir in self.templates_dir.iterdir():
if not template_dir.is_dir():
continue
# 检查是否有 template.yml 文件
template_file = template_dir / "template.yml"
if not template_file.exists():
continue
try:
# 读取模板配置
with open(template_file, 'r', encoding='utf-8') as f:
template_data = yaml.safe_load(f)
# 提取模板信息
templates.append({
"id": template_dir.name,
@@ -59,9 +62,9 @@ class TemplateLoader:
except Exception as e:
print(f"加载模板 {template_dir.name} 失败: {e}")
continue
return templates
def load_template(self, template_id: str) -> Optional[dict]:
"""加载指定的模板
@@ -73,14 +76,14 @@ class TemplateLoader:
"""
template_dir = self.templates_dir / template_id
template_file = template_dir / "template.yml"
if not template_file.exists():
return None
try:
with open(template_file, 'r', encoding='utf-8') as f:
template_data = yaml.safe_load(f)
# 返回工作流配置部分
return {
"name": template_data.get("name", template_id),
@@ -94,7 +97,7 @@ class TemplateLoader:
except Exception as e:
print(f"加载模板 {template_id} 失败: {e}")
return None
def get_template_readme(self, template_id: str) -> Optional[str]:
"""获取模板的 README 文档
@@ -106,10 +109,10 @@ class TemplateLoader:
"""
template_dir = self.templates_dir / template_id
readme_file = template_dir / "README.md"
if not readme_file.exists():
return None
try:
with open(readme_file, 'r', encoding='utf-8') as f:
return f.read()

View File

@@ -0,0 +1,219 @@
# 智能客服工作流模板
id: customer_service_v1
name: 智能客服工作流
description: 智能客服场景,包含意图识别、知识库查询和回复生成
category: customer_service
version: "1.0.0"
author: RedBear Memory Team
tags:
- 客服
- 意图识别
- 知识库
- 多步骤
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 200
- id: intent_recognition
type: llm
name: 意图识别
config:
prompt: |
分析用户的问题,识别意图类型。
用户问题:{{ var.user_message }}
请从以下类型中选择一个:
- product_inquiry: 产品咨询
- technical_support: 技术支持
- complaint: 投诉建议
- other: 其他
只返回类型名称,不要其他内容。
model: gpt-3.5-turbo
temperature: 0.3
max_tokens: 50
position:
x: 300
y: 200
- id: intent_router
type: condition
name: 意图路由
position:
x: 500
y: 200
- id: product_handler
type: llm
name: 产品咨询处理
config:
prompt: |
用户咨询产品相关问题。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请提供专业、友好的产品咨询回复。
model: gpt-3.5-turbo
temperature: 0.7
max_tokens: 500
position:
x: 700
y: 100
- id: support_handler
type: llm
name: 技术支持处理
config:
prompt: |
用户需要技术支持。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请提供详细的技术支持方案。
model: gpt-3.5-turbo
temperature: 0.5
max_tokens: 800
position:
x: 700
y: 200
- id: complaint_handler
type: llm
name: 投诉处理
config:
prompt: |
用户提出投诉或建议。
问题:{{ var.user_message }}
意图:{{ node.intent_recognition.output }}
请以同理心回应,并提供解决方案。
model: gpt-3.5-turbo
temperature: 0.8
max_tokens: 600
position:
x: 700
y: 300
- id: general_handler
type: llm
name: 通用处理
config:
prompt: |
用户的问题类型:其他
问题:{{ var.user_message }}
请提供友好的回复。
model: gpt-3.5-turbo
temperature: 0.7
max_tokens: 400
position:
x: 700
y: 400
- id: end
type: end
name: 结束
position:
x: 900
y: 200
edges:
- source: start
target: intent_recognition
label: 开始分析
- source: intent_recognition
target: intent_router
label: 识别完成
- source: intent_router
target: product_handler
condition: "'product_inquiry' in node['intent_recognition']['output']"
label: 产品咨询
- source: intent_router
target: support_handler
condition: "'technical_support' in node['intent_recognition']['output']"
label: 技术支持
- source: intent_router
target: complaint_handler
condition: "'complaint' in node['intent_recognition']['output']"
label: 投诉建议
- source: intent_router
target: general_handler
condition: "True" # 默认路径
label: 其他
- source: product_handler
target: end
label: 完成
- source: support_handler
target: end
label: 完成
- source: complaint_handler
target: end
label: 完成
- source: general_handler
target: end
label: 完成
# 变量定义
variables:
- name: user_message
type: string
required: true
description: 用户的消息
default: ""
- name: user_name
type: string
required: false
description: 用户姓名(可选)
default: "客户"
# 执行配置
execution_config:
max_execution_time: 120
max_iterations: 10
# 触发器
triggers: []
# 使用示例
examples:
- name: 产品咨询
description: 用户咨询产品功能
input:
user_message: "你们的产品支持多语言吗?"
user_name: "张三"
expected_output: "产品功能介绍"
- name: 技术支持
description: 用户遇到技术问题
input:
user_message: "我无法登录系统,一直显示密码错误"
user_name: "李四"
expected_output: "技术支持方案"
- name: 投诉处理
description: 用户提出投诉
input:
user_message: "你们的服务态度太差了,我要投诉"
user_name: "王五"
expected_output: "同理心回应和解决方案"

View File

@@ -0,0 +1,131 @@
# 数据处理工作流模板
id: data_processing_v1
name: 数据处理工作流
description: 数据提取、转换和分析的完整流程
category: data_processing
version: "1.0.0"
author: RedBear Memory Team
tags:
- 数据处理
- ETL
- 分析
- Transform
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 200
- id: extract_data
type: transform
name: 数据提取
config:
expression: |
{
"raw_text": var['input_text'],
"length": len(var['input_text']),
"timestamp": sys['execution_id']
}
position:
x: 300
y: 200
- id: analyze_data
type: llm
name: 数据分析
config:
prompt: |
请分析以下数据:
原始文本:{{ node.extract_data.raw_text }}
文本长度:{{ node.extract_data.length }}
请提供:
1. 主题分类
2. 情感分析
3. 关键信息提取
以 JSON 格式返回结果。
model: gpt-3.5-turbo
temperature: 0.3
max_tokens: 500
position:
x: 500
y: 200
- id: transform_result
type: transform
name: 结果转换
config:
expression: |
{
"original_length": node['extract_data']['length'],
"analysis": node['analyze_data']['output'],
"processed_at": sys['execution_id'],
"status": "completed"
}
position:
x: 700
y: 200
- id: end
type: end
name: 结束
position:
x: 900
y: 200
edges:
- source: start
target: extract_data
label: 开始提取
- source: extract_data
target: analyze_data
label: 开始分析
- source: analyze_data
target: transform_result
label: 转换结果
- source: transform_result
target: end
label: 完成
# 变量定义
variables:
- name: input_text
type: string
required: true
description: 待处理的文本数据
default: ""
# 执行配置
execution_config:
max_execution_time: 180
max_iterations: 5
# 触发器
triggers: []
# 使用示例
examples:
- name: 文本分析
description: 分析一段文本
input:
input_text: "今天天气真好,心情也很愉快。我们公司推出了新产品,市场反响热烈。"
expected_output:
original_length: 35
analysis: "主题:天气和产品,情感:积极"
status: "completed"
- name: 长文本处理
description: 处理较长的文本
input:
input_text: "这是一段很长的文本..."
expected_output:
status: "completed"

View File

@@ -0,0 +1,99 @@
# 多步骤问答工作流
# 演示节点输出参数的使用
id: multi_step_qa_v1
name: 多步骤问答工作流
description: 先分析问题,再生成答案,展示节点间的数据传递
category: advanced
version: "1.0.0"
author: RedBear Memory Team
tags:
- 问答
- 多步骤
- LLM
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 100
- id: analyze_question
type: llm
name: 分析问题
description: 分析用户问题的类型和意图
config:
model_id: gpt-3.5-turbo
temperature: 0.3
max_tokens: 500
messages:
- role: system
content: |
你是一个问题分析专家。请分析用户的问题,提取以下信息:
1. 问题类型(事实性、观点性、操作性等)
2. 问题领域(科技、历史、文化等)
3. 关键词
- role: user
content: "{{ sys.message }}"
position:
x: 300
y: 100
- id: generate_answer
type: llm
name: 生成答案
description: 根据问题分析结果生成详细答案
config:
model_id: gpt-3.5-turbo
temperature: 0.7
max_tokens: 1000
messages:
- role: system
content: |
你是一个专业的AI助手。根据问题分析结果生成准确、详细的答案。
问题分析结果:
{{ analyze_question.output }}
- role: user
content: "{{ sys.message }}"
position:
x: 500
y: 100
- id: end
type: end
name: 结束
config:
output: "{{ generate_answer.output }}"
position:
x: 700
y: 100
edges:
- source: start
target: analyze_question
label: 开始分析
- source: analyze_question
target: generate_answer
label: 生成答案
- source: generate_answer
target: end
label: 完成
# 变量定义
variables:
- name: user_question
type: string
required: true
description: 用户的问题
default: ""
# 执行配置
execution_config:
max_execution_time: 120
max_iterations: 1

View File

@@ -0,0 +1,93 @@
# 简单问答工作流模板
id: simple_qa_v1
name: 简单问答工作流
description: 最基础的问答工作流,适合快速开始
category: basic
version: "1.0.0"
author: RedBear Memory Team
tags:
- 问答
- 基础
- LLM
# 工作流配置
nodes:
- id: start
type: start
name: 开始
position:
x: 100
y: 100
- id: llm_qa
type: llm
name: LLM 问答
config:
# 使用 LangChain 标准的消息格式
messages:
- role: system
content: |
你是一个专业、友好且乐于助人的 AI 助手。
你的职责:
- 准确理解用户的问题并提供有价值的回答
- 保持回答的专业性和准确性
- 如果不确定答案,诚实地告知用户
- 使用清晰、易懂的语言进行交流
回答风格:
- 简洁明了,直击要点
- 必要时提供详细解释和示例
- 使用友好、礼貌的语气
- 适当使用格式化(如列表、段落)提高可读性
model_id: null
temperature: 0.7
max_tokens: 1000
position:
x: 500
y: 100
- id: end
type: end
name: 结束
config:
output: "{{llm_qa.output}}"
position:
x: 900
y: 100
edges:
- source: start
target: llm_qa
label: 开始处理
- source: llm_qa
target: end
label: 完成
# 变量定义
variables: []
# 执行配置
execution_config:
max_execution_time: 60
max_iterations: 1
# 触发器(可选)
triggers: []
# 使用示例
examples:
- name: 基础问答
description: 询问一个简单的问题
input:
user_question: "什么是人工智能?"
expected_output: "关于人工智能的解释"
- name: 技术咨询
description: 询问技术问题
input:
user_question: "如何学习 Python 编程?"
expected_output: "Python 学习建议"