Files
aiagent/backend/app/services/workflow_templates.py

324 lines
9.2 KiB
Python
Raw Normal View History

2026-01-19 00:09:36 +08:00
"""
工作流模板服务
提供预设的工作流模板支持快速创建
"""
from typing import Dict, Any, List
import logging
logger = logging.getLogger(__name__)
# 预设工作流模板
WORKFLOW_TEMPLATES = {
"simple_llm": {
"name": "简单LLM工作流",
"description": "一个简单的LLM调用工作流包含开始、LLM和结束节点",
"nodes": [
{
"id": "start-1",
"type": "start",
"position": {"x": 100, "y": 100},
"data": {"label": "开始"}
},
{
"id": "llm-1",
"type": "llm",
"position": {"x": 100, "y": 250},
"data": {
"label": "LLM处理",
"prompt": "请处理以下输入:\n{input}",
"provider": "openai",
"model": "gpt-3.5-turbo",
"temperature": 0.7
}
},
{
"id": "end-1",
"type": "end",
"position": {"x": 100, "y": 400},
"data": {"label": "结束"}
}
],
"edges": [
{
"id": "e1",
"source": "start-1",
"target": "llm-1",
"sourceHandle": "bottom",
"targetHandle": "top"
},
{
"id": "e2",
"source": "llm-1",
"target": "end-1",
"sourceHandle": "bottom",
"targetHandle": "top"
}
]
},
"conditional_llm": {
"name": "条件判断LLM工作流",
"description": "根据条件判断调用不同的LLM处理",
"nodes": [
{
"id": "start-1",
"type": "start",
"position": {"x": 100, "y": 100},
"data": {"label": "开始"}
},
{
"id": "condition-1",
"type": "condition",
"position": {"x": 100, "y": 200},
"data": {
"label": "条件判断",
"condition": "{value} > 10"
}
},
{
"id": "llm-true",
"type": "llm",
"position": {"x": -100, "y": 350},
"data": {
"label": "True分支LLM",
"prompt": "值大于10请分析{input}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "llm-false",
"type": "llm",
"position": {"x": 300, "y": 350},
"data": {
"label": "False分支LLM",
"prompt": "值小于等于10请分析{input}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "end-1",
"type": "end",
"position": {"x": 100, "y": 500},
"data": {"label": "结束"}
}
],
"edges": [
{
"id": "e1",
"source": "start-1",
"target": "condition-1"
},
{
"id": "e2",
"source": "condition-1",
"target": "llm-true",
"sourceHandle": "true"
},
{
"id": "e3",
"source": "condition-1",
"target": "llm-false",
"sourceHandle": "false"
},
{
"id": "e4",
"source": "llm-true",
"target": "end-1"
},
{
"id": "e5",
"source": "llm-false",
"target": "end-1"
}
]
},
"data_transform_llm": {
"name": "数据转换+LLM工作流",
"description": "先进行数据转换再调用LLM处理",
"nodes": [
{
"id": "start-1",
"type": "start",
"position": {"x": 100, "y": 100},
"data": {"label": "开始"}
},
{
"id": "transform-1",
"type": "transform",
"position": {"x": 100, "y": 200},
"data": {
"label": "数据转换",
"mode": "mapping",
"mapping": {
"input_text": "raw_input",
"user_id": "id"
}
}
},
{
"id": "llm-1",
"type": "llm",
"position": {"x": 100, "y": 300},
"data": {
"label": "LLM处理",
"prompt": "处理转换后的数据:{input_text}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "end-1",
"type": "end",
"position": {"x": 100, "y": 400},
"data": {"label": "结束"}
}
],
"edges": [
{
"id": "e1",
"source": "start-1",
"target": "transform-1"
},
{
"id": "e2",
"source": "transform-1",
"target": "llm-1"
},
{
"id": "e3",
"source": "llm-1",
"target": "end-1"
}
]
},
"multi_llm_chain": {
"name": "多LLM链式工作流",
"description": "多个LLM节点链式调用实现复杂处理流程",
"nodes": [
{
"id": "start-1",
"type": "start",
"position": {"x": 100, "y": 100},
"data": {"label": "开始"}
},
{
"id": "llm-1",
"type": "llm",
"position": {"x": 100, "y": 200},
"data": {
"label": "第一步分析",
"prompt": "第一步:分析输入数据:{input}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "llm-2",
"type": "llm",
"position": {"x": 100, "y": 300},
"data": {
"label": "第二步处理",
"prompt": "第二步:基于第一步的结果进行处理:{input}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "llm-3",
"type": "llm",
"position": {"x": 100, "y": 400},
"data": {
"label": "第三步总结",
"prompt": "第三步:总结最终结果:{input}",
"provider": "openai",
"model": "gpt-3.5-turbo"
}
},
{
"id": "end-1",
"type": "end",
"position": {"x": 100, "y": 500},
"data": {"label": "结束"}
}
],
"edges": [
{
"id": "e1",
"source": "start-1",
"target": "llm-1"
},
{
"id": "e2",
"source": "llm-1",
"target": "llm-2"
},
{
"id": "e3",
"source": "llm-2",
"target": "llm-3"
},
{
"id": "e4",
"source": "llm-3",
"target": "end-1"
}
]
}
}
def get_template(template_id: str) -> Dict[str, Any]:
"""
获取工作流模板
Args:
template_id: 模板ID
Returns:
模板数据
"""
return WORKFLOW_TEMPLATES.get(template_id)
def list_templates() -> List[Dict[str, Any]]:
"""
获取所有模板列表
Returns:
模板列表每个模板包含idnamedescription
"""
return [
{
"id": template_id,
"name": template["name"],
"description": template["description"]
}
for template_id, template in WORKFLOW_TEMPLATES.items()
]
def create_from_template(template_id: str, name: str = None, description: str = None) -> Dict[str, Any]:
"""
从模板创建工作流数据
Args:
template_id: 模板ID
name: 工作流名称可选默认使用模板名称
description: 工作流描述可选默认使用模板描述
Returns:
工作流数据
"""
template = get_template(template_id)
if not template:
raise ValueError(f"模板不存在: {template_id}")
return {
"name": name or template["name"],
"description": description or template["description"],
"nodes": template["nodes"],
"edges": template["edges"]
}