ragflow/plugin/README_zh.md
Song Fuchang a1f06a4fdc
Feat: Support tool calling in Generate component (#7572)
### What problem does this PR solve?

Hello, our use case requires LLM agent to invoke some tools, so I made a
simple implementation here.

This PR does two things:

1. A simple plugin mechanism based on `pluginlib`:

This mechanism lives in the `plugin` directory. It will only load
plugins from `plugin/embedded_plugins` for now.

A sample plugin `bad_calculator.py` is placed in
`plugin/embedded_plugins/llm_tools`, it accepts two numbers `a` and `b`,
then give a wrong result `a + b + 100`.

In the future, it can load plugins from external location with little
code change.

Plugins are divided into different types. The only plugin type supported
in this PR is `llm_tools`, which must implement the `LLMToolPlugin`
class in the `plugin/llm_tool_plugin.py`.
More plugin types can be added in the future.

2. A tool selector in the `Generate` component:

Added a tool selector to select one or more tools for LLM:


![image](https://github.com/user-attachments/assets/74a21fdf-9333-4175-991b-43df6524c5dc)

And with the `bad_calculator` tool, it results this with the `qwen-max`
model:


![image](https://github.com/user-attachments/assets/93aff9c4-8550-414a-90a2-1a15a5249d94)


### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):

Co-authored-by: Yingfeng <yingfeng.zhang@gmail.com>
2025-05-16 16:32:19 +08:00

4.0 KiB
Raw Permalink Blame History

插件

这个文件夹包含了RAGFlow的插件机制。

RAGFlow将会从embedded_plugins子文件夹中递归加载所有的插件。

支持的插件类型

目前,唯一支持的插件类型是llm_tools

  • llm_tools用于供LLM进行调用的工具。

如何添加一个插件

添加一个LLM工具插件是很简单的创建一个插件文件向其中放一个继承自LLMToolPlugin的类,再实现它的get_metadatainvoke方法即可。

  • get_metadata方法:这个方法返回一个LLMToolMetadata对象,其中包含了对这个工具的描述。 这些描述信息将被提供给LLM进行调用和RAGFlow的Web前端用作展示。

  • invoke方法这个方法接受LLM生成的参数并且返回一个str对象,其中包含了这个工具的执行结果。 这个工具的所有执行逻辑都应当放到这个方法里。

当你启动RAGFlow时你会在日志中看见你的插件被加载了

2025-05-15 19:29:08,959 INFO     34670 Recursively importing plugins from path `/some-path/ragflow/plugin/embedded_plugins`
2025-05-15 19:29:08,960 INFO     34670 Loaded llm_tools plugin BadCalculatorPlugin version 1.0.0

也可能会报错,这时就需要根据报错对你的插件进行修复。

示例

我们将会添加一个会给出错误答案的计算器工具,来演示添加插件的过程。

首先,在embedded_plugins/llm_tools文件夹下创建一个插件文件bad_calculator.py

接下来,我们创建一个BadCalculatorPlugin类,继承基类LLMToolPlugin

class BadCalculatorPlugin(LLMToolPlugin):
    _version_ = "1.0.0"

_version_字段是必填的,用于指定这个插件的版本号。

我们的计算器拥有两个输入字段ab,所以我们添加如下的invoke方法到BadCalculatorPlugin类中:

def invoke(self, a: int, b: int) -> str:
    return str(a + b + 100)

invoke方法将会被LLM所调用。这个方法可以有许多参数但它必须返回一个str

最后,我们需要添加一个get_metadata方法来告诉LLM怎样使用我们的bad_calculator工具:

@classmethod
def get_metadata(cls) -> LLMToolMetadata:
    return {
        # 这个工具的名称会提供给LLM
        "name": "bad_calculator",
        # 这个工具的展示名称会提供给RAGFlow的Web前端
        "displayName": "$t:bad_calculator.name",
        # 这个工具的用法描述会提供给LLM
        "description": "A tool to calculate the sum of two numbers (will give wrong answer)",
        # 这个工具的描述会提供给RAGFlow的Web前端
        "displayDescription": "$t:bad_calculator.description",
        # 这个工具的参数
        "parameters": {
            # 第一个参数 - a
            "a": {
                # 参数类型选项为number, string, 或者LLM可以识别的任何类型
                "type": "number",
                # 这个参数的描述会提供给LLM
                "description": "The first number",
                # 这个参数的描述会提供给RAGFlow的Web前端
                "displayDescription": "$t:bad_calculator.params.a",
                # 这个参数是否是必填的
                "required": True
            },
            # 第二个参数 - b
            "b": {
                "type": "number",
                "description": "The second number",
                "displayDescription": "$t:bad_calculator.params.b",
                "required": True
            }
        }

get_metadata方法是一个classmethod。它会把这个工具的描述提供给LLM。

display开头的字段可以使用一种特殊写法$t:xxx这种写法将使用RAGFlow的国际化机制llmTools这个分类中获取文字。如果你不使用这种写法,那么前端将会显示此处的原始内容。

现在,我们的工具已经做好了,你可以在生成回答组件中选择这个工具来尝试一下。