mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
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:  And with the `bad_calculator` tool, it results this with the `qwen-max` model:  ### 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>
This commit is contained in:
45
plugin/plugin_manager.py
Normal file
45
plugin/plugin_manager.py
Normal file
@ -0,0 +1,45 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import pluginlib
|
||||
|
||||
from .common import PLUGIN_TYPE_LLM_TOOLS
|
||||
|
||||
from .llm_tool_plugin import LLMToolPlugin
|
||||
|
||||
|
||||
class PluginManager:
|
||||
_llm_tool_plugins: dict[str, LLMToolPlugin]
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._llm_tool_plugins = {}
|
||||
|
||||
def load_plugins(self) -> None:
|
||||
loader = pluginlib.PluginLoader(
|
||||
paths=[str(Path(os.path.dirname(__file__), "embedded_plugins"))]
|
||||
)
|
||||
|
||||
for type, plugins in loader.plugins.items():
|
||||
for name, plugin in plugins.items():
|
||||
logging.info(f"Loaded {type} plugin {name} version {plugin.version}")
|
||||
|
||||
if type == PLUGIN_TYPE_LLM_TOOLS:
|
||||
metadata = plugin.get_metadata()
|
||||
self._llm_tool_plugins[metadata["name"]] = plugin
|
||||
|
||||
def get_llm_tools(self) -> list[LLMToolPlugin]:
|
||||
return list(self._llm_tool_plugins.values())
|
||||
|
||||
def get_llm_tool_by_name(self, name: str) -> LLMToolPlugin | None:
|
||||
return self._llm_tool_plugins.get(name)
|
||||
|
||||
def get_llm_tools_by_names(self, tool_names: list[str]) -> list[LLMToolPlugin]:
|
||||
results = []
|
||||
|
||||
for name in tool_names:
|
||||
plugin = self._llm_tool_plugins.get(name)
|
||||
|
||||
if plugin is not None:
|
||||
results.append(plugin)
|
||||
|
||||
return results
|
||||
Reference in New Issue
Block a user