Appearance
Switch 组件
概述
Switch组件是RAGFlow 逻辑控制组件,条件分支组件,基于规则进行条件判断和路由选择。
主要功能
- 🔀 基于条件规则的分支控制
- 📊 支持多种配置选项和参数调节
- 🔧 与其他组件无缝集成
- ⚡ 高性能处理和错误恢复
适用场景
- 基于用户权限的流程分支
- 根据内容长度选择处理方式
- 多条件逻辑判断
- 异常情况处理分流
参数配置
基础参数
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
conditions | array | 是 | [] | 条件规则列表 |
logical_operator | string | 否 | and | 逻辑运算符 |
default_output | string | 否 | 默认输出路径 |
详细说明
主要参数说明
conditions
- 类型: array
- 描述: 条件规则列表
- 默认值: []
- 是否必填: 是
logical_operator
- 类型: string
- 描述: 逻辑运算符
- 默认值: and
- 是否必填: 否
default_output
- 类型: string
- 描述: 默认输出路径
- 默认值:
- 是否必填: 否
输入输出
输入格式
Switch组件接受上游组件的标准输出:
json
{
"content": "输入内容",
"component_id": "upstream_component_id",
"reference": []
}输出格式
json
{
"content": "处理后的输出内容",
"component_id": "switch_component_id",
"metadata": {
"processing_time": 0.123,
"success": true
},
"reference": []
}使用示例
示例1: 基础配置
json
{
"component_name": "Switch",
"params": {
"conditions": []
}
}示例2: 高级配置
json
{
"component_name": "Switch",
"params": {
"conditions": [],
"logical_operator": "and",
"default_output": ""
}
}前端实现
节点组件
typescript
// web/src/pages/flow/canvas/node/switch-node.tsx
export function SwitchNode({ id, data, isConnectable, selected }: NodeProps) {
return (
<section className={`${styles.ragNode} ${selected ? styles.selectedNode : ''}`}>
<Handle type="target" position={Position.Left} isConnectable={isConnectable} />
<Handle type="source" position={Position.Right} isConnectable={isConnectable} />
<NodeHeader id={id} name={data.name} label={data.label} />
<div className={styles.nodeBody}>
<div className={styles.nodeInfo}>
<span className={styles.nodeDescription}>条件分支组件,基于规则进行条件判断和路由选择</span>
</div>
</div>
</section>
);
}配置表单
typescript
// web/src/pages/flow/form/switch-form/index.tsx
const SwitchForm: React.FC<IOperatorForm> = ({ onValuesChange, form }) => {
return (
<Form form={form} layout="vertical" onValuesChange={onValuesChange}>
<Form.Item
name="logical_operator"
label="逻辑运算符"
rules={[{ required: false, message: '请输入逻辑运算符' }]}
>
<Input placeholder="请输入逻辑运算符" />
</Form.Item>
<Form.Item
name="default_output"
label="默认输出路径"
rules={[{ required: false, message: '请输入默认输出路径' }]}
>
<Input placeholder="请输入默认输出路径" />
</Form.Item>
</Form>
);
};后端实现
参数类
python
# agent/component/switch.py
class SwitchParam(ComponentParamBase):
"""
Switch组件参数
"""
def __init__(self):
super().__init__()
self.conditions = [] # 条件规则列表
self.logical_operator = "and" # 逻辑运算符
self.default_output = "" # 默认输出路径
def check(self):
"""参数验证"""
# 验证必填参数
self.check_empty(["conditions"], "必填参数不能为空")
class Switch(ComponentBase):
"""
Switch组件实现
"""
component_name = "Switch"
def _run(self, history, **kwargs):
"""
执行Switch组件逻辑
"""
# 获取输入
input_df = self.get_input()
if input_df.empty:
raise ValueError("Switch组件需要输入数据")
input_content = input_df.iloc[0]["content"]
# 执行核心处理逻辑
result = self._process_content(input_content)
# 返回结果
return pd.DataFrame([{
"content": result,
"component_id": self._id,
"metadata": {
"processing_time": time.time() - start_time,
"success": True
},
"reference": input_df.iloc[0].get("reference", [])
}])
def _process_content(self, content):
"""
处理内容的核心逻辑
"""
# TODO: 实现具体的处理逻辑
return f"已处理: {content}"最佳实践
1. 参数配置建议
- 根据具体使用场景调整参数
- 注意参数之间的依赖关系
- 合理设置超时和重试机制
2. 错误处理
python
def robust_processing(self, content):
"""
带错误处理的处理方法
"""
try:
return self._process_content(content)
except Exception as e:
logger.error(f"Switch组件处理失败: {str(e)}")
return f"处理失败: {str(e)}"3. 性能优化
- 合理使用缓存机制
- 优化处理算法
- 控制资源使用
常见问题
Q1: 组件配置后不生效怎么办?
A: 检查参数格式和必填项是否正确配置。
Q2: 处理大量数据时性能较慢?
A: 可以调整批处理大小或使用异步处理。
Q3: 如何调试组件执行过程?
A: 使用调试模式和日志功能查看详细执行信息。
相关组件
推荐搭配组件
典型工作流模式
Begin → Switch → Answer
其他组件 → Switch → 下游组件组件版本: v1.0.0
最后更新: 2025-07-12