Skip to content

KeywordExtract 组件

概述

KeywordExtract组件是RAGFlow 文本处理组件,关键词提取组件,从文本中提取重要关键词和实体。

主要功能

  • 🔑 关键词和实体提取
  • 📊 支持多种配置选项和参数调节
  • 🔧 与其他组件无缝集成
  • ⚡ 高性能处理和错误恢复

适用场景

  • 文档标签生成
  • 搜索词提取
  • 内容摘要关键词
  • 实体识别

参数配置

基础参数

参数名类型必填默认值说明
methodstringtfidf提取方法
top_knumber10提取数量
min_lengthnumber2最小词长

详细说明

主要参数说明

method
  • 类型: string
  • 描述: 提取方法
  • 默认值: tfidf
  • 是否必填: 否
top_k
  • 类型: number
  • 描述: 提取数量
  • 默认值: 10
  • 是否必填: 否
min_length
  • 类型: number
  • 描述: 最小词长
  • 默认值: 2
  • 是否必填: 否

输入输出

输入格式

KeywordExtract组件接受上游组件的标准输出:

json
{
  "content": "输入内容",
  "component_id": "upstream_component_id",
  "reference": []
}

输出格式

json
{
  "content": "处理后的输出内容",
  "component_id": "keyword_component_id",
  "metadata": {
    "processing_time": 0.123,
    "success": true
  },
  "reference": []
}

使用示例

示例1: 基础配置

json
{
  "component_name": "KeywordExtract",
  "params": {
    "method": tfidf
  }
}

示例2: 高级配置

json
{
  "component_name": "KeywordExtract",
  "params": {
    "method": "tfidf",
    "top_k": 10,
    "min_length": 2
  }
}

前端实现

节点组件

typescript
// web/src/pages/flow/canvas/node/keyword-node.tsx
export function KeywordExtractNode({ 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/keyword-form/index.tsx
const KeywordExtractForm: React.FC<IOperatorForm> = ({ onValuesChange, form }) => {
  return (
    <Form form={form} layout="vertical" onValuesChange={onValuesChange}>

      <Form.Item
        name="method"
        label="提取方法"
        rules={[{ required: false, message: '请输入提取方法' }]}
      >
        <Input placeholder="请输入提取方法" />
      </Form.Item>

      <Form.Item
        name="top_k"
        label="提取数量"
      >
        <InputNumber style={{ width: '100%' }} placeholder="10" />
      </Form.Item>

      <Form.Item
        name="min_length"
        label="最小词长"
      >
        <InputNumber style={{ width: '100%' }} placeholder="2" />
      </Form.Item>

    </Form>
  );
};

后端实现

参数类

python
# agent/component/keyword.py
class KeywordExtractParam(ComponentParamBase):
    """
    KeywordExtract组件参数
    """
    
    def __init__(self):
        super().__init__()
        self.method = "tfidf"  # 提取方法
        self.top_k = 10  # 提取数量
        self.min_length = 2  # 最小词长
        
    def check(self):
        """参数验证"""
        # 验证必填参数

class KeywordExtract(ComponentBase):
    """
    KeywordExtract组件实现
    """
    
    component_name = "KeywordExtract"
    
    def _run(self, history, **kwargs):
        """
        执行KeywordExtract组件逻辑
        """
        # 获取输入
        input_df = self.get_input()
        
        if input_df.empty:
            raise ValueError("KeywordExtract组件需要输入数据")
        
        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"KeywordExtract组件处理失败: {str(e)}")
        return f"处理失败: {str(e)}"

3. 性能优化

  • 合理使用缓存机制
  • 优化处理算法
  • 控制资源使用

常见问题

Q1: 组件配置后不生效怎么办?

A: 检查参数格式和必填项是否正确配置。

Q2: 处理大量数据时性能较慢?

A: 可以调整批处理大小或使用异步处理。

Q3: 如何调试组件执行过程?

A: 使用调试模式和日志功能查看详细执行信息。

相关组件

推荐搭配组件

  • Begin: 工作流入口
  • Answer: 最终输出
  • 其他相关的处理组件

典型工作流模式

Begin → KeywordExtract → Answer
其他组件 → KeywordExtract → 下游组件

组件版本: v1.0.0
最后更新: 2025-07-12